Skip to content
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

[Bug] chat模板不支持自定义的role #1572

Closed
2 tasks done
shell-nlp opened this issue May 9, 2024 · 14 comments
Closed
2 tasks done

[Bug] chat模板不支持自定义的role #1572

shell-nlp opened this issue May 9, 2024 · 14 comments

Comments

@shell-nlp
Copy link

shell-nlp commented May 9, 2024

Checklist

  • 1. I have searched related issues but cannot get the expected help.
  • 2. The bug has not been fixed in the latest version.

Describe the bug

image

我需要使用function /Observation 作为 角色 来进行工具调用,但是报错。
image
而且我使用qwen1.5 14B 实验的时候,stop=["Observation:"] 模型输出无法stop
image

Reproduction

chat

Environment

Error traceback

No response

@lvhan028
Copy link
Collaborator

所有llm模型的chat template都在model.py中定义。不太清楚qwen的function call是怎样的模板,如果你清楚的话,能不能麻烦给提个PR支持下?

@shell-nlp
Copy link
Author

image
如上图代码
现在是问题是框架中的角色 仅仅支持 user assistant system 三种身份, 这种身份可以自定义的, vllm 是支持这种功能的,因为vllm的chat模板是用 tokenizer.apply_chat_template来实现的

@lvhan028
Copy link
Collaborator

lmdeploy 默认支持 stateful inference(interactive mode)。不好直接复用 apply_chat_template。
lmdeploy 目前还是需要按照 chat template,以合适的方式在 model.py 中实现一遍。

@shell-nlp
Copy link
Author

建议在messages2prompt方法中 对role 进行判断,如果 role !=user /assistant /system ,则直接使用用户传过来的 role,这样更加通用

@lvhan028
Copy link
Collaborator

@RunningLeon could you help supporting the thorough chat template of qwen and check the stop words mention in this issue.

@shell-nlp
Copy link
Author

lmdeploy 默认支持 stateful inference(interactive mode)。不好直接复用 apply_chat_template。 lmdeploy 目前还是需要按照 chat template,以合适的方式在 model.py 中实现一遍。

建议可以在messages2prompt方法中 对role 进行判断,如果 role !=user /assistant /system ,则直接使用用户传过来的 role,这样更加通用

@RunningLeon
Copy link
Collaborator

RunningLeon commented May 10, 2024

lmdeploy 默认支持 stateful inference(interactive mode)。不好直接复用 apply_chat_template。 lmdeploy 目前还是需要按照 chat template,以合适的方式在 model.py 中实现一遍。

建议可以在messages2prompt方法中 对role 进行判断,如果 role !=user /assistant /system ,则直接使用用户传过来的 role,这样更加通用

@shell-nlp
你可以使用自定义的对话模板,重写messages2prompt方法,参考文档 https://lmdeploy.readthedocs.io/zh-cn/latest/advance/chat_template.html

@RunningLeon
Copy link
Collaborator

RunningLeon commented May 10, 2024

@RunningLeon could you help supporting the thorough chat template of qwen and check the stop words mention in this issue.

OK

@shell-nlp
hi, could you provide the reproduction code for the issue of stopping words? It works on my side when changing the stop words to Observation in Qwen chat template.

@shell-nlp
Copy link
Author

shell-nlp commented May 10, 2024

  1. 使用lmdeploy 运行 qwen1.5-14B-chat
    CUDA_VISIBLE_DEVICES=3 lmdeploy serve api_server /home/dev/model/qwen/Qwen1___5-14B-Chat/ --model-name qwen --server-port 8081
  2. 使用openai库进行模拟代码如下:

from openai import OpenAI

client = OpenAI(api_key="EMPTY", base_url="http://localhost:8081/v1")

stream = True

output = client.chat.completions.create(
    model="qwen",  # internlm chatglm3  qwen  llama3
    messages=[
        {
            "role": "system",
            "content": """Answer the following questions as best you can. You have access to the following tools:

quark_search: Call this tool to interact with the 夸克搜索 API. What is the 夸克搜索 API useful for? 夸克搜索是一个通用搜索引擎,可用于访问互联网、查询百科知识、了解时事新闻等。 Parameters: [{"name": "search_query", "description": "搜索关键词或短语", "required": true, "schema": {"type": "string"}}] Format the arguments as a JSON object.

image_gen: Call this tool to interact with the 通义万相 API. What is the 通义万相 API useful for? 通义万相是一个AI绘画(图像生成)服务,输入文本描述,返回根据文本作画得到的图片的URL Parameters: [{"name": "query", "description": "中文关键词,描述了希望图像具有什么内容", "required": true, "schema": {"type": "string"}}] Format the arguments as a JSON object.

Use the following format:

Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [quark_search,image_gen]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can be repeated zero or more times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question

Begin!""",
        },
        {"role": "user", "content": "Question: 现在给我画个五彩斑斓的黑。"},
    ],
    stream=stream,
    stop=["Observation:"],
)
if stream:
    for chunk in output:
        print(chunk.choices[0].delta.content or "", end="", flush=True)
else:
    print(output.choices[0].message.content)
print()

输出发现 在Observation 处并没有停止。

@shell-nlp
Copy link
Author

同理。如果 问题 是: 你是谁, stop 设置为通义千问,仍然无法停止。

from openai import OpenAI

# 新版本 opnai
client = OpenAI(api_key="EMPTY", base_url="http://localhost:8081/v1")

stream = True
output = client.chat.completions.create(
    model="qwen",  # internlm chatglm3  qwen  llama3
    messages=[{"role": "user", "content": "你是谁"}],
    stream=stream,
    stop=["通义千问"],
)
if stream:
    for chunk in output:
        print(chunk.choices[0].delta.content or "", end="", flush=True)
else:
    print(output.choices[0].message.content)
print()

输出为: 我是通义千问,由阿里云开发的AI助手。我被设计用来回答各种问题、提供信息和进行对话。有什么我可以帮助你的吗?

@lvhan028
Copy link
Collaborator

如果stop_word encode之后,token的个数超过1,lmdeploy是不支持的。

@shell-nlp
Copy link
Author

如果stop_word encode之后,token的个数超过1,lmdeploy是不支持的。

能否进行后处理来解决这个问题,测试vllm的时候,是不存在这个问题的, 而且token的个数超过1 ,比较常见,很多时候,我们输入的stop_str 就是需要多个token才能表示

@lvhan028
Copy link
Collaborator

lvhan028 commented May 10, 2024

引擎里面不好整。这个需求我们短期内还没有计划,现在的功能需求已经排到6月底了。很抱歉

Copy link

This issue is marked as stale because it has been marked as invalid or awaiting response for 7 days without any further response. It will be closed in 5 days if the stale label is not removed or if there is no further response.

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

No branches or pull requests

3 participants