gpt4 book ai didi

c# - OpenAI ChatGPT (GPT-3.5) API 错误 400 : "Bad Request" when adding multiple questions

转载 作者:行者123 更新时间:2023-12-02 05:48:33 26 4
gpt4 key购买 nike

在下面的代码中,第一次总是工作正常。正如您所看到的,我正在尝试保留以前的问题和答案的列表,以便与 _items 列表保持上下文。

所以第二次它失败并出现错误 400 bad request,但为了测试我尝试添加两个注释行来开始我的第一个问题,输入“他多大了?”所以它有上下文,并且也会失败并出现错误 400,因此它不能是其余代码的任何部分!我不明白为什么它不允许我添加更多上下文,因为我在网上看到的所有示例都说您可以添加多条消息以保留上下文。

private List<RequestMessage> _items = new();

// Method to send a message to the ChatGPT API and return the response
async public Task<string> SendMessage(string message)
{
Request request = new Request();

List<RequestMessage> requestMessages = new();

requestMessages.Add(
new RequestMessage()
{
Role = "system",
Content = $"You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.\nKnowledge cutoff: 2021-09-01\nCurrent date: {DateTime.Now.ToString("yyyy-MM-dd")}",
});
requestMessages.Add(
new RequestMessage()
{
Role = "user",
Content = "How are you?",
});
requestMessages.Add(
new RequestMessage()
{
Role = "assistant",
Content = "I am doing well",
});
/*
requestMessages.Add(
new RequestMessage()
{
Role = "user",
Content = "Who is Brendan Fraser?",
});
requestMessages.Add(
new RequestMessage()
{
Role = "assistant",
Content = "Brendan Fraser is a Canadian American actor",
});
*/

requestMessages.AddRange(_items);

requestMessages.Add(
new RequestMessage()
{
Role = "user",
Content = message,
});

request.Messages = requestMessages.ToArray();

string requestData = JsonSerializer.Serialize(request);
StringContent content = new StringContent(requestData, Encoding.UTF8, "application/json");

using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _apiKey);
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(_apiUrl, content);

if (httpResponseMessage.IsSuccessStatusCode)
{
string responseString = await httpResponseMessage.Content.ReadAsStringAsync();
Response response = JsonSerializer.Deserialize<Response>(responseString);
string responseText = response.Choices[0].Message.Content;

_items.Add(new RequestMessage() { Role = "user", Content = message });
_items.Add(new RequestMessage() { Role = "assistant", Content = responseText });

return responseText;
}
else
{
return $"Error: {httpResponseMessage.StatusCode} - {httpResponseMessage.ReasonPhrase}";
}
}

}

最佳答案

问题出在我使用的所有精美的 Json 对象上。我没有费心正确地调试它来查看为什么会出错,但我对此进行了更改:

public class Request
{
[JsonPropertyName("model")]
public string Model { get; set; } = "gpt-3.5-turbo";
[JsonPropertyName("max_tokens")]
public int MaxTokens { get; set; } = 4000;
[JsonPropertyName("messages")]
public RequestMessage[] Messages { get; set; }
}

public class RequestMessage
{
[JsonPropertyName("role")]
public string Role { get; set; }
[JsonPropertyName("content")]
public string Content { get; set; }
}

public class Response
{
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("created")]
public int Created { get; set; }
[JsonPropertyName("model")]
public string Model { get; set; }
[JsonPropertyName("usage")]
public ResponseUsage Usage { get; set; }
[JsonPropertyName("choices")]
public ResponseChoice[] Choices { get; set; }
}

public class ResponseUsage
{
[JsonPropertyName("prompt_tokens")]
public int PromptTokens { get; set; }
[JsonPropertyName("completion_tokens")]
public int CompletionTokens { get; set; }
[JsonPropertyName("total_tokens")]
public int TotalTokens { get; set; }
}

public class ResponseChoice
{
[JsonPropertyName("message")]
public ResponseMessage Message { get; set; }
[JsonPropertyName("finish_reason")]
public string FinishReason { get; set; }
[JsonPropertyName("index")]
public int Index { get; set; }
}

public class ResponseMessage
{
[JsonPropertyName("role")]
public string Role { get; set; }
[JsonPropertyName("content")]
public string Content { get; set; }
}

使用直接json

private List<dynamic> messages = new List<dynamic>
{
new {role = "system",
content = "You are ChatGPT, a large language " +
"model trained by OpenAI. " +
"Answer as concisely as possible. " +
"Make a joke every few lines just to spice things up."},
new {role = "assistant",
content = "How can I help you?"}
};
messages.Add(new { role = "user", content = message });
var request = new
{
messages,
model = "gpt-3.5-turbo",
max_tokens = 300,
};

然后是一个更简单的responseObject

var responseObject = JsonConvert.DeserializeAnonymousType(responseString, new
{
choices = new[] { new { message = new { role = string.Empty, content = string.Empty } } },
error = new { message = string.Empty }
});

var messageObject = responseObject?.choices[0].message;
messages.Add(messageObject);

关于c# - OpenAI ChatGPT (GPT-3.5) API 错误 400 : "Bad Request" when adding multiple questions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76945543/

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