11using BotSharp . Abstraction . CodeInterpreter ;
2+ using BotSharp . Abstraction . Files . Proccessors ;
23using BotSharp . Abstraction . Instructs ;
34using BotSharp . Abstraction . Instructs . Models ;
45using 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}
0 commit comments