PythonでOllama Web Search APIを使用する

PythonとOllamaを使ってAI検索エージェントを構築する

目次

OllamaのPythonライブラリは、今やOLlama web searchのネイティブな機能を含んでいます。わずか数行のコードで、ローカルのLLMをインターネット上のリアルタイム情報を補完し、幻覚を減らし、正確性を向上させることができます。

digital space

セットアップの開始

Ollama Pythonライブラリをウェブ検索用にインストールするにはどうすればよいですか? pip install 'ollama>=0.6.0'を使用して0.6.0以上のバージョンをインストールしてください。このバージョンにはweb_searchweb_fetchの関数が含まれています。

pip install 'ollama>=0.6.0'

Python環境とパッケージを管理するには、uv、高速なPythonパッケージマネージャー、またはvenvを使用して仮想環境を設定し、依存関係を分離してください。

OllamaアカウントからAPIキーを作成し、環境変数に設定してください:

export OLLAMA_API_KEY="your_api_key"

Windows PowerShellでは:

$env:OLLAMA_API_KEY = "your_api_key"

基本的なウェブ検索

Ollamaを使用してウェブを検索する最も簡単な方法:

import ollama

# シンプルなウェブ検索
response = ollama.web_search("What is Ollama?")
print(response)

出力:

results = [
    {
        "title": "Ollama",
        "url": "https://ollama.com/",
        "content": "Cloud models are now available in Ollama..."
    },
    {
        "title": "What is Ollama? Features, Pricing, and Use Cases",
        "url": "https://www.walturn.com/insights/what-is-ollama",
        "content": "Our services..."
    },
    {
        "title": "Complete Ollama Guide: Installation, Usage & Code Examples",
        "url": "https://collabnix.com/complete-ollama-guide",
        "content": "Join our Discord Server..."
    }
]

結果数の制御

import ollama

# より多くの結果を取得
response = ollama.web_search("latest AI news", max_results=10)

for result in response.results:
    print(f"📌 {result.title}")
    print(f"   {result.url}")
    print(f"   {result.content[:100]}...")
    print()

フルページコンテンツの取得

Ollama Pythonでのweb_searchとweb_fetchの違いは? web_searchはインターネットをクエリし、タイトル、URL、スニペットを含む複数の検索結果を返します。web_fetchは特定のURLのフルコンテンツを取得し、ページのタイトル、マーカードンコンテンツ、リンクを返します。web_fetchが返すマーカードンコンテンツは、さらなる処理に最適です—HTMLをマーカードンに変換する必要がある場合は、PythonでHTMLをマーカードンに変換するに関するガイドをご覧ください。

from ollama import web_fetch

result = web_fetch('https://ollama.com')
print(result)

出力:

WebFetchResponse(
    title='Ollama',
    content='[Cloud models](https://ollama.com/blog/cloud-models) are now available in Ollama\n\n**Chat & build with open models**\n\n[Download](https://ollama.com/download) [Explore models](https://ollama.com/models)\n\nAvailable for macOS, Windows, and Linux',
    links=['https://ollama.com/', 'https://ollama.com/models', 'https://github.com/ollama/ollama']
)

検索と取得の組み合わせ

よく使われるパターンは、まず検索し、その後関連する結果からフルコンテンツを取得することです:

from ollama import web_search, web_fetch

# 情報を検索
search_results = web_search("Ollama new features 2025")

# 最初の結果からフルコンテンツを取得
if search_results.results:
    first_url = search_results.results[0].url
    full_content = web_fetch(first_url)
    
    print(f"Title: {full_content.title}")
    print(f"Content: {full_content.content[:500]}...")
    print(f"Links found: {len(full_content.links)}")

検索エージェントの構築

Ollama検索エージェントに最適なPythonモデルは? ツール使用能力が強いモデルが最適です。qwen3gpt-oss、およびクラウドモデルのqwen3:480b-clouddeepseek-v3.1-cloudなどが該当します。これらのモデルから構造化された出力を必要とする高度なユースケースについては、OllamaとQwen3を使用したLLMの構造化出力に関するガイドをご覧ください。

まず、有効なモデルをプルします:

ollama pull qwen3: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[:200], '...')
    
    if response.message.content:
        print('💬 Response:', 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], '...')
                # コンテキスト長さの制限に合わせて結果を切り詰める
                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

Pythonで大規模なウェブ検索結果を処理するにはどうすればよいですか? コンテキストの制限に合わせて結果を切り詰めてください。推奨されるアプローチは、結果文字列を約8000文字(2000トークン × 4文字)に切り詰めてモデルに渡すことです。

エラーハンドリング付きの高度な検索エージェント

以下はエラーハンドリング付きの拡張バージョンです:

