Skip to content

Commit 9670bc2

Browse files
author
xusenlin
committed
fix chatglm4 template
1 parent 60dc8f9 commit 9670bc2

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

api/adapter/template.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -582,22 +582,25 @@ def postprocess_messages(
582582
{
583583
"role": Role.SYSTEM,
584584
"content": None,
585-
"tools": functions or [t["function"] for t in tools]
585+
"tools": tools or [{"type": "function", "function": f} for f in functions]
586586
}
587587
)
588588

589589
for m in _messages:
590590
role, content, func_call = m["role"], m["content"], m.get("function_call")
591-
if role == Role.FUNCTION:
591+
if role in [Role.FUNCTION, Role.TOOL]:
592592
messages.append(
593593
{
594594
"role": "observation",
595595
"content": content
596596
}
597597
)
598598
elif role == "assistant" and func_call is not None:
599-
for response in content.split(""):
600-
metadata, sub_content = response.split("\n", maxsplit=1)
599+
for response in content.split("<|assistant|>"):
600+
if "\n" in response:
601+
metadata, sub_content = response.split("\n", maxsplit=1)
602+
else:
603+
metadata, sub_content = "", response
601604
messages.append(
602605
{
603606
"role": role,
@@ -620,7 +623,7 @@ def parse_assistant_response(
620623
tools: Optional[List[Dict[str, Any]]] = None,
621624
) -> Tuple[str, Optional[Union[str, Dict[str, Any]]]]:
622625
content = ""
623-
for response in output.split(""):
626+
for response in output.split("<|assistant|>"):
624627
if "\n" in response:
625628
metadata, content = response.split("\n", maxsplit=1)
626629
else:
@@ -630,8 +633,7 @@ def parse_assistant_response(
630633
content = content.strip()
631634
else:
632635
if functions or tools:
633-
content = "\n".join(content.split("\n")[1:-1])
634-
parameters = eval(content)
636+
parameters = eval(content.strip())
635637
if functions:
636638
content = {
637639
"name": metadata.strip(),

0 commit comments

Comments
 (0)