Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic web tool to access a web page at a url. #150

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

0.7.2 -
------------------

- Add a web tool, giving Ollama access to the web.
[ggozad]

0.7.1 - 2025-01-02
------------------

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ Since version `0.6.0` `oterm` supports integration with tools. Tools are special

The following tools are currently supported:

* `fetch_url` - allows your models access to the web, fetches a URL and provides the content as input to the model.
* `date_time` - provides the current date and time in ISO format.
* `current_location` - provides the current location of the user (longitude, latitude, city, region, country). Uses [ipinfo.io](https://ipinfo.io) to determine the location.
* `current_weather` - provides the current weather in the user's location. Uses [OpenWeatherMap](https://openweathermap.org) to determine the weather. You need to provide your (free) API key in the OPEN_WEATHER_MAP_API_KEY environment variable.
Expand Down
4 changes: 3 additions & 1 deletion src/oterm/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from typing import Awaitable, Callable, Sequence, TypedDict
from typing import Sequence

from oterm.tools.date_time import DateTimeTool, date_time
from oterm.tools.location import LocationTool, current_location
from oterm.tools.shell import ShellTool, shell_command
from oterm.tools.weather import WeatherTool, current_weather
from oterm.tools.web import WebTool, fetch_url
from oterm.types import ToolDefinition

available: Sequence[ToolDefinition] = [
{"tool": DateTimeTool, "callable": date_time},
{"tool": ShellTool, "callable": shell_command},
{"tool": LocationTool, "callable": current_location},
{"tool": WeatherTool, "callable": current_weather},
{"tool": WebTool, "callable": fetch_url},
]
51 changes: 51 additions & 0 deletions src/oterm/tools/web.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import json
from html.parser import HTMLParser

import httpx

from oterm.types import Tool

WebTool = Tool(
type="function",
function=Tool.Function(
name="fetch_url",
description="Function to return the contents of a website in text format.",
parameters=Tool.Function.Parameters(
type="object",
properties={
"url": Tool.Function.Parameters.Property(
type="str", description="The URL of the website to fetch."
),
},
required=["url"],
),
),
)


class HTML2Text(HTMLParser):
text = ""

def handle_data(self, data):
self.text += data


async def fetch_url(url: str) -> str:
async with httpx.AsyncClient() as client:
try:
response = await client.get(url)

if response.status_code == 200:
html = response.text
parser = HTML2Text()
parser.feed(html)
return parser.text

else:
return json.dumps(
{
"error": f"Failed to fetch URL: {url}. Status code: {response.status_code}"
}
)
except Exception as e:
return json.dumps({"error": str(e)})
15 changes: 15 additions & 0 deletions tests/tools/test_web_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest

from oterm.ollamaclient import OllamaLLM
from oterm.tools.web import WebTool, fetch_url


@pytest.mark.asyncio
async def test_web():
llm = OllamaLLM(
tool_defs=[{"tool": WebTool, "callable": fetch_url}],
)
res = await llm.completion(
"What's oterm in a single phrase? oterm is hosted at https://github.com/ggozad/oterm."
)
assert "Ollama" in res