@@ -50,14 +50,15 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
5050 var content = value . Content ;
5151 var text = content . FirstOrDefault ( ) ? . Text ?? string . Empty ;
5252
53- if ( reason == ChatFinishReason . FunctionCall )
53+ if ( reason == ChatFinishReason . FunctionCall || reason == ChatFinishReason . ToolCalls )
5454 {
55+ var toolCall = value . ToolCalls . FirstOrDefault ( ) ;
5556 responseMessage = new RoleDialogModel ( AgentRole . Function , text )
5657 {
5758 CurrentAgentId = agent . Id ,
5859 MessageId = conversations . LastOrDefault ( ) ? . MessageId ?? string . Empty ,
59- FunctionName = value . FunctionCall . FunctionName ,
60- FunctionArgs = value . FunctionCall . FunctionArguments
60+ FunctionName = toolCall ? . FunctionName ,
61+ FunctionArgs = toolCall ? . FunctionArguments ? . ToString ( )
6162 } ;
6263
6364 // Somethings LLM will generate a function name with agent name.
@@ -66,17 +67,17 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
6667 responseMessage . FunctionName = responseMessage . FunctionName . Split ( '.' ) . Last ( ) ;
6768 }
6869 }
69- else if ( reason == ChatFinishReason . ToolCalls )
70- {
71- var toolCall = value . ToolCalls . FirstOrDefault ( ) ;
72- responseMessage = new RoleDialogModel ( AgentRole . Function , text )
73- {
74- CurrentAgentId = agent . Id ,
75- MessageId = conversations . LastOrDefault ( ) ? . MessageId ?? string . Empty ,
76- FunctionName = toolCall ? . FunctionName ,
77- FunctionArgs = toolCall ? . FunctionArguments
78- } ;
79- }
70+ // else if (reason == ChatFinishReason.ToolCalls)
71+ // {
72+ // var toolCall = value.ToolCalls.FirstOrDefault();
73+ // responseMessage = new RoleDialogModel(AgentRole.Function, text)
74+ // {
75+ // CurrentAgentId = agent.Id,
76+ // MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty,
77+ // FunctionName = toolCall?.FunctionName,
78+ // FunctionArgs = toolCall?.FunctionArguments
79+ // };
80+ // }
8081 else
8182 {
8283 responseMessage = new RoleDialogModel ( AgentRole . Assistant , text )
@@ -113,8 +114,8 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
113114 Prompt = prompt ,
114115 Provider = Provider ,
115116 Model = _model ,
116- PromptCount = value ? . Usage ? . InputTokens ?? 0 ,
117- CompletionCount = value ? . Usage ? . OutputTokens ?? 0
117+ PromptCount = value ? . Usage ? . InputTokenCount ?? 0 ,
118+ CompletionCount = value ? . Usage ? . OutputTokenCount ?? 0
118119 } ) ;
119120 }
120121
@@ -157,20 +158,21 @@ public async Task<bool> GetChatCompletionsAsync(Agent agent,
157158 Prompt = prompt ,
158159 Provider = Provider ,
159160 Model = _model ,
160- PromptCount = response . Value . Usage . InputTokens ,
161- CompletionCount = response . Value . Usage . OutputTokens
161+ PromptCount = response . Value ? . Usage ? . InputTokenCount ?? 0 ,
162+ CompletionCount = response . Value ? . Usage ? . OutputTokenCount ?? 0
162163 } ) ;
163164 }
164165
165- if ( reason == ChatFinishReason . FunctionCall )
166+ if ( reason == ChatFinishReason . FunctionCall || reason == ChatFinishReason . ToolCalls )
166167 {
167- _logger . LogInformation ( $ "[{ agent . Name } ]: { value . FunctionCall . FunctionName } ({ value . FunctionCall . FunctionArguments } )") ;
168+ var toolCall = value . ToolCalls ? . FirstOrDefault ( ) ;
169+ _logger . LogInformation ( $ "[{ agent . Name } ]: { toolCall ? . FunctionName } ({ toolCall ? . FunctionArguments } )") ;
168170
169171 var funcContextIn = new RoleDialogModel ( AgentRole . Function , text )
170172 {
171173 CurrentAgentId = agent . Id ,
172- FunctionName = value . FunctionCall ? . FunctionName ,
173- FunctionArgs = value . FunctionCall ? . FunctionArguments
174+ FunctionName = toolCall ? . FunctionName ,
175+ FunctionArgs = toolCall ? . FunctionArguments ? . ToString ( )
174176 } ;
175177
176178 // Somethings LLM will generate a function name with agent name.
@@ -201,19 +203,20 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
201203
202204 await foreach ( var choice in response )
203205 {
204- if ( choice . FinishReason == ChatFinishReason . FunctionCall )
206+ if ( choice . FinishReason == ChatFinishReason . FunctionCall || choice . FinishReason == ChatFinishReason . ToolCalls )
205207 {
206- Console . Write ( choice . FunctionCallUpdate ? . FunctionArgumentsUpdate ) ;
208+ var update = choice . ToolCallUpdates ? . FirstOrDefault ( ) ? . FunctionArgumentsUpdate ? . ToString ( ) ?? string . Empty ;
209+ Console . Write ( update ) ;
207210
208- await onMessageReceived ( new RoleDialogModel ( AgentRole . Assistant , choice . FunctionCallUpdate ? . FunctionArgumentsUpdate ) ) ;
211+ await onMessageReceived ( new RoleDialogModel ( AgentRole . Assistant , update ) ) ;
209212 continue ;
210213 }
211214
212215 if ( choice . ContentUpdate . IsNullOrEmpty ( ) ) continue ;
213216
214217 _logger . LogInformation ( choice . ContentUpdate [ 0 ] ? . Text ) ;
215218
216- await onMessageReceived ( new RoleDialogModel ( choice . Role . ToString ( ) , choice . ContentUpdate [ 0 ] ? . Text ?? string . Empty ) ) ;
219+ await onMessageReceived ( new RoleDialogModel ( choice . Role ? . ToString ( ) ?? ChatMessageRole . Assistant . ToString ( ) , choice . ContentUpdate [ 0 ] ? . Text ?? string . Empty ) ) ;
217220 }
218221
219222 return true ;
@@ -235,7 +238,7 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
235238 var options = new ChatCompletionOptions ( )
236239 {
237240 Temperature = temperature ,
238- MaxTokens = maxTokens
241+ MaxOutputTokenCount = maxTokens
239242 } ;
240243
241244 foreach ( var function in agent . Functions )
@@ -267,21 +270,35 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
267270 messages . Add ( sample . Role == AgentRole . User ? new UserChatMessage ( sample . Content ) : new AssistantChatMessage ( sample . Content ) ) ;
268271 }
269272
270- foreach ( var message in conversations )
273+ var filteredMessages = conversations . Select ( x => x ) . ToList ( ) ;
274+ var firstUserMsgIdx = filteredMessages . FindIndex ( x => x . Role == AgentRole . User ) ;
275+ if ( firstUserMsgIdx > 0 )
276+ {
277+ filteredMessages = filteredMessages . Where ( ( _ , idx ) => idx >= firstUserMsgIdx ) . ToList ( ) ;
278+ }
279+
280+ foreach ( var message in filteredMessages )
271281 {
272282 if ( message . Role == AgentRole . Function )
273283 {
274- messages . Add ( new AssistantChatMessage ( string . Empty )
284+ //messages.Add(new AssistantChatMessage(string.Empty)
285+ //{
286+ // FunctionCall = new ChatFunctionCall(message.FunctionName, message.FunctionArgs ?? string.Empty)
287+ //});
288+
289+ //messages.Add(new FunctionChatMessage(message.FunctionName, message.Content));
290+
291+ messages . Add ( new AssistantChatMessage ( new List < ChatToolCall >
275292 {
276- FunctionCall = new ChatFunctionCall ( message . FunctionName , message . FunctionArgs ?? string . Empty )
277- } ) ;
293+ ChatToolCall . CreateFunctionToolCall ( message . FunctionName , message . FunctionName , BinaryData . FromString ( message . FunctionArgs ?? string . Empty ) )
294+ } ) ) ;
278295
279- messages . Add ( new FunctionChatMessage ( message . FunctionName , message . Content ) ) ;
296+ messages . Add ( new ToolChatMessage ( message . FunctionName , message . Content ) ) ;
280297 }
281298 else if ( message . Role == AgentRole . User )
282299 {
283300 var text = ! string . IsNullOrWhiteSpace ( message . Payload ) ? message . Payload : message . Content ;
284- var textPart = ChatMessageContentPart . CreateTextMessageContentPart ( text ) ;
301+ var textPart = ChatMessageContentPart . CreateTextPart ( text ) ;
285302 var contentParts = new List < ChatMessageContentPart > { textPart } ;
286303
287304 if ( allowMultiModal && ! message . Files . IsNullOrEmpty ( ) )
@@ -291,20 +308,20 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
291308 if ( ! string . IsNullOrEmpty ( file . FileData ) )
292309 {
293310 var ( contentType , bytes ) = FileUtility . GetFileInfoFromData ( file . FileData ) ;
294- var contentPart = ChatMessageContentPart . CreateImageMessageContentPart ( BinaryData . FromBytes ( bytes ) , contentType , ImageChatMessageContentPartDetail . Low ) ;
311+ var contentPart = ChatMessageContentPart . CreateImagePart ( BinaryData . FromBytes ( bytes ) , contentType , ChatImageDetailLevel . Low ) ;
295312 contentParts . Add ( contentPart ) ;
296313 }
297314 else if ( ! string . IsNullOrEmpty ( file . FileStorageUrl ) )
298315 {
299316 var contentType = FileUtility . GetFileContentType ( file . FileStorageUrl ) ;
300317 var bytes = fileStorage . GetFileBytes ( file . FileStorageUrl ) ;
301- var contentPart = ChatMessageContentPart . CreateImageMessageContentPart ( BinaryData . FromBytes ( bytes ) , contentType , ImageChatMessageContentPartDetail . Low ) ;
318+ var contentPart = ChatMessageContentPart . CreateImagePart ( BinaryData . FromBytes ( bytes ) , contentType , ChatImageDetailLevel . Low ) ;
302319 contentParts . Add ( contentPart ) ;
303320 }
304321 else if ( ! string . IsNullOrEmpty ( file . FileUrl ) )
305322 {
306323 var uri = new Uri ( file . FileUrl ) ;
307- var contentPart = ChatMessageContentPart . CreateImageMessageContentPart ( uri , ImageChatMessageContentPartDetail . Low ) ;
324+ var contentPart = ChatMessageContentPart . CreateImagePart ( uri , ChatImageDetailLevel . Low ) ;
308325 contentParts . Add ( contentPart ) ;
309326 }
310327 }
@@ -347,7 +364,7 @@ private string GetPrompt(IEnumerable<ChatMessage> messages, ChatCompletionOption
347364 . Where ( x => x as SystemChatMessage == null )
348365 . Select ( x =>
349366 {
350- var fnMessage = x as FunctionChatMessage ;
367+ var fnMessage = x as ToolChatMessage ;
351368 if ( fnMessage != null )
352369 {
353370 return $ "{ AgentRole . Function } : { fnMessage . Content . FirstOrDefault ( ) ? . Text ?? string . Empty } ";
@@ -365,8 +382,9 @@ private string GetPrompt(IEnumerable<ChatMessage> messages, ChatCompletionOption
365382 var assistMessage = x as AssistantChatMessage ;
366383 if ( assistMessage != null )
367384 {
368- return assistMessage . FunctionCall != null ?
369- $ "{ AgentRole . Assistant } : Call function { assistMessage . FunctionCall . FunctionName } ({ assistMessage . FunctionCall . FunctionArguments } )" :
385+ var toolCall = assistMessage . ToolCalls ? . FirstOrDefault ( ) ;
386+ return toolCall != null ?
387+ $ "{ AgentRole . Assistant } : Call function { toolCall ? . FunctionName } ({ toolCall ? . FunctionArguments } )" :
370388 $ "{ AgentRole . Assistant } : { assistMessage . Content . FirstOrDefault ( ) ? . Text ?? string . Empty } ";
371389 }
372390
0 commit comments