modelscope / modelscope/ms-swift

训练Agent是否支持 "role": "tool_response",和"role": "user",一起出现

Open
#8,499 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
15.7k
Forks
1.7k
Avg merge
1d 16h
Merged PRs (30d)
136

Description

Checklist / 检查清单
  • I have searched existing issues, and this is a new feature request. / 我已经搜索过现有的 issues,确认这是一个新的 Feature Request。
Feature Request Description / Feature Request 描述

from swift import get_processor, get_template

data = {
"tools": [
{
"type": "function",
"function": {
"name": "realtime_aqi",
"description": "天气预报。获取实时空气质量。当前空气质量,PM2.5,PM10信息",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名,例如:上海"
}
},
"required": ["city"]
}
}
},
{
"type": "function",
"function": {
"name": "realtime_aqi_2",
"description": "天气预报。获取实时空气质量。当前空气质量,PM2.5,PM10信息",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名,例如:上海"
}
},
"required": ["city"]
}
}
}
],
"messages": [
{
"role": "system",
"content": "你是一个智能助手,能够使用工具来帮助用户查询信息。"
},
{
"role": "user",
"content": "北京和上海今天的天气情况"
},
{
"role": "assistant",
"content": "\n用户询问北京和上海两个城市的天气情况。我需要:\n1. 识别出需要查询的城市:北京、上海\n2. 使用realtime_aqi工具分别查询这两个城市的空气质量\n3. 由于这是两个独立的查询,我应该并行调用工具以提高效率\n\n我来帮你查询这两个城市的天气情况。"
},
{
"role": "tool_call",
"content": {"name": "realtime_aqi", "arguments": {"city": "北京"}}
},
{
"role": "tool_call",
"content": {"name": "realtime_aqi", "arguments": {"city": "上海"}}
},
{
"role": "tool_response",
"content": "{"city": "北京", "aqi": "10", "unit": "celsius"}"
},
{
"role": "tool_response",
"content": "{"city": "上海", "aqi": "72", "unit": "fahrenheit"}"
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "[Request interrupted by user]\n"
}
]
},
{
"role": "assistant",
"content": "\n工具返回了两个城市的空气质量数据:\n- 北京:AQI为10,空气质量优良\n- 上海:AQI为72,属于轻度污染\n我需要将这些数据整合后,用清晰易懂的方式告诉用户。\n\n根据天气预报工具,北京今天的空气质量指数为10,属于良好水平;上海今天的空气质量指数为72,属于轻度污染水平。"
}
]
}

tokenizer = get_processor('/models/Qwen/Qwen3.5-27B')
template = get_template(tokenizer) # 使用默认agent模板

template = get_template(tokenizer, agent_template='qwen3_5')

print(f'agent_template: {template._agent_template}')

template.set_mode('train')
encoded = template.encode(data)
print(f'[INPUT_IDS] {template.safe_decode(encoded["input_ids"])}\n')
print(f'[LABELS] {template.safe_decode(encoded["labels"])}')

报错:Traceback (most recent call last):
File "/work/distill/custom/test/messages/test.py", line 105, in
encoded = template.encode(data)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 124, in decorate_context
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/swift/utils/utils.py", line 444, in new_func
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/swift/template/base.py", line 525, in encode
encoded = self._encode_truncated(chosen)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/swift/template/base.py", line 1288, in _encode_truncated
encoded = self._encode(inputs)
^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/swift/template/templates/qwen.py", line 511, in _encode
encoded = Template._encode(self, inputs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/swift/template/base.py", line 1340, in _encode
self._swift_encode(inputs) if template_backend == 'swift' else self._jinja_encode(inputs))
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/swift/template/base.py", line 1174, in _swift_encode
assert response_role in {'assistant'}, f'response_role: "{response_role}"'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: response_role: "user"

Pull Request / Pull Request 信息

我理解应该是支持工具调用结果和用户输入一起作为模型的输入的吧

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example with swift/template/base.py and swift/template/templates/qwen.py, starting at Template._swift_encode and the response_role assertion. Trace how mixed tool_response and user messages are encoded in train mode, then check existing template tests if available. Done means the supplied message sequence encodes without the response_role="user" assertion and preserves the intended training labels.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.