-
Notifications
You must be signed in to change notification settings - Fork 838
[model] support glm-4.5 agent #5305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -22,7 +22,6 @@ def _find_function_call(single_content: str) -> Optional['Function']: | |||||||||||||
matches = pattern.findall(single_content) | ||||||||||||||
if not matches: | ||||||||||||||
return | ||||||||||||||
|
||||||||||||||
name, arguments = matches[0] | ||||||||||||||
return Function(name=name, arguments=arguments) | ||||||||||||||
|
||||||||||||||
|
@@ -77,3 +76,72 @@ def _format_tool_calls(self, tool_call_messages) -> str: | |||||||||||||
|
||||||||||||||
class GLM4_0414AgentTemplate(GLM4AgentTemplate): | ||||||||||||||
is_glm4_0414 = True | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
class GLM4_5AgentTemplate(BaseAgentTemplate): | ||||||||||||||
|
||||||||||||||
@staticmethod | ||||||||||||||
def _find_function_call(single_content: str) -> Optional['Function']: | ||||||||||||||
from swift.llm.infer import Function | ||||||||||||||
single_content = single_content.strip() | ||||||||||||||
func_name_match = re.match(r'^([^\n<]+)', single_content) | ||||||||||||||
if not func_name_match: | ||||||||||||||
return None | ||||||||||||||
func_name = func_name_match.group(1).strip() | ||||||||||||||
keys = re.findall(r'<arg_key>(.*?)</arg_key>', single_content, re.DOTALL) | ||||||||||||||
values = re.findall(r'<arg_value>(.*?)</arg_value>', single_content, re.DOTALL) | ||||||||||||||
if len(keys) != len(values): | ||||||||||||||
return None | ||||||||||||||
args = {k.strip(): v.strip() for k, v in zip(keys, values)} | ||||||||||||||
return Function(name=func_name, arguments=json.dumps(args, ensure_ascii=False)) | ||||||||||||||
|
||||||||||||||
def get_toolcall(self, response: str) -> List['Function']: | ||||||||||||||
toolcall_list = re.findall(r'<tool_call>(.*?)</tool_call>', response, re.DOTALL) | ||||||||||||||
functions = [] | ||||||||||||||
for toolcall in toolcall_list: | ||||||||||||||
function = self._find_function_call(toolcall) | ||||||||||||||
if function: | ||||||||||||||
functions.append(function) | ||||||||||||||
if len(functions) == 0: | ||||||||||||||
# compat react_en | ||||||||||||||
return super().get_toolcall(response) | ||||||||||||||
return functions | ||||||||||||||
|
||||||||||||||
def _format_tools(self, tools: List[Union[str, dict]], system: str, user_message=None) -> str: | ||||||||||||||
laocheyujie marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
tool_descs = [ | ||||||||||||||
'# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>' | ||||||||||||||
] | ||||||||||||||
for tool in tools: | ||||||||||||||
tool_descs.append(f'{json.dumps(tool, ensure_ascii=False)}') | ||||||||||||||
tool_descs.append( | ||||||||||||||
'</tools>\n\nFor each function call, output the function name and arguments within the following XML format:\n<tool_call>{function-name}\n<arg_key>{arg-key-1}</arg_key>\n<arg_value>{arg-value-1}</arg_value>\n<arg_key>{arg-key-2}</arg_key>\n<arg_value>{arg-value-2}</arg_value>\n...\n</tool_call>' | ||||||||||||||
) | ||||||||||||||
tool_descs = '\n'.join(tool_descs) | ||||||||||||||
if system.strip(): | ||||||||||||||
tool_descs += '<|system|>\n' + system.strip() | ||||||||||||||
return tool_descs | ||||||||||||||
|
||||||||||||||
def _format_tool_responses( | ||||||||||||||
self, | ||||||||||||||
assistant_content: str, | ||||||||||||||
tool_messages, | ||||||||||||||
) -> Tuple[str, 'Prompt']: | ||||||||||||||
with_action = self.keyword.action in assistant_content and self.keyword.action_input in assistant_content | ||||||||||||||
if with_action: | ||||||||||||||
return super()._format_tool_responses(assistant_content, tool_messages) | ||||||||||||||
res = [] | ||||||||||||||
for _, tool_message in enumerate(tool_messages): | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||
tool_content = tool_message['content'] | ||||||||||||||
res.append(f'\n<tool_response>\n{tool_content}\n</tool_response>') | ||||||||||||||
res.append('<|assistant|>\n') | ||||||||||||||
return assistant_content, res | ||||||||||||||
|
||||||||||||||
def _format_tool_calls(self, tool_call_messages) -> str: | ||||||||||||||
tool_calls = [] | ||||||||||||||
for message in tool_call_messages: | ||||||||||||||
tool_call = self._parse_tool_call(message['content']) | ||||||||||||||
tool_calls.append(f"<tool_call>{tool_call['name']}") | ||||||||||||||
for arg_key, arg_value in tool_call['arguments'].items(): | ||||||||||||||
tool_calls.append(f'<arg_key>{arg_key}</arg_key>\n<arg_value>{arg_value}</arg_value>') | ||||||||||||||
Comment on lines
+144
to
+145
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code assumes that
Suggested change
|
||||||||||||||
tool_calls.append('</tool_call>') | ||||||||||||||
return '\n'.join(tool_calls) + '<|observation|>' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The loop for collecting functions can be made more concise and Pythonic by using a list comprehension with a walrus operator (PEP 572). This can improve readability and reduce boilerplate code.