-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
38 lines (32 loc) · 1.19 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
from exa_py import Exa
from langchain.agents import tool
class ExaSearchToolset:
@tool
def search(query: str):
"""Search for a webpage based on the query."""
return ExaSearchToolset._exa().search(f"{query}", use_autoprompt=True, num_results=3)
@tool
def find_similar(url: str):
"""Search for webpages similar to a given URL.
The url passed in should be a URL returned from `search`.
"""
return ExaSearchToolset._exa().find_similar(url, num_results=3)
@tool
def get_contents(ids: str):
"""Get the contents of a webpage.
The ids must be passed in as a list, a list of ids returned from `search`.
"""
ids = eval(ids)
contents = str(ExaSearchToolset._exa().get_contents(ids))
contents = contents.split("URL:")
contents = [content[:1000] for content in contents]
return "\n\n".join(contents)
def tools():
return [
ExaSearchToolset.search,
ExaSearchToolset.find_similar,
ExaSearchToolset.get_contents
]
def _exa():
return Exa(api_key=os.environ.get('EXA_API_KEY'))