from ollama import chat, web_fetch, web_search
import json

class SearchAgent:
    def __init__(self, model: str = 'qwen3:4b'):
        self.model = model
        self.tools = {'web_search': web_search, 'web_fetch': web_fetch}
        self.messages = []
        self.max_iterations = 10
        
    def query(self, question: str) -> str:
        self.messages = [{'role': 'user', 'content': question}]
        
        for iteration in range(self.max_iterations):
            try:
                response = chat(
                    model=self.model,
                    messages=self.messages,
                    tools=[web_search, web_fetch],
                    think=True
                )
            except Exception as e:
                return f"Error during chat: {e}"
            
            self.messages.append(response.message)
            
            # ツールコールがない場合、最終的な答えがある
            if not response.message.tool_calls:
                return response.message.content or "No response generated"
            
            # ツールコールを実行
            for tool_call in response.message.tool_calls:
                result = self._execute_tool(tool_call)
                self.messages.append({
                    'role': 'tool',
                    'content': result,
                    'tool_name': tool_call.function.name
                })
        
        return "Max iterations reached without final answer"
    
    def _execute_tool(self, tool_call) -> str:
        func_name = tool_call.function.name
        args = tool_call.function.arguments
        
        if func_name not in self.tools:
            return f"Unknown tool: {func_name}"
        
        try:
            result = self.tools[func_name](**args)
            # コンテキスト制限に合わせて切り詰める
            result_str = str(result)
            if len(result_str) > 8000:
                result_str = result_str[:8000] + "... [truncated]"
            return result_str
        except Exception as e:
            return f"Tool error: {e}"

# 使用例
agent = SearchAgent(model='qwen3:4b')
answer = agent.query("What are the latest features in Ollama?")
print(answer)

非同期ウェブ検索

Ollama Pythonのウェブ検索を非同期コードで使用できますか? はい、Ollama Pythonライブラリは非同期操作をサポートしています。AsyncClientを使用して、非同期アプリケーションでウェブ検索や取得を非ブロッキングで実行してください。サーバーレス環境でのPythonと他の言語のパフォーマンス比較については、AWS Lambdaのパフォーマンス: JavaScript vs Python vs Golangに関する分析をご覧ください。

import asyncio
from ollama import AsyncClient

async def async_search():
    client = AsyncClient()
    
    # 複数の検索を同時に実行
    tasks = [
        client.web_search("Ollama features"),
        client.web_search("local LLM tools"),
        client.web_search("AI search agents"),
    ]
    
    results = await asyncio.gather(*tasks)
    
    for i, result in enumerate(results):
        print(f"Search {i + 1}:")
        for r in result.results[:2]:
            print(f"  - {r.title}")
        print()

# 非同期検索を実行
asyncio.run(async_search())

非同期検索エージェント

import asyncio
from ollama import AsyncClient

async def async_research_agent(question: str):
    client = AsyncClient()
    messages = [{'role': 'user', 'content': question}]
    
    while True:
        response = await client.chat(
            model='qwen3:4b',
            messages=messages,
            tools=[client.web_search, client.web_fetch],
        )
        
        messages.append(response.message)
        
        if not response.message.tool_calls:
            return response.message.content
        
        # ツールコールを非同期に実行
        tool_tasks = []
        for tool_call in response.message.tool_calls:
            if tool_call.function.name == 'web_search':
                task = client.web_search(**tool_call.function.arguments)
            elif tool_call.function.name == 'web_fetch':
                task = client.web_fetch(**tool_call.function.arguments)
            else:
                continue
            tool_tasks.append((tool_call.function.name, task))
        
        # 結果を取得
        for tool_name, task in tool_tasks:
            result = await task
            messages.append({
                'role': 'tool',
                'content': str(result)[:8000],
                'tool_name': tool_name
            })

# 実行
answer = asyncio.run(async_research_agent("What's new in Python 3.13?"))
print(answer)

コンテキスト長さとパフォーマンス

Python検索エージェントのコンテキスト長さをどのように設定すればよいですか? 約32000トークンのコンテキスト長さを設定してください。検索エージェントは、web_searchweb_fetchが数千トークンを返すため、フルコンテキスト長さで動作するのが最適です。

from ollama import chat, web_search

# 検索中心のタスクにはより高いコンテキストを設定
response = chat(
    model='qwen3:4b',
    messages=[{'role': 'user', 'content': 'Research the latest AI developments'}],
    tools=[web_search],
    options={
        'num_ctx': 32768,  # 32Kコンテキスト
    }
)

MCPサーバー統合

