Tool / Function Calling

#122
by smcleod - opened

Is there a working template for tool / function calling with DeepSeek R1?

I've tried a number of templates with Ollama, llama.cpp etc... but haven't been able to get something reliable working.

{{- /* Initial system message with core instructions */ -}}
{{- if .Messages }}
{{- if or .System .Tools }}
<|im_start|>system
{{- if .System }}
{{ .System }}
{{- end }}

{{- if .Tools }}
# Tools and XML Schema
You have access to the following tools. Each tool must be used according to this XML schema:

<tools>
{{- range .Tools }}
{{ .Function }}
{{- end }}
</tools>

## Tool Use Format
1. Think about the approach in <thinking> tags
2. Call tool using XML format, for example:

<tool_name>
<parameter1_name>value1</parameter1_name>
<parameter2_name>value2</parameter2_name>
</tool_name>

3. Process tool response, for example:

<tool_response>result</tool_response>

Examples:

<attempt_completion>
<result>
I have completed the task...
</result>
</attempt_completion>

Always adhere to this format for all tool uses to ensure proper parsing and execution.

{{- end }}
<|im_end|>
{{- end }}

{{- /* Message handling loop */ -}}
{{- range $i, $_ := .Messages }}
{{- $last := eq (len (slice $.Messages $i)) 1 }}

{{- /* User messages */ -}}
{{- if eq .Role "user" }}
<|im_start|>user
{{ .Content }}
<|im_end|>

{{- /* Assistant messages */ -}}
{{- else if eq .Role "assistant" }}
<|im_start|>assistant
{{- if .Content }}
{{ .Content }}
{{- else if .ToolCalls }}
{{- range .ToolCalls }}
<thinking>
[Analysis of current state and next steps]
</thinking>

<{{ .Function.Name }}>
{{- range $key, $value := .Function.Arguments }}
<{{ $key }}>{{ $value }}</{{ $key }}>
{{- end }}
</{{ .Function.Name }}>
{{- end }}
{{- end }}
{{- if not $last }}<|im_end|>{{- end }}

{{- /* Tool response handling */ -}}
{{- else if eq .Role "tool" }}
<|im_start|>user
<tool_response>
{{ .Content }}
</tool_response>
<|im_end|>
{{- end }}

{{- /* Prepare for next assistant response if needed */ -}}
{{- if and (ne .Role "assistant") $last }}
<|im_start|>assistant
{{- end }}
{{- end }}

{{- /* Handle single message case */ -}}
{{- else }}
{{- if .System }}
<|im_start|>system
{{ .System }}
<|im_end|>
{{- end }}

{{- if .Prompt }}
<|im_start|>user
{{ .Prompt }}
<|im_end|>
{{- end }}

<|im_start|>assistant
{{- end }}
{{ .Response }}
{{- if .Response }}<|im_end|>{{- end }}

Deepseek R1 doesnt support tool/function calling

https://github.com/deepseek-ai/DeepSeek-R1/issues/9

Deepseek R1 doesnt support tool/function calling

https://github.com/deepseek-ai/DeepSeek-R1/issues/9

It seems to work for me? https://x.com/zackangelo/status/1887543668782850287

Deepseek R1 doesnt support tool/function calling

https://github.com/deepseek-ai/DeepSeek-R1/issues/9

But there is tools relevant script in chat template: https://hf.co/deepseek-ai/DeepSeek-R1/blob/main/tokenizer_config.json. I think it supports tools using. In fact, they just updated this template two days ago, appended "" at the end. I guess this is the missing part which causes function call not working. I think it should have been fixed now.

Sign up or log in to comment