Skip to content

Ollama 支持工具调用(也称为函数调用),允许模型调用工具并将结果整合到其回复中。

调用单个工具

调用单个工具并将其实际响应包含在后续请求中。

这也被称为“单次”(single-shot)工具调用。

cURL

并行工具调用

cURL

并行请求多个工具调用,然后将所有工具响应发送回模型。

shell
curl -s http://localhost:11434/api/chat -H "Content-Type: application/json" -d '{
  "model": "qwen3",
  "messages": [{"role": "user", "content": "What are the current weather conditions and temperature in New York and London?"}],
  "stream": false,
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_temperature",
        "description": "Get the current temperature for a city",
        "parameters": {
          "type": "object",
          "required": ["city"],
          "properties": {
            "city": {"type": "string", "description": "The name of the city"}
          }
        }
      }
    },
    {
      "type": "function",
      "function": {
        "name": "get_conditions",
        "description": "Get the current weather conditions for a city",
        "parameters": {
          "type": "object",
          "required": ["city"],
          "properties": {
            "city": {"type": "string", "description": "The name of the city"}
          }
        }
      }
    ]
  }'

使用包含多个工具结果的响应

shell
curl -s http://localhost:11434/api/chat -H "Content-Type: application/json" -d '{
  "model": "qwen3",
  "messages": [
    {"role": "user", "content": "What are the current weather conditions and temperature in New York and London?"},
    {
      "role": "assistant",
      "tool_calls": [
        {
          "type": "function",
          "function": {
            "index": 0,
            "name": "get_temperature",
            "arguments": {"city": "New York"}
          }
        },
        {
          "type": "function",
          "function": {
            "index": 1,
            "name": "get_conditions",
            "arguments": {"city": "New York"}
          }
        },
        {
          "type": "function",
          "function": {
            "index": 2,
            "name": "get_temperature",
            "arguments": {"city": "London"}
          }
        },
        {
          "type": "function",
          "function": {
            "index": 3,
            "name": "get_conditions",
            "arguments": {"city": "London"}
          }
        }
      ]
    },
    {"role": "tool", "tool_name": "get_temperature", "content": "22°C"},
    {"role": "tool", "tool_name": "get_conditions", "content": "Partly cloudy"},
    {"role": "tool", "tool_name": "get_temperature", "content": "15°C"},
    {"role": "tool", "tool_name": "get_conditions", "content": "Rainy"}
  ],
  "stream": false
}'

Python

python
from ollama import chat

def get_temperature(city: str) -> str:
  """Get the current temperature for a city

  Args:
    city: The name of the city

  Returns:
    The current temperature for the city
  """
  temperatures = {
    "New York": "22°C",
    "London": "15°C",
    "Tokyo": "18°C"
  }
  return temperatures.get(city, "Unknown")

def get_conditions(city: str) -> str:
  """Get the current weather conditions for a city

  Args:
    city: The name of the city

  Returns:
    The current weather conditions for the city
  """
  conditions = {
    "New York": "Partly cloudy",
    "London": "Rainy",
    "Tokyo": "Sunny"
  }
  return conditions.get(city, "Unknown")


messages = [{'role': 'user', 'content': 'What are the current weather conditions and temperature in New York and London?'}]

# Python 客户端会自动将函数解析为工具架构(schema),因此我们可以直接传递它们
# 也可以在 tools 列表中直接传递 Schema
response = chat(model='qwen3', messages=messages, tools=[get_temperature, get_conditions], think=True)

# 将助手消息添加到 messages 中
messages.append(response.message)
if response.message.tool_calls:
  # 处理每个工具调用
  for call in response.message.tool_calls:
    # 执行相应的工具
    if call.function.name == 'get_temperature':
      result = get_temperature(**call.function.arguments)
    elif call.function.name == 'get_conditions':
      result = get_conditions(**call.function.arguments)
    else:
      result = 'Unknown tool'
    # 将工具结果添加到 messages 中
    messages.append({'role': 'tool',  'tool_name': call.function.name, 'content': str(result)})

  # 生成最终响应
  final_response = chat(model='qwen3', messages=messages, tools=[get_temperature, get_conditions], think=True)
  print(final_response.message.content)

JavaScript

typescript
import ollama from 'ollama'

function getTemperature(city: string): string {
  const temperatures: { [key: string]: string } = {
    "New York": "22°C",
    "London": "15°C",
    "Tokyo": "18°C"
  }
  return temperatures[city] || "Unknown"
}

function getConditions(city: string): string {
  const conditions: { [key: string]: string } = {
    "New York": "Partly cloudy",
    "London": "Rainy",
    "Tokyo": "Sunny"
  }
  return conditions[city] || "Unknown"
}

