What is wrong with Completion API?

Here is the code that I have in C#. It worked perfectly before. I use gpt-3.5-turbo model.
Now it returns BadRequest (400).
I verified that ApiKey is correct.
The URL is: https://api.openai.com/v1/chat/completions

So what do I do wrong?

            HttpClient client = new HttpClient();

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

            string jsonContent = JsonConvert.SerializeObject(request, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
            var stringContent = new StringContent(jsonContent, UnicodeEncoding.UTF8, "application/json");
            string url = String.Format("{0}/chat/completions", Api.BaseUrl);

            Log.VerboseFormat("Calling SendAsync with URL: {0}, and Engine: {1}", url, request.Model);

            using (HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, url))
            {
                string apiKey = Api.mConfig.SafeStorage.DecryptString(Api.mConfig.ApiKey);
                req.Content = stringContent;
                req.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", apiKey);
                req.Headers.Add("Accept", "application/json");
                req.Headers.Add("User-Agent", "GSS/OpenAI_GPT3");

                var response = await client.SendAsync(req, HttpCompletionOption.ResponseHeadersRead);

                if (response.IsSuccessStatusCode)
                {
                    // API failed with BadRequest (400)
                }

The json string input:

Answering my own question:
It was an issue with max_tokens - I originally had it at 4096, and when I lowered it to 3,500 the response is successful. My guess is that the initial size of the prompt and context matter and is part of 4,096.

1 Like