Ollama:Web搜索API和MCP

如果您曾经尝试过 LLM,您就会明白其中的痛点:模型​​在模式匹配方面非常出色,但往往会虚构一些东西。

如果你问起上周发生的事情,突然间,您得到的只是来自 2022 年的鬼故事。

这次更新改变了这一切。

基本上,Ollama 为他们的模型提供了一种访问互联网的途径。

它们无需产生幻觉,而是可以在回答之前提取真实的结果。

而且设置起来也很简单——你可以获得一个 REST API 以及一些简单的 Python 和 JavaScript 钩子函数。

此外,还有一个免费套餐,这总是一个不错的补充。

1、为什么这很酷

模型会冻结在时间中。

除非你输入数据,否则它们根本“不知道”昨天发生了什么。

所以,如果你想让你的机器人回答诸如“Ollama 引擎本月有什么新功能?”之类的问题,你要么花费数小时使用抓取的数据进行微调……要么干脆让它搜索网页。

现在有了这个 API:

  • 你可以获取最新信息。
  • 减少“嗯,实际上”的幻觉。
  • 构建一个小型的研究助手,在回复之前进行搜索。

感觉就像把谷歌融入了你的聊天机器人的大脑。

2、首次测试:经典的 cURL

我从简单的 cURL 开始。刚刚从我的 Ollama 账户中获取了一个 API 密钥并导出:

export OLLAMA_API_KEY="your_api_key"

然后运行:

curl https://ollama.com/api/web_search \
  --header "Authorization: Bearer $OLLAMA_API_KEY" \
  -d '{
    "query": "what is ollama?"
  }'

输出 — JSON 结果:

{
  "results": [
    {
      "title": "Ollama",
      "url": "https://ollama.com/",
      "content": "Cloud models are now available..."
    },
    {
      "title": "What is Ollama? Introduction to the AI model management tool",
      "url": "https://www.hostinger.com/tutorials/what-is-ollama",
      "content": "6min read..."
    }
  ]
}

对于一个命令来说还不错。

3、Python 版本

由于我大部分时间都用 Python,所以我切换到了 Python 库。安装方法如下:

pip install "ollama>=0.6.0"

然后:

import ollama

response = ollama.web_search("What is Ollama?")
print(response)

返回的结果基本上是一个简洁的结果列表:

results = [
  {
    "title": "Ollama",
    "url": "https://ollama.com/",
    "content": "Cloud models are now available..."
  },
  {
    "title": "Complete Ollama Guide",
    "url": "https://collabnix.com/complete-ollama-guide-installation-usage-code-examples",
    "content": "Join our Discord Server..."
  }
]

非常简洁——我将其集成到另一个总结内容的函数中。

4、JavaScript

如果你更喜欢 JS/Node,那么操作基本相同。

npm install "ollama@>=0.6.0"

然后:

import { Ollama } from "ollama";

const client = new Ollama();
const results = await client.webSearch({ query: "what is ollama?" });
console.log(JSON.stringify(results, null, 2));

同样,返回 JSON。很容易输入到代理甚至前端小部件中。

5、迷你搜索代理

接下来就是它的亮点了。Ollama 允许你将搜索插入到模型循环中,这样 LLM 就可以决定何时进行搜索。我用 Qwen 3 (4B) 测试了它。

以下是基本代码:

from ollama import chat, web_fetch, web_search

available_tools = {'web_search': web_search, 'web_fetch': web_fetch}

messages = [{'role': 'user', 'content': "what is ollama's new engine"}]

while True:
  response = chat(
    model='qwen3:4b',
    messages=messages,
    tools=[web_search, web_fetch],
    think=True
    )
  if response.message.thinking:
    print('Thinking: ', response.message.thinking)
  if response.message.content:
    print('Content: ', response.message.content)
  messages.append(response.message)
  if response.message.tool_calls:
    print('Tool calls: ', response.message.tool_calls)
    for tool_call in response.message.tool_calls:
      function_to_call = available_tools.get(tool_call.function.name)
      if function_to_call:
        args = tool_call.function.arguments
        result = function_to_call(**args)
        print('Result: ', str(result)[:200]+'...')
        # Result is truncated for limited context lengths
        messages.append({'role': 'tool', 'content': str(result)[:2000 * 4], 'tool_name': tool_call.function.name})
      else:
        messages.append({'role': 'tool', 'content': f'Tool {tool_call.function.name} not found', 'tool_name': tool_call.function.name})
  else:
    break

会发生什么这里:

  • 你提出一个问题。
  • 模型思考后会说:“嗯,我应该搜索一下。”
  • 访问网络,提取数据,然后据此做出回答。

感觉很像 ChatGPT 的浏览模式,只不过你可以控制每个部分。

6、抓取完整页面

有时你不只是想要片段,你想要的是整个页面。Ollama 为此添加了一个网页抓取功能。

在 Python中:

from ollama import web_fetch

page = web_fetch("https://ollama.com")
print(page)

在 JS 中:

const page = await client.webFetch({ url: "https://ollama.com" });
console.log(page);

您将获得标题、完整内容,甚至所有链接。非常适合摘要器或爬虫。

6、额外功能:MCP + Cline

如果您已经在使用 MCP 服务器(例如 Cline),Ollama 搜索功能将立即可用。请将以下内容添加到您的配置中:

管理 MCP 服务器 > 配置 MCP 服务器 > 添加以下配置:

{
  "mcpServers": {
    "web_search_and_fetch": {
      "type": "stdio",
      "command": "uv",
      "args": ["run", "path/to/web-search-mcp.py"],
      "env": { "OLLAMA_API_KEY": "your_api_key_here" }
    }
  }
}

现在,每个 MCP 客户端都可以访问实时网页搜索,非常方便。

您可以通过 Goose 的扩展程序连接 Ollama。


原文链接:Ollama's New Web Search API and MCP server: Smarter Agents with Real-Time Knowledge

汇智网翻译整理,转载请标明出处