jcrodriguez1989 / chatgpt Goto Github PK
View Code? Open in Web Editor NEWInterface to ChatGPT from R
License: GNU General Public License v3.0
Interface to ChatGPT from R
License: GNU General Public License v3.0
Excuse me, if using a third-party intermediary service, how should I set up the API URL for chatgpt?
Cool stuff, thanks for putting this together!
How about a write function like:
write_code <- function(description) {
prompt <- paste0('Write R code that: "', description, '"')
trimws(sapply(chatgpt:::gpt_get_completions(prompt)$choices, function(x) x$text))
}
cat(write_code("produces a bar plot using ggplot"))
Which returned:
# Create data frame
d <- data.frame(
place = c("USA", "UK", "Canada", "Australia"),
values = c(25, 18, 8, 7)
)
# Create bar plot
ggplot(d, aes(x = place, y = values)) +
geom_col()
I'm happy to put in a PR if you like the recommendation, but I did almost nothing :) Just a copy and paste of your function with a new name and request.
When calling ask_chatgpt
in a for
loop, I get maybe 3 returns with answers, but after that it will return "character(0)" which just seems to be a null character object. When this occurs in the loop, it stops operation with an error about "...replacement has length 0."
I used this workaround, which was effective for my use: insert sys.sleep(x)
into the loop, where x is a number of seconds to temporarily suspend operation. 5 wasn't enough and 15 worked, but it appears as if more complex queries and/or higher numbers of loop iterations requires higher times; I had to move to 20 to get a loop to run 121 times.
I'm not sure what's causing the null response and this workaround suits me fine, but would be interested to learn why it occurs in case faster loops are possible.
Hi, maybe I missed something, but this package should work with a free plan on chatgpt? I am able to ask questions on the chatgpt webpage but in Rstudio I get the message:
Error in gpt_get_completions(question, messages = chat_session_messages) :
list(message = "You exceeded your current quota, please check your plan and billing details.", type = "insufficient_quota", param = NULL, code = NULL)
when I run
cat(ask_chatgpt("What is you purpose?"))
Maybe it only works with a payed subscription?
Thanks,
Tom
Sometimes, ChatGPT will generate testthat
use cases that use edition 2 functions rather than edition 3.
For example, ChatGPT often uses testthat::context
, which was deprecated with testthat
3e.
It might be useful to specify that ChatGPT should use testthat
3e or add a function that asks ChatGPT to convert the output of create_unit_tests
from testthat
2e to testthat
3e.
Greeting,
I have tried to use usethis::edit_r_environ() and add OPENAI_MODEL="gpt-4," but it does not work for me to switch to gpt-4. Am I right on that?
Thanks.
RStudio-2023.12.1-402
R-4.3.3 for Windows
In Rgui on Windows, I get
ask_chatgpt("How many hours are there in a day?")
Error in gpt_get_completions(question, messages = chat_session_messages) :
OPENAI_API_KEY
not provided.
How do I provide OPENAI_API_KEY?
Hello there - great package, I just thought about building something like this, but it is already done ๐
I think a good addition could be using the function code that is copied to the clipboard, similar to reprex
package. User will select the code, Ctrl/Command + C and run any of the package functions.
It will require clipr
as a dependency and one more enviroment variable that will use this "new" approach by default.
Alternatively we can allow running functions without specified value for code
argument and it will use the code from clipboard and stop()
when the clipboard is NULL.
I just tried it and works quite well.
I can work on it and raise a PR if you're interested. Please let me know your thoughts.
Have a great day!
Denis
When I use chatgpt I recieve an error:
Warning: Error in gpt_get_completions: list(message = "You exceeded your current quota, please check your plan and billing details.", type = "insufficient_quota", param = NULL, code = NULL)
However if I sign in in OpenaAI I can use it without any problem.
Hi @guoliangzeng ,
I created a branch that allows changing the OPENAI's API URL. Could you try doing this?
Fully restart your R session, and then:
remotes::install_github("jcrodriguez1989/[email protected]_api_url")
Sys.setenv(OPENAI_API_URL = "https://api.chatanywhere.com.cn/v1")
# If this doesn't work, do `Sys.setenv(OPENAI_API_URL = "https://api.chatanywhere.com.cn")`
library("chatgpt")
cat(ask_chatgpt("What do you think about R language?"))
Please, let me know if you get any errors.
Originally posted by @jcrodriguez1989 in #48 (comment)
Could you please show how to change the model from gpt 3.5 to gpt 4 by changing the settings? Thank you
Any chance that we can pass an argument to specify the model to use for the queries made through this package? Appreciate you putting it together and prevent meaningless JSON writing.
is there a way to connect the package to an already created custom assistant from the playground
A new model for chatGPT has been released, but it uses a different endpoint. Here's some example code to get a result:
library(chatgpt)
library(httr)
library(jsonlite)
Sys.setenv(OPENAI_MODEL = "gpt-3.5-turbo")
Sys.setenv(OPENAI_API_KEY = readLines("~/api_keys/openapi.txt"))
openai_api_key = Sys.getenv("OPENAI_API_KEY")
gpt_model = Sys.getenv("OPENAI_MODEL")
messages = list(
list(role="system", content= "You are a helpful assistant."),
list(role="user", content= "Who won the world series in 2020?"),
list(role="assistant", content= "The Los Angeles Dodgers won the World Series in 2020."),
list(role="user", content= "Where was it played?")
)
params = list(model = gpt_model,
messages = messages)
response <- content(
POST(
"https://api.openai.com/v1/chat/completions",
add_headers(Authorization = paste("Bearer",
openai_api_key)),
content_type_json(),
body = toJSON(params, auto_unbox = TRUE)
)
)
Ask ChatGPT: Opens an interactive chat session with ChatGPT
Does this means that a new session is opened every time the ask_chatgpt
function is called, or is the same session used for each subsequent prompt?
If the former, then how does one start a new chatGPT session? Simply restarting the Rstudio session?
If the latter, how can we make the session persistent across queries?
Thanks,
Brian
Dear Juan,
I confronted an error during using chatgpt package,
the error is "
Thank you!
best wishes,
Shunsen
I am trying to tweak the get parameters, but the document that is linked does not clearly show how to do so. Some examples or explanation would be really helpful.
Warning: Error in curl::curl_fetch_memory: Timeout was reached: [api.openai.com] Failed to connect to api.openai.com port 443 after 10006 ms: Timeout was reached
Hey,
Seems like gpt-4 models listed below are still not covered.
All models;
OpenAI models page
Error that occured when tried "gpt-4" for Sys.setenv(OPENAI_MODEL = "gpt-4")
Error in gpt_get_completions(question) :
list(message = "This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?", type = "invalid_request_error", param = "model", code = NULL)
"The following environment variables variables can be set ..." - the word "variables" is repeated.
Thanks for the helpful package.
I tried the comment_code function and it does what is expected. However, when I give it a somewhat larger chunk (e.g a function with 12-15 commands), it only comments on the first few and ignores the rest. So I wanted to know if there is a limit as to how many lines/commands it can comment?
And is that limit set by you or by chatgpt?
I can provide an example if you want but any code with more than 10 lines would reproduce the behaviour, I think.
Thanks in advance
Rohit
I have my model set, but I want to be able to have confirmation attached to output about which model was used to generate the response. ChatGPT suggests this is possible using Python, where "metadata" is another option that can be set along with temperature, max tokens, etc. Below is the Python code it suggested to me; I looked through the repository and didn't see a metadata option.
Use case: My account provider is telling me I should have access to GPT-4. After generating an API key under the proper default organization, and attempting to set GPT-4 in R, I get the error suggesting I don't have access to it. In the Rate Limits and Playground, I also don't see evidence of having access to GPT-4, only GPT-3.5-turbo. That's fine, that appears to be an administrative error.
But then when having 3.5-turbo set in R, and I ask_chatgpt which model it is, it tells me it is "a simple GPT-2 model." On the other hand, when using the chat.openai.com, it gives me a dramatically different answers about which model it is. Asking for this information probably isn't a reliable solution. So while the OPENAI_MODEL should be the straightforward validation of which model is generating the response, I'm hoping for another way of confirming.
import openai
openai.api_key = "YOUR_API_KEY"
prompt = "Some text you want to generate a response for"
model = "text-davinci-002" # Replace with the CHTAGPT model ID
temperature = 0.5
max_tokens = 50
metadata = True # Include metadata in the response
response = openai.Completion.create(
engine=model,
prompt=prompt,
temperature=temperature,
max_tokens=max_tokens,
metadata=metadata
)
print(response.choices[0].text) # Print the generated text
print(response.choices[0].metadata) # Print the metadata
I followed the installation instructions and got the error:
`
remotes::install_github("jcrodriguez1989/chatgpt")
Downloading GitHub repo jcrodriguez1989/chatgpt@HEAD
Error in utils::download.file(url, path, method = method, quiet = quiet, :
cannot open URL 'https://api.github.com/repos/jcrodriguez1989/chatgpt/tarball/HEAD'
sessionInfo()
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)`
When calling chatgpt()
from R, the text body's formatting is lost. Any line breaks, numbered list or code blocks in the text is gone. The text body includes symbols, such as /n, /n1 and ```, that I presume denotes the aforementioned, respectively. In a Jupyter notebook, I tried changing the output from HTML to Markdown. This will change any code to inline code. Line breaks and lists are still not displayed correctly.
It would be helpful if the function had the ability to return ChatGPT's answer with the correct formatting.
Hi,
I was looking for an R package to make the API calls to chatGPT and I came across your package. You package does two things:
Would it be possible to separate the first into its own package? I think a lot of R users would be interested in it.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.