@@ -180,17 +180,12 @@ func (o *LLM) convertMessageContent(mc llms.MessageContent) (api.Message, error)
180180func (o * LLM ) convertToolCall (toolCall llms.ToolCall ) (api.ToolCall , error ) {
181181 tc := api.ToolCall {
182182 Function : api.ToolCallFunction {
183- Name : toolCall .FunctionCall .Name ,
183+ Name : toolCall .FunctionCall .Name ,
184+ Index : parseToolCallID (toolCall .ID ),
184185 },
185186 }
186187
187- var err error
188- tc .Function .Index , err = parseToolCallID (toolCall .ID )
189- if err != nil {
190- return api.ToolCall {}, fmt .Errorf ("error converting tool call ID to int: %w" , err )
191- }
192-
193- err = json .Unmarshal ([]byte (toolCall .FunctionCall .Arguments ), & tc .Function .Arguments )
188+ err := json .Unmarshal ([]byte (toolCall .FunctionCall .Arguments ), & tc .Function .Arguments )
194189 if err != nil {
195190 return api.ToolCall {}, fmt .Errorf ("error unmarshalling tool call arguments: %w" , err )
196191 }
@@ -474,16 +469,22 @@ func makeToolCallID(index int, name string) string { //nolint:gosec
474469 return fmt .Sprintf ("ollama-%s-%d" , encHash , index )
475470}
476471
477- func parseToolCallID (id string ) (int , error ) {
472+ func parseToolCallID (id string ) int { //nolint:gosec
473+ fallback := func () int {
474+ hash := crc32 .NewIEEE ()
475+ hash .Write ([]byte (id ))
476+ return int (hash .Sum32 ())
477+ }
478+
478479 parts := strings .Split (id , "-" )
479480 if len (parts ) != 3 {
480- return 0 , fmt . Errorf ( "invalid tool call id: %s" , id )
481+ return fallback ( )
481482 }
482483
483484 index , err := strconv .Atoi (parts [2 ])
484485 if err != nil {
485- return 0 , fmt . Errorf ( "invalid tool call id: %s" , id )
486+ return fallback ( )
486487 }
487488
488- return index , nil
489+ return index
489490}
0 commit comments