Ollamaは、ウェブ検索を任意のMCPクライアントで使用できるPythonのMCPサーバーを提供しています。MCPサーバーの構築、ウェブ検索およびスクレイピング機能を備えたPythonでのMCPサーバーの詳細なガイドについては、PythonでのMCPサーバーの構築に関する詳細なチュートリアルをご覧ください。

Cline統合

Cline設定でMCPサーバーを構成してください:

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" }
    }
  }
}

Codex統合

~/.codex/config.tomlに追加してください:

[mcp_servers.web_search]
command = "uv"
args = ["run", "path/to/web-search-mcp.py"]
env = { "OLLAMA_API_KEY" = "your_api_key_here" }

自分自身のMCPサーバーの作成

#!/usr/bin/env python3
"""Ollamaウェブ検索用のシンプルなMCPサーバー。"""

import os
from mcp.server import Server
from mcp.types import Tool, TextContent
from ollama import web_search, web_fetch

app = Server("ollama-web-search")

@app.tool()
async def search_web(query: str, max_results: int = 5) -> str:
    """ウェブで情報を検索します。"""
    results = web_search(query, max_results=max_results)
    
    output = []
    for r in results.results:
        output.append(f"**{r.title}**\n{r.url}\n{r.content}\n")
    
    return "\n---\n".join(output)

@app.tool()
async def fetch_page(url: str) -> str:
    """ウェブページのフルコンテンツを取得します。"""
    result = web_fetch(url)
    return f"# {result.title}\n\n{result.content}"

if __name__ == "__main__":
    app.run()

実用的な例

これらの例は、Ollamaのウェブ検索APIの現実的な応用を示しています。これらを拡張して、より複雑なシステムを構築できます—例えば、検索結果をPythonでのPDF生成と組み合わせて研究レポートを作成するなどです。

ニュース要約ツール

from ollama import chat, web_search

def summarize_news(topic: str) -> str:
    # 最新のニュースを検索
    results = web_search(f"{topic} latest news", max_results=5)
    
    # モデルに渡す検索結果のフォーマット
    news_content = "\n\n".join([
        f"**{r.title}**\n{r.content}"
        for r in results.results
    ])
    
    # 要約を生成
    response = chat(
        model='qwen3:4b',
        messages=[{
            'role': 'user',
            'content': f"Summarize these news items about {topic}:\n\n{news_content}"
        }]
    )
    
    return response.message.content

summary = summarize_news("artificial intelligence")
print(summary)

研究アシスタント

from ollama import chat, web_search, web_fetch
from dataclasses import dataclass

@dataclass
class ResearchResult:
    question: str
    sources: list
    answer: str

def research(question: str) -> ResearchResult:
    # 関連情報を検索
    search_results = web_search(question, max_results=3)
    
    # 上位のソースからフルコンテンツを取得
    sources = []
    full_content = []
    
    for result in search_results.results[:3]:
        try:
            page = web_fetch(result.url)
            sources.append(result.url)
            full_content.append(f"Source: {result.url}\n{page.content[:2000]}")
        except:
            continue
    
    # 総合的な答えを生成
    context = "\n\n---\n\n".join(full_content)
    
    response = chat(
        model='qwen3:4b',
        messages=[{
            'role': 'user',
            'content': f"""以下のソースに基づいて、この質問に答えてください: {question}

Sources:
{context}

詳細な答えを提供し、ソースへの引用を含めてください。"""
        }]
    )
    
    return ResearchResult(
        question=question,
        sources=sources,
        answer=response.message.content
    )

# 使用例
result = research("How does Ollama's new model scheduling work?")
print(f"Question: {result.question}")
print(f"Sources: {result.sources}")
print(f"Answer: {result.answer}")

推奨モデル

モデル パラメータ 最適な用途
qwen3:4b 4B すばやいローカル検索
qwen3 8B 一般的な目的のエージェント
gpt-oss 多様 研究タスク
qwen3:480b-cloud 480B 複雑な推論(クラウド)
gpt-oss:120b-cloud 120B 長文研究(クラウド)
deepseek-v3.1-cloud - 高度な分析(クラウド)

ベストプラクティス

  1. 結果の切り詰め: コンテキストの制限に合わせてウェブ結果を常に切り詰めてください(~8000文字)
  2. エラーハンドリング: ネットワーク失敗に備えてツールコールをtry/exceptでラップしてください
  3. レート制限: Ollamaのウェブ検索APIレート制限を尊重してください
  4. コンテキスト長さ: 検索エージェントには~32000トークンを使用してください
  5. スケーラビリティ: 非同期操作にAsyncClientを使用してください
  6. テスト: ユニットテストを検索エージェントに記述して信頼性を確保してください
  7. Pythonの基本: Pythonチートシートを手元に置いて、構文や一般的なパターンを素早く参照してください

有用なリンク