@@ -117,15 +117,28 @@ func mistralChatParamsFromCallOptions(callOpts *llms.CallOptions) sdk.ChatReques
117
117
chatOpts .Temperature = callOpts .Temperature
118
118
chatOpts .RandomSeed = callOpts .Seed
119
119
chatOpts .Tools = make ([]sdk.Tool , 0 )
120
- for _ , function := range callOpts .Functions {
121
- chatOpts .Tools = append (chatOpts .Tools , sdk.Tool {
122
- Type : "function" ,
123
- Function : sdk.Function {
124
- Name : function .Name ,
125
- Description : function .Description ,
126
- Parameters : function .Parameters ,
127
- },
128
- })
120
+ if len (callOpts .Tools ) > 0 {
121
+ for _ , tool := range callOpts .Tools {
122
+ chatOpts .Tools = append (chatOpts .Tools , sdk.Tool {
123
+ Type : "function" ,
124
+ Function : sdk.Function {
125
+ Name : tool .Function .Name ,
126
+ Description : tool .Function .Description ,
127
+ Parameters : tool .Function .Parameters ,
128
+ },
129
+ })
130
+ }
131
+ } else {
132
+ for _ , function := range callOpts .Functions {
133
+ chatOpts .Tools = append (chatOpts .Tools , sdk.Tool {
134
+ Type : "function" ,
135
+ Function : sdk.Function {
136
+ Name : function .Name ,
137
+ Description : function .Description ,
138
+ Parameters : function .Parameters ,
139
+ },
140
+ })
141
+ }
129
142
}
130
143
return chatOpts
131
144
}
@@ -159,6 +172,16 @@ func generateNonStreamingContent(ctx context.Context, m *Model, callOptions *llm
159
172
toolCalls := choice .Message .ToolCalls
160
173
if len (toolCalls ) > 0 {
161
174
langchainContentResponse .Choices [idx ].FuncCall = (* llms .FunctionCall )(& toolCalls [0 ].Function )
175
+ for _ , tool := range toolCalls {
176
+ langchainContentResponse .Choices [0 ].ToolCalls = append (langchainContentResponse .Choices [0 ].ToolCalls , llms.ToolCall {
177
+ ID : tool .Id ,
178
+ Type : string (tool .Type ),
179
+ FunctionCall : & llms.FunctionCall {
180
+ Name : tool .Function .Name ,
181
+ Arguments : tool .Function .Arguments ,
182
+ },
183
+ })
184
+ }
162
185
}
163
186
}
164
187
m .CallbacksHandler .HandleLLMGenerateContentEnd (ctx , langchainContentResponse )
@@ -192,6 +215,16 @@ func generateStreamingContent(ctx context.Context, m *Model, callOptions *llms.C
192
215
langchainContentResponse .Choices [0 ].StopReason = string (choice .FinishReason )
193
216
if len (choice .Delta .ToolCalls ) > 0 {
194
217
langchainContentResponse .Choices [0 ].FuncCall = (* llms .FunctionCall )(& choice .Delta .ToolCalls [0 ].Function )
218
+ for _ , tool := range choice .Delta .ToolCalls {
219
+ langchainContentResponse .Choices [0 ].ToolCalls = append (langchainContentResponse .Choices [0 ].ToolCalls , llms.ToolCall {
220
+ ID : tool .Id ,
221
+ Type : string (tool .Type ),
222
+ FunctionCall : & llms.FunctionCall {
223
+ Name : tool .Function .Name ,
224
+ Arguments : tool .Function .Arguments ,
225
+ },
226
+ })
227
+ }
195
228
}
196
229
}
197
230
err := callOptions .StreamingFunc (ctx , []byte (chunkStr ))
@@ -209,19 +242,25 @@ func generateStreamingContent(ctx context.Context, m *Model, callOptions *llms.C
209
242
func convertToMistralChatMessages (langchainMessages []llms.MessageContent ) ([]sdk.ChatMessage , error ) {
210
243
messages := make ([]sdk.ChatMessage , 0 )
211
244
for _ , msg := range langchainMessages {
212
- msgText := ""
213
245
for _ , part := range msg .Parts {
214
- textContent , ok := part .(llms.TextContent )
215
- if ! ok {
246
+ switch p := part .(type ) {
247
+ case llms.TextContent :
248
+ chatMsg := sdk.ChatMessage {Content : p .Text , Role : string (msg .Role )}
249
+ setMistralChatMessageRole (& msg , & chatMsg ) // #nosec G601
250
+ if chatMsg .Content != "" && chatMsg .Role != "" {
251
+ messages = append (messages , chatMsg )
252
+ }
253
+ case llms.ToolCallResponse :
254
+ chatMsg := sdk.ChatMessage {Role : string (msg .Role ), Content : p .Content }
255
+ setMistralChatMessageRole (& msg , & chatMsg ) // #nosec G601
256
+ messages = append (messages , chatMsg )
257
+ case llms.ToolCall :
258
+ chatMsg := sdk.ChatMessage {Role : string (msg .Role ), ToolCalls : []sdk.ToolCall {{Id : p .ID , Type : sdk .ToolTypeFunction , Function : sdk.FunctionCall {Name : p .FunctionCall .Name , Arguments : p .FunctionCall .Arguments }}}}
259
+ setMistralChatMessageRole (& msg , & chatMsg ) // #nosec G601
260
+ messages = append (messages , chatMsg )
261
+ default :
216
262
return nil , errors .New ("unsupported content type encountered while preparing chat messages to send to mistral platform" )
217
263
}
218
- msgText += textContent .Text
219
- }
220
- chatMsg := sdk.ChatMessage {Content : msgText , Role : "user" }
221
-
222
- setMistralChatMessageRole (& msg , & chatMsg ) // #nosec G601
223
- if chatMsg .Content != "" && chatMsg .Role != "" {
224
- messages = append (messages , chatMsg )
225
264
}
226
265
}
227
266
return messages , nil
0 commit comments