-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternet_search.py
70 lines (62 loc) · 2.25 KB
/
internet_search.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from app.agents.tools.agent_tool import AgentTool
from app.repositories.models.custom_bot import BotModel
from app.routes.schemas.conversation import type_model_name
from duckduckgo_search import DDGS
from pydantic import BaseModel, Field, root_validator
class InternetSearchInput(BaseModel):
query: str = Field(description="The query to search for on the internet.")
country: str = Field(
description="The country code you wish for search. Must be one of: jp-jp (Japan), kr-kr (Korea), cn-zh (China), fr-fr (France), de-de (Germany), es-es (Spain), it-it (Italy), us-en (United States)"
)
time_limit: str = Field(
description="The time limit for the search. Options are 'd' (day), 'w' (week), 'm' (month), 'y' (year)."
)
@root_validator(pre=True)
def validate_country(cls, values):
country = values.get("country")
if country not in [
"jp-jp",
"kr-kr",
"cn-zh",
"fr-fr",
"de-de",
"es-es",
"it-it",
"us-en",
]:
raise ValueError(
f"Country must be one of: jp-jp (Japan), kr-kr (Korea), cn-zh (China), fr-fr (France), de-de (Germany), es-es (Spain), it-it (Italy), us-en (United States)"
)
return values
def internet_search(
tool_input: InternetSearchInput, bot: BotModel | None, model: type_model_name | None
) -> list:
query = tool_input.query
time_limit = tool_input.time_limit
country = tool_input.country
REGION = country
SAFE_SEARCH = "moderate"
MAX_RESULTS = 20
BACKEND = "api"
with DDGS() as ddgs:
return [
{
"content": result["body"],
"source_name": result["title"],
"source_link": result["href"],
}
for result in ddgs.text(
keywords=query,
region=REGION,
safesearch=SAFE_SEARCH,
timelimit=time_limit,
max_results=MAX_RESULTS,
backend=BACKEND,
)
]
internet_search_tool = AgentTool(
name="internet_search",
description="Search the internet for information.",
args_schema=InternetSearchInput,
function=internet_search,
)