调用OpenAI Chat Completions API遇400错误请求,求排查原因
调用OpenAI Chat Completions API出现400 Bad Request错误的排查
我尝试调用OpenAI的Chat Completions API(使用gpt-3.5-turbo模型),但收到了400 Bad Request错误。
我的调用代码(C#)
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; } }
OpenAI官方示例代码
curl示例
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!"}] }'
Python示例
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 }}
错误原因分析
API端点错误:你的代码中请求的是
https://api.openai.com/v1/completions,这是文本补全类模型(如gpt-3.5-turbo-instruct)的接口端点。而gpt-3.5-turbo对应的Chat Completions API正确端点是https://api.openai.com/v1/chat/completions,这是触发400错误的核心原因。响应字段解析错误:即使修正端点,当前代码中
responseObj.choices[0].text的写法也不符合Chat Completions API的返回结构。该接口的响应内容嵌套在message.content字段下,正确取值应为responseObj.choices[0].message.content(text是文本补全接口的返回字段)。messages格式校验:需额外确认
data变量的结构是否合规,每个消息对象必须包含role(可选值:system、user、assistant)和content字段,格式示例:[{"role":"user","content":"你的问题内容"}],格式不符也会触发400错误。
内容的提问来源于stack exchange,提问作者Robert Benedetto
相关产品推荐
相关产品推荐

