-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathAnswerCommandHandler.cs
More file actions
174 lines (148 loc) · 7.24 KB
/
AnswerCommandHandler.cs
File metadata and controls
174 lines (148 loc) · 7.24 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System.Text.Json;
using MaIN.Domain.Configuration;
using MaIN.Domain.Entities;
using MaIN.Domain.Entities.Agents.Knowledge;
using MaIN.Domain.Models;
using MaIN.Services.Constants;
using MaIN.Services.Services.Abstract;
using MaIN.Services.Services.LLMService;
using MaIN.Services.Services.LLMService.Factory;
using MaIN.Services.Services.Models;
using MaIN.Services.Services.Models.Commands;
using MaIN.Services.Utils;
namespace MaIN.Services.Services.Steps.Commands;
public class AnswerCommandHandler(
ILLMServiceFactory llmServiceFactory,
IMcpService mcpService,
INotificationService notificationService,
IImageGenServiceFactory imageGenServiceFactory,
MaINSettings settings)
: ICommandHandler<AnswerCommand, Message?>
{
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNameCaseInsensitive = true
};
public async Task<Message?> HandleAsync(AnswerCommand command)
{
ChatResult? result;
var llmService = llmServiceFactory.CreateService(command.Chat.Backend ?? settings.BackendType);
var imageGenService = imageGenServiceFactory.CreateService(command.Chat.Backend ?? settings.BackendType);
switch (command.KnowledgeUsage)
{
case KnowledgeUsage.UseMemory:
result = await llmService.AskMemory(command.Chat,
new ChatMemoryOptions { Memory = command.Chat.Memory });
return result!.Message;
case KnowledgeUsage.UseKnowledge:
var isKnowledgeNeeded = await ShouldUseKnowledge(command.Knowledge, command.Chat);
if (isKnowledgeNeeded)
{
return await ProcessKnowledgeQuery(command.Knowledge, command.Chat, command.AgentId);
}
break;
case KnowledgeUsage.AlwaysUseKnowledge:
return await ProcessKnowledgeQuery(command.Knowledge, command.Chat, command.AgentId);
}
result = command.Chat.Visual
? await imageGenService!.Send(command.Chat)
: await llmService.Send(command.Chat,
new ChatRequestOptions { InteractiveUpdates = command.Chat.Interactive, TokenCallback = command.Callback });
return result!.Message;
}
private async Task<bool> ShouldUseKnowledge(Knowledge? knowledge, Chat chat)
{
var originalContent = chat.Messages.Last().Content;
var indexAsKnowledge = knowledge?.Index.Items.ToDictionary(x => x.Name, x => x.Tags);
var index = JsonSerializer.Serialize(indexAsKnowledge, JsonOptions);
chat.InterferenceParams.Grammar = new Grammar(ServiceConstants.Grammars.DecisionGrammar, GrammarFormat.GBNF);
chat.Messages.Last().Content =
$"""
KNOWLEDGE:
{index}
Based on the following prompt, decide if you should use external knowledge.
Use external knowledge unless you're certain the question requires only basic facts (like "What is 2+2?" or "Capital of France?").
When in doubt, use it - external knowledge often provides more current, specific, or contextual information.
Content of available knowledge has source tags. Prompt: {originalContent}
""";
var service = llmServiceFactory.CreateService(chat.Backend ?? settings.BackendType);
var result = await service.Send(chat, new ChatRequestOptions()
{
SaveConv = false
});
var decision = JsonSerializer.Deserialize<JsonElement>(result!.Message.Content, JsonOptions);
var decisionValue = decision.GetProperty("decision").GetRawText();
chat.InterferenceParams.Grammar = null;
var shouldUseKnowledge = bool.Parse(decisionValue.Trim('"'));
chat.Messages.Last().Content = originalContent;
return shouldUseKnowledge!;
}
private async Task<Message?> ProcessKnowledgeQuery(Knowledge? knowledge, Chat chat, string agentId)
{
var originalContent = chat.Messages.Last().Content;
var indexAsKnowledge = knowledge?.Index.Items.ToDictionary(x => x.Name, x => x.Tags);
var index = JsonSerializer.Serialize(indexAsKnowledge, JsonOptions);
chat.InterferenceParams.Grammar = new Grammar(ServiceConstants.Grammars.KnowledgeGrammar, GrammarFormat.GBNF);
chat.Messages.Last().Content =
$"""
KNOWLEDGE:
{index}
Find tags that fits user query based on available knowledge (provided to you above as pair of item names with tags).
Always return at least 1 tag in array, and no more than 4. Prompt: {originalContent}
""";
var llmService = llmServiceFactory.CreateService(chat.Backend ?? settings.BackendType);
var searchResult = await llmService.Send(chat, new ChatRequestOptions()
{
SaveConv = false
});
var matchedTags = JsonSerializer.Deserialize<List<string>>(searchResult!.Message.Content, JsonOptions);
var knowledgeItems = knowledge!.Index.Items
.Where(x => x.Tags.Intersect(matchedTags!).Any() ||
matchedTags!.Contains(x.Name))
.ToList();
//NOTE: perhaps good idea for future to combine knowledge form MCP and from KM
var memoryOptions = new ChatMemoryOptions();
var mcpConfig = BuildMemoryOptionsFromKnowledgeItems(knowledgeItems, memoryOptions);
chat.Messages.Last().Content = $"{originalContent} - Use information given you as memory.";
chat.MemoryParams.IncludeQuestionSource = true;
chat.MemoryParams.Grammar = null;
await notificationService.DispatchNotification(NotificationMessageBuilder.CreateActorKnowledgeStepProgress(
agentId,
knowledgeItems.Select(x => $" {x.Name}|{x.Type} ").ToList(),
mcpConfig?.Model ?? chat.Model), "ReceiveAgentUpdate");
if (mcpConfig != null)
{
var result = await mcpService.Prompt(mcpConfig, chat.Messages);
return result.Message;
}
var knowledgeResult = await llmService.AskMemory(chat, memoryOptions);
chat.Messages.Last().Content = originalContent;
return knowledgeResult?.Message;
}
private static Mcp? BuildMemoryOptionsFromKnowledgeItems(List<KnowledgeIndexItem>? knowledgeItems,
ChatMemoryOptions memoryOptions)
{
//First or default because we cannot combine response from multiple servers in one go at the moment
var mcp = knowledgeItems?.FirstOrDefault(x => x.Type == KnowledgeItemType.Mcp);
if (mcp != null)
{
return JsonSerializer.Deserialize<Mcp>(mcp.Value, JsonOptions);
}
foreach (var item in knowledgeItems!)
{
switch (item.Type)
{
case KnowledgeItemType.File:
memoryOptions.FilesData.TryAdd(item.Name, item.Value);
break;
case KnowledgeItemType.Text:
memoryOptions.TextData.TryAdd(item.Name, item.Value);
break;
case KnowledgeItemType.Url:
memoryOptions.WebUrls.Add(item.Value);
break;
}
}
return null;
}
}