我想从R使用以下curl命令访问OpenAI应用程序接口:
curl https://api.openai.com/v1/engines/davinci/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"prompt": "This is a test", "max_tokens": 5}'我认为curl包(在CRAN上)是最好的选择(?)。我从来没有用过这个包,所以有人能帮我开始这个简单的调用吗?
发布于 2021-05-04 03:29:43
这可以通过httr包(在CRAN上)轻松完成,向@r2evans致敬:
library(httr)
myurl <- "https://api.openai.com/v1/engines/davinci/completions"
apikey <- "YOUR_API_KEY"
seed_text <- "This is a test"
tokens <- 5
output <- POST(myurl, body = list(prompt = seed_text, max_tokens = tokens), add_headers(Authorization = paste("Bearer", apikey)), encode = "json")
content(output)$choices[[1]]$text
## [1] " of a national emergency communication"https://stackoverflow.com/questions/67372903
复制相似问题