从R访问OpenAI(json)API

gmxoilav  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(258)

我想从R使用以下curl命令访问OpenAI API:

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包(在cram上)将是最好的选择(?)。我从来没有用过这个包,所以有没有人能帮我开始这个简单的调用?

ghhaqwfi

ghhaqwfi1#

使用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"

相关问题