|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | +using Azure.AI.Agents.Persistent; |
| 3 | +using Microsoft.SemanticKernel; |
| 4 | +using Microsoft.SemanticKernel.Agents.AzureAI; |
| 5 | +using Microsoft.SemanticKernel.ChatCompletion; |
| 6 | + |
| 7 | +namespace GettingStarted.AzureAgents; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Demonstrate parsing JSON response. |
| 11 | +/// </summary> |
| 12 | +public class Step10_JsonResponse(ITestOutputHelper output) : BaseAzureAgentTest(output) |
| 13 | +{ |
| 14 | + private const string TutorInstructions = |
| 15 | + """ |
| 16 | + Think step-by-step and rate the user input on creativity and expressiveness from 1-100. |
| 17 | +
|
| 18 | + Respond in JSON format with the following JSON schema: |
| 19 | +
|
| 20 | + { |
| 21 | + "score": "integer (1-100)", |
| 22 | + "notes": "the reason for your score" |
| 23 | + } |
| 24 | + """; |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public async Task UseJsonObjectResponse() |
| 28 | + { |
| 29 | + PersistentAgent definition = |
| 30 | + await this.Client.Administration.CreateAgentAsync( |
| 31 | + TestConfiguration.AzureAI.ChatModelId, |
| 32 | + instructions: TutorInstructions, |
| 33 | + responseFormat: |
| 34 | + BinaryData.FromString( |
| 35 | + """ |
| 36 | + { |
| 37 | + "type": "json_object" |
| 38 | + } |
| 39 | + """)); |
| 40 | + |
| 41 | + AzureAIAgent agent = new(definition, this.Client); |
| 42 | + |
| 43 | + await ExecuteAgent(agent); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public async Task UseJsonSchemaResponse() |
| 48 | + { |
| 49 | + PersistentAgent definition = |
| 50 | + await this.Client.Administration.CreateAgentAsync( |
| 51 | + TestConfiguration.AzureAI.ChatModelId, |
| 52 | + instructions: TutorInstructions, |
| 53 | + responseFormat: BinaryData.FromString( |
| 54 | + """ |
| 55 | + { |
| 56 | + "type": "json_schema", |
| 57 | + "json_schema": |
| 58 | + { |
| 59 | + "type": "object", |
| 60 | + "name": "scoring", |
| 61 | + "schema": { |
| 62 | + "type": "object", |
| 63 | + "properties": { |
| 64 | + "score": { |
| 65 | + "type": "number" |
| 66 | + }, |
| 67 | + "notes": { |
| 68 | + "type": "string" |
| 69 | + } |
| 70 | + }, |
| 71 | + "required": [ |
| 72 | + "score", |
| 73 | + "notes" |
| 74 | + ], |
| 75 | + "additionalProperties": false |
| 76 | + }, |
| 77 | + "strict": true |
| 78 | + } |
| 79 | + } |
| 80 | + """)); |
| 81 | + |
| 82 | + AzureAIAgent agent = new(definition, this.Client); |
| 83 | + |
| 84 | + await ExecuteAgent(agent); |
| 85 | + } |
| 86 | + |
| 87 | + private async Task ExecuteAgent(AzureAIAgent agent) |
| 88 | + { |
| 89 | + AzureAIAgentThread thread = new(agent.Client); |
| 90 | + |
| 91 | + await InvokeAgentAsync("The sunset is very colorful."); |
| 92 | + await InvokeAgentAsync("The sunset is setting over the mountains."); |
| 93 | + await InvokeAgentAsync("The sunset is setting over the mountains and filled the sky with a deep red flame, setting the clouds ablaze."); |
| 94 | + |
| 95 | + // Local function to invoke agent and display the conversation messages. |
| 96 | + async Task InvokeAgentAsync(string input) |
| 97 | + { |
| 98 | + ChatMessageContent message = new(AuthorRole.User, input); |
| 99 | + this.WriteAgentChatMessage(message); |
| 100 | + |
| 101 | + await foreach (ChatMessageContent response in agent.InvokeAsync(message, thread)) |
| 102 | + { |
| 103 | + this.WriteAgentChatMessage(response); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments