OpenAI ChatGPT (GPT-3.5) API错误: “状态码: 429, 原因短语: ‘请求过多'”

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

OpenAI ChatGPT (GPT-3.5) API error: "StatusCode: 429, ReasonPhrase: 'Too Many Requests'"

问题

我正在使用 .net core web api 将请求发送到 OpenAI API。我收到以下错误作为响应。我在我的 OpenAI 帐户中有余额。

我编写的代码和响应如下:

  1. public async Task<string> SendPromptAndGetResponse()
  2. {
  3. const string requestUri = "https://api.openai.com/v1/chat/completions";
  4. var requestBody = new
  5. {
  6. model = "gpt-3.5-turbo",
  7. messages = "How are you?",
  8. temperature = 0,
  9. max_tokens = 100
  10. };
  11. _httpClient.DefaultRequestHeaders.Authorization =
  12. new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", ApiKey);
  13. var response = await _httpClient.PostAsync(
  14. requestUri,
  15. new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json"));
  16. response.EnsureSuccessStatusCode();
  17. var responseBody = JsonConvert.DeserializeObject<ResponseBody>(await response.Content.ReadAsStringAsync());
  18. return responseBody.Choices[0].Message.Content.Trim();
  19. }

输出:

  1. StatusCode: 429, ReasonPhrase: 'Too Many Requests'
英文:

I am passing requests to the OpenAI API with .net core web api. I am getting the following error as a response. I have a balance in my OpenAI account.

The codes I wrote and the response are as follows:

  1. public async Task&lt;string&gt; SendPromptAndGetResponse()
  2. {
  3. const string requestUri = &quot;https://api.openai.com/v1/chat/completions&quot;;
  4. var requestBody = new
  5. {
  6. model = &quot;gpt-3.5-turbo&quot;,
  7. messages = &quot;How are you?&quot;,
  8. temperature = 0,
  9. max_tokens = 100
  10. };
  11. _httpClient.DefaultRequestHeaders.Authorization =
  12. new System.Net.Http.Headers.AuthenticationHeaderValue(&quot;Bearer&quot;, ApiKey);
  13. var response = await _httpClient.PostAsync(
  14. requestUri,
  15. new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8, &quot;application/json&quot;));
  16. response.EnsureSuccessStatusCode();
  17. var responseBody = JsonConvert.DeserializeObject&lt;ResponseBody&gt;(await response.Content.ReadAsStringAsync());
  18. return responseBody.Choices[0].Message.Content.Trim();
  19. }

Output:

  1. StatusCode: 429, ReasonPhrase: &#39;Too Many Requests&#39;

答案1

得分: 1

问题

你没有正确设置 messages 参数。 messages 参数的 rolecontent 属性是必需的。

错误示例:

  1. messages = &quot;How are you?&quot;,

请参阅官方 OpenAI 文档

OpenAI ChatGPT (GPT-3.5) API错误: “状态码: 429, 原因短语: ‘请求过多'”

<br>

解决方法

正确示例:

  1. messages = new[] {
  2. new { role = &quot;system&quot;, content = &quot;You are a helpful assistant.&quot; },
  3. new { role = &quot;user&quot;, content = &quot;Hello!&quot; },
  4. },

<br>

工作示例

此外,这篇博文可能会对你有所帮助。

免责声明:以下代码和截图的所有权归博客作者 Ricardo Mauro 所有。

步骤 1:安装 Standard.AI.OpenAI C# 库

  1. dotnet add Standard.AI.OpenAI

步骤 2:创建 OpenAI 帐户

步骤 3:创建 OpenAIProxy 类

  1. using Standard.AI.OpenAI.Models.Services.Foundations.ChatCompletions;
  2. namespace ConsoleAppOpenAI;
  3. public interface IOpenAIProxy
  4. {
  5. Task&lt;ChatCompletionMessage[]&gt; SendChatMessage(string message);
  6. }

