gpt4 book ai didi

r - OpenAI ChatGPT (GPT-3.5) API 错误 400 : "' user' is not of type 'object' "

转载 作者:行者123 更新时间:2023-12-02 05:47:17 25 4
gpt4 key购买 nike

我与您分享我的代码,以从 OPENAI chatgpt api 中使用 R 的 POST 请求获取响应:

param <- list(model = "gpt-3.5-turbo",
messages = c("role" = "user",
"content" = "Hello"))

result <- POST("https://api.openai.com/v1/chat/completions",
body = param,
add_headers(Authorization=openai_secret_key),
encode = "json")

这是结果:

Response [https://api.openai.com/v1/chat/completions]Date: 2023-03-02 16:28Status: 400Content-Type: application/jsonSize: 158 B{“error”: {“message”: “‘user’ is not of type ‘object’ - ‘messages.0’”,“type”: “invalid_request_error”,“param”: null,“code”: null}}

因此用户和内容部分不起作用,但模型正在起作用

非常感谢

在 postman 中,我可以使用此 JSON,但无法使其在 R 中工作

{
"model":"gpt-3.5-turbo",
"messages":[
{
"role":"user",
"content":"Hello!"
}
]
}

最佳答案

如果您运行 test.r,OpenAI API 将返回以下完成信息:

[1] "\n\nHello! How may I assist you today?"

测试.r

library(httr)
library(jsonlite)

OPENAI_API_KEY <- "sk-xxxxxxxxxxxxxxxxxxxx"

param <- list(model = "gpt-3.5-turbo",
messages = list(list(role = "user", content = "Hello"))
)

result <- POST("https://api.openai.com/v1/chat/completions",
body = param,
add_headers("Authorization" = paste("Bearer", OPENAI_API_KEY)),
encode = "json")

response_content <- fromJSON(rawToChar(result$content))
print(response_content$choices[[1]]$content)

关于r - OpenAI ChatGPT (GPT-3.5) API 错误 400 : "' user' is not of type 'object' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75625675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com