gpt4 book ai didi

post - OpenAI ChatGPT (GPT-3.5) API 错误 400 : "Bad Request" (migrating from GPT-3 API to GPT-3. 5 API)

转载 作者:行者123 更新时间:2023-12-02 22:45:35 26 4
gpt4 key购买 nike

尝试调用刚刚为 ChatGPT 发布的 got-3.5-turbo API,但收到错误请求错误?


var body = new
{
model = "gpt-3.5-turbo",
messages = data
};

string jsonMessage = JsonConvert.SerializeObject(body);

using (HttpClient client = new HttpClient())
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

HttpRequestMessage requestMessage = new
HttpRequestMessage(HttpMethod.Post, "https://api.openai.com/v1/completions")
{
Content = new StringContent(jsonMessage, Encoding.UTF8, "application/json")
};

string api_key = PageExtension_CurrentUser.Community.CAIChatGPTAPIKey.Length > 30 ? PageExtension_CurrentUser.Community.CAIChatGPTAPIKey : Genesis.Generic.ReadAppSettingsValue("chatGPTAPIKey");
requestMessage.Headers.Add("Authorization", $"Bearer {api_key}");

HttpResponseMessage response = client.SendAsync(requestMessage).Result;
if (response.StatusCode == HttpStatusCode.OK)
{
string responseData = response.Content.ReadAsStringAsync().Result;
dynamic responseObj = JsonConvert.DeserializeObject(responseData);
string choices = responseObj.choices[0].text;

}

他们的 API 文档中有代码:

curl https://api.openai.com/v1/chat/completions \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer YOUR_API_KEY' \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello!"}]
}'

..这是另一个示例:

openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"},
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
{"role": "user", "content": "Where was it played?"}
]
)

有人能明白为什么我会收到错误吗?

编辑:错误消息

{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Connection: keep-alive
Access-Control-Allow-Origin: *
Openai-Organization: user-lmjzqj7ba2bggaekkhr68aqn
Openai-Processing-Ms: 141
Openai-Version: 2020-10-01
Strict-Transport-Security: max-age=15724800; includeSubDomains
X-Request-Id: 9eddf8bb8dcc106ca11d44ad7f8bbecc
Date: Mon, 06 Mar 2023 12:49:46 GMT
Content-Length: 201
Content-Type: application/json
}}



{Method: POST, RequestUri: 'https://api.openai.com/v1/chat/completions', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
Authorization: Bearer sk-ihUxxxxxxxxxxxxxxxxxx[JUST REMOVED MY API KEY]xxxxxxxxxxxxxxx
Content-Type: application/json; charset=utf-8
Content-Length: 79
}}

最佳答案

您使用的是 gpt-3.5-turbo 型号。

Chat Completions API 之间存在三个主要区别(即 GPT-3.5 API)和 Completions API (即 GPT-3 API):

  1. API端点
    • Completions API:https://api.openai.com/v1/completions
    • 聊天完成 API:https://api.openai.com/v1/chat/completions
  2. prompt 参数(Completions API)已替换为 messages 参数(Chat Completions API)
  3. 响应访问
    • Completions API:
      response.getJSONArray("choices").getJSONObject(0).getString("text")
    • 聊天完成 API:response.getJSONArray("choices").getJSONObject(0).getString("message")

问题 1:您使用了错误的 API 端点

更改此(Completions API)...

https://api.openai.com/v1/completions

...对此(聊天完成 API)。

https://api.openai.com/v1/chat/completions

问题 2:确保 messages 参数的 JSON 有效

  • cURL: "messages": [{"role": "user", "content": "Hello!"}]

  • Python:messages = [{"role": "user", "content": "Hello!"}]

  • NodeJS:消息:[{角色:“用户”,内容:“Hello world”}]


问题 3:您访问响应的方式不正确

注意:OpenAI NodeJS SDK v4released于2023年8月16日发布,并且是对SDK的完全重写。除此之外,提取消息内容方面也发生了变化。请参阅 v3v4 migration guide .

改变这个...

string choices = responseObj.choices[0].text;

...对此。

string choices = responseObj.choices[0].message.content;

问题 4:您没有设置 Content-Type header

添加此:

requestMessage.Headers.Add("Content-Type", "application/json");

注意:"application/json, UTF-8" 不起作用,正如 @Srishti 在下面的评论中提到的。

关于post - OpenAI ChatGPT (GPT-3.5) API 错误 400 : "Bad Request" (migrating from GPT-3 API to GPT-3. 5 API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75650840/

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