@@ -696,8 +696,8 @@ def _print_received_message(self, message: Union[Dict, str], sender: Agent):
696
696
id_key = "name"
697
697
else :
698
698
id_key = "tool_call_id"
699
-
700
- func_print = f"***** Response from calling { message ['role' ]} \" { message [ id_key ] } \" *****"
699
+ id = message . get ( id_key , "No id found" )
700
+ func_print = f"***** Response from calling { message ['role' ]} ( { id } ) *****"
701
701
print (colored (func_print , "green" ), flush = True )
702
702
print (message ["content" ], flush = True )
703
703
print (colored ("*" * len (func_print ), "green" ), flush = True )
@@ -714,7 +714,7 @@ def _print_received_message(self, message: Union[Dict, str], sender: Agent):
714
714
if "function_call" in message and message ["function_call" ]:
715
715
function_call = dict (message ["function_call" ])
716
716
func_print = (
717
- f"***** Suggested function Call : { function_call .get ('name' , '(No function name found)' )} *****"
717
+ f"***** Suggested function call : { function_call .get ('name' , '(No function name found)' )} *****"
718
718
)
719
719
print (colored (func_print , "green" ), flush = True )
720
720
print (
@@ -726,9 +726,9 @@ def _print_received_message(self, message: Union[Dict, str], sender: Agent):
726
726
print (colored ("*" * len (func_print ), "green" ), flush = True )
727
727
if "tool_calls" in message and message ["tool_calls" ]:
728
728
for tool_call in message ["tool_calls" ]:
729
- id = tool_call .get ("id" , "( No id found) " )
729
+ id = tool_call .get ("id" , "No tool call id found" )
730
730
function_call = dict (tool_call .get ("function" , {}))
731
- func_print = f"***** Suggested tool Call ({ id } ): { function_call .get ('name' , '(No function name found)' )} *****"
731
+ func_print = f"***** Suggested tool call ({ id } ): { function_call .get ('name' , '(No function name found)' )} *****"
732
732
print (colored (func_print , "green" ), flush = True )
733
733
print (
734
734
"Arguments: \n " ,
@@ -1309,6 +1309,12 @@ def _generate_oai_reply_from_client(self, llm_client, messages, cache) -> Union[
1309
1309
)
1310
1310
for tool_call in extracted_response .get ("tool_calls" ) or []:
1311
1311
tool_call ["function" ]["name" ] = self ._normalize_name (tool_call ["function" ]["name" ])
1312
+ # Remove id and type if they are not present.
1313
+ # This is to make the tool call object compatible with Mistral API.
1314
+ if tool_call .get ("id" ) is None :
1315
+ tool_call .pop ("id" )
1316
+ if tool_call .get ("type" ) is None :
1317
+ tool_call .pop ("type" )
1312
1318
return extracted_response
1313
1319
1314
1320
async def a_generate_oai_reply (
@@ -1525,7 +1531,6 @@ def generate_tool_calls_reply(
1525
1531
message = messages [- 1 ]
1526
1532
tool_returns = []
1527
1533
for tool_call in message .get ("tool_calls" , []):
1528
- id = tool_call ["id" ]
1529
1534
function_call = tool_call .get ("function" , {})
1530
1535
func = self ._function_map .get (function_call .get ("name" , None ), None )
1531
1536
if inspect .iscoroutinefunction (func ):
@@ -1543,13 +1548,24 @@ def generate_tool_calls_reply(
1543
1548
loop .close ()
1544
1549
else :
1545
1550
_ , func_return = self .execute_function (function_call )
1546
- tool_returns .append (
1547
- {
1548
- "tool_call_id" : id ,
1551
+ content = func_return .get ("content" , "" )
1552
+ if content is None :
1553
+ content = ""
1554
+ tool_call_id = tool_call .get ("id" , None )
1555
+ if tool_call_id is not None :
1556
+ tool_call_response = {
1557
+ "tool_call_id" : tool_call_id ,
1549
1558
"role" : "tool" ,
1550
- "content" : func_return . get ( " content" , "" ) ,
1559
+ "content" : content ,
1551
1560
}
1552
- )
1561
+ else :
1562
+ # Do not include tool_call_id if it is not present.
1563
+ # This is to make the tool call object compatible with Mistral API.
1564
+ tool_call_response = {
1565
+ "role" : "tool" ,
1566
+ "content" : content ,
1567
+ }
1568
+ tool_returns .append (tool_call_response )
1553
1569
if tool_returns :
1554
1570
return True , {
1555
1571
"role" : "tool" ,
0 commit comments