-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
OpenAIAssistant_ChartMaker.cs
79 lines (68 loc) · 2.75 KB
/
OpenAIAssistant_ChartMaker.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
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.OpenAI;
using Microsoft.SemanticKernel.ChatCompletion;
using OpenAI.Files;
namespace Agents;
/// <summary>
/// Demonstrate using code-interpreter with <see cref="OpenAIAssistantAgent"/> to
/// produce image content displays the requested charts.
/// </summary>
public class OpenAIAssistant_ChartMaker(ITestOutputHelper output) : BaseAgentsTest(output)
{
private const string AgentName = "ChartMaker";
private const string AgentInstructions = "Create charts as requested without explanation.";
[Fact]
public async Task GenerateChartWithOpenAIAssistantAgentAsync()
{
OpenAIClientProvider provider = this.GetClientProvider();
OpenAIFileClient fileClient = provider.Client.GetOpenAIFileClient();
// Define the agent
OpenAIAssistantAgent agent =
await OpenAIAssistantAgent.CreateAsync(
provider,
definition: new OpenAIAssistantDefinition(this.Model)
{
Instructions = AgentInstructions,
Name = AgentName,
EnableCodeInterpreter = true,
Metadata = AssistantSampleMetadata,
},
kernel: new());
// Create a chat for agent interaction.
AgentGroupChat chat = new();
// Respond to user input
try
{
await InvokeAgentAsync(
"""
Display this data using a bar-chart:
Banding Brown Pink Yellow Sum
X00000 339 433 126 898
X00300 48 421 222 691
X12345 16 395 352 763
Others 23 373 156 552
Sum 426 1622 856 2904
""");
await InvokeAgentAsync("Can you regenerate this same chart using the category names as the bar colors?");
await InvokeAgentAsync("Perfect, can you regenerate this as a line chart?");
}
finally
{
await agent.DeleteAsync();
}
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
{
ChatMessageContent message = new(AuthorRole.User, input);
chat.AddChatMessage(new(AuthorRole.User, input));
this.WriteAgentChatMessage(message);
await foreach (ChatMessageContent response in chat.InvokeAsync(agent))
{
this.WriteAgentChatMessage(response);
await this.DownloadResponseImageAsync(fileClient, response);
}
}
}
}