Skip to content

Commit 9dab7c8

Browse files
author
Jicheng Lu
committed
add file llm processor
1 parent 32bea33 commit 9dab7c8

File tree

7 files changed

+125
-17
lines changed

7 files changed

+125
-17
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace BotSharp.Abstraction.Files.Models;
2+
3+
public class FileLlmProcessOptions
4+
{
5+
/// <summary>
6+
/// Llm provider
7+
/// </summary>
8+
public string? LlmProvider { get; set; }
9+
10+
/// <summary>
11+
/// llm model
12+
/// </summary>
13+
public string? LlModel { get; set; }
14+
15+
/// <summary>
16+
/// Reasoning effort level
17+
/// </summary>
18+
public string? ReasoningEfforLevel { get; set; }
19+
20+
/// <summary>
21+
/// Instruction
22+
/// </summary>
23+
public string? Instruction { get; set; }
24+
25+
/// <summary>
26+
/// Template name in Agent
27+
/// </summary>
28+
public string? TemplateName { get; set; }
29+
30+
/// <summary>
31+
/// Data that is used to render instruction
32+
/// </summary>
33+
public Dictionary<string, object>? Data { get; set; }
34+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace BotSharp.Abstraction.Files.Proccessors;
2+
3+
public interface IFileLlmProcessor
4+
{
5+
public string Provider { get; }
6+
7+
Task<RoleDialogModel> GetFileLlmInferenceAsync(Agent agent, string text, IEnumerable<InstructFileModel> files, FileLlmProcessOptions? options = null)
8+
=> throw new NotImplementedException();
9+
}

src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ public interface IInstructService
1313
/// <param name="templateName"></param>
1414
/// <param name="files"></param>
1515
/// <param name="codeOptions"></param>
16+
/// <param name="fileOptions"></param>
1617
/// <returns></returns>
1718
Task<InstructResult> Execute(string agentId, RoleDialogModel message,
1819
string? instruction = null, string? templateName = null,
19-
IEnumerable<InstructFileModel>? files = null, CodeInstructOptions? codeOptions = null);
20+
IEnumerable<InstructFileModel>? files = null,
21+
CodeInstructOptions? codeOptions = null,
22+
FileInstructOptions? fileOptions = null);
2023

2124
/// <summary>
2225
/// A generic way to execute completion by using specified instruction or template
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace BotSharp.Abstraction.Instructs.Models;
2+
3+
public class FileInstructOptions
4+
{
5+
public string? FileLlmProcessorProvider { get; set; }
6+
}

src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.CodeInterpreter;
2+
using BotSharp.Abstraction.Files.Proccessors;
23
using BotSharp.Abstraction.Instructs;
34
using BotSharp.Abstraction.Instructs.Models;
45
using BotSharp.Abstraction.MLTasks;
@@ -14,9 +15,11 @@ public async Task<InstructResult> Execute(
1415
string? instruction = null,
1516
string? templateName = null,
1617
IEnumerable<InstructFileModel>? files = null,
17-
CodeInstructOptions? codeOptions = null)
18+
CodeInstructOptions? codeOptions = null,
19+
FileInstructOptions? fileOptions = null)
1820
{
1921
var agentService = _services.GetRequiredService<IAgentService>();
22+
var state = _services.GetRequiredService<IConversationStateService>();
2023
Agent agent = await agentService.LoadAgent(agentId);
2124

2225
var response = new InstructResult
@@ -68,6 +71,7 @@ public async Task<InstructResult> Execute(
6871

6972
var provider = string.Empty;
7073
var model = string.Empty;
74+
var result = string.Empty;
7175

7276
// Render prompt
7377
var prompt = string.IsNullOrEmpty(templateName) ?
@@ -83,35 +87,45 @@ public async Task<InstructResult> Execute(
8387
provider = textCompleter.Provider;
8488
model = textCompleter.Model;
8589

86-
var result = await textCompleter.GetCompletion(prompt, agentId, message.MessageId);
90+
result = await GetTextCompletion(textCompleter, agent, prompt, message.MessageId);
8791
response.Text = result;
8892
}
8993
else if (completer is IChatCompletion chatCompleter)
9094
{
9195
provider = chatCompleter.Provider;
9296
model = chatCompleter.Model;
97+
9398

9499
if (instruction == "#TEMPLATE#")
95100
{
96101
instruction = prompt;
97102
prompt = message.Content;
98103
}
99104

100-
var result = await chatCompleter.GetChatCompletions(new Agent
105+
IFileLlmProcessor? fileProcessor = null;
106+
if (!files.IsNullOrEmpty() && !string.IsNullOrEmpty(fileOptions?.FileLlmProcessorProvider))
101107
{
102-
Id = agentId,
103-
Name = agent.Name,
104-
Instruction = instruction
105-
}, new List<RoleDialogModel>
108+
fileProcessor = _services.GetServices<IFileLlmProcessor>()
109+
.FirstOrDefault(x => x.Provider.IsEqualTo(fileOptions?.FileLlmProcessorProvider));
110+
}
111+
112+
if (fileProcessor != null)
106113
{
107-
new RoleDialogModel(AgentRole.User, prompt)
114+
var inference = await fileProcessor.GetFileLlmInferenceAsync(agent, prompt, files, new FileLlmProcessOptions
108115
{
109-
CurrentAgentId = agentId,
110-
MessageId = message.MessageId,
111-
Files = files?.Select(x => new BotSharpFile { FileUrl = x.FileUrl, FileData = x.FileData, ContentType = x.ContentType }).ToList() ?? []
112-
}
113-
});
114-
response.Text = result.Content;
116+
LlmProvider = provider,
117+
LlModel = model,
118+
Instruction = instruction,
119+
TemplateName = templateName,
120+
Data = state.GetStates().ToDictionary(x => x.Key, x => (object)x.Value)
121+
});
122+
result = inference?.Content ?? string.Empty;
123+
}
124+
else
125+
{
126+
result = await GetChatCompletion(chatCompleter, agent, instruction, prompt, message.MessageId, files);
127+
}
128+
response.Text = result;
115129
}
116130

