|
1 | 1 | //<chat_completion> |
2 | | -using Azure; |
| 2 | +using System.ClientModel.Primitives; |
3 | 3 | using Azure.Identity; |
4 | | -using Azure.AI.Projects; |
5 | | -using Azure.AI.Inference; |
| 4 | +using OpenAI; |
| 5 | +using OpenAI.Chat; |
6 | 6 |
|
7 | | -var projectEndpoint = new Uri(System.Environment.GetEnvironmentVariable("AZURE_AI_ENDPOINT")); |
8 | | -var modelDeploymentName = System.Environment.GetEnvironmentVariable("AZURE_AI_MODEL"); |
9 | | -var credential = new DefaultAzureCredential(); |
| 7 | +#pragma warning disable OPENAI001 |
10 | 8 |
|
11 | | -AIProjectClient client = new AIProjectClient(projectEndpoint, credential); |
| 9 | +string projectEndpoint = System.Environment.GetEnvironmentVariable("AZURE_AI_ENDPOINT")!; |
| 10 | +string modelDeploymentName = System.Environment.GetEnvironmentVariable("AZURE_AI_MODEL")!; |
12 | 11 |
|
13 | | -ChatCompletionsClient chatClient = client.GetChatCompletionsClient(); |
| 12 | +BearerTokenPolicy tokenPolicy = new( |
| 13 | + new DefaultAzureCredential(), |
| 14 | + "https://ai.azure.com/.default"); |
| 15 | +OpenAIClient openAIClient = new( |
| 16 | + authenticationPolicy: tokenPolicy, |
| 17 | + options: new OpenAIClientOptions() |
| 18 | + { |
| 19 | + Endpoint = new($"{projectEndpoint}/openai/v1"), |
| 20 | + }); |
| 21 | +ChatClient chatClient = openAIClient.GetChatClient(modelDeploymentName); |
14 | 22 |
|
15 | | -var requestOptions = new ChatCompletionsOptions() |
16 | | -{ |
17 | | - Messages = |
18 | | - { |
19 | | - new ChatRequestSystemMessage("You are a helpful assistant."), |
20 | | - new ChatRequestUserMessage("How many feet are in a mile?"), |
21 | | - }, |
22 | | - Model = modelDeploymentName |
23 | | -}; |
24 | | -Response<ChatCompletions> response = chatClient.Complete(requestOptions); |
25 | | -Console.WriteLine(response.Value.Content); |
| 23 | +ChatCompletion completion = await chatClient.CompleteChatAsync( |
| 24 | + [ |
| 25 | + new SystemChatMessage("You are a helpful assistant."), |
| 26 | + new UserChatMessage("How many feet are in a mile?") |
| 27 | + ]); |
| 28 | + |
| 29 | +Console.WriteLine(completion.Content[0].Text); |
26 | 30 | // </chat_completion> |
0 commit comments