Search code examples
c#azureazure-openai

c# Azure.AI.OpenAI with errors at start


I am trying to follow the Microsoft how to (in its c# way):
https://learn.microsoft.com/en-us/azure/ai-services/openai/use-your-data-quickstart?tabs=command-line%2Cpython&pivots=programming-language-csharp

Creating the code with the template as instructions indicate:
dotnet new console -n azure-openai-quickstart

After I have added requested package:
dotnet add package Azure.AI.OpenAI --prerelease
It added the package Azure.AI.OpenAI 1.0.0-beta.12

Open code with my Visual Studio and I have several errors.
Starting with ChatMessage and following with SearchKey property inside AzureCognitiveSearchChatExtensionConfiguration class:

enter image description here

Do you have any idea how to solve this and follow with the How-to?

Why Microsoft leave an incorrect and not functional how-to to "help" people to start with a technology?

This is the .csproj file as requested in comments:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <RootNamespace>azure_openai_quickstart</RootNamespace>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.12" />
  </ItemGroup>

</Project>

Solution

  • The issue is with version 1.0.0-beta.12 of Azure.AI.OpenAI Nuget package. The code in the sample is compatible with version 1.0.0-beta.8 and there have been many changes (including breaking ones) between these 2 versions. For more details, please see the changelog.

    There are 2 possible solutions:

    1. Downgrade to version 1.0.0-beta.8. You would need to uninstall the current version and install 1.0.0-beta.8 using dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.8. Though I would not recommend doing it.
    2. Change the code so that it is compatible with version 1.0.0-beta.12.

    I made some changes in the code so that at least it compiles (haven't tried to run it though). Here's the code that compiles:

    using Azure;
    using Azure.AI.OpenAI;
    using System.Text.Json;
    using static System.Environment;
    
    string azureOpenAIEndpoint = GetEnvironmentVariable("AOAIEndpoint");
    string azureOpenAIKey = GetEnvironmentVariable("AOAIKey");
    string searchEndpoint = GetEnvironmentVariable("SearchEndpoint");
    string searchKey = GetEnvironmentVariable("SearchKey");
    string searchIndex = GetEnvironmentVariable("SearchIndex");
    string deploymentName = GetEnvironmentVariable("AOAIDeploymentId");
    
    var client = new OpenAIClient(new Uri(azureOpenAIEndpoint), new AzureKeyCredential(azureOpenAIKey));
    
    var chatCompletionsOptions = new ChatCompletionsOptions()
    {
        Messages =
        {
            new ChatRequestUserMessage("What are the differences between Azure Machine Learning and Azure AI services?"),
        },
        AzureExtensionsOptions = new AzureChatExtensionsOptions()
        {
            Extensions =
            {
                new AzureCognitiveSearchChatExtensionConfiguration()
                {
                    SearchEndpoint = new Uri(searchEndpoint),
                    Key = searchKey,
                    IndexName = searchIndex,
                },
            }
        },
        DeploymentName = deploymentName
    };
    
    Response<ChatCompletions> response = client.GetChatCompletions(chatCompletionsOptions);
    
    ChatResponseMessage responseMessage = response.Value.Choices[0].Message;
    
    Console.WriteLine($"Message from {responseMessage.Role}:");
    Console.WriteLine("===");
    Console.WriteLine(responseMessage.Content);
    Console.WriteLine("===");
    
    Console.WriteLine($"Context information (e.g. citations) from chat extensions:");
    Console.WriteLine("===");
    foreach (ChatResponseMessage contextMessage in responseMessage.AzureExtensionsContext.Messages)
    {
        string contextContent = contextMessage.Content;
        try
        {
            var contextMessageJson = JsonDocument.Parse(contextMessage.Content);
            contextContent = JsonSerializer.Serialize(contextMessageJson, new JsonSerializerOptions()
            {
                WriteIndented = true,
            });
        }
        catch (JsonException)
        {}
        Console.WriteLine($"{contextMessage.Role}: {contextContent}");
    }
    Console.WriteLine("===");
    

    BTW, I also opened an issue on GitHub regarding this: https://github.com/MicrosoftDocs/azure-docs/issues/118601.