Skip to content

add custom chat template #3251

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 31 commits into from
Aug 18, 2025
Merged

add custom chat template #3251

merged 31 commits into from
Aug 18, 2025

Conversation

luukunn
Copy link
Collaborator

@luukunn luukunn commented Aug 6, 2025

增加自定义chat template
1.启动服务时通过--chat-template参数可以指定加载的chat-template,会覆盖模型原有的模板
2.请求时通过chat_template参数可以传递chat-template模板,传递的模板会覆盖模型原有的模板,以及启动服务时通过--chat-template指定的模板

Copy link

paddle-bot bot commented Aug 6, 2025

Thanks for your contribution!

@paddle-bot paddle-bot bot added the contributor External developers label Aug 6, 2025
@@ -161,6 +161,9 @@ The following extra parameters are supported:
chat_template_kwargs: Optional[dict] = None
# Additional parameters passed to the chat template, used for customizing dialogue formats (default None).

chat_template: Optional[str] = None
Copy link
Collaborator

Choose a reason for hiding this comment

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

docs/zh/parameters.md 和 docs/parameters.md 中补充启动参数说明。 中补充启动参数说明。


messages_len = len(messages)
for i in range(messages_len):
messages[i] = {"messages": messages[i]}
messages[i] = {"messages": messages[i], "chat_template": chat_template}
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个参数,不建议放在 messages 中, 放在 chat_template_kwargs 中吧? 后面的透传行为可以继续传下去。

@@ -104,6 +105,7 @@ async def lifespan(app: FastAPI):
pid = os.getppid()
else:
pid = os.getpid()
chat_template = load_chat_template(args.chat_template)
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个适合放在外面进行 load, 这里面会启动多进程。

@@ -84,6 +85,8 @@ async def create_chat_completion(self, request: ChatCompletionRequest):
api_server_logger.info(f"create chat completion request: {request_id}")

try:
if request.chat_template is None:
request.chat_template = self.chat_template
Copy link
Collaborator

Choose a reason for hiding this comment

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

要放在 to_dict_for_infer 之后, 因为chat_template 不是 openai 标准参数, 不能直接放在 request 传递。

@@ -0,0 +1,22 @@
import os
Copy link
Collaborator

Choose a reason for hiding this comment

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

单测除了验证模板打开, 还需要验证请求时增加 自定义模板。

@LiqinruiG LiqinruiG merged commit 9c12981 into PaddlePaddle:develop Aug 18, 2025
13 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants