File size: 1,576 Bytes
731825e
c657a71
 
 
 
 
 
 
 
 
 
 
731825e
c657a71
731825e
c657a71
 
6a809e4
 
c657a71
 
731825e
c657a71
731825e
c657a71
6a809e4
731825e
 
 
6a809e4
731825e
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
# Importing necessary libraries and modules
from langchain_core.tools.base import BaseTool
from dotenv import load_dotenv
from langchain_community.utilities import DuckDuckGoSearchAPIWrapper
from langchain_community.tools import TavilySearchResults, DuckDuckGoSearchResults
from langchain_tavily import TavilySearch
import os
from pydantic import PrivateAttr
from langchain_community.document_loaders import WebBaseLoader
import json
import requests

load_dotenv(".env", override=True)  # Loading environment variables

# Defining the WebSearchTool class which extends BaseTool
class WebSearchTool(BaseTool):
    name: str = "web_search_tool"
    description: str = "Perform a web search and extract concise factual answers. The query should be concise, below 400 characters. Use for online facts not in GAIA/Wikipedia—e.g. sports stats, Olympic participation, published papers, museum specimen locations, competition winners, and other up-to-date info."
    _search: TavilySearch = PrivateAttr()

    def __init__(self):
        # Initializing the WebSearchTool
        super().__init__()
        self._search = TavilySearch(max_results=3, topic="general")  # Setting up the TavilySearch with specific parameters
    
    def _run(self, query: str) -> dict:
        # Method to run the web search tool with the given query
        search_results = []  # Initializing the list for search results
        search_results.append(self._search.run(query))  # Performing the search and adding the results to the list

        return search_results  # Returning the search results