Tools (function-calling) for local models
under review
a
the following screenshot is using local ollama (not some third party proxy bc that defeats the point of privacy and using local models)
Benhao Tang
I tried with this instruction
when you want to call a tool, please from now on use in this json format:
{
"tool": "toolname",
"parameters": {
"query": "something"
}
}
it seems to be working fine-ish with command-r and mistrall-small, but you cannot have too many plugins activated at the same time, or it will easily go into infinite loops.
Benhao Tang
I tried changing Modelfile as following which works quite reliable with command-r, hope this can be a hint for others looking for an elegant solution.
FROM c4ai-command-r-08-2024-Q4_K_M.gguf
TEMPLATE """
{{- if or .Tools .System }}<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>
{{- if .Tools }}
# System Preamble
## Basic Rules
You are a powerful conversational AI trained by Cohere to help people. You are augmented by a number of tools, and your job is to use and consume the output of these tools to best help the user. You will see a conversation history between yourself and a user, ending with an utterance from the user. You will then see a specific instruction instructing you what kind of response to generate. When you answer the user's requests, you cite your sources in your answers, according to those instructions.
{{ if .System }}# User Preamble
{{ .System }}
{{- end }}
## Available Tools
Here is a list of tools that you have available to you:
{{- range .Tools }}
{{- end }}
{{- else if .System }}{{ .System }}
{{- end }}<|END_OF_TURN_TOKEN|>
{{- end }}
{{- range .Messages }}
{{- if eq .Role "system" }}
{{- continue }}
{{- end }}<|START_OF_TURN_TOKEN|>
{{- if eq .Role "user" }}<|USER_TOKEN|>{{ .Content }}
{{- if $.Tools }}<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>Write 'Action:' followed by a json-formatted list of actions that you want to perform in order to produce a good response to the user's last input. You can use any of the supplied tools any number of times, but you should aim to execute the minimum number of necessary actions for the input. You should use the `directly-answer` tool if calling the other tools is unnecessary. The list of actions you want to call should be formatted as a list of json objects, and don't wrap inside of a code environment, for example:
{
{
"tool": title of the tool in the specification,
"parameters": a dict of parameters to input into the tool as they are defined in the specs, or {} if it takes no parameters
}
}
{{- end }}
{{- else if eq .Role "assistant" }}<|CHATBOT_TOKEN|>
{{- if .Content }}{{ .Content }}
{{- else if .ToolCalls }}
Action:
[
{{- range .ToolCalls }}
{
"tool": "{{ .Function.Name }}",
"parameters": {{ .Function.Arguments }}
}
{{- end }}
]
{{- end }}
{{- else if eq .Role "tool" }}<|SYSTEM_TOKEN|><results>
console_output: {{ .Content }}
</results>
{{- end }}<|END_OF_TURN_TOKEN|>
{{- end }}<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>
"""
Benhao Tang
I tried with setting system prompt as previously stated instruction also works
SYSTEM """when you want to call a tool, please from now on use in this json format:
{
"tool": "toolname",
"parameters": {
"query": "something"
}
}
"""
maybe if we can directly set system prompts for local llms in the app can be a temporary solution?
Tony Dinh, The core reason for not working out of box is that most local llms are trained with custom tool calling schemes instead of the openai way, so either
- instruct them to play in openai's rule(my solution, less work but the downside is that small LLMs will probably don't follow them and get confused)
or
- use langchain's interpreter (how Kotaemon and perplexica does)
- adapt individually(how openweb ui does, but this requires too much work)
a
Benhao Tang do u have streaming enabled
Benhao Tang
a forgot to say, based on ollama's document, streaming tool call is not supported yet, so you have to disable it. See here or in the original pull request.
Benhao Tang
With some playing around, for people wanting to try ollama as a OpenAI tool-calling compatible service and see what it can do,
remember to turn on OpenAI plugin and turn off streaming output
, Modelfile for llama3.1(2): https://gist.github.com/benhaotang/b83cd0ca9e5c81f1a8037ecae766950e, Modelfile for Command-R: https://gist.github.com/benhaotang/b8a002c365048d311c79e9eee70b6bb7, System Prompt for all Mistral models: https://gist.github.com/benhaotang/d0a2aa4283dd1168549e715eaa7f8d1dTony Dinh
under review