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.