Features
When using this Ollama client class, messages are tailored to accommodate the specific requirements of Ollama’s API and this includes message role sequences, support for function/tool calling, and token usage.Installing Ollama
For Mac and Windows, download Ollama. For Linux:Downloading models for Ollama
Ollama has a library of models to choose from, see them here. Before you can use a model, you need to download it (using the name of the model from the library):Getting started with AG2 and Ollama
When installing AG2, you need to install the AG2 package with the Ollama extra.If you have been using oras
autogen
or ag2
, all you need to do is upgrade it using:autogen
and ag2
are aliases for the same PyPI package.OAI_CONFIG_LIST
below showing how the Ollama client class is used by specifying the api_type
as ollama
.
client_host
key in your config as per the below example:
API parameters
The following Ollama parameters can be added to your config. See this link for further information on them.- num_predict (integer): -1 is infinite, -2 is fill context, 128 is default
- num_ctx (integer)
- repeat_penalty (float)
- seed (integer)
- stream (boolean)
- temperature (float)
- top_k (int)
- top_p (float)
Two-Agent Coding Example
In this example, we run a two-agent chat with an AssistantAgent (primarily a coding agent) to generate code to count the number of prime numbers between 1 and 10,000 and then it will be executed. We’ll use Meta’s Llama 3.1 model which is suitable for coding. In this example we will specify the URL for the Ollama installation usingclient_host
.
Tool Calling - Native vs Manual
Ollama supports native tool calling (Ollama v0.3.1 library onward). If you install AG2 withpip install ag2[ollama]
you will be able to use native tool calling.
The parameter native_tool_calls
in your configuration allows you to specify if you want to use Ollama’s native tool calling (default) or manual tool calling.
manual_tool_call_instruction
manual_tool_call_step1
manual_tool_call_step2
native_tool_calls
to False
.
Reducing repetitive tool calls
By incorporating tools into a conversation, LLMs can often continually recommend them to be called, even after they’ve been called and a result returned. This can lead to a never-ending cycle of tool calls. To remove the chance of an LLM recommending a tool call, an additional parameter calledhide_tools
can be used to specify when tools are hidden from the LLM. The string values for the parameter are:
- ‘never’: tools are never hidden
- ‘if_all_run’: tools are hidden if all tools have been called
- ‘if_any_run’: tools are hidden if any tool has been called
Tool Call Example
In this example, instead of writing code, we will have an agent assist with some trip planning using multiple tool calling. Again, we’ll use Meta’s versatile Llama 3.1. Native Ollama tool calling will be used and we’ll utilise thehide_tools
parameter to hide the tools once all have been called.