Getting a 404 error when calling OpenAI resource

Peter Beery 30 Reputation points
2024-01-04T15:00:32.8733333+00:00

I am making a call to an OpenAI resource, but I am receiving a 404 resource not found error. I am unsure what is causing this issue. Can someone offer any guidance or potential reasons why this error is occurring?

        private async Task<string> TranslateTextWithOpenAI(string text, string targetLanguage)
        {
            var data = new
            {
                prompt = $"{text}\n\nTranslate this to {targetLanguage}." + OpenAIPrompt,
                max_tokens = 200,
                temperature = 0.7,
                model = "text-davinci-003"
            };

            var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"); // this content is correct. - PDB
            _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", OpenAI_ApiKey);

            var response = await _httpClient.PostAsync("https://myfoo.openai.azure.com/", content);  // this call is failing - PDB
            if (response.IsSuccessStatusCode)
            {
                var responseString = await response.Content.ReadAsStringAsync();
                var result = JsonConvert.DeserializeObject<OpenAIResponse>(responseString);
                return result.Choices[0].Text;
            }

            return null;
        }

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
4,080 questions
0 comments No comments
{count} votes

Accepted answer
  1. Saurabh Sharma 23,846 Reputation points Microsoft Employee Moderator
    2024-01-05T01:42:05.8133333+00:00

    Hi @Peter Beery

    Welcome to Microsoft Q&A! Thanks for posting the question.

    You are getting this error as you are not passing the open ai completion endpoint correctly. If you are using the api-key then you have change the headers to include "api-key" attribute. Also, you need to serialize the data object before passing it to the url.

    Please refer to REST API versioning for endpoint details.

    You can use the code something like below to get the results.

    var data = new
    {
        prompt = $"{text}\n\nTranslate this to {targetLanguage}." + OpenAIPrompt,
        max_tokens = 200,
        temperature = 0.7                
    };
    
    var dataJson = JsonSerializer.Serialize(data);
    var content = new StringContent(dataJson, Encoding.UTF8, "application/json");
    
    OpenAI_Endpoint = "https://{openairesource}.openai.azure.com/openai/deployments/test_chatgpt/completions?api-version=2023-09-15-preview";          
    httpClient.DefaultRequestHeaders.Add("api-key", OpenAI_ApiKey);
    
    var response = await httpClient.PostAsync(OpenAI_Endpoint, content);  // this call is failing - PDB
    if (response.IsSuccessStatusCode)
    {
        var responseString = await response.Content.ReadAsStringAsync();
        return responseString;
    }
    

    In the above code "test_chatgpt" will be the model you have deployed your Open AI resource.

    I have tested the above code and it works as expected.

    Also, I would suggest you leverage Azure Open AI SDK to easily implement the completions or chat completions.

    Additional resources - https://github.com/Azure-Samples/openai-dotnet-samples
    Please let me know if you have any other questions.

    Thanks

    Saurabh


    Please 'Accept as answer' and Upvote if it helped so that it can help others in the community looking for help on similar topics.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.