-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathStep02_Assistant_Plugins.cs
92 lines (80 loc) · 3.34 KB
/
Step02_Assistant_Plugins.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
91
92
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents.OpenAI;
using Microsoft.SemanticKernel.ChatCompletion;
using OpenAI.Assistants;
using Plugins;
namespace GettingStarted.OpenAIAssistants;
/// <summary>
/// Demonstrate creation of <see cref="OpenAIAssistantAgent"/> with a <see cref="KernelPlugin"/>,
/// and then eliciting its response to explicit user messages.
/// </summary>
public class Step02_Assistant_Plugins(ITestOutputHelper output) : BaseAssistantTest(output)
{
[Fact]
public async Task UseAssistantWithPluginAsync()
{
// Define the agent
OpenAIAssistantAgent agent = await CreateAssistantAgentAsync(
plugin: KernelPluginFactory.CreateFromType<MenuPlugin>(),
instructions: "Answer questions about the menu.",
name: "Host");
// Create a thread for the agent conversation.
string threadId = await this.AssistantClient.CreateThreadAsync(metadata: SampleMetadata);
// Respond to user input
try
{
await InvokeAgentAsync(agent, threadId, "Hello");
await InvokeAgentAsync(agent, threadId, "What is the special soup and its price?");
await InvokeAgentAsync(agent, threadId, "What is the special drink and its price?");
await InvokeAgentAsync(agent, threadId, "Thank you");
}
finally
{
await this.AssistantClient.DeleteThreadAsync(threadId);
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
}
}
[Fact]
public async Task UseAssistantWithPluginEnumParameterAsync()
{
// Define the agent
OpenAIAssistantAgent agent = await CreateAssistantAgentAsync(plugin: KernelPluginFactory.CreateFromType<WidgetFactory>());
// Create a thread for the agent conversation.
string threadId = await this.AssistantClient.CreateThreadAsync(metadata: SampleMetadata);
// Respond to user input
try
{
await InvokeAgentAsync(agent, threadId, "Create a beautiful red colored widget for me.");
}
finally
{
await this.AssistantClient.DeleteThreadAsync(threadId);
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
}
}
private async Task<OpenAIAssistantAgent> CreateAssistantAgentAsync(KernelPlugin plugin, string? instructions = null, string? name = null)
{
// Define the assistant
Assistant assistant =
await this.AssistantClient.CreateAssistantAsync(
this.Model,
name,
instructions: instructions,
metadata: SampleMetadata);
// Create the agent
OpenAIAssistantAgent agent = new(assistant, this.AssistantClient, [plugin]);
return agent;
}
// Local function to invoke agent and display the conversation messages.
private async Task InvokeAgentAsync(OpenAIAssistantAgent agent, string threadId, string input)
{
ChatMessageContent message = new(AuthorRole.User, input);
await agent.AddChatMessageAsync(threadId, message);
this.WriteAgentChatMessage(message);
await foreach (ChatMessageContent response in agent.InvokeAsync(threadId))
{
this.WriteAgentChatMessage(response);
}
}
}