Search code examples
.net-coreopenai-apichatgpt-api

OpenAI Chat Completions API error: "StatusCode: 429, ReasonPhrase: 'Too Many Requests'"


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

Code:

public async Task<string> SendPromptAndGetResponse()
    {
        const string requestUri = "https://api.openai.com/v1/chat/completions";
        var requestBody = new
        {
            model = "gpt-3.5-turbo",
            messages = "How are you?",
            temperature = 0,
            max_tokens = 100
        };

        _httpClient.DefaultRequestHeaders.Authorization =
            new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", ApiKey);

        var response = await _httpClient.PostAsync(
            requestUri,
            new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json"));

        response.EnsureSuccessStatusCode();

        var responseBody = JsonConvert.DeserializeObject<ResponseBody>(await response.Content.ReadAsStringAsync());
        return responseBody.Choices[0].Message.Content.Trim();
    }

Error:

StatusCode: 429, ReasonPhrase: 'Too Many Requests'

Solution

  • Problem

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

    Wrong:

    messages = "How are you?",
    

    See the official OpenAI documentation.

    Screenshot


    Solution

    Correct:

    messages = new[] {
      new { role = "system", content =  "You are a helpful assistant." },
      new { role = "user", content =  "Hello!" },
    },
    

    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

    dotnet add Standard.AI.OpenAI
    

    STEP 2: Create an OpenAI account

    STEP 3: Create the class OpenAIProxy

    using Standard.AI.OpenAI.Models.Services.Foundations.ChatCompletions;
    
    namespace ConsoleAppOpenAI;
    
    public interface IOpenAIProxy
    {
      Task<ChatCompletionMessage[]> SendChatMessage(string message);
    }
    

    STEP 4: Create the implementation class OpenAIProxy.cs

    using Standard.AI.OpenAI.Clients.OpenAIs;
    using Standard.AI.OpenAI.Models.Configurations;
    using Standard.AI.OpenAI.Models.Services.Foundations.ChatCompletions;
    
    namespace ConsoleAppOpenAI;
    
    public class OpenAIProxy : IOpenAIProxy
    {
      readonly OpenAIClient openAIClient;
    
        //all messages in the conversation
      readonly List<ChatCompletionMessage> _messages;
    
      public OpenAIProxy(string apiKey, string organizationId)
      {
        //initialize the configuration with api key and sub
        var openAIConfigurations = new OpenAIConfigurations
        {
          ApiKey = apiKey,
          OrganizationId = organizationId
        };
    
        openAIClient = new OpenAIClient(openAIConfigurations);
    
        _messages = new();
      }
    
      void StackMessages(params ChatCompletionMessage[] message)
      {
        _messages.AddRange(message);
      }
    
      static ChatCompletionMessage[] ToCompletionMessage(
        ChatCompletionChoice[] choices)
        => choices.Select(x => x.Message).ToArray();
    
      //Public method to Send messages to OpenAI
      public Task<ChatCompletionMessage[]> SendChatMessage(string message)
      {
        var chatMsg = new ChatCompletionMessage() 
        { 
          Content = message, 
          Role = "user" 
        };
        return SendChatMessage(chatMsg);
      }
        
      //Where business happens
      async Task<ChatCompletionMessage[]> SendChatMessage(
        ChatCompletionMessage message)
      {
        //we should send all the messages
        //so we can give Open AI context of conversation
        StackMessages(message);
    
        var chatCompletion = new ChatCompletion
        {
          Request = new ChatCompletionRequest
          {
            Model = "gpt-3.5-turbo",
            Messages = _messages.ToArray(),
            Temperature = 0.2,
            MaxTokens = 800
          }
        };
    
        var result = await openAIClient
          .ChatCompletions
          .SendChatCompletionAsync(chatCompletion);
    
        var choices = result.Response.Choices;
    
        var messages = ToCompletionMessage(choices);
    
        //stack the response as well - everything is context to Open AI
        StackMessages(messages);
    
        return messages;
      }
    }
    

    STEP 5: Set up the API key

    IOpenAIProxy chatOpenAI = new OpenAIProxy(
        apiKey: "YOUR-API-KEY",
        organizationId: "YOUR-ORGANIZATION-ID");
    

    STEP 6: Use the Chat GPT model in our application

    var msg = Console.ReadLine();
    
    do
    {
      var results = await chatOpenAI.SendChatMessage(msg);
    
      foreach (var item in results)
      {
        Console.WriteLine($"{item.Role}: {item.Content}");
      }
      
      Console.WriteLine("Next Prompt:");
      msg = Console.ReadLine();
      
    } while (msg != "bye");
    

    Screenshot:

    Screenshot