-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathAgentContext.cs
More file actions
329 lines (286 loc) · 9.03 KB
/
AgentContext.cs
File metadata and controls
329 lines (286 loc) · 9.03 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using MaIN.Domain.Configuration;
using MaIN.Domain.Entities;
using MaIN.Domain.Entities.Agents;
using MaIN.Domain.Entities.Agents.AgentSource;
using MaIN.Domain.Models;
using MaIN.Services.Services.Abstract;
using MaIN.Services.Services.Models;
using MaIN.Core.Hub.Utils;
using MaIN.Domain.Entities.Agents.Knowledge;
using MaIN.Domain.Entities.Tools;
namespace MaIN.Core.Hub.Contexts;
public class AgentContext
{
private readonly IAgentService _agentService;
private InferenceParams? _inferenceParams;
private MemoryParams? _memoryParams;
private bool _disableCache;
private readonly Agent _agent;
internal Knowledge? _knowledge;
internal AgentContext(IAgentService agentService)
{
_agentService = agentService;
_agent = new Agent
{
Id = Guid.NewGuid().ToString(),
Behaviours = new Dictionary<string, string>(),
Name = $"Agent-{Guid.NewGuid()}",
Description = "Agent created by MaIN",
CurrentBehaviour = "Default",
Flow = false,
Context = new AgentData()
{
Instruction = "Hello, I'm your personal assistant. How can I assist you today?",
Relations = [],
Source = null,
Steps = ["ANSWER"]
}
};
}
internal AgentContext(IAgentService agentService, Agent existingAgent)
{
_agentService = agentService;
_agent = existingAgent;
}
public AgentContext WithId(string id)
{
_agent.Id = id;
return this;
}
public string GetAgentId() => _agent.Id;
public Agent GetAgent() => _agent;
public Knowledge? GetKnowledge() => _knowledge;
public AgentContext WithOrder(int order)
{
_agent.Order = order;
return this;
}
public AgentContext DisableCache()
{
_disableCache = true;
return this;
}
public AgentContext WithSource(IAgentSource source, AgentSourceType type)
{
_agent.Context.Source = new AgentSource()
{
Details = source,
Type = type
};
return this;
}
public AgentContext WithName(string name)
{
_agent.Name = name;
return this;
}
public AgentContext WithBackend(BackendType backendType)
{
_agent.Backend = backendType;
return this;
}
public AgentContext WithModel(string model)
{
_agent.Model = model;
return this;
}
public AgentContext WithMcpConfig(Mcp mcpConfig)
{
if (_agent.Backend != null)
{
mcpConfig.Backend = _agent.Backend;
}
_agent.Context.McpConfig = mcpConfig;
return this;
}
public AgentContext WithInferenceParams(InferenceParams inferenceParams)
{
_inferenceParams = inferenceParams;
return this;
}
public AgentContext WithMemoryParams(MemoryParams memoryParams)
{
_memoryParams = memoryParams;
return this;
}
public AgentContext WithCustomModel(string model, string path, string? mmProject = null)
{
KnownModels.AddModel(model, path, mmProject);
_agent.Model = model;
return this;
}
public AgentContext WithInitialPrompt(string prompt)
{
_agent.Context.Instruction = prompt;
return this;
}
public AgentContext WithSteps(List<string>? steps)
{
_agent.Context.Steps = steps;
return this;
}
public AgentContext WithKnowledge(Func<KnowledgeBuilder, KnowledgeBuilder> knowledgeConfig)
{
var builder = KnowledgeBuilder.Instance.ForAgent(_agent);
_knowledge = knowledgeConfig(builder).Build();
return this;
}
public AgentContext WithKnowledge(KnowledgeBuilder knowledge)
{
_knowledge = knowledge.ForAgent(_agent).Build();
return this;
}
public AgentContext WithKnowledge(Knowledge knowledge)
{
_knowledge = knowledge;
return this;
}
public AgentContext WithInMemoryKnowledge(Func<KnowledgeBuilder, KnowledgeBuilder> knowledgeConfig)
{
var builder = KnowledgeBuilder.Instance
.ForAgent(_agent)
.DisablePersistence();
_knowledge = knowledgeConfig(builder).Build();
return this;
}
public AgentContext WithBehaviour(string name, string instruction)
{
_agent.Behaviours ??= new Dictionary<string, string>();
_agent.Behaviours[name] = instruction;
_agent.CurrentBehaviour = name;
return this;
}
public async Task<AgentContext> CreateAsync(bool flow = false, bool interactiveResponse = false)
{
await _agentService.CreateAgent(_agent, flow, interactiveResponse, _inferenceParams, _memoryParams, _disableCache);
return this;
}
public AgentContext Create(bool flow = false, bool interactiveResponse = false)
{
_ = _agentService.CreateAgent(_agent, flow, interactiveResponse, _inferenceParams, _memoryParams, _disableCache).Result;
return this;
}
public AgentContext WithTools(ToolsConfiguration toolsConfiguration)
{
_agent.ToolsConfiguration = toolsConfiguration;
return this;
}
internal void LoadExistingKnowledgeIfExists()
{
_knowledge ??= new Knowledge(_agent);
try
{
_knowledge.Load();
}
catch (FileNotFoundException)
{
Console.WriteLine("Knowledge cannot be loaded - new one will be created");
}
}
public async Task<ChatResult> ProcessAsync(Chat chat, bool translate = false)
{
if (_knowledge == null)
{
LoadExistingKnowledgeIfExists();
}
var result = await _agentService.Process(chat, _agent.Id, _knowledge, translate);
var message = result.Messages.LastOrDefault()!;
return new ChatResult()
{
Done = true,
Model = result.Model,
Message = message,
CreatedAt = DateTime.Now
};
}
public async Task<ChatResult> ProcessAsync(string message, bool translate = false)
{
if (_knowledge == null)
{
LoadExistingKnowledgeIfExists();
}
var chat = await _agentService.GetChatByAgent(_agent.Id);
chat.Messages.Add(new Message()
{
Content = message,
Role = "User",
Type = MessageType.LocalLLM, //TODO this need an improvement - we dont know if the message is from local or cloud
Time = DateTime.Now
});
var result = await _agentService.Process(chat, _agent.Id, _knowledge, translate);
var messageResult = result.Messages.LastOrDefault()!;
return new ChatResult()
{
Done = true,
Model = result.Model,
Message = messageResult,
CreatedAt = DateTime.Now
};
}
public async Task<ChatResult> ProcessAsync(Message message, bool translate = false)
{
if (_knowledge == null)
{
LoadExistingKnowledgeIfExists();
}
var chat = await _agentService.GetChatByAgent(_agent.Id);
chat.Messages.Add(message);
var result = await _agentService.Process(chat, _agent.Id, _knowledge, translate);
var messageResult = result.Messages.LastOrDefault()!;
return new ChatResult()
{
Done = true,
Model = result.Model,
Message = messageResult,
CreatedAt = DateTime.Now
};
}
public async Task<Chat> GetChat()
{
return await _agentService.GetChatByAgent(_agent.Id);
}
public async Task<Chat> RestartChat()
{
return await _agentService.Restart(_agent.Id);
}
public async Task<List<Agent>> GetAllAgents()
{
return await _agentService.GetAgents();
}
public async Task<Agent?> GetAgentById(string id)
{
return await _agentService.GetAgentById(id);
}
public async Task Delete()
{
await _agentService.DeleteAgent(_agent.Id);
}
public async Task<bool> Exists()
{
return await _agentService.AgentExists(_agent.Id);
}
public static async Task<AgentContext> FromExisting(IAgentService agentService, string agentId)
{
var existingAgent = await agentService.GetAgentById(agentId);
if (existingAgent == null)
throw new ArgumentException("Agent not found", nameof(agentId));
var context = new AgentContext(agentService, existingAgent);
context.LoadExistingKnowledgeIfExists();
return context;
}
}
public static class AgentExtensions
{
public static async Task<ChatResult> ProcessAsync(
this Task<AgentContext> agentTask,
string message,
bool translate = false)
{
var agent = await agentTask;
if (agent._knowledge == null)
{
agent.LoadExistingKnowledgeIfExists();
}
return await agent.ProcessAsync(message, translate);
}
}