-
-
Save jerryan999/e1150535b1f0fc8a6158aeb020c829a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
from typing import List | |
from baidusearch.baidusearch import search | |
from app.tool.base import BaseTool | |
class GoogleSearch(BaseTool): | |
name: str = "baidu_search" | |
description: str = """Perform a Baidu search and return a list of relevant links. | |
Use this tool when you need to find information on the web, get up-to-date data, or research specific topics. | |
The tool returns a list of URLs that match the search query. | |
""" | |
parameters: dict = { | |
"type": "object", | |
"properties": { | |
"query": { | |
"type": "string", | |
"description": "(required) The search query to submit to Baidu.", | |
}, | |
"num_results": { | |
"type": "integer", | |
"description": "(optional) The number of search results to return. Default is 10.", | |
"default": 10, | |
}, | |
}, | |
"required": ["query"], | |
} | |
async def execute(self, query: str, num_results: int = 10) -> List[str]: | |
""" | |
Execute a Baidu search and return a list of URLs. | |
Args: | |
query (str): The search query to submit to Baidu. | |
num_results (int, optional): The number of search results to return. Default is 10. | |
Returns: | |
List[str]: A list of URLs matching the search query. | |
""" | |
# Run the search in a thread pool to prevent blocking | |
loop = asyncio.get_event_loop() | |
links = await loop.run_in_executor( | |
None, lambda: [result['url'] for result in search(query, num_results=num_results)] | |
) | |
return links |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment