Skip to content

Azure's example code does not compile when I reference Azure.AI.OpenAI 2.1.0 #311

@MarkWard0110

Description

@MarkWard0110

Service

OpenAI

Describe the bug

I clicked on the view code in a Chat playground and selected C# language.

It provides the following code that has reference errors.
Executing dotnet add package Azure.AI.OpenAI --prerelease adds reference to Azure.AI.OpenAI 2.1.0

The code it provides is the following

      // Install the .NET library via NuGet: dotnet add package Azure.AI.OpenAI --prerelease
      using System;  
      using System.Collections.Generic;  
      using System.Threading.Tasks;  
      using Azure;  
      using Azure.AI.OpenAI;  
      using Azure.Identity;  
      using Microsoft.CognitiveServices.Speech;  
      using Microsoft.CognitiveServices.Speech.Audio;  
      using static System.Environment;  
        
      async Task RunAsync()  
      {  
          // Retrieve the OpenAI endpoint from environment variables
          string endpoint = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");  
          if (string.IsNullOrEmpty(endpoint))  
          {  
              Console.WriteLine("Please set the AZURE_OPENAI_ENDPOINT environment variable.");  
              return;  
          }  
        
          // Use DefaultAzureCredential for Entra ID authentication
          var credential = new DefaultAzureCredential();  
        
          // Initialize the AzureOpenAIClient
          var azureClient = new AzureOpenAIClient(new Uri(endpoint), credential);  
        
          // Initialize the ChatClient with the specified deployment name
          ChatClient chatClient = azureClient.GetChatClient("Meta-Llama-3.1-8B-Instruct");  
          
          // Create a list of chat messages
          var messages = new List<ChatMessage>  
          {  
              new SystemChatMessage(""),  
                
          };  
        
          // Create chat completion options
          var options = new ChatCompletionsOptions  
          {  
              PastMessages = 10,  
              Temperature = (float)0.7,  
              MaxTokens = 800,  
                
              NucleusSamplingFactor = (float)0.95,  
              FrequencyPenalty = 0,  
              PresencePenalty = 0,  
          };  
        
          try  
          {  
              // Create the chat completion request
              ChatCompletion completion = await chatClient.CompleteChatAsync(messages, options);  
        
              // Print the response
              if (completion.Choices != null && completion.Choices.Count > 0)  
              {  
                  Console.WriteLine($"{completion.Choices[0].Message.Role}: {completion.Choices[0].Message.Content}");  
              }  
              else  
              {  
                  Console.WriteLine("No response received.");  
              }  
          }  
          catch (Exception ex)  
          {  
              Console.WriteLine($"An error occurred: {ex.Message}");  
          }  
      }  
        
      await RunAsync();

The errors

The type or namespace name 'ChatCompletionsOptions' could not be found (are you missing a using directive or an assembly reference?)
'ChatCompletion' does not contain a definition for 'Choices' and no accessible extension method 'Choices' accepting a first argument of type 'ChatCompletion' could be found (are you missing a using directive or an assembly reference?)
'ChatCompletion' does not contain a definition for 'Choices' and no accessible extension method 'Choices' accepting a first argument of type 'ChatCompletion' could be found (are you missing a using directive or an assembly reference?)
'ChatCompletion' does not contain a definition for 'Choices' and no accessible extension method 'Choices' accepting a first argument of type 'ChatCompletion' could be found (are you missing a using directive or an assembly reference?)
'ChatCompletion' does not contain a definition for 'Choices' and no accessible extension method 'Choices' accepting a first argument of type 'ChatCompletion' could be found (are you missing a using directive or an assembly reference?)

Steps to reproduce

Create a Meta Llama 3.1 8b model+endpoint and open it in the playground. click on the view code. Copy the provided code into a C# source file, add the reference to the Azure.AI.OpenAI and try to build.

Code snippets

No response

OS

winOS

.NET version

.NET 8

Library version

2.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugCategory: Something isn't working and appears to be a defect in the client library.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions