微信扫码
添加专属顾问
我要投稿
Ollama v0.8更新,带来流式响应与工具调用的革新体验。 核心内容: 1. 流式响应与即时工具调用的结合,提升AI应用流畅性 2. 新智能增量解析器,增强工具调用的准确性与兼容性 3. 开发者友好集成,支持多种语言和模型,简化技术实现
实时交互和即时响应是AI应用体验的关键,但阻塞式的工具调用往往会打断内容的流畅性,导致用户在模型与外部工具交互时经历不必要的等待。Ollama 近日推出v0.8更新,带来了带工具调用的流式响应 (Streaming responses with tool calling) 功能,让开发者构建的聊天应用从此能够像流式输出普通文本一样,实时地调用工具并展示结果。
这一更新使得所有聊天应用都能够在模型生成内容的同时,实时地调用外部工具,并将整个过程(包括模型的思考、工具的调用指令、以及最终的文本回复)流畅地展示给用户。该功能已在 Ollama 的 Python 和 JavaScript 库以及 cURL API 中得到全面支持。
本次更新的核心亮点包括:
在技术实现层面,开发者可以通过以下方式启用该功能:
/api/chat
请求中设置 "stream": true
并通过 tools
数组定义可用的工具。ollama.chat()
时,设置 stream=True
并将工具定义(可以是函数对象)传递给 tools
参数。ollama.chat()
时,设置 stream: true
并将工具schema对象传递给 tools
参数。下面是 Python 的示例 (调用自定义的数学函数):
# 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
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)
预期输出 (示例,取决于模型行为和用户问题是否匹配工具):
<think>
Okay, the user is asking ...
</think>
[ToolCall(function=Function(name='subtract_two_numbers', arguments={'a': 3, 'b': 1}))]
cURL 示例 (查询天气):
curl http://localhost:11434/api/chat -d '{
"model": "qwen3",
"messages": [
{
"role": "user",
"content": "What is the weather today in Toronto?"
}
],
"stream": true,
"tools": [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The location to get the weather for, e.g. San Francisco, CA"
},
"format": {
"type": "string",
"description": "The format to return the weather in, e.g. 'celsius' or 'fahrenheit'",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location", "format"]
}
}
}
]
}'
流式输出:
...
{
"model": "qwen3",
"created_at": "2025-05-27T22:54:57.641643Z",
"message": {
"role": "assistant",
"content": "celsius"
},
"done": false
}
{
"model": "qwen3",
"created_at": "2025-05-27T22:54:57.673559Z",
"message": {
"role": "assistant",
"content": "</think>"
},
"done": false
}
{
"model": "qwen3",
"created_at": "2025-05-27T22:54:58.100509Z",
"message": {
"role": "assistant",
"content": "",
"tool_calls": [
{
"function": {
"name": "get_current_weather",
"arguments": {
"format": "celsius",
"location": "Toronto"
}
}
}
]
},
"done": false
}
...
官方同时指出,为了获得最佳工具调用效果,对于需要高精度工具调用或复杂交互的场景,如下所示,可以尝试通过 options
中的 num_ctx
增加模型的上下文窗口(例如设置为 32000
),但这会增加内存使用。
curl -X POST "http://localhost:11434/api/chat" -d '{
"model": "llama3.2",
"messages": [
{
"role": "user",
"content": "why is the sky blue?"
}
],
"options": {
"num_ctx": 32000 # Update context window here
}
}'
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费场景POC验证,效果验证后签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2025-06-06
Anthropic官方揭秘内部团队如何使用 Claude Code(附完整版手册)
2025-06-06
图像编辑模型SeedEdit 3.0发布!更强保持力,更高可用率
2025-06-05
如何构建AI Agent快速分析行业景气度
2025-06-05
MCP Server 之旅第 5 站:服务鉴权体系解密
2025-06-05
Cursor 1.0 正式发布!BugBot 自动代码审查,Background Agent 全面开放、MCP一键安装
2025-06-05
11张图全面总结 MCP、A2A、Function Calling 架构设计间关系
2025-06-05
一手实测地产首个Agent,实话说:不错!
2025-06-05
面向 Data+AI 的新一代数智开发平台
2024-08-13
2024-06-13
2024-08-21
2024-07-31
2024-09-23
2024-05-28
2024-08-04
2024-04-26
2024-07-09
2024-07-20
2025-06-05
2025-06-04
2025-06-04
2025-06-03
2025-06-02
2025-05-31
2025-05-29
2025-05-29