forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStep02_BedrockAgent_CodeInterpreter.cs
90 lines (80 loc) · 3.49 KB
/
Step02_BedrockAgent_CodeInterpreter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) Microsoft. All rights reserved.
using System.Reflection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents.Bedrock;
using Microsoft.SemanticKernel.Agents.Bedrock.Extensions;
namespace GettingStarted.BedrockAgents;
/// <summary>
/// This example demonstrates how to interact with a <see cref="BedrockAgent"/> with code interpreter enabled.
/// </summary>
public class Step02_BedrockAgent_CodeInterpreter(ITestOutputHelper output) : BaseBedrockAgentTest(output)
{
private const string UserQuery = @"Create a bar chart for the following data:
Panda 5
Tiger 8
Lion 3
Monkey 6
Dolphin 2";
/// <summary>
/// Demonstrates how to create a new <see cref="BedrockAgent"/> with code interpreter enabled and interact with it.
/// The agent will respond to the user query by creating a Python code that will be executed by the code interpreter.
/// The output of the code interpreter will be a file containing the bar chart, which will be returned to the user.
/// </summary>
[Fact]
public async Task UseAgentWithCodeInterpreterAsync()
{
// Create the agent
var bedrockAgent = await this.CreateAgentAsync("Step02_BedrockAgent_CodeInterpreter");
// Respond to user input
try
{
BinaryContent? binaryContent = null;
var responses = bedrockAgent.InvokeAsync(BedrockAgent.CreateSessionId(), UserQuery, null);
await foreach (var response in responses)
{
if (response.Content != null)
{
this.Output.WriteLine(response.Content);
}
if (binaryContent == null && response.Items.Count > 0)
{
binaryContent = response.Items.OfType<BinaryContent>().FirstOrDefault();
}
}
if (binaryContent == null)
{
throw new InvalidOperationException("No file found in the response.");
}
// Save the file to the same directory as the test assembly
var filePath = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!,
binaryContent.Metadata!["Name"]!.ToString()!);
this.Output.WriteLine($"Saving file to {filePath}");
binaryContent.WriteToFile(filePath, overwrite: true);
// Expected output:
// Here is the bar chart for the given data:
// [A bar chart showing the following data:
// Panda 5
// Tiger 8
// Lion 3
// Monkey 6
// Dolphin 2]
// Saving file to ...
}
finally
{
await this.Client.DeleteAgentAsync(new() { AgentId = bedrockAgent.Id });
}
}
protected override async Task<BedrockAgent> CreateAgentAsync(string agentName)
{
// Create a new agent on the Bedrock Agent service and prepare it for use
var agentModel = await this.Client.CreateAndPrepareAgentAsync(this.GetCreateAgentRequest(agentName));
// Create a new BedrockAgent instance with the agent model and the client
// so that we can interact with the agent using Semantic Kernel contents.
var bedrockAgent = new BedrockAgent(agentModel, this.Client);
// Create the code interpreter action group and prepare the agent for interaction
await bedrockAgent.CreateCodeInterpreterActionGroupAsync();
return bedrockAgent;
}
}