Skip to content

fix get_toolcall & fix ci #3999

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

Merged
merged 4 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion swift/llm/infer/infer_engine/infer_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ def infer(self,

@staticmethod
def _get_toolcall(response: str, template: Template) -> Optional[List[ChatCompletionMessageToolCall]]:
functions = template.agent_template.get_toolcall(response)
try:
functions = template.agent_template.get_toolcall(response)
except Exception:
functions = None
if functions:
return [ChatCompletionMessageToolCall(function=function) for function in functions]

Expand Down
2 changes: 1 addition & 1 deletion swift/plugin/agent_template/hermes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def get_toolcall(self, response: str) -> List['Function']:
functions = []
for res in res_list:
res = self._parse_json(res)
if res is not None:
if isinstance(res, dict) and 'name' in res and 'arguments' in res:
functions.append(Function(name=res['name'], arguments=res['arguments']))
if len(functions) == 0:
# compat react_en
Expand Down
2 changes: 1 addition & 1 deletion swift/plugin/agent_template/llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def get_toolcall(self, response: str) -> List['Function']:
res_list = re.findall(r'{[^{]*?"name":.*?"parameters":\s*?{.*?}\s*?}', response, re.DOTALL)
for res in res_list:
res = self._parse_json(res)
if res is not None:
if isinstance(res, dict) and 'name' in res and 'parameters' in res:
functions.append(Function(name=res['name'], arguments=res['parameters']))
if len(functions) == 0:
# compat react_en
Expand Down
12 changes: 6 additions & 6 deletions tests/llm/test_template.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
import unittest

if __name__ == '__main__':
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
os.environ['SWIFT_DEBUG'] = '1'
from swift.llm import PtEngine, RequestConfig, get_model_tokenizer, get_template
from swift.utils import get_logger, seed_everything
from swift.llm import PtEngine, RequestConfig, get_model_tokenizer, get_template
from swift.utils import get_logger, seed_everything

# os.environ['CUDA_VISIBLE_DEVICES'] = '0'
os.environ['SWIFT_DEBUG'] = '1'

logger = get_logger()

Expand Down
Loading