如何用Ollama调用工具
Ollama在2025年5月的官方文档中发布目前最新版本的软件可支持tools工具调用,而且列出了目前支持tools的模型。

Ollama也一直更新Tools Calling的功能,包括现在也支持MCP协议了。工具调用使得LLMs不仅能输出文本,而且能够调用Python等函数,从而能够执行编程语言能完成的任何操作。官方文档举了一个例子:
import ollama
# Define the python function
def add_two_numbers(a: int, b: int) -> int:
"""
Add two numbers
Args:
a (set): The first number as an int
b (set): The second number as an int
Returns:
int: The sum of the two numbers
"""
return a + b
from ollama import chat, ChatResponse
messages = [{'role': 'user', 'content': 'what is three minus one?'}]
response: ChatResponse = chat(
model='qwen3',
messages=messages,
tools=[add_two_numbers], # Python SDK supports passing tools as functions
stream=True
)
for chunk in response:
# Print model content
print(chunk.message.content, end='', flush=True)
# Print the tool call
if chunk.message.tool_calls:
print(chunk.message.tool_calls)
我们来分解下以上的步骤:
定义工具
In Ollama, a tool refers to an external function or capability that a language model can call dynamically during a conversation or task execution.
定义工具其实就是定义一个函数。函数def
之后会有一个docstring
,作用就是描述这个函数。Docstrings are typically enclosed in triple quotes (""" or ''') and can span multiple lines. They are accessible via the function's __doc__ attribute or tools like help(). 如下这个函数:
# Define the function with type hints and conversion
def add_numbers(a: int, b: int) -> int:
"""Adds two numbers and returns their sum.
Args:
a (int or float): The first number.
b (int or float): The second number.
Returns:
int or float: The sum of a and b.
Example:
>>> add_numbers(2, 3)
5
"""
return a + b
# Function mapping
functions_map = {
"add_numbers": add_numbers
}
docstring
的作用如下:
- Purpose: What the function does (e.g., "Adds two numbers and returns their sum").
- Parameters: The inputs, their types, and purpose (e.g., a and b as numbers).
- Return value: What the function returns and its type (e.g., sum as int or float).
- Examples (optional): Usage examples to clarify behavior.
- Additional info (optional): Exceptions, side effects, or notes.
A Python function'sdocstring
plays a critical role in defining how the language model (e.g., Llama 3.1) understands and interacts with the function as a tool. When a function is passed as a tool to Ollama’s chat API, the library parses thedocstring
to generate the tool’s JSON representation, which the model uses to decide when and how to call the function.
例如上面的函数,Ollama的API在调用函数的时候会根据docstring
的描述生成如下的JSON格式,LLMs只能理解这个JSON schema,而不会理解Python函数本身。
{
"function_name": "add_numbers",
"description": "Adds two numbers and returns their sum.",
"parameters": [
{
"name": "a",
"type": "int",
"description": "The first number."
},
{
"name": "b",
"type": "int",
"description": "The second number."
}
],
"returns": {
"type": "int",
"description": "The sum of a and b."
},
"example": {
"input": "add_numbers(2, 3)",
"output": 5
}
}
The model doesn’t “see” or “run” your Python code. It only sees the tool schema (a JSON-like description you provide) and makes decisions. Ollama Python library uses docstring parsing combined with type annotations (via Pydantic) to automatically generate the JSON schema that describes the tool’s interface—its parameters, their types, and descriptions. This JSON schema is essential for the model to understand how to call the tool correctly during interaction.
模型调用工具
定义工具之后,需要让大模型可根据用户输入的自然语言来决定选择哪一个工具。我们可以定义一个Agent Functions
:
def chat_with_ollama(message):
messages = [{"role": "system", "content": "You are a helpful AI assistant that can call tools when needed."}]
messages.append({"role": "user", "content": message})
response = ollama.chat(
model='qwen3:0.6b',
messages=messages,
tools=[add_numbers], # Python SDK supports passing tools as functions
stream=True
)
ollama.chat
是与本地模型交互的函数,函数返回的类型是ChatResponse
。The ChatResponse
object returned by ollama.chat()
includes a dictionary-like structure representing the chat message and any tool calls the model wants to invoke.
调用函数并得到response
之后,还需要解析LLMs的反馈。
def chat_with_ollama(message):
messages = [{"role": "system", "content": "You are a helpful AI assistant that can call tools when needed."}]
messages.append({"role": "user", "content": message})
response = ollama.chat(
model='qwen3:0.6b',
messages=messages,
tools=[add_numbers], # Python SDK supports passing tools as functions
stream=True
)
# 以下为response的解释过程
for chunk in response:
print(chunk['message']['content'], end='', flush=True)
if chunk['message'].tool_calls:
function_name = chunk['message'].tool_calls[0].function.name
args = chunk['message'].tool_calls[0].function.arguments
# Parse args if it's a JSON string
if isinstance(args, str):
try:
args = json.loads(args)
except json.JSONDecodeError:
print(f"Error: Invalid JSON in arguments: {args}")
continue
func = functions_map.get(function_name)
if func:
try:
result = func(**args) # Call function with parsed args
print("✅ Result:", result)
Ollama Python Library的指导请参考官方文档。最后可以测试下结果:
# Test the function
chat_with_ollama('add 3 and 5')