Friday, September 26, 2025

What is Ollama WebSearch

A new web search API is now available in Ollama. Ollama provides a generous free tier of web searches for individuals to use, and higher rate limits are available via Ollama’s cloud.

This web search capability can augment models with the latest information from the web to reduce hallucinations and improve accuracy.

Web search is provided as a REST API with deeper tool integrations in Ollama’s Python and JavaScript libraries. This also enables models such as OpenAI’s gpt-oss models to conduct long-running research tasks.


curl https://ollama.com/api/web_search \

  --header "Authorization: Bearer $OLLAMA_API_KEY" \

  -d '{

    "query": "what is ollama?"

  }'


Example output


{

  "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": "Ariffud M. 6min Read..."

    },

    {

      "title": "Ollama Explained: Transforming AI Accessibility and Language ...",

      "url": "https://www.geeksforgeeks.org/artificial-intelligence/ollama-explained-transforming-ai-accessibility-and-language-processing/",

      "content": "Data Science Data Science Projects Data Analysis..."

    }

  ]

}


To build an agent using web search, below is the process 



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)

  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




No comments:

Post a Comment