File size: 11,772 Bytes
b3bb0f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# ##not implemented include_images and include_image_descriptions attributes

# import os

# from tavily import TavilyClient
# from smolagents import Tool
# #from pydantic import BaseModel, Field 
# from typing import Optional,List, Dict, Any, Literal


# # class TavilySearchInput(BaseModel):
# #     """Input for Tavily Search."""
# #     query : str = Field(description="The search query string.")
# #     max_results : Optional[int] = Field(default = 5,
# #                                         description="Maximum number of search results to return.")
# #     topic : Optional[str] = Field(default = "general",
# #                                   description="Category of the search. Can be 'general', 'news', or 'finance'.")
# #     include_answer : Optional[bool] = Field(default = False,
# #                                             description="Whether to include an answer to original query in results.")
# #     include_raw_content : Optional[bool] = Field(default = False,
# #                                                  description="Whether to include cleaned and parsed HTML of each search result.")
# #     search_depth: Optional[str] = Field(default = "basic",
# #                                         description="Depth of the search, either 'basic' or 'advanced'.")
# #     time_range: Optional[str] = Field(default = None,
# #                                       description="The time range back from the current date to filter results - 'day', 'week', 'month', or 'year'.")
# #     include_domains: Optional[List[str]] = Field(default = None,
# #                                                  description="List of domains to specifically include.")
# #     exclude_domains: Optional[List[str]] = Field(default = None,
# #                                                  description="List of domains to specifically exclude.")


# # class TavilySearchTool(Tool):
# #     name: str = "tavily_search"
# #     description = """
# #     A tool to perform comprehensive web searches using the Tavily Search API.
# #     It can retrieve real-time, accurate, and factual information from across the web.
# #     Useful for answering questions, gathering information, and reducing hallucinations."""

# #     inputs = inputs

# #     output_type = "list"

# #     def __init__(self, api_key:Optional[str] = None, **kwargs):
# #         super().__init__(**kwargs)
# #         if api_key is None:
# #             api_key = os.getenv("TRAVILY_SECRET_KEY")
# #         if not api_key:
# #             raise ValueError(
# #                 "Tavily API key not found. Please set the TAVILY_API_KEY environment variable "
# #                 "or pass it directly to TavilySearchTool(api_key='YOUR_API_KEY')."
# #             )
# #         self.tavily_client = TavilyClient(api_key=api_key)
# #         print('TavilyClient is initiated')

# #     def forward(self, query: str, 
# #                 max_results: int = 5, 
# #                 include_answer: bool = False, 
# #                 include_raw_content: bool = False,
# #                 search_depth: str = "basic",
# #                 topic: str = "general",
# #                 time_range: Optional[str] = None,
# #                 include_domains: Optional[List[str]] = None,
# #                 exclude_domains: Optional[List[str]] = None) -> List[Dict[str, Any]]:
# #         """
# #         Executes a search query using the Tavily API.

# #         Args:
# #             query (str): The search query.
# #             max_results (int): Maximum number of search results to return.
# #             include_answer (bool): Whether to include a direct answer synthesized from the search results.
# #             include_raw_content (bool): Whether to include the raw HTML content of the searched pages.
# #             search_depth (str): Depth of the search, either 'basic' or 'advanced'.
# #             topic (str): Category of the search. Can be 'general', 'news', or 'finance'.
# #             time_range (Optional[str]): The time range back from the current date to filter results - 'day', 'week', 'month', or 'year'.
# #             include_domains (Optional[List[str]]): List of domains to specifically include.
# #             exclude_domains (Optional[List[str]]): List of domains to specifically exclude.

# #         Returns:
# #             List[Dict[str, Any]]: A list of dictionaries, where each dictionary represents a search result.
# #                                   Each result typically contains 'title', 'url', and 'content'.
# #                                   If include_answer is True, it will also include the 'answer' field
# #                                   in the top-level response.
# #         """
# #         try:
# #             response = self.tavily_client.search(
# #                 query=query,
# #                 max_results=max_results,
# #                 include_answer=include_answer,
# #                 include_raw_content=include_raw_content,
# #                 search_depth=search_depth,
# #                 topic=topic,
# #                 time_range=time_range,
# #                 include_domains=include_domains,
# #                 exclude_domains=exclude_domains
# #             )

# #             return response

# #         except Exception as e:
# #             return [{"error": f"Tavily Search Error: {e}"}]
        


# # class TavilySearchTool(Tool):
# #     name = "tavily_search"
# #     description = """
# #     A tool to perform comprehensive web searches using the Tavily Search API.
# #     It can retrieve real-time, accurate, and factual information from across the web.
# #     Useful for answering questions, gathering information, and reducing hallucinations."""
    
