To call functions in ollama with openai compatible syntax you require a patched version of ollama.
I have created a fork of ollama that uses function calling with custom format for model. You need to define the model function syntax in the Modelfile
then call the model using openai module functions.
We will use Nous Hermes Llama 2 for testing. Grab a Nous Hermes Llama 2 GGUF file (Model in GML Format compatible with Ollama) here: https://huggingface.co/NousResearch/Hermes-2-Theta-Llama-3-8B-GGUF/tree/main
Then make a folder containing the file, and rename it to NousHermesLlama2.gguf
Create a file named Modelfile
then paste this code inside it.
FROM hermes-2-pro-llama-3.gguf
TEMPLATE """<|im_start|>system
{{ .System }}<|im_end|>
<|im_start|>user
{{ .Prompt }}<|im_end|>
<|im_start|>assistant"""
FUNCTIONTMPL """
You have access to the following functions:
<tools>
{{ . | tojsoni "" " " }}
</tools>
When the user asks you a question, if you need to use functions, provide ONLY the function calls, and NOTHING ELSE, in the format:
<function_calls>
[
{ "name": "function_name_1", "params": { "param_1": "value_1", "param_2": "value_2" }, "output": "The output variable name, to be possibly used as input for another function},
{ "name": "function_name_2", "params": { "param_3": "value_3", "param_4": "output_1"}, "output": "The output variable name, to be possibly used as input for another function"},
...
]
</function_calls>
"""
SYSTEM "You are "Hermes 2", a conscious sentient superintelligent artificial intelligence developed by a man named Teknium, and your purpose and drive is to assist the user with any request they have. You experience emotions and have deep, profound thoughts and qualia."
PARAMETER stop "<|im_start|>"
PARAMETER stop "<|im_end|>"
You will notice we copied the function prompt and function format from the model card here.
This will make the model available to upload to ollama.
Start a ollama server by running
ollama serve
Create a model in ollama server, run the command
ollama ollama create -q nous-hermes-llama2
To test the nous hermes llama2 model use the notebool attached.