-
-
Notifications
You must be signed in to change notification settings - Fork 628
Expand file tree
/
Copy pathInstructService.Execute.cs
More file actions
386 lines (339 loc) · 12.8 KB
/
InstructService.Execute.cs
File metadata and controls
386 lines (339 loc) · 12.8 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
using BotSharp.Abstraction.Coding;
using BotSharp.Abstraction.Coding.Contexts;
using BotSharp.Abstraction.Coding.Enums;
using BotSharp.Abstraction.Coding.Utils;
using BotSharp.Abstraction.Files.Options;
using BotSharp.Abstraction.Files.Proccessors;
using BotSharp.Abstraction.Instructs;
using BotSharp.Abstraction.Instructs.Enums;
using BotSharp.Abstraction.Instructs.Models;
using BotSharp.Abstraction.Instructs.Options;
using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.Models;
using BotSharp.Abstraction.Shared;
namespace BotSharp.Core.Instructs;
public partial class InstructService
{
public async Task<InstructResult> Execute(
string agentId,
RoleDialogModel message,
string? instruction = null,
string? templateName = null,
IEnumerable<InstructFileModel>? files = null,
CodeInstructOptions? codeOptions = null,
FileInstructOptions? fileOptions = null,
ResponseFormatType? responseFormat = null)
{
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(agentId);
var response = new InstructResult
{
MessageId = message.MessageId,
Template = templateName
};
if (agent == null)
{
response.Text = $"Agent (id: {agentId}) does not exist!";
return response;
}
if (agent.Disabled)
{
var content = $"This agent ({agent.Name}) is disabled, please install the corresponding plugin ({agent.Plugin.Name}) to activate this agent.";
response.Text = content;
return response;
}
// Run code template
var codeResponse = await RunCode(agent, message, templateName, codeOptions);
if (codeResponse != null)
{
return codeResponse;
}
response = await RunLlm(agent, message, instruction, templateName, files, fileOptions, responseFormat);
return response;
}
/// <summary>
/// Get code response
/// </summary>
/// <param name="agent"></param>
/// <param name="message"></param>
/// <param name="templateName"></param>
/// <param name="codeOptions"></param>
/// <returns></returns>
private async Task<InstructResult?> RunCode(
Agent agent,
RoleDialogModel message,
string? templateName,
CodeInstructOptions? codeOptions)
{
InstructResult? instructResult = null;
if (agent == null)
{
return instructResult;
}
var agentService = _services.GetRequiredService<IAgentService>();
var state = _services.GetRequiredService<IConversationStateService>();
var codingSettings = _services.GetRequiredService<CodingSettings>();
var hooks = _services.GetHooks<IInstructHook>(agent.Id);
var codeProvider = codeOptions?.Processor ?? codingSettings.CodeExecution?.Processor;
codeProvider = !string.IsNullOrEmpty(codeProvider) ? codeProvider : BuiltInCodeProcessor.PyInterpreter;
var codeProcessor = _services.GetServices<ICodeProcessor>()
.FirstOrDefault(x => x.Provider.IsEqualTo(codeProvider));
if (codeProcessor == null)
{
#if DEBUG
_logger.LogWarning($"No code processor found. (Agent: {agent.Id}, Code processor: {codeProvider})");
#endif
return instructResult;
}
// Get code script name
var scriptName = string.Empty;
if (!string.IsNullOrEmpty(codeOptions?.ScriptName))
{
scriptName = codeOptions.ScriptName;
}
else if (!string.IsNullOrEmpty(templateName))
{
scriptName = $"{templateName}.py";
}
if (string.IsNullOrEmpty(scriptName))
{
#if DEBUG
_logger.LogWarning($"Empty code script name. (Agent: {agent.Id}, {scriptName})");
#endif
return instructResult;
}
// Get code script
var scriptType = codeOptions?.ScriptType ?? AgentCodeScriptType.Src;
var codeScript = await agentService.GetAgentCodeScript(agent.Id, scriptName, scriptType);
if (string.IsNullOrWhiteSpace(codeScript?.Content))
{
#if DEBUG
_logger.LogWarning($"Empty code script. (Agent: {agent.Id}, {scriptName})");
#endif
return instructResult;
}
// Get code arguments
var arguments = codeOptions?.Arguments ?? [];
if (arguments.IsNullOrEmpty())
{
arguments = state.GetStates().Select(x => new KeyValue(x.Key, x.Value)).ToList();
}
var context = new CodeExecutionContext
{
CodeScript = codeScript,
Arguments = arguments
};
// Before code execution
foreach (var hook in hooks)
{
await hook.BeforeCompletion(agent, message);
await hook.BeforeCodeExecution(agent, context);
// Interrupted by hook
if (message.StopCompletion)
{
return new InstructResult
{
MessageId = message.MessageId,
Text = message.Content
};
}
}
// Run code script
var (useLock, useProcess, timeoutSeconds) = CodingUtil.GetCodeExecutionConfig(codingSettings);
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutSeconds));
var codeResponse = codeProcessor.Run(context.CodeScript?.Content ?? string.Empty, options: new()
{
ScriptName = context.CodeScript?.Name,
Arguments = context.Arguments,
UseLock = useLock,
UseProcess = useProcess
}, cancellationToken: cts.Token);
if (codeResponse == null || !codeResponse.Success)
{
return instructResult;
}
instructResult = new InstructResult
{
MessageId = message.MessageId,
Template = context.CodeScript?.Name,
Text = codeResponse.Result
};
var codeExecution = new CodeExecutionResponseModel
{
CodeProcessor = codeProcessor.Provider,
CodeScript = context.CodeScript,
ExecutionResult = codeResponse,
Text = message.Content,
Arguments = context.Arguments?.DistinctBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value ?? string.Empty)
};
// After code execution
foreach (var hook in hooks)
{
await hook.AfterCompletion(agent, instructResult);
await hook.AfterCodeExecution(agent, context, codeExecution);
}
return instructResult;
}
private async Task<InstructResult> RunLlm(
Agent agent,
RoleDialogModel message,
string? instruction,
string? templateName,
IEnumerable<InstructFileModel>? files = null,
FileInstructOptions? fileOptions = null,
ResponseFormatType? responseFormat = null)
{
var agentService = _services.GetRequiredService<IAgentService>();
var state = _services.GetRequiredService<IConversationStateService>();
var response = new InstructResult
{
MessageId = message.MessageId,
Template = templateName
};
// Before completion hooks
var hooks = _services.GetHooks<IInstructHook>(agent.Id);
foreach (var hook in hooks)
{
await hook.BeforeCompletion(agent, message);
// Interrupted by hook
if (message.StopCompletion)
{
return new InstructResult
{
MessageId = message.MessageId,
Text = message.Content
};
}
}
var provider = string.Empty;
var model = string.Empty;
var result = string.Empty;
// Render prompt
var prompt = string.Empty;
var llmConfig = agent.LlmConfig;
if (!string.IsNullOrEmpty(templateName))
{
prompt = agentService.RenderTemplate(agent, templateName);
var templateLlmConfig = agent.Templates?.FirstOrDefault(x => x.Name.IsEqualTo(templateName))?.LlmConfig;
if (templateLlmConfig?.IsValid == true)
{
llmConfig = new AgentLlmConfig
{
Provider = templateLlmConfig.Provider,
Model = templateLlmConfig.Model,
MaxOutputTokens = templateLlmConfig.MaxOutputTokens,
ReasoningEffortLevel = templateLlmConfig.ReasoningEffortLevel
};
}
}
else
{
prompt = agentService.RenderInstruction(agent);
}
var completer = CompletionProvider.GetCompletion(_services,
agentConfig: llmConfig);
if (completer is ITextCompletion textCompleter)
{
instruction = null;
provider = textCompleter.Provider;
model = textCompleter.Model;
result = await GetTextCompletion(textCompleter, agent, prompt, message.MessageId);
response.Text = result;
}
else if (completer is IChatCompletion chatCompleter)
{
provider = chatCompleter.Provider;
model = chatCompleter.Model;
if (instruction == "#TEMPLATE#")
{
instruction = prompt;
prompt = message.Content;
}
IFileProcessor? fileProcessor = null;
if (!files.IsNullOrEmpty() && fileOptions != null)
{
fileProcessor = _services.GetServices<IFileProcessor>()
.FirstOrDefault(x => x.Provider.IsEqualTo(fileOptions.Processor));
}
if (fileProcessor != null)
{
var fileResponse = await fileProcessor.HandleFilesAsync(agent, prompt, files, new FileHandleOptions
{
Provider = provider,
Model = model,
Instruction = instruction,
UserMessage = message.Content,
TemplateName = templateName,
InvokeFrom = $"{nameof(InstructService)}.{nameof(Execute)}",
Data = state.GetStates().ToDictionary(x => x.Key, x => (object)x.Value)
});
result = fileResponse.Result.IfNullOrEmptyAs(string.Empty);
}
else
{
result = await GetChatCompletion(chatCompleter, agent, instruction, prompt, message.MessageId, llmConfig, files);
}
// Repair JSON format if needed
responseFormat ??= agentService.GetTemplateResponseFormat(agent, templateName);
if (responseFormat == ResponseFormatType.Json)
{
var jsonRepairService = _services.GetRequiredService<IJsonRepairService>();
result = await jsonRepairService.RepairAsync(result);
}
response.Text = result;
}
response.LogId = Guid.NewGuid().ToString();
// After completion hooks
foreach (var hook in hooks)
{
await hook.AfterCompletion(agent, response);
await hook.OnResponseGenerated(new InstructResponseModel
{
LogId = response.LogId,
AgentId = agent.Id,
Provider = provider,
Model = model,
TemplateName = templateName,
UserMessage = prompt,
SystemInstruction = instruction,
CompletionText = response.Text
});
}
return response;
}
private async Task<string> GetTextCompletion(
ITextCompletion textCompleter,
Agent agent,
string text,
string messageId)
{
var result = await textCompleter.GetCompletion(text, agent.Id, messageId);
return result;
}
private async Task<string> GetChatCompletion(
IChatCompletion chatCompleter,
Agent agent,
string instruction,
string text,
string messageId,
AgentLlmConfig? llmConfig = null,
IEnumerable<InstructFileModel>? files = null)
{
var result = await chatCompleter.GetChatCompletions(new Agent
{
Id = agent.Id,
Name = agent.Name,
Instruction = instruction,
LlmConfig = llmConfig ?? agent.LlmConfig
}, new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, text)
{
CurrentAgentId = agent.Id,
MessageId = messageId,
Files = files?.Select(x => new BotSharpFile { FileUrl = x.FileUrl, FileData = x.FileData, ContentType = x.ContentType }).ToList() ?? []
}
});
return result.Content;
}
}