英文:
OpenAI ChatGPT (GPT-3.5) API error 400: "'user' is not of type 'object'"
问题
以下是您的代码中需要翻译的部分:
I share with you my code bellow to get a response from a POST request with R from OPENAI chatgpt api :
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")
Here is the result :
>Response [https://api.openai.com/v1/chat/completions]
Date: 2023-03-02 16:28
Status: 400
Content-Type: application/json
Size: 158 B
{
“error”: {
“message”: “‘user’ is not of type ‘object’ - ‘messages.0’”,
“type”: “invalid_request_error”,
“param”: null,
“code”: null
}
}
So the user and the content part is not working but the model is working
Thanks a lot
In postman, I have this JSON working but can't make it work in R
{
"model":"gpt-3.5-turbo",
"messages":[
{
"role":"user",
"content":"Hello!"
}
]
}
英文:
I share with you my code bellow to get a response from a POST request with R from OPENAI chatgpt api :
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")
Here is the result :
>Response [https://api.openai.com/v1/chat/completions]
Date: 2023-03-02 16:28
Status: 400
Content-Type: application/json
Size: 158 B
{
“error”: {
“message”: “‘user’ is not of type ‘object’ - ‘messages.0’”,
“type”: “invalid_request_error”,
“param”: null,
“code”: null
}
}
So the user and the content part is not working but the model is working
Thanks a lot
In postman, I have this JSON working but can't make it work in R
{
"model":"gpt-3.5-turbo",
"messages":[
{
"role":"user",
"content":"Hello!"
}
]
}
答案1
得分: 0
如果您运行test.r
,OpenAI API将返回以下完成:
> [1] "\n\n你好!我可以帮助您今天吗?"
test.r
library(httr)
library(jsonlite)
OPENAI_API_KEY <- "sk-xxxxxxxxxxxxxxxxxxxx"
param <- list(model = "gpt-3.5-turbo",
messages = list(list(role = "user", content = "你好"))
)
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)
英文:
If you run test.r
the OpenAI API will return the following completion:
> [1] "\n\nHello! How may I assist you today?"
test.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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论