3
3
from langchain .chat_models import ChatOpenAI
4
4
from langchain .schema import HumanMessage
5
5
import httpx
6
+ from serpapi import GoogleSearch
7
+
6
8
7
9
from app .core .config import settings
8
10
12
14
13
15
class GeneralKnowledgeTool (BaseTool ):
14
16
name = "Search"
15
- description = (
16
- # "Useful when needs to recommend general knowledge and mantains a conversation"
17
- # "Useful when needs general answers and mantains a conversation"
18
- "useful for when you need to answer questions about current events"
19
- )
17
+ description = "useful for when you need to answer questions about current events"
20
18
21
19
def __init__ (self ):
22
20
super ().__init__ ()
23
- # self.return_direct = True
24
21
25
22
def _run (self , query : str , run_manager : Optional [Any ] = None ) -> str :
26
23
"""Use the tool."""
27
24
pass
28
25
29
26
async def _arun (self , query : str , run_manager : Optional [Any ] = None ) -> str :
30
27
"""Use the tool asynchronously."""
31
- # create a async client with httpx and get request
32
28
chat = ChatOpenAI ()
33
29
response = await chat .agenerate ([[HumanMessage (content = query )]])
34
30
message = response .generations [0 ][0 ].text
@@ -38,11 +34,9 @@ async def _arun(self, query: str, run_manager: Optional[Any] = None) -> str:
38
34
class PokemonSearchTool (BaseTool ):
39
35
name = "search_pokemon"
40
36
description = " Useful when asked to answer information about a pokemon"
41
- # return_direct = True
42
37
43
38
def __init__ (self ):
44
39
super ().__init__ ()
45
- # self.return_direct = True
46
40
47
41
def _run (self , query : str , run_manager : Optional [Any ] = None ) -> str :
48
42
"""Use the tool."""
@@ -54,13 +48,11 @@ def _run(self, query: str, run_manager: Optional[Any] = None) -> str:
54
48
55
49
async def _arun (self , query : str , run_manager : Optional [Any ] = None ) -> str :
56
50
"""Use the tool asynchronously."""
57
- # create a async client with httpx and get request
58
51
async with httpx .AsyncClient () as client :
59
52
pokemon_url = pokemon_api_url + query .lower ()
60
- print ("#" * 100 )
61
- print (pokemon_url )
62
- print ("#" * 100 )
63
53
response = await client .get (pokemon_url )
54
+ if response .status_code == 404 :
55
+ return "Pokemon not found"
64
56
body = response .json ()
65
57
pokemon_number = body ["id" ]
66
58
pokemon_name = body ["name" ]
@@ -77,7 +69,6 @@ async def _arun(self, query: str, run_manager: Optional[Any] = None) -> str:
77
69
for ability in abilities :
78
70
abilities_list_names .append (ability ["ability" ]["name" ])
79
71
abilities_names = ", " .join (abilities_list_names )
80
- # create a card with the information
81
72
return f"""{ pokemon_image } \n
82
73
{ pokemon_name } - #{ pokemon_number } \n
83
74
Abilities: { abilities_names } \n
@@ -95,20 +86,17 @@ class ImageSearchTool(BaseTool):
95
86
96
87
def __init__ (self ):
97
88
super ().__init__ ()
98
- # self.return_direct = True
99
89
100
90
def _run (self , query : str , run_manager : Optional [Any ] = None ) -> str :
101
91
"""Use the tool."""
102
- response = httpx .get (unsplash_api_url + query .lower ())
103
- body = response .json ()
104
- pokemon_number = body ["id" ]
105
- pokemon_name = body ["name" ]
106
- return f"{ pokemon_name } - #{ pokemon_number } "
92
+ pass
107
93
108
94
async def _arun (self , query : str , run_manager : Optional [Any ] = None ) -> str :
109
95
"""Use the tool asynchronously."""
110
- # create a async client with httpx and get request
111
96
async with httpx .AsyncClient () as client :
97
+ if settings .UNSPLASH_API_KEY == "" :
98
+ return "You need to set a UNSPLASH_API_KEY"
99
+
112
100
unsplash_url = unsplash_api_url + query .lower ()
113
101
response = await client .get (unsplash_url )
114
102
body = response .json ()
@@ -120,5 +108,39 @@ async def _arun(self, query: str, run_manager: Optional[Any] = None) -> str:
120
108
image_list_string = "\n " .join (
121
109
[f"{ i + 1 } . [Image { i + 1 } ]({ url } )" for i , url in enumerate (images_urls )]
122
110
)
123
-
124
111
return image_list_string
112
+
113
+
114
+ class YoutubeSearchTool (BaseTool ):
115
+ name = "search_videos"
116
+ description = " Useful when asked to answer information about find videos"
117
+ return_direct = True
118
+
119
+ def __init__ (self ):
120
+ super ().__init__ ()
121
+
122
+ def _run (self , query : str , run_manager : Optional [Any ] = None ) -> str :
123
+ """Use the tool."""
124
+ pass
125
+
126
+ async def _arun (self , query : str , run_manager : Optional [Any ] = None ) -> str :
127
+ """Use the tool asynchronously."""
128
+ async with httpx .AsyncClient () as client :
129
+ if not settings .SERP_API_KEY or settings .SERP_API_KEY == "" :
130
+ return "You need to set a SERP_API_KEY"
131
+
132
+ params = {
133
+ "engine" : "youtube" ,
134
+ "search_query" : query ,
135
+ "api_key" : settings .SERP_API_KEY ,
136
+ }
137
+ search = GoogleSearch (params )
138
+ results = search .get_dict ()
139
+ videos = results ["video_results" ]
140
+ video_list_string = "\n " .join (
141
+ [
142
+ f"{ i + 1 } . [{ video ['title' ]} ]({ video ['link' ]} )"
143
+ for i , video in enumerate (videos )
144
+ ]
145
+ )
146
+ return video_list_string
0 commit comments