-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathStep01_Assistant.cs
66 lines (60 loc) · 2.36 KB
/
Step01_Assistant.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
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents.OpenAI;
using OpenAI.Assistants;
using Resources;
namespace GettingStarted.OpenAIAssistants;
/// <summary>
/// This example demonstrates using <see cref="OpenAIAssistantAgent"/> with templatized instructions.
/// </summary>
public class Step01_Assistant(ITestOutputHelper output) : BaseAssistantTest(output)
{
[Fact]
public async Task UseTemplateForAssistantAgentAsync()
{
// Define the agent
string generateStoryYaml = EmbeddedResource.Read("GenerateStory.yaml");
PromptTemplateConfig templateConfig = KernelFunctionYaml.ToPromptTemplateConfig(generateStoryYaml);
// Instructions, Name and Description properties defined via the PromptTemplateConfig.
Assistant definition = await this.AssistantClient.CreateAssistantFromTemplateAsync(this.Model, templateConfig, metadata: SampleMetadata);
OpenAIAssistantAgent agent = new(
definition,
this.AssistantClient,
templateFactory: new KernelPromptTemplateFactory(),
templateFormat: PromptTemplateConfig.SemanticKernelTemplateFormat)
{
Arguments =
{
{ "topic", "Dog" },
{ "length", "3" }
}
};
// Create a thread for the agent conversation.
string threadId = await this.AssistantClient.CreateThreadAsync(metadata: SampleMetadata);
try
{
// Invoke the agent with the default arguments.
await InvokeAgentAsync();
// Invoke the agent with the override arguments.
await InvokeAgentAsync(
new()
{
{ "topic", "Cat" },
{ "length", "3" },
});
}
finally
{
await this.AssistantClient.DeleteThreadAsync(threadId);
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
}
// Local function to invoke agent and display the response.
async Task InvokeAgentAsync(KernelArguments? arguments = null)
{
await foreach (ChatMessageContent response in agent.InvokeAsync(threadId, arguments))
{
WriteAgentChatMessage(response);
}
}
}
}