# #     inputs = {
# #         "query": {"type": "string", "description": "The search query string."},
# #         "max_results": {"type": "integer", "description": "Maximum number of results.", "default": 5, "nullable": False},
# #         "topic": {"type": "string", "description": "Search category: 'general', 'news', or 'finance'.", "default": "general", "nullable": False},
# #         "include_answer": {"type": "boolean", "description": "Whether to include answer in results.", "default": False, "nullable": False},
# #         "include_raw_content": {"type": "boolean", "description": "Whether to include HTML content.", "default": False, "nullable": False},
# #         "search_depth": {"type": "string", "description": "Search depth: 'basic' or 'advanced'.", "default": "basic", "nullable": False},
# #         "time_range": {"type": "string", "description": "Filter by time range.", "default": None, "nullable": True},
# #         "include_domains": {"type": "array", "description": "List of domains to include.", "default": None, "nullable": True},
# #         "exclude_domains": {"type": "array", "description": "List of domains to exclude.", "default": None, "nullable": True},
# #     }

# #     output_type = "any"

# #     def __init__(self, api_key:Optional[str] = None, **kwargs):
# #         super().__init__(**kwargs)
# #         if api_key is None:
# #             api_key = os.getenv("TRAVILY_SECRET_KEY")
# #         if not api_key:
# #             raise ValueError(
# #                 "Tavily API key not found. Please set the TAVILY_API_KEY environment variable "
# #                 "or pass it directly to TavilySearchTool(api_key='YOUR_API_KEY')."
# #             )
# #         self.tavily_client = TavilyClient(api_key=api_key)
# #         print('TavilyClient is initiated')

# #     def forward(self, query: str, 
# #                 max_results: int = 5, 
# #                 include_answer: bool = False, 
# #                 include_raw_content: bool = False,
# #                 search_depth: str = "basic",
# #                 topic: str = "general",
# #                 time_range: Optional[str] = None,
# #                 include_domains: Optional[List[str]] = None,
# #                 exclude_domains: Optional[List[str]] = None) -> List[Dict[str, Any]]:
# #         """
# #         Executes a search query using the Tavily API.

# #         Args:
# #             query (str): The search query.
# #             max_results (int): Maximum number of search results to return.
# #             include_answer (bool): Whether to include a direct answer synthesized from the search results.
# #             include_raw_content (bool): Whether to include the raw HTML content of the searched pages.
# #             search_depth (str): Depth of the search, either 'basic' or 'advanced'.
# #             topic (str): Category of the search. Can be 'general', 'news', or 'finance'.
# #             time_range (Optional[str]): The time range back from the current date to filter results - 'day', 'week', 'month', or 'year'.
# #             include_domains (Optional[List[str]]): List of domains to specifically include.
# #             exclude_domains (Optional[List[str]]): List of domains to specifically exclude.

# #         Returns:
# #             List[Dict[str, Any]]: A list of dictionaries, where each dictionary represents a search result.
# #                                   Each result typically contains 'title', 'url', and 'content'.
# #                                   If include_answer is True, it will also include the 'answer' field
# #                                   in the top-level response.
# #         """
# #         try:
# #             response = self.tavily_client.search(
# #                 query=query,
# #                 max_results=max_results,
# #                 include_answer=include_answer,
# #                 include_raw_content=include_raw_content,
# #                 search_depth=search_depth,
# #                 topic=topic,
# #                 time_range=time_range,
# #                 include_domains=include_domains,
# #                 exclude_domains=exclude_domains
# #             )

# #             return response

# #         except Exception as e:
# #             return [{"error": f"Tavily Search Error: {e}"}]



# class WebSearchTool(Tool):
#     name = "web_search"
#     description = """
#     A tool to perform comprehensive web searches using the Tavily Search API.
#     It can retrieve real-time, accurate, and factual information from across the web.
#     Useful for answering questions, gathering information, and reducing hallucinations."""
    
#     inputs = {
#         "query": {"type": "string",
#                   "description": "The search query string."
#                  }
#      }

#     output_type = "any"

#     def __init__(self, api_key:Optional[str] = None, **kwargs):
#         super().__init__(**kwargs)
#         if api_key is None:
#             api_key = os.getenv("TRAVILY_SECRET_KEY")
#         if not api_key:
#             raise ValueError(
#                 "Tavily API key not found. Please set the TAVILY_API_KEY environment variable "
#                 "or pass it directly to TavilySearchTool(api_key='YOUR_API_KEY')."
#             )
#         self.tavily_client = TavilyClient(api_key=api_key)
#         print('TavilyClient is initiated')

#     def forward(self, 
#                 query: str) -> Any:
#         try:
#             print(f'trying travily search...')
#             response = self.tavily_client.search(
#                 query=query,
#                 max_results=5,
#                 search_depth="advanced",
#                 topic='general',
#                 )
#             #print(response)
#             if "results" in response:
#                 return response.get("results", [])
#             else:
#                 return('try shorter query or different approach.')

#         except Exception as e:
#             return [{"error": f"Tavily Search Error: {e}"}]