步骤 4:创建实现类 OpenAIProxy.cs

  1. using Standard.AI.OpenAI.Clients.OpenAIs;
  2. using Standard.AI.OpenAI.Models.Configurations;
  3. using Standard.AI.OpenAI.Models.Services.Foundations.ChatCompletions;
  4. namespace ConsoleAppOpenAI;
  5. public class OpenAIProxy : IOpenAIProxy
  6. {
  7. readonly OpenAIClient openAIClient;
  8. //对话中的所有消息
  9. readonly List&lt;ChatCompletionMessage&gt; _messages;
  10. public OpenAIProxy(string apiKey, string organizationId)
  11. {
  12. // 使用 API 密钥和组织 ID 初始化配置
  13. var openAIConfigurations = new OpenAIConfigurations
  14. {
  15. ApiKey = apiKey,
  16. OrganizationId = organizationId
  17. };
  18. openAIClient = new OpenAIClient(openAIConfigurations);
  19. _messages = new();
  20. }
  21. void StackMessages(params ChatCompletionMessage[] message)
  22. {
  23. _messages.AddRange(message);
  24. }
  25. static ChatCompletionMessage[] ToCompletionMessage(
  26. ChatCompletionChoice[] choices)
  27. =&gt; choices.Select(x =&gt; x.Message).ToArray();
  28. //向 OpenAI 发送消息的公共方法
  29. public Task&lt;ChatCompletionMessage[]&gt; SendChatMessage(string message)
  30. {
  31. var chatMsg = new ChatCompletionMessage()
  32. {
  33. Content = message,
  34. Role = &quot;user&quot;
  35. };
  36. return SendChatMessage(chatMsg);
  37. }
  38. //业务逻辑发生的地方
  39. async Task&lt;ChatCompletionMessage[]&gt; SendChatMessage(
  40. ChatCompletionMessage message)
  41. {
  42. //我们应该发送所有消息
  43. //这样我们可以为 Open AI 提供对话的上下文
  44. StackMessages(message);
  45. var chatCompletion = new ChatCompletion
  46. {
  47. Request = new ChatCompletionRequest
  48. {
  49. Model = &quot;gpt-3.5-turbo&quot;,
  50. Messages = _messages.ToArray(),
  51. Temperature = 0.2,
  52. MaxTokens = 800
  53. }
  54. };
  55. var result = await openAIClient
  56. .ChatCompletions
  57. .SendChatCompletionAsync(chatCompletion);
  58. var choices = result.Response.Choices;
  59. var messages = ToCompletionMessage(choices);
  60. //也将响应堆叠起来 - 对于 Open AI 一切都是上下文
  61. StackMessages(messages);
  62. return messages;
  63. }
  64. }

步骤 5:设置 API 密钥

  1. IOpenAIProxy chatOpenAI = new OpenAIProxy(
  2. apiKey: &quot;YOUR-API-KEY&quot;,
  3. organizationId: &quot;YOUR-ORGANIZATION-ID&quot;);

步骤 6:在我们的应用程序中使用 Chat GPT 模型

  1. var msg = Console.ReadLine();
  2. do
  3. {
  4. var results = await chatOpenAI.SendChatMessage(msg);
  5. foreach (var item in results)
  6. {
  7. Console.WriteLine($&quot;{item.Role}: {item.Content}&quot;);
  8. }
  9. Console.WriteLine(&quot;下一个提示:&quot;);
  10. msg = Console.ReadLine();
  11. } while (msg != &quot;再见&quot;);

截图:

OpenAI ChatGPT (GPT-3.5) API错误: “状态码: 429, 原因短语: ‘请求过多'”

英文:

Problem

You didn't set the messages parameter correctly. The role and content properties of the messages parameter are required.

Wrong:

  1. messages = &quot;How are you?&quot;,

See the official OpenAI documentation.

OpenAI ChatGPT (GPT-3.5) API错误: “状态码: 429, 原因短语: ‘请求过多'”

<br>

Solution

Correct:

  1. messages = new[] {
  2. new { role = &quot;system&quot;, content = &quot;You are a helpful assistant.&quot; },
  3. new { role = &quot;user&quot;, content = &quot;Hello!&quot; },
  4. },

<br>

Working example

Also, this blog post might help you.

DISCLAIMER: All credit for the code and screenshot below goes to the author of the blog, Ricardo Mauro.

