[GH-ISSUE #8015] Web browsing #67186

Closed
opened 2026-05-04 09:35:21 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @Moineau54 on GitHub (Dec 9, 2024).
Original GitHub issue: https://github.com/ollama/ollama/issues/8015

Hello,

I was wondering if it was possible to give ollama an Internet access to search (for ex: using tor or duckduckgo or bing or google) for informations to give an answer to a user prompt.

Originally created by @Moineau54 on GitHub (Dec 9, 2024). Original GitHub issue: https://github.com/ollama/ollama/issues/8015 Hello, I was wondering if it was possible to give ollama an Internet access to search (for ex: using tor or duckduckgo or bing or google) for informations to give an answer to a user prompt.
GiteaMirror added the feature request label 2026-05-04 09:35:21 -05:00
Author
Owner

@rick-github commented on GitHub (Dec 9, 2024):

https://github.com/ollama/ollama/issues/7981

<!-- gh-comment-id:2528750589 --> @rick-github commented on GitHub (Dec 9, 2024): https://github.com/ollama/ollama/issues/7981
Author
Owner

@Moineau54 commented on GitHub (Dec 9, 2024):

How can I use it ? Does it run per default when i ask the llm a question?

<!-- gh-comment-id:2528806790 --> @Moineau54 commented on GitHub (Dec 9, 2024): How can I use it ? Does it run per default when i ask the llm a question?
Author
Owner

@JourneyJ012 commented on GitHub (Dec 12, 2024):

OpenWebUI has this btw, but you could probably implement this as a tool

<!-- gh-comment-id:2539950055 --> @JourneyJ012 commented on GitHub (Dec 12, 2024): OpenWebUI has this btw, but you could probably implement this as a tool
Author
Owner

@Moineau54 commented on GitHub (Dec 22, 2024):

And if I make a script that calls ollama? Does it work too ?

<!-- gh-comment-id:2558393738 --> @Moineau54 commented on GitHub (Dec 22, 2024): And if I make a script that calls ollama? Does it work too ?
Author
Owner

@rick-github commented on GitHub (Dec 22, 2024):

Search engines typically require an API key for programmatic searches, so the example below uses The Wikipedia API, but the same principles apply.

#!/usr/bin/env python3

import ollama
import sys
import readline
import argparse
import json

parser = argparse.ArgumentParser()
parser.add_argument("model", nargs="*", default="qwen2.5")
args = parser.parse_args()

def add(l: list) -> float:
  """
  Add two or more numbers

  Args:
    l (list): A list of numbers

  Returns:
    float: the sum of the numbers
  """
  return sum(l)

def wiki_search(topic: str) -> str:
  """
  Search Wikipedia for a topic.

  Args:
    topic (str): The topic to search for.

  Returns:
    A summary of the topic.
  """
  import requests
  response = requests.get("https://en.wikipedia.org/w/api.php",
      params={"action": "query",
              "titles": topic,
              "format": "json",
              "prop": "extracts|info",
              "exintro": True,
              "explaintext": True,
              "inprop": "url",
              "exsentences": 3})
  if response.status_code == requests.codes.ok:
    data = response.json()
    page = next(iter(data["query"]["pages"].values()))
    return f"Title: {page.get('title', 'None')}\nSummmary: {page.get('extract', 'None')}\nCitation: {page.get('fullurl', 'None')}"
  return("")
  
tools = [ add, wiki_search ]
toolmap = {f.__name__:f for f in tools}

messages = []

while True:
  try:
    prompt = input(">>> ")
  except:
    print()
    break
  if prompt == "/bye":
    break
  messages.append({"role":"user", "content":prompt})
  response =  ollama.chat(
    args.model,
    messages=messages,
    tools=tools,
  )

  messages.append(response.message)
  if response.message.tool_calls:
    for tool in response.message.tool_calls:
      if func := toolmap.get(tool.function.name):
        output = func(**tool.function.arguments)
        print(f"{tool.function.name}({tool.function.arguments})")
        messages.append({"role":"tool", "content":str(output), "name":tool.function.name})
    response =  ollama.chat(
      args.model,
      messages=messages,
      tools=tools,
    )
    messages.append(response.message)
  print(response.message.content)

print(json.dumps(messages, default=str, indent=4))
$ ./8015.py 
>>> what is 2+2?
add({'l': [2, 2]})
The answer to 2 + 2 is 4.
>>> what can you tell me about einstein?
wiki_search({'topic': 'Albert Einstein'})
Albert Einstein was a German-born theoretical physicist renowned for developing the theory of relativity.
He also made significant contributions to quantum mechanics and is perhaps best known for his
mass-energy equivalence formula, E = mc². This equation has become one of the most famous in
the world. For more details, you can visit the Wikipedia page on Albert Einstein.
>>> write a haiku about albert
Genius mind sparkled,
Equations danced in his head—
Relativity.
>>> 
[
    {
        "role": "user",
        "content": "what is 2+2?"
    },
    "role='assistant' content='' images=None tool_calls=[ToolCall(function=Function(name='add', arguments={'l': [2, 2]}))]",
    {
        "role": "tool",
        "content": "4",
        "name": "add"
    },
    "role='assistant' content='The answer to 2 + 2 is 4.' images=None tool_calls=None",
    {
        "role": "user",
        "content": "what can you tell me about einstein?"
    },
    "role='assistant' content='' images=None tool_calls=[ToolCall(function=Function(name='wiki_search', arguments={'topic': 'Albert Einstein'}))]",
    {
        "role": "tool",
        "content": "Title: Albert Einstein\nSummmary: Albert Einstein (, EYEN-styne; German: [\u02c8alb\u025b\u0281t \u02c8\u0294a\u026an\u0283ta\u026an] ; 14 March 1879 \u2013 18 April 1955) was a German-born theoretical physicist who is best known for developing the theory of relativity. Einstein also made important contributions to quantum mechanics. His mass\u2013energy equivalence formula E = mc2, which arises from special relativity, has been called \"the world's most famous equation\".\nCitation: https://en.wikipedia.org/wiki/Albert_Einstein",
        "name": "wiki_search"
    },
    "role='assistant' content='Albert Einstein was a German-born theoretical physicist renowned for developing the theory of relativity. He also made significant contributions to quantum mechanics and is perhaps best known for his mass-energy equivalence formula, E = mc\u00b2. This equation has become one of the most famous in the world. For more details, you can visit the Wikipedia page on Albert Einstein.' images=None tool_calls=None",
    {
        "role": "user",
        "content": "write a haiku about albert"
    },
    "role='assistant' content='Genius mind sparkled,\\nEquations danced in his head\u2014\\nRelativity.' images=None tool_calls=None"
]
<!-- gh-comment-id:2558425276 --> @rick-github commented on GitHub (Dec 22, 2024): Search engines typically require an API key for programmatic searches, so the example below uses The Wikipedia API, but the same principles apply. ```python #!/usr/bin/env python3 import ollama import sys import readline import argparse import json parser = argparse.ArgumentParser() parser.add_argument("model", nargs="*", default="qwen2.5") args = parser.parse_args() def add(l: list) -> float: """ Add two or more numbers Args: l (list): A list of numbers Returns: float: the sum of the numbers """ return sum(l) def wiki_search(topic: str) -> str: """ Search Wikipedia for a topic. Args: topic (str): The topic to search for. Returns: A summary of the topic. """ import requests response = requests.get("https://en.wikipedia.org/w/api.php", params={"action": "query", "titles": topic, "format": "json", "prop": "extracts|info", "exintro": True, "explaintext": True, "inprop": "url", "exsentences": 3}) if response.status_code == requests.codes.ok: data = response.json() page = next(iter(data["query"]["pages"].values())) return f"Title: {page.get('title', 'None')}\nSummmary: {page.get('extract', 'None')}\nCitation: {page.get('fullurl', 'None')}" return("") tools = [ add, wiki_search ] toolmap = {f.__name__:f for f in tools} messages = [] while True: try: prompt = input(">>> ") except: print() break if prompt == "/bye": break messages.append({"role":"user", "content":prompt}) response = ollama.chat( args.model, messages=messages, tools=tools, ) messages.append(response.message) if response.message.tool_calls: for tool in response.message.tool_calls: if func := toolmap.get(tool.function.name): output = func(**tool.function.arguments) print(f"{tool.function.name}({tool.function.arguments})") messages.append({"role":"tool", "content":str(output), "name":tool.function.name}) response = ollama.chat( args.model, messages=messages, tools=tools, ) messages.append(response.message) print(response.message.content) print(json.dumps(messages, default=str, indent=4)) ``` ```console $ ./8015.py >>> what is 2+2? add({'l': [2, 2]}) The answer to 2 + 2 is 4. >>> what can you tell me about einstein? wiki_search({'topic': 'Albert Einstein'}) Albert Einstein was a German-born theoretical physicist renowned for developing the theory of relativity. He also made significant contributions to quantum mechanics and is perhaps best known for his mass-energy equivalence formula, E = mc². This equation has become one of the most famous in the world. For more details, you can visit the Wikipedia page on Albert Einstein. >>> write a haiku about albert Genius mind sparkled, Equations danced in his head— Relativity. >>> [ { "role": "user", "content": "what is 2+2?" }, "role='assistant' content='' images=None tool_calls=[ToolCall(function=Function(name='add', arguments={'l': [2, 2]}))]", { "role": "tool", "content": "4", "name": "add" }, "role='assistant' content='The answer to 2 + 2 is 4.' images=None tool_calls=None", { "role": "user", "content": "what can you tell me about einstein?" }, "role='assistant' content='' images=None tool_calls=[ToolCall(function=Function(name='wiki_search', arguments={'topic': 'Albert Einstein'}))]", { "role": "tool", "content": "Title: Albert Einstein\nSummmary: Albert Einstein (, EYEN-styne; German: [\u02c8alb\u025b\u0281t \u02c8\u0294a\u026an\u0283ta\u026an] ; 14 March 1879 \u2013 18 April 1955) was a German-born theoretical physicist who is best known for developing the theory of relativity. Einstein also made important contributions to quantum mechanics. His mass\u2013energy equivalence formula E = mc2, which arises from special relativity, has been called \"the world's most famous equation\".\nCitation: https://en.wikipedia.org/wiki/Albert_Einstein", "name": "wiki_search" }, "role='assistant' content='Albert Einstein was a German-born theoretical physicist renowned for developing the theory of relativity. He also made significant contributions to quantum mechanics and is perhaps best known for his mass-energy equivalence formula, E = mc\u00b2. This equation has become one of the most famous in the world. For more details, you can visit the Wikipedia page on Albert Einstein.' images=None tool_calls=None", { "role": "user", "content": "write a haiku about albert" }, "role='assistant' content='Genius mind sparkled,\\nEquations danced in his head\u2014\\nRelativity.' images=None tool_calls=None" ] ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#67186