@@ -50,14 +50,15 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
50
50
var content = value . Content ;
51
51
var text = content . FirstOrDefault ( ) ? . Text ?? string . Empty ;
52
52
53
- if ( reason == ChatFinishReason . FunctionCall )
53
+ if ( reason == ChatFinishReason . FunctionCall || reason == ChatFinishReason . ToolCalls )
54
54
{
55
+ var toolCall = value . ToolCalls . FirstOrDefault ( ) ;
55
56
responseMessage = new RoleDialogModel ( AgentRole . Function , text )
56
57
{
57
58
CurrentAgentId = agent . Id ,
58
59
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 ( )
61
62
} ;
62
63
63
64
// Somethings LLM will generate a function name with agent name.
@@ -66,17 +67,17 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
66
67
responseMessage . FunctionName = responseMessage . FunctionName . Split ( '.' ) . Last ( ) ;
67
68
}
68
69
}
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
+ // }
80
81
else
81
82
{
82
83
responseMessage = new RoleDialogModel ( AgentRole . Assistant , text )
@@ -113,8 +114,8 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
113
114
Prompt = prompt ,
114
115
Provider = Provider ,
115
116
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
118
119
} ) ;
119
120
}
120
121
@@ -157,20 +158,21 @@ public async Task<bool> GetChatCompletionsAsync(Agent agent,
157
158
Prompt = prompt ,
158
159
Provider = Provider ,
159
160
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
162
163
} ) ;
163
164
}
164
165
165
- if ( reason == ChatFinishReason . FunctionCall )
166
+ if ( reason == ChatFinishReason . FunctionCall || reason == ChatFinishReason . ToolCalls )
166
167
{
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 } )") ;
168
170
169
171
var funcContextIn = new RoleDialogModel ( AgentRole . Function , text )
170
172
{
171
173
CurrentAgentId = agent . Id ,
172
- FunctionName = value . FunctionCall ? . FunctionName ,
173
- FunctionArgs = value . FunctionCall ? . FunctionArguments
174
+ FunctionName = toolCall ? . FunctionName ,
175
+ FunctionArgs = toolCall ? . FunctionArguments ? . ToString ( )
174
176
} ;
175
177
176
178
// Somethings LLM will generate a function name with agent name.
@@ -201,19 +203,20 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
201
203
202
204
await foreach ( var choice in response )
203
205
{
204
- if ( choice . FinishReason == ChatFinishReason . FunctionCall )
206
+ if ( choice . FinishReason == ChatFinishReason . FunctionCall || choice . FinishReason == ChatFinishReason . ToolCalls )
205
207
{
206
- Console . Write ( choice . FunctionCallUpdate ? . FunctionArgumentsUpdate ) ;
208
+ var update = choice . ToolCallUpdates ? . FirstOrDefault ( ) ? . FunctionArgumentsUpdate ? . ToString ( ) ?? string . Empty ;
209
+ Console . Write ( update ) ;
207
210
208
- await onMessageReceived ( new RoleDialogModel ( AgentRole . Assistant , choice . FunctionCallUpdate ? . FunctionArgumentsUpdate ) ) ;
211
+ await onMessageReceived ( new RoleDialogModel ( AgentRole . Assistant , update ) ) ;
209
212
continue ;
210
213
}
211
214
212
215
if ( choice . ContentUpdate . IsNullOrEmpty ( ) ) continue ;
213
216
214
217
_logger . LogInformation ( choice . ContentUpdate [ 0 ] ? . Text ) ;
215
218
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 ) ) ;
217
220
}
218
221
219
222
return true ;
@@ -235,7 +238,7 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
235
238
var options = new ChatCompletionOptions ( )
236
239
{
237
240
Temperature = temperature ,
238
- MaxTokens = maxTokens
241
+ MaxOutputTokenCount = maxTokens
239
242
} ;
240
243
241
244
foreach ( var function in agent . Functions )
@@ -267,21 +270,35 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
267
270
messages . Add ( sample . Role == AgentRole . User ? new UserChatMessage ( sample . Content ) : new AssistantChatMessage ( sample . Content ) ) ;
268
271
}
269
272
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 )
271
281
{
272
282
if ( message . Role == AgentRole . Function )
273
283
{
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 >
275
292
{
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
+ } ) ) ;
278
295
279
- messages . Add ( new FunctionChatMessage ( message . FunctionName , message . Content ) ) ;
296
+ messages . Add ( new ToolChatMessage ( message . FunctionName , message . Content ) ) ;
280
297
}
281
298
else if ( message . Role == AgentRole . User )
282
299
{
283
300
var text = ! string . IsNullOrWhiteSpace ( message . Payload ) ? message . Payload : message . Content ;
284
- var textPart = ChatMessageContentPart . CreateTextMessageContentPart ( text ) ;
301
+ var textPart = ChatMessageContentPart . CreateTextPart ( text ) ;
285
302
var contentParts = new List < ChatMessageContentPart > { textPart } ;
286
303
287
304
if ( allowMultiModal && ! message . Files . IsNullOrEmpty ( ) )
@@ -291,20 +308,20 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
291
308
if ( ! string . IsNullOrEmpty ( file . FileData ) )
292
309
{
293
310
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 ) ;
295
312
contentParts . Add ( contentPart ) ;
296
313
}
297
314
else if ( ! string . IsNullOrEmpty ( file . FileStorageUrl ) )
298
315
{
299
316
var contentType = FileUtility . GetFileContentType ( file . FileStorageUrl ) ;
300
317
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 ) ;
302
319
contentParts . Add ( contentPart ) ;
303
320
}
304
321
else if ( ! string . IsNullOrEmpty ( file . FileUrl ) )
305
322
{
306
323
var uri = new Uri ( file . FileUrl ) ;
307
- var contentPart = ChatMessageContentPart . CreateImageMessageContentPart ( uri , ImageChatMessageContentPartDetail . Low ) ;
324
+ var contentPart = ChatMessageContentPart . CreateImagePart ( uri , ChatImageDetailLevel . Low ) ;
308
325
contentParts . Add ( contentPart ) ;
309
326
}
310
327
}
@@ -347,7 +364,7 @@ private string GetPrompt(IEnumerable<ChatMessage> messages, ChatCompletionOption
347
364
. Where ( x => x as SystemChatMessage == null )
348
365
. Select ( x =>
349
366
{
350
- var fnMessage = x as FunctionChatMessage ;
367
+ var fnMessage = x as ToolChatMessage ;
351
368
if ( fnMessage != null )
352
369
{
353
370
return $ "{ AgentRole . Function } : { fnMessage . Content . FirstOrDefault ( ) ? . Text ?? string . Empty } ";
@@ -365,8 +382,9 @@ private string GetPrompt(IEnumerable<ChatMessage> messages, ChatCompletionOption
365
382
var assistMessage = x as AssistantChatMessage ;
366
383
if ( assistMessage != null )
367
384
{
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 } )" :
370
388
$ "{ AgentRole . Assistant } : { assistMessage . Content . FirstOrDefault ( ) ? . Text ?? string . Empty } ";
371
389
}
372
390
0 commit comments