Grounding With Web Search

Give local models live information with a web search tool, then ground answers in the results.

TL;DR

  1. Give the model a search_web tool to fetch facts.
  2. Inject live results into the prompt, then call /api/chat.
  3. Or use Ollama's built-in web_search API after ollama signin.

The Grounding Loop

    Expose search_web

    Offer a web search function as a tool.

    # tools: [ search_web ]
    Model Requests It

    The model asks to search when unsure.

    # tool_calls -> search_web(query)
    Run The Search

    Your app calls a real search API.

    # results = search_web("...")
    Inject Results

    Add snippets back as a tool message.

    { "role": "tool", "content": snippets }
    ollama signin

    Authenticate to enable hosted web search.

    ollama signin
    web_search

    Query the hosted search API directly.

    ollama.web_search(query="latest news")
    web_fetch

    Fetch a page's content by URL.

    ollama.web_fetch(url="https://...")

DIY Search Tool

    Define The Tool

    Wrap any search API as a function.

    def search_web(query: str) -> str: ...
    Bind It

    Attach the tool to a chat model.

    llm.bind_tools([search_web])
    Return Snippets

    Feed the results back for the answer.

    # Append results, then call again

Keep It Grounded

    Cite Sources

    Ask the model to use retrieved text only.

    "prompt": "Answer using only the results"
    Trim Noise

    Pass only the top relevant snippets.

    # Keep the best 3 results, not all
    Fresh Over Memory

    Prefer live data over model recall.

    # Instruct: prefer search over memory

Tips

  1. Treat web search as a tool the model calls with bind_tools or a tools array, then feed the results back for grounding.
  2. Insert only the most relevant snippets into the prompt, since dumping entire pages fills the context window and buries the answer.

Warnings

  1. Local models have a training cutoff; without a search_web tool, they answer current-events questions with stale or made-up facts.
  2. Ollama's hosted web_search proxies through ollama.com and needs ollama signin; a search_web function you write stays fully local.

In Practice

FAQ