-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathStep05_AssistantTool_FileSearch.cs
73 lines (63 loc) · 2.66 KB
/
Step05_AssistantTool_FileSearch.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
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents.OpenAI;
using Microsoft.SemanticKernel.ChatCompletion;
using OpenAI.Assistants;
using Resources;
namespace GettingStarted.OpenAIAssistants;
/// <summary>
/// Demonstrate using <see cref="OpenAIAssistantAgent"/> with file search.
/// </summary>
public class Step05_AssistantTool_FileSearch(ITestOutputHelper output) : BaseAssistantTest(output)
{
[Fact]
public async Task UseFileSearchToolWithAssistantAgentAsync()
{
// Define the assistant
Assistant assistant =
await this.AssistantClient.CreateAssistantAsync(
this.Model,
enableFileSearch: true,
metadata: SampleMetadata);
// Create the agent
OpenAIAssistantAgent agent = new(assistant, this.AssistantClient);
// Upload file - Using a table of fictional employees.
await using Stream stream = EmbeddedResource.ReadStream("employees.pdf")!;
string fileId = await this.Client.UploadAssistantFileAsync(stream, "employees.pdf");
// Create a vector-store
string vectorStoreId =
await this.Client.CreateVectorStoreAsync(
[fileId],
waitUntilCompleted: true,
metadata: SampleMetadata);
// Create a thread associated with a vector-store for the agent conversation.
string threadId = await this.AssistantClient.CreateThreadAsync(
vectorStoreId: vectorStoreId,
metadata: SampleMetadata);
// Respond to user input
try
{
await InvokeAgentAsync("Who is the youngest employee?");
await InvokeAgentAsync("Who works in sales?");
await InvokeAgentAsync("I have a customer request, who can help me?");
}
finally
{
await this.AssistantClient.DeleteThreadAsync(threadId);
await this.AssistantClient.DeleteAssistantAsync(agent.Id);
await this.Client.DeleteVectorStoreAsync(vectorStoreId);
await this.Client.DeleteFileAsync(fileId);
}
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(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);
}
}
}
}