const tools = [
  {
    type: 'function',
    function: {
      name: 'get_temperature',
      description: 'Get the current temperature for a city',
      parameters: {
        type: 'object',
        required: ['city'],
        properties: {
          city: { type: 'string', description: 'The name of the city' },
        },
      },
    },
  },
  {
    type: 'function',
    function: {
      name: 'get_conditions',
      description: 'Get the current weather conditions for a city',
      parameters: {
        type: 'object',
        required: ['city'],
        properties: {
          city: { type: 'string', description: 'The name of the city' },
        },
      },
    },
  }
]

const messages = [{ role: 'user', content: 'What are the current weather conditions and temperature in New York and London?' }]

const response = await ollama.chat({
  model: 'qwen3',
  messages,
  tools,
  think: true
})

// 将助手消息添加到 messages 中
messages.push(response.message)
if (response.message.tool_calls) {
  // 处理每个工具调用
  for (const call of response.message.tool_calls) {
    // 执行相应的工具
    let result: string
    if (call.function.name === 'get_temperature') {
      const args = call.function.arguments as { city: string }
      result = getTemperature(args.city)
    } else if (call.function.name === 'get_conditions') {
      const args = call.function.arguments as { city: string }
      result = getConditions(args.city)
    } else {
      result = 'Unknown tool'
    }
    // 将工具结果添加到 messages 中
    messages.push({ role: 'tool', tool_name: call.function.name, content: result })
  }

  // 生成最终响应
  const finalResponse = await ollama.chat({ model: 'qwen3', messages, tools, think: true })
  console.log(finalResponse.message.content)
}

多轮工具调用(Agent 循环)

Agent 循环允许模型决定何时调用工具,并将工具执行结果包含在回复中。

同时,告知模型它处于循环中并可以进行多次工具调用可能也会有所帮助。

Python

python
from ollama import chat, ChatResponse


def add(a: int, b: int) -> int:
  """相加两个数字"""
  """
  Args:
    a: 第一个数字
    b: 第二个数字

  Returns:
    两个数字的和
  """
  return a + b


def multiply(a: int, b: int) -> int:
  """相乘两个数字"""
  """
  Args:
    a: 第一个数字
    b: 第二个数字

  Returns:
    两个数字的乘积
  """
  return a * b


available_functions = {
  'add': add,
  'multiply': multiply,
}

