Skip to content

Conversation

@hpsun1109
Copy link
Contributor

@hpsun1109 hpsun1109 commented Oct 14, 2025

PR type

  • Bug Fix
  • New Feature
  • Document Updates
  • More Models or Datasets Support

PR information

Write the detail information belongs to this PR.

Experiment results

Paste your experiment result here(if needed).

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @hpsun1109, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the swift library by adding a SeedAgentTemplate, which provides a dedicated and structured approach for integrating tool-calling capabilities into AI models. This new template defines how tools are presented to the model, how the model's tool calls are parsed, and how tool outputs are formatted, thereby improving the framework's ability to handle complex, multi-step agent interactions.

Highlights

  • New Agent Template: Introduced a new SeedAgentTemplate to support specific tool-calling functionalities within the framework.
  • Tool Calling Logic: The new template includes custom methods for parsing tool calls from model responses, formatting tool definitions, and structuring tool responses using specific <seed:tool_call> and <seed:bos>/<seed:eos> tokens.
  • Integration: The SeedAgentTemplate has been integrated into the swift/plugin/agent_template/__init__.py file, making it available for use via the seed_oss key in the AGENT_TEMPLATE_MAPPING.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new SeedAgentTemplate for a specific model. The implementation is generally well-structured, but there are several areas for improvement regarding code style, consistency, and maintainability. My review includes suggestions to maintain alphabetical ordering for imports and dictionary keys, remove redundant and unused code, address language inconsistencies in the generated prompts, and refactor duplicated logic for better code clarity.

from .mistral import MistralAgentTemplate
from .qwen import QwenEnAgentTemplate, QwenEnParallelAgentTemplate, QwenZhAgentTemplate, QwenZhParallelAgentTemplate
from .qwen3_coder import Qwen3CoderAgentTemplate
from .seed_oss import SeedAgentTemplate
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better code readability and maintainability, it's good practice to keep imports sorted alphabetically. The seed_oss import should be placed after the react import.

'llama4': Llama4AgentTemplate,
# ref: https://huggingface.co/deepseek-ai/DeepSeek-V3.1
'deepseek_v3_1': DeepSeekV31AgentTemplate,
'seed_oss': SeedAgentTemplate,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve readability and make it easier to find templates, please keep the keys in the agent_templates dictionary sorted alphabetically. The 'seed_oss' key should be placed after 'react_grpo'.

return SeedAgentTemplate._PY_TYPE_MAPPING.get(t, 'Any')

def get_toolcall(self, response: str) -> List['Function']:
from swift.llm.infer import Function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Function is already imported for type checking at the top of the file (line 8). This local import is redundant and should be removed. The type checker will resolve the name from the top-level import.


return f"Function:\ndef {func_name}({param_str}):\n{docstring}"

def _format_tools(self, tools: List[Union[str, dict]], system: Optional[str] = None, user_message=None) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The parameter user_message is defined but not used within the _format_tools method. It should be removed to clean up the code and avoid confusion.

Suggested change
def _format_tools(self, tools: List[Union[str, dict]], system: Optional[str] = None, user_message=None) -> str:
def _format_tools(self, tools: List[Union[str, dict]], system: Optional[str] = None) -> str:

Comment on lines +100 to +132
def _format_tools(self, tools: List[Union[str, dict]], system: Optional[str] = None, user_message=None) -> str:
if not tools:
return system or ''

if system:
system_prompt = system
else:
system_prompt = "You are Doubao, a helpful AI assistant. You may call one or more functions to assist with the user query."

tool_defs = [
self._build_tool_def_string(self.wrap_tool(tool))
for tool in tools if self.wrap_tool(tool).get('type') == 'function'
]
tool_defs_joined = '\n\n'.join(tool_defs)

tool_call_format_instruction = (
"工具调用请遵循如下格式:\n"
f"{self.TOOL_CALL_START}\n"
f"<{self.FUNCTION_TAG}=example_function_name>\n"
f"<{self.PARAMETER_TAG}=example_parameter_1>value_1</{self.PARAMETER_TAG}>\n"
f"<{self.PARAMETER_TAG}=example_parameter_2>This is the value for the second parameter\n"
"that can span\n"
f"multiple lines</{self.PARAMETER_TAG}>\n"
f"</{self.FUNCTION_TAG}>\n"
f"{self.TOOL_CALL_END}"
)

split_token = "<seed:eos><seed:bos>system"
if split_token in system_prompt:
parts = system_prompt.split(split_token, 1)
return f"{parts[0]}\n\n{tool_defs_joined}\n{tool_call_format_instruction}\n{split_token}{parts[1]}"
else:
return f"{system_prompt}\n\n{tool_defs_joined}\n{tool_call_format_instruction}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There's an inconsistency in the language used for constructing the system prompt. The default system prompt (line 107) is in English, while the tool call instructions (line 116) and req_tag in _build_tool_def_string (line 82) are in Chinese. This could lead to confusing prompts for the model and for developers using this template. Please ensure consistency by using a single language.

Comment on lines +78 to +93
if properties:
docstring_parts.append('\n Args:')
required_params = parameters.get('required', [])
for name, spec in properties.items():
req_tag = '[必填]' if name in required_params else '[选填]'
desc = spec.get("description", "")
type_str = self._py_type(spec.get("type", "any"))
docstring_parts.append(f' - {name} ({type_str}) {req_tag}: {desc}')

returns_props = func.get('returns', {}).get('properties', {})
if returns_props:
docstring_parts.append('\n Returns:')
for name, spec in returns_props.items():
desc = spec.get("description", "")
type_str = self._py_type(spec.get("type", "any"))
docstring_parts.append(f' - {name} ({type_str}): {desc}')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There is duplicated logic for processing properties in the if properties: block (lines 78-85) and the if returns_props: block (lines 87-93). The code for extracting description and type, and formatting the output line is very similar. Consider refactoring this into a helper function to reduce code duplication and improve maintainability. A helper function could take a dictionary of properties and format them into a list of strings.

@Jintao-Huang
Copy link
Collaborator

hello 😊

please run following command

pip install pre-commit
pre-commit run --all-files

and please write a test case, referring to here.

https://github.com/modelscope/ms-swift/blob/main/tests/test_align/test_template/test_agent.py

@hpsun1109
Copy link
Contributor Author

hello 😊

please run following command

pip install pre-commit
pre-commit run --all-files

and please write a test case, referring to here.

https://github.com/modelscope/ms-swift/blob/main/tests/test_align/test_template/test_agent.py

image

@Jintao-Huang
Copy link
Collaborator

Hello! Could you use pre-commit to format the code to pass the lint tests?

You need to go into the ms-swift directory.

(use py310/py311)

pip install pre-commit
pre-commit run --all-files

@hpsun1109
Copy link
Contributor Author

Hello! Could you use pre-commit to format the code to pass the lint tests?

You need to go into the ms-swift directory.

(use py310/py311)

pip install pre-commit
pre-commit run --all-files

@hpsun1109 hpsun1109 closed this Oct 23, 2025
@hpsun1109
Copy link
Contributor Author

I made some revisions and resubmitted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants