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

Add browser_tool example to fetch webpages #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ typing_extensions==4.8.0
urllib3==1.26.18
wikipedia==1.4.0
yarl==1.9.2
html2text==2024.2.26
35 changes: 35 additions & 0 deletions tool_use_package/browser_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This contains the code for browser tool that opens a single webpage at a url
import html2text
import requests

from .tools.base_tool import BaseTool
from .tool_user import ToolUser

class BrowserTool(BaseTool):
"""Retrieves a webpage and formats to text for Claude"""

def use_tool(self, url):
try:
response = requests.get(url)
html = response.text
h = html2text.HTML2Text()
text = h.handle(html)
return text
except requests.exceptions.RequestException:
return f"There was an error fetching {url}"

tool_name = "get_webpage"
tool_description = """The get_webpage tool will return the text of a webpage."""
tool_parameters = [
{"name": "url", "type": "str", "description": "The URL of the webpage to get."}
]

browser_tool = BrowserTool(tool_name, tool_description, tool_parameters)

# Pass the tool instance into the ToolUser
tool_user = ToolUser([browser_tool])

# Call the tool_user with a prompt to get a version of Claude that can use your tools!
if __name__ == '__main__':
messages = [{"role":"user", "content":f"Summarize http://docs.python.org"}]
print(tool_user.use_tools(messages, execution_mode="automatic"))