Azure OpenAI - C# Sample Code Not working - ChatCompletionsOptions not resolved

Onyango, David 21 Reputation points
2024-12-31T13:15:39.8833333+00:00

Hi,

I need help with this sample code below generated on Azure OpenAI after creating a model. I have installed all possible Nuget packages including prereleases and still cannot get it to work on both Visual Studio Code and Enterprise. In particular, some properties such as "PastMessages" in ChatCompletionsOptions class is not getting resolved. Same behaviour on ChatCompletion class.

// Install the .NET library via NuGet: dotnet add package Azure.AI.OpenAI --prerelease using Azure; using Azure.AI.OpenAI; using Azure.Identity; using OpenAI.Chat; using static System.Environment; string endpoint = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"); string key = GetEnvironmentVariable("AZURE_OPENAI_KEY"); AzureKeyCredential credential = new AzureKeyCredential(key); AzureOpenAIClient azureClient = new(new Uri(endpoint), credential); ChatClient chatClient = azureClient.GetChatClient("gpt-35-turbo"); ChatCompletion completion = chatClient.CompleteChat( new ChatMessage[] { new SystemChatMessage("You are an AI assistant that helps people find information."), }, new ChatCompletionsOptions() { PastMessages = 10, Temperature = (float)0.7, MaxTokens = 800, StopSequences = [], NucleusSamplingFactor = (float)0.95, FrequencyPenalty = (float)0, PresencePenalty = (float)0, } ); Console.WriteLine($"{completion.Choices[0].Message.Role}: {completion.Choices[0].Message.Content}");

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

1 answer

Sort by: Most helpful
  1. Daniel FANG 960 Reputation points MVP
    2025-01-02T10:37:49.13+00:00

    my best guess is that Azure.AI.OpenAI --prerelease could be an old version of the package. here is a working version of c# sample for chat completion based on ("Azure.AI.OpenAI" Version="2.1.0")

    using System;
    using System.ClientModel;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Azure.AI.OpenAI;
    using OpenAI.Chat;
    
    class Program
    {
        static async Task Main(string[] args)
        {
    
            var deploymentName = "xxxxx";
            var endpointUrl = "https://xxxxx-openai.openai.azure.com/";
            var key = "xxxx";
    
            var client = new AzureOpenAIClient(new Uri(endpointUrl), new ApiKeyCredential(key));
            var chatClient = client.GetChatClient(deploymentName);
            var messages = new List<ChatMessage>();
            messages.Add(new SystemChatMessage("You are an AI assistant that helps people find information."));
            messages.Add(new UserChatMessage("hi"));
    
            var response = await chatClient.CompleteChatAsync(messages,  new ChatCompletionOptions()
                        {
                            Temperature = (float)0.7,
                            FrequencyPenalty = (float)0,
                            PresencePenalty = (float)0,
                        });
            var chatResponse = response.Value.Content.Last().Text;
    
            Console.WriteLine(chatResponse);
    
        }
    }
    

    it should return below

    User's image Not sure which page in azure portal you got the sample c# code. here are a few repos for more example c# codes:

    https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/openai/Azure.AI.OpenAI/README.md

    https://www.nuget.org/packages/Azure.AI.OpenAI


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.