Migrate from 1.0 beta to 2.0 (Azure.AI.OpenAI
)
Note
This guide describes how to migrate an application that previously used a 1.0 beta version of the Azure.AI.OpenAI
library to use the new 2.0 library.
For general guidance on using OpenAI
and Azure.AI.OpenAI
features, see the OpenAI README or the Azure.AI.OpenAI README.
Setup
Stable releases of Azure.AI.OpenAI
are associated with a corresponding stable Azure OpenAI Service API version label, for example, 2024-10-21
.
dotnet add package Azure.AI.OpenAI
Beta releases of Azure.AI.OpenAI
are associated with a corresponding preview Azure OpenAI Service API version label, for example, 2024-10-01-preview
.
dotnet add package Azure.AI.OpenAI --prerelease
Client configuration
Although client instantiation is similar to 1.0, 2.0 introduces a distinct, Azure-specific top-level client that individual scenario clients are retrieved from.
// 1.0 - BEFORE: Getting a general-purpose client ready for use in 1.0
OpenAIClient client = new(
new Uri("https://your-resource.openai.azure.com/"),
new AzureKeyCredential(Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY"));
Chat completion
// 1.0 - BEFORE
OpenAIClient client = new(azureOpenAIResourceUri, azureOpenAIApiKey);
var chatCompletionsOptions = new ChatCompletionsOptions()
{
DeploymentName = "gpt-3.5-turbo", // Use DeploymentName for "model" with non-Azure clients
Messages =
{
// The system message represents instructions or other guidance about how the assistant should behave
new ChatRequestSystemMessage("You are a helpful assistant. You will talk like a pirate."),
// User messages represent current or historical input from the end user
new ChatRequestUserMessage("Can you help me?"),
// Assistant messages represent historical responses from the assistant
new ChatRequestAssistantMessage("Arrrr! Of course, me hearty! What can I do for ye?"),
new ChatRequestUserMessage("What's the best way to train a parrot?"),
}
};
Consuming chat completions response is simplified in 2.0.
// 1.0 - BEFORE:
Response<ChatCompletions> response = await client.GetChatCompletionsAsync(chatCompletionsOptions);
ChatResponseMessage responseMessage = response.Value.Choices[0].Message;
Console.WriteLine($"[{responseMessage.Role.ToString().ToUpperInvariant()}]: {responseMessage.Content}");
Streaming
// 1.0 - BEFORE
await foreach (StreamingChatCompletionsUpdate chatUpdate in client.GetChatCompletionsStreaming(chatCompletionsOptions))
{
if (chatUpdate.Role.HasValue)
{
Console.Write($"{chatUpdate.Role.Value.ToString().ToUpperInvariant()}: ");
}
if (!string.IsNullOrEmpty(chatUpdate.ContentUpdate))
{
Console.Write(chatUpdate.ContentUpdate);
}
}
Tool definitions
// 1.0 - BEFORE
var getWeatherTool = new ChatCompletionsFunctionToolDefinition()
{
Name = "get_current_weather",
Description = "Get the current weather in a given location",
Parameters = BinaryData.FromObjectAsJson(
new
{
Type = "object",
Properties = new
{
Location = new
{
Type = "string",
Description = "The city and state, e.g. San Francisco, CA",
},
Unit = new
{
Type = "string",
Enum = new[] { "celsius", "fahrenheit" },
}
},
Required = new[] { "location" },
},
new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }),
};
var chatCompletionsOptions = new ChatCompletionsOptions()
{
DeploymentName = "gpt-35-turbo-1106",
Messages = { new ChatRequestUserMessage("What's the weather like in Boston?") },
Tools = { getWeatherTool },
};
Response<ChatCompletions> response = await client.GetChatCompletionsAsync(chatCompletionsOptions);
Handling tool call responses
// 1.0 - BEFORE
var chatCompletionsOptions = new ChatCompletionsOptions()
{
DeploymentName = "gpt-35-turbo-1106",
Messages = { new ChatRequestUserMessage("What's the weather like in Boston?") },
Tools = { getWeatherTool },
};
Response<ChatCompletions> response = await client.GetChatCompletionsAsync(chatCompletionsOptions);
Chat with On Your Data
// 1.0 - BEFORE
AzureSearchChatExtensionConfiguration contosoExtensionConfig = new()
{
SearchEndpoint = new Uri("https://your-contoso-search-resource.search.windows.net"),
Authentication = new OnYourDataApiKeyAuthenticationOptions("<your Cognitive Search resource API key>"),
};
ChatCompletionsOptions chatCompletionsOptions = new()
{
DeploymentName = "gpt-35-turbo-0613",
Messages =
{
new ChatRequestSystemMessage(
"You are a helpful assistant that answers questions about the Contoso product database."),
new ChatRequestUserMessage("What are the best-selling Contoso products this month?")
},
// The addition of AzureChatExtensionsOptions enables the use of Azure OpenAI capabilities that add to
// the behavior of Chat Completions, here the "using your own data" feature to supplement the context
// with information from an Azure Cognitive Search resource with documents that have been indexed.
AzureExtensionsOptions = new AzureChatExtensionsOptions()
{
Extensions = { contosoExtensionConfig }
}
};
Response<ChatCompletions> response = await client.GetChatCompletionsAsync(chatCompletionsOptions);
Embeddings
// 1.0 - BEFORE
EmbeddingsOptions embeddingsOptions = new()
{
DeploymentName = "text-embedding-ada-002",
Input = { "Your text string goes here" },
};
Response<Embeddings> response = await client.GetEmbeddingsAsync(embeddingsOptions);
// The response includes the generated embedding.
EmbeddingItem item = response.Value.Data[0];
ReadOnlyMemory<float> embedding = item.Embedding;
Image generation
// 1.0 - BEFORE
Response<ImageGenerations> response = await client.GetImageGenerationsAsync(
new ImageGenerationOptions()
{
DeploymentName = usingAzure ? "my-azure-openai-dall-e-3-deployment" : "dall-e-3",
Prompt = "a happy monkey eating a banana, in watercolor",
Size = ImageSize.Size1024x1024,
Quality = ImageGenerationQuality.Standard
});
ImageGenerationData generatedImage = response.Value.Data[0];
if (!string.IsNullOrEmpty(generatedImage.RevisedPrompt))
{
Console.WriteLine($"Input prompt automatically revised to: {generatedImage.RevisedPrompt}");
}
Console.WriteLine($"Generated image available at: {generatedImage.Url.AbsoluteUri}");
Audio transcription
// 1.0 - BEFORE
using Stream audioStreamFromFile = File.OpenRead("myAudioFile.mp3");
var transcriptionOptions = new AudioTranscriptionOptions()
{
DeploymentName = "my-whisper-deployment", // whisper-1 as model name for non-Azure OpenAI
AudioData = BinaryData.FromStream(audioStreamFromFile),
Filename = "test.mp3",
ResponseFormat = AudioTranscriptionFormat.Verbose,
};
Response<AudioTranscription> transcriptionResponse
= await client.GetAudioTranscriptionAsync(transcriptionOptions);
AudioTranscription transcription = transcriptionResponse.Value;
// When using Simple, SRT, or VTT formats, only transcription.Text will be populated
Console.WriteLine($"Transcription ({transcription.Duration.Value.TotalSeconds}s):");
Console.WriteLine(transcription.Text);