Skip to content

Commit

Permalink
Fix GLM4 agent toolcall (modelscope#1767)
Browse files Browse the repository at this point in the history
  • Loading branch information
tastelikefeet committed Aug 19, 2024
1 parent 0fe6b2d commit 39513cf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
19 changes: 14 additions & 5 deletions swift/llm/agent/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
2.Do not use origin tool names, use only subfunctions' names.
Specifically, you have access to the following APIs: {tool_list}'''

GLM4_PROMPT = '''你是一个名为 ChatGLM 的人工智能助手。你是基于智谱AI训练的语言模型 GLM-4 模型开发的,你的任务是针对用户的问题和要求提供适当的答复和支持。
# 可用工具
{tool_list}'''


def calculate_loss_scale(query: str,
response: str,
Expand Down Expand Up @@ -186,10 +192,13 @@ def get_tools_prompt(TOOLS: List[Dict[str, Union[str, dict]]], prompt_format: st
print('invalid tools format, please check'
'https://github.com/modelscope/swift/blob/main/docs/source_en/LLM/Agent-deployment-best-practice.md')
return None
tool_descs = '\n\n'.join(tool_descs)
tool_names = ','.join(tool_names)
if prompt_format == 'react_en':
return REACT_PROMPT.format(tool_list=tool_descs, tool_names=tool_names)
return REACT_PROMPT.format(tool_list='\n\n'.join(tool_descs), tool_names=','.join(tool_names))
elif prompt_format == 'react_zh':
return REACT_ZH_PROMPT.format(tool_list=tool_descs, tool_names=tool_names)
return TOOLBENCH_PROMPT.format(tool_list=tool_descs)
return REACT_ZH_PROMPT.format(tool_list='\n\n'.join(tool_descs), tool_names=','.join(tool_names))
elif prompt_format == 'glm4':
tool_list = ''
for name, tool in zip(tool_names, tool_descs):
tool_list += f'## {name}\n\n{tool}\n\n'
return GLM4_PROMPT.format(tool_list=tool_list)
return TOOLBENCH_PROMPT.format(tool_list='\n\n'.join(tool_descs))
2 changes: 0 additions & 2 deletions swift/llm/utils/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2321,15 +2321,13 @@ def _preprocess_toolbench(dataset: DATASET_TYPE) -> DATASET_TYPE:

def reorganize_row(row):
convs = row['conversations']
sys = convs[0]['value']
history = []
history_roles = []
for idx in range(1, len(convs) - 2, 2):
history.append((convs[idx]['value'], convs[idx + 1]['value']))
history_roles.append((convs[idx]['from'], convs[idx + 1]['from']))

return {
'system': sys,
'history': history,
'history_roles': history_roles,
'query': convs[-2]['value'],
Expand Down
4 changes: 2 additions & 2 deletions swift/llm/utils/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@ def cross_entropy_forward(self, inputs: Tensor, target: Tensor) -> Tensor:
ModelType.glm4_9b_chat,
'ZhipuAI/glm-4-9b-chat',
LoRATM.chatglm,
TemplateType.chatglm3,
TemplateType.chatglm4,
support_flash_attn=True,
support_vllm=True,
support_lmdeploy=True,
Expand All @@ -1663,7 +1663,7 @@ def cross_entropy_forward(self, inputs: Tensor, target: Tensor) -> Tensor:
ModelType.glm4_9b_chat_1m,
'ZhipuAI/glm-4-9b-chat-1m',
LoRATM.chatglm,
TemplateType.chatglm3,
TemplateType.chatglm4,
support_flash_attn=True,
support_vllm=True,
support_lmdeploy=True,
Expand Down
8 changes: 8 additions & 0 deletions swift/llm/utils/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class TemplateType:
baichuan = 'baichuan'
chatglm2 = 'chatglm2'
chatglm3 = 'chatglm3'
chatglm4 = 'chatglm4'
codegeex4 = 'codegeex4'
llama = 'llama' # llama2
llama3 = 'llama3'
Expand Down Expand Up @@ -1397,6 +1398,13 @@ def data_collator(self, batch: List[Dict[str, Any]], padding_to: Optional[int] =
TemplateType.chatglm3,
GLMTemplate([], ['<|user|>\n{{QUERY}}<|assistant|>\n'], [], ['<|user|>'], None, ['<|system|>\n{{SYSTEM}}']))

register_template(
TemplateType.chatglm4,
GLMTemplate([], ['<|user|>\n{{QUERY}}<|assistant|>\n'], [], ['<|user|>'],
None, ['<|system|>\n{{SYSTEM}}'],
tools_prompt='glm4',
tool_prompt=['<|observation|>\n{{QUERY}}<|assistant|>\n']))

codegeex4_system = '你是一位智能编程助手,你叫CodeGeeX。你会为用户回答关于编程、代码、计算机方面的任何问题,并提供格式规范、可以执行、准确安全的代码,并在必要时提供详细的解释。'

register_template(
Expand Down

0 comments on commit 39513cf

Please sign in to comment.