Skip to content

OnnxRuntimeGenAIChatClientOptions support DeepSeek-r1-Distill-14b #1380

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

Open
williamlzw opened this issue Apr 7, 2025 · 3 comments
Open

Comments

@williamlzw
Copy link

I am not sure if the OnnxRuntimeGenAIChatClientOptions of the following deepseek model is correct. The value of message.Role.Value is fixed to "user". The user prompt tag of this deepseek model is < | User | >.
model:https://huggingface.co/onnxruntime/DeepSeek-R1-Distill-ONNX/tree/main/deepseek-r1-distill-qwen-14B/gpu/gpu-int4-rtn-block-32

public static OnnxRuntimeGenAIChatClientOptions GetPhi4() => new()
    {
        StopSequences = ["<|system|>", "<|user|>", "<|assistant|>", "<|end|>"],
        PromptFormatter = static (messages, _) =>
        {
            var prompt = new StringBuilder();
            foreach (var message in messages)
                foreach (var content in message.Contents.OfType<TextContent>())
                    prompt.Append($"<|{message.Role.Value}|>\n{content.Text}<|end|>\n");
            return prompt.Append("<|assistant|>\n").ToString();
        }
    };

    public static OnnxRuntimeGenAIChatClientOptions GetDeepSeek() => new()
    {
        StopSequences = ["<|end▁of▁sentence|>"],
        PromptFormatter = static (messages, _) =>
        {
            var prompt = new StringBuilder();
            foreach (var message in messages)
                foreach (var content in message.Contents.OfType<TextContent>())
                    prompt.Append($"<|begin▁of▁sentence|><|{message.Role.Value}|>\n{content.Text}<|end▁of▁sentence|>\n");
            return prompt.Append("<|Assistant|>\n").ToString();
        }
    };
@John0King
Copy link

@williamlzw I'm search the deepseek template for a while , and now I find that there is a chatTempalte in tokenizer_config.json,

and it's complex and this is the define (with indent)

{% if not add_generation_prompt is defined %}
{% set add_generation_prompt = false %}
{% endif %}
{% set ns = namespace(is_first=false, is_tool=false, is_output_first=true, system_prompt='') %}
{%- for message in messages %}
    {%- if message['role'] == 'system' %}
    {% set ns.system_prompt = message['content'] %}
    {%- endif %}
{%- endfor %}
{{bos_token}}
{{ns.system_prompt}}
{%- for message in messages %}
    {%- if message['role'] == 'user' %}
        {%- set ns.is_tool = false -%}
        {{'<|User|>' + message['content']}}
    {%- endif %}
    {%- if message['role'] == 'assistant' and message['content'] is none %}
        {%- set ns.is_tool = false -%}
        {%- for tool in message['tool_calls']%}
            {%- if not ns.is_first %}
                {{'<|Assistant|><|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\\n' + '```json' + '\\n' + tool['function']['arguments'] + '\\n' + '```' + '<|tool▁call▁end|>'}}
                {%- set ns.is_first = true -%}
            {%- else %}
                {{'\\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\\n' + '```json' + '\\n' + tool['function']['arguments'] + '\\n' + '```' + '<|tool▁call▁end|>'}}
                {{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}
            {%- endif %}
        {%- endfor %}
    {%- endif %}
    {%- if message['role'] == 'assistant' and message['content'] is not none %}
        {%- if ns.is_tool %}
            {{'<|tool▁outputs▁end|>' + message['content'] + '<|end▁of▁sentence|>'}}
            {%- set ns.is_tool = false -%}
        {%- else %}
            {% set content = message['content'] %}
            {% if '</think>' in content %}
                {% set content = content.split('</think>')[-1] %}
            {% endif %}
            {{'<|Assistant|>' + content + '<|end▁of▁sentence|>'}}
        {%- endif %}
    {%- endif %}
    {%- if message['role'] == 'tool' %}
        {%- set ns.is_tool = true -%}
        {%- if ns.is_output_first %}
        {{'<|tool▁outputs▁begin|><|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
        {%- set ns.is_output_first = false %}{%- else %}{{'\\n<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
        {%- endif %}
    {%- endif %}
{%- endfor -%}
{% if ns.is_tool %}
    {{'<|tool▁outputs▁end|>'}}
{% endif %}
{% if add_generation_prompt and not ns.is_tool %}
    {{'<|Assistant|>'}}
{% endif %}

@John0King
Copy link

this is ported version , I'm not sure about the tool part

PromptFormatter = static (chatMessages, context) =>
{
    string systemPrompt;
    bool add_generation_prompt = true, isFirst = false, isTool = false, isOutputFirst = true;

    systemPrompt = string.Join("\n", chatMessages.Where(x => x.Role == ChatRole.System).Select(x=>x.Text));

    var sb = new StringBuilder();
    sb.Append("<|begin▁of▁sentence|>");
    sb.Append(systemPrompt);
    foreach(var x in chatMessages)
    {
        if(x.Role == ChatRole.User)
        {
            isTool = false;
            sb.Append($"<|User|>{x.Text}");
        }
        if(x.Role == ChatRole.Assistant && string.IsNullOrWhiteSpace(x.Text))
        {
            isTool = false;
            foreach(var tool in x.Contents.OfType<FunctionCallContent>())
            {
                if(!isFirst)
                {
                    sb.Append($"<|Assistant|><|tool▁calls▁begin|><|tool▁call▁begin|>functionCall<|tool▁sep|>{tool.Name}\n```json\n{JsonSerializer.Serialize(tool.Arguments)}\n```<|tool▁call▁end|>");
                    isFirst = true;
                }
                else
                {
                    sb.Append($@"\n<|tool▁call▁begin|>functionCall<|tool▁sep|>{tool.Name}\n```json\n{JsonSerializer.Serialize(tool.Arguments)}\n```<|tool▁call▁end|><|tool▁calls▁end|><|end▁of▁sentence|>");
                }
            }
        }
        if(x.Role == ChatRole.Assistant && !string.IsNullOrWhiteSpace(x.Text))
        {
            if (isTool)
            {
                sb.Append($"<|tool▁outputs▁end|>{x.Text}<|end▁of▁sentence|>");
                isTool = false;
            }
            else
            {
                var content = string.Join("",x.Text.Split("</think>")[^1..]);
                sb.Append($"<|Assistant|>{content}<|end▁of▁sentence|>");

            }
        }
        if(x.Role == ChatRole.Tool)
        {
            var toolContent = x.Contents.OfType<FunctionResultContent>().FirstOrDefault();
            isTool = true;
            if (isOutputFirst)
            {
                sb.Append($"<|tool▁outputs▁begin|><|tool▁output▁begin|>{JsonSerializer.Serialize(toolContent?.Result)}<|tool▁output▁end|>");
                isOutputFirst = false;
            }
            else
            {
                sb.Append($"\n<|tool▁output▁begin|>{JsonSerializer.Serialize(toolContent?.Result)}<|tool▁output▁end|>");
            }
        }
    }
    if (isTool)
    {
        sb.Append("<|tool▁outputs▁end|>");
    }
    if(add_generation_prompt && !isTool)
    {
        sb.Append("<|Assistant|>");
    }
    return sb.ToString();
},

@williamlzw
Copy link
Author

Thanks.

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

No branches or pull requests

2 participants