STEP 1: Install Standard.AI.OpenAI C# library

  1. dotnet add Standard.AI.OpenAI

STEP 2: Create an OpenAI account

STEP 3: Create the class OpenAIProxy

  1. using Standard.AI.OpenAI.Models.Services.Foundations.ChatCompletions;
  2. namespace ConsoleAppOpenAI;
  3. public interface IOpenAIProxy
  4. {
  5. Task&lt;ChatCompletionMessage[]&gt; SendChatMessage(string message);
  6. }

STEP 4: Create the implementation class OpenAIProxy.cs

  1. using Standard.AI.OpenAI.Clients.OpenAIs;
  2. using Standard.AI.OpenAI.Models.Configurations;
  3. using Standard.AI.OpenAI.Models.Services.Foundations.ChatCompletions;
  4. namespace ConsoleAppOpenAI;
  5. public class OpenAIProxy : IOpenAIProxy
  6. {
  7. readonly OpenAIClient openAIClient;
  8. //all messages in the conversation
  9. readonly List&lt;ChatCompletionMessage&gt; _messages;
  10. public OpenAIProxy(string apiKey, string organizationId)
  11. {
  12. //initialize the configuration with api key and sub
  13. var openAIConfigurations = new OpenAIConfigurations
  14. {
  15. ApiKey = apiKey,
  16. OrganizationId = organizationId
  17. };
  18. openAIClient = new OpenAIClient(openAIConfigurations);
  19. _messages = new();
  20. }
  21. void StackMessages(params ChatCompletionMessage[] message)
  22. {
  23. _messages.AddRange(message);
  24. }
  25. static ChatCompletionMessage[] ToCompletionMessage(
  26. ChatCompletionChoice[] choices)
  27. =&gt; choices.Select(x =&gt; x.Message).ToArray();
  28. //Public method to Send messages to OpenAI
  29. public Task&lt;ChatCompletionMessage[]&gt; SendChatMessage(string message)
  30. {
  31. var chatMsg = new ChatCompletionMessage()
  32. {
  33. Content = message,
  34. Role = &quot;user&quot;
  35. };
  36. return SendChatMessage(chatMsg);
  37. }
  38. //Where business happens
  39. async Task&lt;ChatCompletionMessage[]&gt; SendChatMessage(
  40. ChatCompletionMessage message)
  41. {
  42. //we should send all the messages
  43. //so we can give Open AI context of conversation
  44. StackMessages(message);
  45. var chatCompletion = new ChatCompletion
  46. {
  47. Request = new ChatCompletionRequest
  48. {
  49. Model = &quot;gpt-3.5-turbo&quot;,
  50. Messages = _messages.ToArray(),
  51. Temperature = 0.2,
  52. MaxTokens = 800
  53. }
  54. };
  55. var result = await openAIClient
  56. .ChatCompletions
  57. .SendChatCompletionAsync(chatCompletion);
  58. var choices = result.Response.Choices;
  59. var messages = ToCompletionMessage(choices);
  60. //stack the response as well - everything is context to Open AI
  61. StackMessages(messages);
  62. return messages;
  63. }
  64. }

STEP 5: Set up the API key

  1. IOpenAIProxy chatOpenAI = new OpenAIProxy(
  2. apiKey: &quot;YOUR-API-KEY&quot;,
  3. organizationId: &quot;YOUR-ORGANIZATION-ID&quot;);

STEP 6: Use the Chat GPT model in our application

  1. var msg = Console.ReadLine();
  2. do
  3. {
  4. var results = await chatOpenAI.SendChatMessage(msg);
  5. foreach (var item in results)
  6. {
  7. Console.WriteLine($&quot;{item.Role}: {item.Content}&quot;);
  8. }
  9. Console.WriteLine(&quot;Next Prompt:&quot;);
  10. msg = Console.ReadLine();
  11. } while (msg != &quot;bye&quot;);

Screenshot:

OpenAI ChatGPT (GPT-3.5) API错误: “状态码: 429, 原因短语: ‘请求过多'”

huangapple
  • 本文由 发表于 2023年7月27日 19:21:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779241.html
匿名

发表评论

匿名网友

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

确定