117131
// After completion hooks
@@ -141,7 +155,11 @@ await hook.OnResponseGenerated(new InstructResponseModel
141155
/// <param name="templateName"></param>
142156
/// <param name="codeOptions"></param>
143157
/// <returns></returns>
144-
private async Task<InstructResult?> GetCodeResponse(Agent agent, RoleDialogModel message, string templateName, CodeInstructOptions? codeOptions)
158+
private async Task<InstructResult?> GetCodeResponse(
159+
Agent agent,
160+
RoleDialogModel message,
161+
string templateName,
162+
CodeInstructOptions? codeOptions)
145163
{
146164
InstructResult? response = null;
147165

@@ -261,4 +279,40 @@ await hook.OnResponseGenerated(new InstructResponseModel
261279

262280
return response;
263281
}
282+
283+
private async Task<string> GetTextCompletion(
284+
ITextCompletion textCompleter,
285+
Agent agent,
286+
string text,
287+
string messageId)
288+
{
289+
var result = await textCompleter.GetCompletion(text, agent.Id, messageId);
290+
return result;
291+
}
292+
293+
private async Task<string> GetChatCompletion(
294+
IChatCompletion chatCompleter,
295+
Agent agent,
296+
string instruction,
297+
string text,
298+
string messageId,
299+
IEnumerable<InstructFileModel>? files = null)
300+
{
301+
var result = await chatCompleter.GetChatCompletions(new Agent
302+
{
303+
Id = agent.Id,
304+
Name = agent.Name,
305+
Instruction = instruction
306+
}, new List<RoleDialogModel>
307+
{
308+
new RoleDialogModel(AgentRole.User, text)
309+
{
310+
CurrentAgentId = agent.Id,
311+
MessageId = messageId,
312+
Files = files?.Select(x => new BotSharpFile { FileUrl = x.FileUrl, FileData = x.FileData, ContentType = x.ContentType }).ToList() ?? []
313+
}
314+
});
315+
316+
return result.Content;
317+
}
264318
}

src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public async Task<InstructResult> InstructCompletion([FromRoute] string agentId,
3939
instruction: input.Instruction,
4040
templateName: input.Template,
4141
files: input.Files,
42-
codeOptions: input.CodeOptions);
42+
codeOptions: input.CodeOptions,
43+
fileOptions: input.FileOptions);
4344

4445
result.States = state.GetStates();
4546
return result;

src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/Request/InstructMessageModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class InstructMessageModel : IncomingMessageModel
1212
public string? Template { get; set; }
1313
public List<InstructFileModel> Files { get; set; } = [];
1414
public CodeInstructOptions? CodeOptions { get; set; }
15+
public FileInstructOptions? FileOptions { get; set; }
1516
}
1617

1718

0 commit comments

Comments
 (0)