messages = [{'role': 'user', 'content': 'What is (11434+12341)*412?'}]
while True:
    response: ChatResponse = chat(
        model='qwen3',
        messages=messages,
        tools=[add, multiply],
        think=True,
    )
    messages.append(response.message)
    print("Thinking: ", response.message.thinking)
    print("Content: ", response.message.content)
    if response.message.tool_calls:
        for tc in response.message.tool_calls:
            if tc.function.name in available_functions:
                print(f"Calling {tc.function.name} with arguments {tc.function.arguments}")
                result = available_functions[tc.function.name](./**tc.function.arguments)
                print(f"Result: {result}")
                # 将工具结果添加到消息中
                messages.append({'role': 'tool', 'tool_name': tc.function.name, 'content': str(result)})
    else:
        # 当没有更多工具调用时结束循环
        break
    # 使用更新后的消息继续循环

JavaScript

typescript
import ollama from 'ollama'

type ToolName = 'add' | 'multiply'

function add(a: number, b: number): number {
  return a + b
}

function multiply(a: number, b: number): number {
  return a * b
}

const availableFunctions: Record<ToolName, (a: number, b: number) => number> = {
  add,
  multiply,
}

const tools = [
  {
    type: 'function',
    function: {
      name: 'add',
      description: '相加两个数字',
      parameters: {
        type: 'object',
        required: ['a', 'b'],
        properties: {
          a: { type: 'integer', description: '第一个数字' },
          b: { type: 'integer', description: '第二个数字' },
        },
      },
    },
  },
  {
    type: 'function',
    function: {
      name: 'multiply',
      description: '相乘两个数字',
      parameters: {
        type: 'object',
        required: ['a', 'b'],
        properties: {
          a: { type: 'integer', description: '第一个数字' },
          b: { type: 'integer', description: '第二个数字' },
        },
      },
    },
  },
]

async function agentLoop() {
  const messages = [{ role: 'user', content: 'What is (11434+12341)*412?' }]

  while (true) {
    const response = await ollama.chat({
      model: 'qwen3',
      messages,
      tools,
      think: true,
    })

    messages.push(response.message)
    console.log('Thinking:', response.message.thinking)
    console.log('Content:', response.message.content)

    const toolCalls = response.message.tool_calls ?? []
    if (toolCalls.length) {
      for (const call of toolCalls) {
        const fn = availableFunctions[call.function.name as ToolName]
        if (!fn) {
          continue
        }

        const args = call.function.arguments as { a: number; b: number }
        console.log(`Calling ${call.function.name} with arguments`, args)
        const result = fn(args.a, args.b)
        console.log(`Result: ${result}`)
        messages.push({ role: 'tool', tool_name: call.function.name, content: String(result) })
      }
    } else {
      break
    }
  }
}

agentLoop().catch(console.error)

带流式传输的工具调用

在流式传输时,收集 thinkingcontenttool_calls 的每一个数据块(chunk),然后在后续请求中将这些字段与任何工具结果一起返回。

Python

python
from ollama import chat


def get_temperature(city: str) -> str:
"""获取城市的当前温度

Args:
  city: 城市名称

Returns:
  该城市的当前温度
"""
temperatures = {
  'New York': '22°C',
  'London': '15°C',
}
return temperatures.get(city, 'Unknown')


messages = [{'role': 'user', 'content': "What is the temperature in New York?"}]

while True:
stream = chat(
  model='qwen3',
  messages=messages,
  tools=[get_temperature],
  stream=True,
  think=True,
)

thinking = ''
content = ''
tool_calls = []

done_thinking = False
# 累积部分字段
for chunk in stream:
  if chunk.message.thinking:
    thinking += chunk.message.thinking
    print(chunk.message.thinking, end='', flush=True)
  if chunk.message.content:
    if not done_thinking:
      done_thinking = True
      print('\n')
    content += chunk.message.content
    print(chunk.message.content, end='', flush=True)
  if chunk.message.tool_calls:
    tool_calls.extend(chunk.message.tool_calls)
    print(chunk.message.tool_calls)

# 将累积的字段添加到消息中
if thinking or content or tool_calls:
  messages.append({'role': 'assistant', 'thinking': thinking, 'content': content, 'tool_calls': tool_calls})

if not tool_calls:
  break

for call in tool_calls:
  if call.function.name == 'get_temperature':
    result = get_temperature(**call.function.arguments)
  else:
    result = 'Unknown tool'
  messages.append({'role': 'tool', 'tool_name': call.function.name, 'content': result})

JavaScript

typescript


function getTemperature(city: string): string {
const temperatures: Record<string, string> = {
  'New York': '22°C',
  'London': '15°C',
}
return temperatures[city] ?? 'Unknown'
}

const getTemperatureTool = {
type: 'function',
function: {
  name: 'get_temperature',
  description: '获取城市的当前温度',
  parameters: {
    type: 'object',
    required: ['city'],
    properties: {
      city: { type: 'string', description: '城市名称' },
    },
  },
},
}

async function agentLoop() {
const messages = [{ role: 'user', content: "What is the temperature in New York?" }]

while (true) {
  const stream = await ollama.chat({
    model: 'qwen3',
    messages,
    tools: [getTemperatureTool],
    stream: true,
    think: true,
  })

  let thinking = ''
  let content = ''
  const toolCalls: any[] = []
  let doneThinking = false

  for await (const chunk of stream) {
    if (chunk.message.thinking) {
      thinking += chunk.message.thinking
      process.stdout.write(chunk.message.thinking)
    }
    if (chunk.message.content) {
      if (!doneThinking) {
        doneThinking = true
        process.stdout.write('\n')
      }
      content += chunk.message.content
      process.stdout.write(chunk.message.content)
    }
    if (chunk.message.tool_calls?.length) {
      toolCalls.push(...chunk.message.tool_calls)
      console.log(chunk.message.tool_calls)
    }
  }

  if (thinking || content || toolCalls.length) {
    messages.push({ role: 'assistant', thinking, content, tool_calls: toolCalls } as any)
  }

  if (!toolCalls.length) {
    break
  }

  for (const call of toolCalls) {
    if (call.function.name === 'get_temperature') {
      const args = call.function.arguments as { city: string }
      const result = getTemperature(args.city)
      messages.push({ role: 'tool', tool_name: call.function.name, content: result } )
    } else {
      messages.push({ role: 'tool', tool_name: call.function.name, content: 'Unknown tool' } )
    }
  }
}
}

agentLoop().catch(console.error)
  ```



此循环流式传输助手的响应,累积部分字段,将它们一起传回,并附加工具结果,以便模型完成其回答。


## 在 Ollama Python SDK 中将函数作为工具使用
Python SDK 会自动将函数解析为工具架构(schema),因此我们可以直接传递它们。
如果需要,仍可以传递 Schema。

```python
from ollama import chat

def get_temperature(city: str) -> str:
"""获取城市的当前温

Args:
  city: 城市名称

Returns:
  该城市的当前温度
"""
temperatures = {
  'New York': '22°C',
  'London': '15°C',
}
return temperatures.get(city, 'Unknown')

available_functions = {
'get_temperature': get_temperature,
}
# 直接将函数作为工具列表的一部分传递
response = chat(model='qwen3', messages=messages, tools=available_functions.values(), think=True)