fix: merge consecutive tool_calls into single assistant message for Crof/OpenAI-compat
This commit is contained in:
Binary file not shown.
@@ -148,8 +148,19 @@ def oa_input_to_messages(input_data):
|
|||||||
if isinstance(input_data, str):
|
if isinstance(input_data, str):
|
||||||
msgs.append({"role": "user", "content": input_data})
|
msgs.append({"role": "user", "content": input_data})
|
||||||
elif isinstance(input_data, list):
|
elif isinstance(input_data, list):
|
||||||
|
pending_tool_calls = []
|
||||||
for item in input_data:
|
for item in input_data:
|
||||||
t = item.get("type")
|
t = item.get("type")
|
||||||
|
if t == "function_call":
|
||||||
|
pending_tool_calls.append(
|
||||||
|
{"id": item.get("call_id", item.get("id", uid("tc"))),
|
||||||
|
"type": "function",
|
||||||
|
"function": {"name": item.get("name", ""),
|
||||||
|
"arguments": item.get("arguments", "{}")}})
|
||||||
|
continue
|
||||||
|
if pending_tool_calls:
|
||||||
|
msgs.append({"role": "assistant", "content": None, "tool_calls": pending_tool_calls})
|
||||||
|
pending_tool_calls = []
|
||||||
if t == "message":
|
if t == "message":
|
||||||
role = item.get("role", "user")
|
role = item.get("role", "user")
|
||||||
if role == "developer":
|
if role == "developer":
|
||||||
@@ -167,16 +178,11 @@ def oa_input_to_messages(input_data):
|
|||||||
break
|
break
|
||||||
if text is not None:
|
if text is not None:
|
||||||
msgs.append({"role": role, "content": text})
|
msgs.append({"role": role, "content": text})
|
||||||
elif t == "function_call":
|
|
||||||
msgs.append({"role": "assistant", "content": None, "tool_calls": [
|
|
||||||
{"id": item.get("call_id", item.get("id", uid("tc"))),
|
|
||||||
"type": "function",
|
|
||||||
"function": {"name": item.get("name", ""),
|
|
||||||
"arguments": item.get("arguments", "{}")}}
|
|
||||||
]})
|
|
||||||
elif t == "function_call_output":
|
elif t == "function_call_output":
|
||||||
msgs.append({"role": "tool", "tool_call_id": item.get("id", ""),
|
msgs.append({"role": "tool", "tool_call_id": item.get("id", ""),
|
||||||
"content": item.get("output", "")})
|
"content": item.get("output", "")})
|
||||||
|
if pending_tool_calls:
|
||||||
|
msgs.append({"role": "assistant", "content": None, "tool_calls": pending_tool_calls})
|
||||||
return msgs
|
return msgs
|
||||||
|
|
||||||
def oa_convert_tools(tools):
|
def oa_convert_tools(tools):
|
||||||
|
|||||||
Reference in New Issue
Block a user