在本快速入门中,你将创建一个聊天应用,用于请求具有 结构化输出的响应。 结构化输出响应是一个聊天响应,它是你指定的类型,而不是纯文本。 在本快速入门中创建的聊天应用分析各种产品评论的情绪,根据自定义枚举的值对每个评审进行分类。
先决条件
配置 AI 服务
若要使用 Azure 门户预配 Azure OpenAI 服务和模型,请完成创建和部署 Azure OpenAI 服务资源一文中的步骤。 在“部署模型”步骤中,选择模型 gpt-4o
。
创建聊天应用
完成以下步骤以创建连接到 gpt-4o
AI 模型的控制台应用。
在终端窗口中,导航到要在其中创建应用的目录,并使用
dotnet new
以下命令创建新的控制台应用:dotnet new console -o SOChat
导航到
SOChat
目录,并将必要的包添加到应用:dotnet add package Azure.AI.OpenAI dotnet add package Azure.Identity dotnet add package Microsoft.Extensions.AI dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease dotnet add package Microsoft.Extensions.Configuration dotnet add package Microsoft.Extensions.Configuration.UserSecrets
运行以下命令,为 Azure OpenAI 终结点、模型名称和租户 ID 添加 应用机密 :
dotnet user-secrets init dotnet user-secrets set AZURE_OPENAI_ENDPOINT <your-Azure-OpenAI-endpoint> dotnet user-secrets set AZURE_OPENAI_GPT_NAME gpt-4o dotnet user-secrets set AZURE_TENANT_ID <your-tenant-ID>
注释
根据环境,可能不需要租户 ID。 在这种情况下,请将其从实例化 DefaultAzureCredential的代码中删除。
在所选编辑器中打开新应用。
添加代码
定义描述不同情绪的枚举。
public enum Sentiment { Positive, Negative, Neutral }
创建用于与模型通信的IChatClient。
IConfigurationRoot config = new ConfigurationBuilder() .AddUserSecrets<Program>() .Build(); string endpoint = config["AZURE_OPENAI_ENDPOINT"]; string model = config["AZURE_OPENAI_GPT_NAME"]; string tenantId = config["AZURE_TENANT_ID"]; // Get a chat client for the Azure OpenAI endpoint. AzureOpenAIClient azureClient = new( new Uri(endpoint), new DefaultAzureCredential(new DefaultAzureCredentialOptions() { TenantId = tenantId })); IChatClient chatClient = azureClient .GetChatClient(deploymentName: model) .AsIChatClient();
注释
DefaultAzureCredential 搜索来自环境或本地工具的身份验证凭据。 需要将
Azure AI Developer
角色分配给用于登录到 Visual Studio 或 Azure CLI 的帐户。 有关详细信息,请参阅使用 .NET 向 Azure AI 服务进行身份验证。使用单个产品评审向模型发送请求,然后将分析的情绪输出到控制台。 通过将请求的结构化输出类型作为类型参数 ChatClientStructuredOutputExtensions.GetResponseAsync<T>(IChatClient, String, ChatOptions, Nullable<Boolean>, CancellationToken) 传递给扩展方法来声明它。
string review = "I'm happy with the product!"; var response = await chatClient.GetResponseAsync<Sentiment>($"What's the sentiment of this review? {review}"); Console.WriteLine($"Sentiment: {response.Result}");
此代码生成类似于:
Sentiment: Positive
可以分析一系列评论,而不是只分析单个评论。
string[] inputs = [ "Best purchase ever!", "Returned it immediately.", "Hello", "It works as advertised.", "The packaging was damaged but otherwise okay." ]; foreach (var i in inputs) { var response2 = await chatClient.GetResponseAsync<Sentiment>($"What's the sentiment of this review? {i}"); Console.WriteLine($"Review: {i} | Sentiment: {response2.Result}"); }
此代码生成类似于:
Review: Best purchase ever! | Sentiment: Positive Review: Returned it immediately. | Sentiment: Negative Review: Hello | Sentiment: Neutral Review: It works as advertised. | Sentiment: Neutral Review: The packaging was damaged but otherwise okay. | Sentiment: Neutral
除了只请求已分析的枚举值,还可以请求文本响应以及分析的值。
定义包含文本响应和分析情绪的记录类型:
record SentimentRecord(string ResponseText, Sentiment ReviewSentiment);
将使用记录类型作为类型参数的请求发送到
GetResponseAsync<T>
:var review3 = "This product worked okay."; var response3 = await chatClient.GetResponseAsync<SentimentRecord>($"What's the sentiment of this review? {review3}"); Console.WriteLine($"Response text: {response3.Result.ResponseText}"); Console.WriteLine($"Sentiment: {response3.Result.ReviewSentiment}");
此代码生成类似于:
Response text: Certainly, I have analyzed the sentiment of the review you provided. Sentiment: Neutral
清理资源
如果不再需要它们,请删除 Azure OpenAI 资源和 GPT-4 模型部署。
- 在 Azure 门户中,导航到 Azure OpenAI 资源。
- 选择 Azure OpenAI 资源,然后选择 删除。