OpenAI ChatGPT (GPT-3.5) API错误 400: “‘user’ 不是类型为 ‘object’ 的对象”

huangapple go评论72阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年3月3日 18:07:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75625675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定