-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathStep05_AzureAIAgent_FileSearch.cs
69 lines (62 loc) · 2.66 KB
/
Step05_AzureAIAgent_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
// Copyright (c) Microsoft. All rights reserved.
using Azure.AI.Projects;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents.AzureAI;
using Microsoft.SemanticKernel.ChatCompletion;
using Resources;
namespace GettingStarted.AzureAgents;
/// <summary>
/// Demonstrate using <see cref="AzureAIAgent"/> with file search.
/// </summary>
public class Step05_AzureAIAgent_FileSearch(ITestOutputHelper output) : BaseAzureAgentTest(output)
{
[Fact]
public async Task UseFileSearchToolWithAgent()
{
// Define the agent
await using Stream stream = EmbeddedResource.ReadStream("employees.pdf")!;
AgentFile fileInfo = await this.AgentsClient.UploadFileAsync(stream, AgentFilePurpose.Agents, "employees.pdf");
VectorStore fileStore =
await this.AgentsClient.CreateVectorStoreAsync(
[fileInfo.Id],
metadata: new Dictionary<string, string>() { { SampleMetadataKey, bool.TrueString } });
Agent agentModel = await this.AgentsClient.CreateAgentAsync(
TestConfiguration.AzureAI.ChatModelId,
tools: [new FileSearchToolDefinition()],
toolResources: new()
{
FileSearch = new()
{
VectorStoreIds = { fileStore.Id },
}
},
metadata: new Dictionary<string, string>() { { SampleMetadataKey, bool.TrueString } });
AzureAIAgent agent = new(agentModel, this.AgentsClient);
// Create a thread associated for the agent conversation.
Microsoft.SemanticKernel.Agents.AgentThread thread = new AzureAIAgentThread(this.AgentsClient, 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 thread.DeleteAsync();
await this.AgentsClient.DeleteAgentAsync(agent.Id);
await this.AgentsClient.DeleteVectorStoreAsync(fileStore.Id);
await this.AgentsClient.DeleteFileAsync(fileInfo.Id);
}
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
{
ChatMessageContent message = new(AuthorRole.User, input);
this.WriteAgentChatMessage(message);
await foreach (ChatMessageContent response in agent.InvokeAsync(message, thread))
{
this.WriteAgentChatMessage(response);
}
}
}
}