-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentService.cs
More file actions
49 lines (43 loc) · 1.75 KB
/
AgentService.cs
File metadata and controls
49 lines (43 loc) · 1.75 KB
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
using Microsoft.Extensions.AI;
using Microsoft.Extensions.VectorData;
public class AgentService
{
private readonly IChatClient _chatClient;
private readonly VectorStoreCollection<string, ClimateFact> _collection;
private readonly IEmbeddingGenerator<string, Embedding<float>> _embeddingGenerator;
private readonly IList<AITool> _tools;
public AgentService(
IChatClient chatClient,
VectorStoreCollection<string, ClimateFact> collection,
IEmbeddingGenerator<string, Embedding<float>> embeddingGenerator)
{
_chatClient = chatClient;
_collection = collection;
_embeddingGenerator = embeddingGenerator;
_tools =
[
AIFunctionFactory.Create(ClimateSearchToolAsync, "climate_search_tool"),
AIFunctionFactory.Create(SystemInfoTool, "system_info_tool"),
];
}
private async Task<string> ClimateSearchToolAsync(string query)
{
var embeddings = await _embeddingGenerator.GenerateAsync([query], new EmbeddingGenerationOptions { Dimensions = 384 });
var embedding = embeddings[0].Vector;
var results = _collection.SearchAsync(embedding, 1, new VectorSearchOptions<ClimateFact>());
await foreach (var result in results)
{
return result.Record.Text;
}
return "No results found.";
}
private string SystemInfoTool(string query)
{
return "I am a .NET 10 AI Agent using the Microsoft Agent Framework";
}
public async Task<AskResponse> AskAsync(string question)
{
var response = await _chatClient.GetResponseAsync(question, new ChatOptions { Tools = _tools, ToolMode = ChatToolMode.Auto });
return new AskResponse(response.Text, "_tool_", "_thought_");
}
}