|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Text.Json; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Azure.Identity; |
| 10 | +using NUnit.Framework; |
| 11 | + |
| 12 | +namespace Azure.AI.OpenAI.Tests.Samples |
| 13 | +{ |
| 14 | + public partial class StreamingChat |
| 15 | + { |
| 16 | + [Test] |
| 17 | + [Ignore("Only verifying that the sample builds")] |
| 18 | + public async Task ChatUsingYourOwnData() |
| 19 | + { |
| 20 | + string endpoint = "https://myaccount.openai.azure.com/"; |
| 21 | + var client = new OpenAIClient(new Uri(endpoint), new DefaultAzureCredential()); |
| 22 | + |
| 23 | + #region Snippet:ChatUsingYourOwnData |
| 24 | + var chatCompletionsOptions = new ChatCompletionsOptions() |
| 25 | + { |
| 26 | + Messages = |
| 27 | + { |
| 28 | + new ChatMessage( |
| 29 | + ChatRole.System, |
| 30 | + "You are a helpful assistant that answers questions about the Contoso product database."), |
| 31 | + new ChatMessage(ChatRole.User, "What are the best-selling Contoso products this month?") |
| 32 | + }, |
| 33 | + // The addition of AzureChatExtensionsOptions enables the use of Azure OpenAI capabilities that add to |
| 34 | + // the behavior of Chat Completions, here the "using your own data" feature to supplement the context |
| 35 | + // with information from an Azure Cognitive Search resource with documents that have been indexed. |
| 36 | + AzureExtensionsOptions = new AzureChatExtensionsOptions() |
| 37 | + { |
| 38 | + Extensions = |
| 39 | + { |
| 40 | + new AzureCognitiveSearchChatExtensionConfiguration() |
| 41 | + { |
| 42 | + SearchEndpoint = new Uri("https://your-contoso-search-resource.search.windows.net"), |
| 43 | + IndexName = "contoso-products-index", |
| 44 | + SearchKey = new AzureKeyCredential("<your Cognitive Search resource API key>"), |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + }; |
| 49 | + Response<ChatCompletions> response = await client.GetChatCompletionsAsync( |
| 50 | + "gpt-35-turbo-0613", |
| 51 | + chatCompletionsOptions); |
| 52 | + ChatMessage message = response.Value.Choices[0].Message; |
| 53 | + // The final, data-informed response still appears in the ChatMessages as usual |
| 54 | + Console.WriteLine($"{message.Role}: {message.Content}"); |
| 55 | + // Responses that used extensions will also have Context information that includes special Tool messages |
| 56 | + // to explain extension activity and provide supplemental information like citations. |
| 57 | + Console.WriteLine($"Citations and other information:"); |
| 58 | + foreach (ChatMessage contextMessage in message.AzureExtensionsContext.Messages) |
| 59 | + { |
| 60 | + // Note: citations and other extension payloads from the "tool" role are often encoded JSON documents |
| 61 | + // and need to be parsed as such; that step is omitted here for brevity. |
| 62 | + Console.WriteLine($"{contextMessage.Role}: {contextMessage.Content}"); |
| 63 | + } |
| 64 | + #endregion |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments