I registered at this link to get an API for ChatGPT.
And I used the chatgpt package in R.
library(chatgpt)
And set environment:
Sys.setenv(OPENAI_API_KEY = "sk-YOURAPI")
I used this function:
chatgpt::ask_chatgpt("How to make a strawberry pie to donate to my wife? Ingredients, please.")
Sometimes the output is missing words. For example:
*** ChatGPT input:
How to make a strawberry pie to donate to my wife? Ingredients, please.
[1] ... and your wife is"
It does not complete the text after is. Is there any solution for this on R?
Similar question on reddit.
EDIT
I tried increasing the processing time of the function (so that the chat would finish typing all the content, without stopping midway). Like this:
for (i in 35) {
print(chatgpt::ask_chatgpt("How to make a strawberry pie to donate to my wife? Ingredients, please."))
Sys.sleep(i)
}
What you are running into is prompt engineering. GPT is a sophisticated autocomplete engine.
If you are not getting the right response you need to recraft your prompt.
You can always test your prompts in the OpenAI playground:
https://platform.openai.com/playground
I was able to get a list of ingredients and cooking steps by using the following prompt:
How can I make a strawberry pie to donate to my wife? Please provide first a numbered list of ingredients, and secondly a numbered lists of steps.
Here is the output I got in the playground:
Ingredients:
1. 2 ½ cups of fresh or frozen strawberries
2. 1 9-inch pre-made pie crust
3. ¾ cup of granulated sugar
4. 2 tablespoons of cornstarch
5. ¼ teaspoon of salt
6. 1 tablespoon of fresh lemon juice
Steps:
1. Preheat oven to 425 degrees F.
2. Place the pre-made pie crust in a 9-inch pie dish and set aside.
3. In a medium bowl, combine the strawberries, sugar, cornstarch, salt, and lemon juice. Stir until the mixture is combined.
4. Pour the strawberry mixture into the pre-made pie crust.
5. Place the pie dish on a baking sheet and bake for 15 minutes.
6. Reduce the oven temperature to 375 degrees F and bake for an additional 25 minutes.
7. Allow the pie to cool completely before serving.
Another thing to note, per the Github repo for the chatgpt R library it says "The {chatgpt} R package provides a set of features to assist in R coding."
Ref: https://github.com/jcrodriguez1989/chatgpt
I would use the OpenAI APIs directly, this way you will have a lot more control over your response. I am not an R specialist, but this is how the OpenAI Playground showed me how to do it.
library(httr)
response <- GET("https://api.openai.com/v1/completions",
query = list(
prompt = "How can I make a strawberry pie to donate to my wife? Please provide first a numbered list of ingredients, and secondly a numbered lists of steps.",
max_tokens = 200,
model = 'text-davinci-003'
),
add_headers(Authorization = "bearer YOUR_OPENAI_API_KEY")
)
content(response)
Ref: OpenAI playground
Increase max_tokens to get longer answer.
Related
I'm trying to crack a crypt with a known key length. I deduced that the operation made was a hex XOR. Here is the crypt:
330a1448010816101c1e470b0248104711050903040a0844511317130d030817024812150d014817150d1c48050f0751181415071f0618060e510618000a051b190606144822080e1006040a42051d1302101e1b040a423d4651330a14480608101548010816101c1e470f1011511507170d0347161e48050f0751181d060c0548181311140417470b1f48100306181c18080c511c1e4716190d510206180a1d0242051d1302105f4838094205001447231f0c14144e511f1902101448050f07511b010201180d02470b0248180906180f14090d041b5d4716190d030242101a1447111e0514470d050014154212041e14071d115115071d09050206510b040b16181e1013071548010816101c1e4711010d120e07024651370d0509050807024806021014481809160307151201140c510817051b180307511c1902423006150211511a14000b1e0651010d041a5104071f1c04150b141b5106051e4451060c154819061414481302011e051447031f48180916140f03060e5118101516510717470f040b19470d1748050f07511f1e150e154f0247041e071547110418010b1b5f48381342181b51130a14480608101d0c561442170704151619451d0610160d02134217071e0342121a1e174e510e1e0b0e1e1f1809055105100e18144451100a14090547031f0c51150b120d5f
I have tried to use this tool to decrypt it. The tool outputted multiple possible keys and for each key, an attempt do decipher the crypt. I know the result should be plain text English. The closest I got was this:
T-e po1ato ,s a 6tarc-y, t0bero0s cr*p fr*m th per nnia) nig-tsha!e So)anumetube7osumeL. T-e wo7d po1ato (ay r fer 1o th pla+t it6elf ,n ad!itio+ to 1he e!ibleetube7. Inethe ndesi whe7e th spe&ies ,s in!igen*us, 1hereeare 6ome *thereclos ly r late! cul1ivat d po1ato 6peci s.P*tato s we7e in1rodu&ed o0tsid theeAnde6 reg,on f*ur c ntur,es a"o,a+d ha3e be&ome $n in1egra) par1 of (uch *f th wor)d's #ood 6uppl<. Iteis t-e wo7ld'sefour1h-la7gestefoodecropi fol)owin" mai?e, w-eat $nd r,ce.
After some digging and manually tweaking the text, I got this:
The potato is a starchy, tuberous crop from the perennial nightshade Solanum tuberosum L. The word "potato" may refer to the plant, itself, in addition to the edible tuber. In the Andes, where the species is indigenous, there are some other closely related cultivated potato species. Potatoes were introduced outside the Andes region four centuries ago, and have become an integral part of much of the world's food supply. It is the world's fourth-largest food crop, following maize, wheat and rice.
It unfortunately did not work. I am now trying to find a clue as to what I should be doing to find the answer.
I am new to R and I am facing difficulties to convert my dataframe (named dffinal) which contains list into a csv.
I tried the following code which gave a csv that is not usable:
dput(dffinal, file="out.txt")
new <- source("out.txt")
write.csv2(dffinal,"C:/Users\\final.csv", row.names = FALSE)
I tried all the option but I found nothing! Here is a sample of my dataframe:
dput(head(dffinal[1:2]))
structure(list(V1 = list("I heard about your products and I would like to give it a try but I'm not sure which product is better for my dry skin, Almond products or Shea Butter products? Thank you",
"Hi,\n\nCan you please tell me the difference between the shea shower oil limited edition and the other shower gels? I got a sample of one in a kit that had a purple label on it. (Please see attached photo.) I love it!\nBut, what makes it limited edition, the smell or what? It is out of stock and I was wondering if it is going to be restocked or not?\n\nAlso, what makes it different from the almond one?\n\nThank you for your help.",
"Hello, Have you discontinued Eau de toilette", "I both an eGift card for my sister and she hasn't received anything via her email\n\nPlease advise \n\nThank you \n\n cann",
"I do not get Coco Pillow Mist. yet. When are you going to deliver it? I need it before January 3rd.",
"Hello,\nI wish to follow up on an email I just received from Lol, notifying\nme that I've \"successfully canceled my subscription of bun Complete.\"\nHowever, I didn't request a cancelation and was expecting my next scheduled\nfulfillment later this month. Could you please advise and help? I'd\nappreciate it if you could reinstate my subscription.\n"),
V2 = list("How long can I keep a product before opening it? shea butter original hand cream large size 5oz, i like to buy a lot during sales promotions, is this alright or should i only buy what i'll use immediately, are these natural organic products that will still have a long stable shelf life? thank you",
"Hi,\nI recently checked to see if my order had been delivered, and I only received my gift box and free sample. Can you please send the advent calendar? Does not seem to have been included in the shipping. Thank you",
"Is the gade fragrance still available?", "I previously contacted you because I purchased your raspberry lip scrub. When I opened the scrub, 25% of the product was missing. Your customer service department agreed to send me a replacement, but I never received the replacement rasberry lip scrub. Could you please tell me when I will receive the replacement product? Thanks, me",
"To whom it may concern:\n\nI have 3 items in my order: 1 Shea Butter Intensive Hand Balm and 2 S‚r‚nit‚ Relaxing Pillow Mist. I have just received the hand balm this morning. I was wondering when I would receive the two bottles of pillow mist.\n\nThanks and regards,\n\nMe",
"I have not received 2X Body Scalp Essence or any shipment information regarding these items. Please let me know if and when you will be shipping these items, otherwise please credit my card. Thanks")), row.names = c(NA,
6L), class = "data.frame")
We can do this in tidyverse
library(dplyr)
library(readr)
dffinal %>%
mutate(across(everything(), unlist)) %>%
write_csv('result.csv')
If you have list of only length 1 for all the rows as shared in the example using unlist will work -
dffinal[] <- lapply(dffinal, unlist)
If the length of list is greater than 1 use -
dffinal[] <- lapply(dffinal, sapply, toString)
Write the data with write.csv -
write.csv(dffinal, 'result.csv', row.names = FALSE)
I am using the newsanchor package in R to try to extract entire article content via NewsAPI. For now I have done the following :
require(newsanchor)
results <- get_everything(query = "Trump +Trade", language = "en")
test <- results$results_df
This give me a dataframe full of info of (maximum) a 100 articles. These however do not containt the entire actual article text. Rather they containt something like the following:
[1] "Tensions between China and the U.S. ratcheted up several notches over the weekend as Washington sent a warship into the disputed waters of the South China Sea. Meanwhile, Google dealt Huaweis smartphone business a crippling blow and an escalating trade war co… [+5173 chars]"
Is there a way to extract the remaining 5173 chars. I have tried to read the documentation but I am not really sure.
I don't think that is possible at least with free plan. If you go through the documentation at https://newsapi.org/docs/endpoints/everything in the Response object section it says :
content - string
The unformatted content of the article, where available. This is truncated to 260 chars for Developer plan users.
So all the content is restricted to only 260 characters. However, test$url has the link of the source article which you can use to scrape the entire content but since it is being aggregated from various sources I don't think there is one automated way to do this.
This question already has an answer here:
avoid string printed to console getting truncated (in RStudio)
(1 answer)
Closed 6 years ago.
As a beginner, I am currently working with web scraping with R, using the 'rvest' package. My goal is to get the lyrics of any song from 'www.musixmatch.com'. This is my attempt:
library(rvest)
url <- "https://www.musixmatch.com/lyrics/Red-Hot-Chili-Peppers/Can-t-Stop"
musixmatch <- read_html(url)
lyrics <- musixmatch%>%html_nodes(".mxm-lyrics__content")%>%html_text()
This code creates a vector 'lyrics' with 2 rows, containing the lyrics:
[1] "Can't stop addicted to the shindig\nChop top he says I'm gonna win big\nChoose not a life of imitation"
[2] "Distant cousin to the reservation\n\nDefunkt the pistol that you pay for\nThis punk the feeling that you stay for\nIn time I want to be your best friend\nEastside love is living on the Westend\n\nKnock out but boy you better come to\nDon't die you know the truth is some do\nGo write your message on the pavement\nBurn so bright I wonder what the wave meant\n\nWhite heat is screaming in the jungle\nComplete the motion if you stumble\nGo ask the dust for any answers\nCome back strong with 50 belly dancers\n\nThe world I love\nThe tears I drop\nTo be part of\nThe wave can't stop\nEver wonder if it's all for you\nThe world I love\nThe trains I hop\nTo be part of\nThe wave can't stop\n\nCome and tell me when it's time to\n\nSweetheart is bleeding in the snow cone\nSo smart she's leading me to ozone\nMusic the great communicator\nUse two sticks to make it in the nature\nI'll get you into penetration\nThe gender of a generation\nThe birth of every other nation\nWorth your weight the gold ... <truncated>
The problem is that the 2nd row gets truncated at some point. From what I know about rvest, there is no parameter to adjust truncation. Also, I could not find anything on the internet about this issue. Does anybody know how to adjust/ disable truncation for this feature? Thanks a lot in advance!
Best regards,
Jan
I think its better to copy and paste the lyrics into your Notepad or Wordpad. Save as a .txt file
Then use the readLines function, it prints our a warning message but I was able to have the entire lyrics in 84x1 chacacter vector which you can clean or do whatever you please.
words <- readLines("redhot.txt")
> head(words)
[1] "Can't stop addicted to the shindig"
[2] "Chop top he says I'm gonna win big"
[3] "Choose not a life of imitation"
[4] "Distant cousin to the reservation"
[5] "Defunkt the pistol that you pay for"
[6] "This punk the feeling that you stay for"
No truncation problem here.
I have been reading tutorials and I need to use cat and paste functions (for making a kind of array, table) the thing is that all tutorials use these commands on files, and reading files from the hard drive are making my task very very slow, so I wanted to know how to use them with variables, I tried and got erroneous results, so maybe you can help me with the sintaxis using that.
basically I want to make a table in a variable like this:
00001 Tacos
00023 pizza
00076 burger
00103 chopsuey
00167 burrito
01034 Tamales
And I'm getting every element after executing a program and taking specific data from the output, so I'm getting:
00001
Tacos
00023
pizza
....
You dont have to do the program, just wanted to be sure that cat and paste are the ones to use here and their syntax, if they aren't I accept any suggestion.
Sorry, I maybe did not explain myself, sorry about that, I have a and b, both variables, a is 00001 and b y is Tacos, then i want them to merge together and store them in a variable, then do the same again but to put them in a new line. Sorry for the misundertanding.
at the end i want a variable with this in it:
00001 Tacos
00023 pizza
00076 burger
00103 chopsuey
00167 burrito
01034 Tamales
Thanks!
If you have a tool only working on files as input (e. g. diff or paste), you can use the <(…) notation to create a fake file whose contents is created by a shell command:
cat <(echo "hello world")
This will print hello world. The fake file lacks some of the abilities of on-disk files; it cannot be seeked for instance. So programs which want to seek a specific position in the file, for instance to read the file twice, will fail on this. But for your case, it should suffice and you can use stuff like this:
paste <(echo "$a") <(echo "$b")
For your case more concrete:
cat input.txt | {
x=''
y=''
while read a
do
read b
x=$(echo "$x"; echo "$a")
y=$(echo "$y"; echo "$b")
done
paste <(echo "$x") <(echo "$y")
}
(I'm assuming the input to be this here:)
00001
Tacos
00023
pizza
00076
burger
00103
chopsuey
00167
burrito
01034
Tamales