I'm getting 404 resource not found

Jhope 20 Reputation points
2024-01-06T14:45:00.07+00:00

I'm getting an 404 error the resources is not found, I want to call to my OpenAI resource. I don't know why I'm getting the error. I'll give the code below suggest if I'm missing anything

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,049 questions
0 comments No comments
{count} votes

Accepted answer
  1. Azar 29,515 Reputation points MVP Volunteer Moderator
    2024-01-06T14:56:21.17+00:00

    Hi
    Jhope
    Thanks for posting the question.

    I think you are not passing the open ai completion endpoint correctly. let me give you an updated code let me know if it helps,

    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;
    }
    
    
    

    If this helps kindly accept the answer and thanks again.

    0 comments No comments

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.