feat: support duckduckgo search engine

This commit is contained in:
He Tao
2025-04-10 11:45:04 +08:00
parent 03798ded08
commit 1195612c47
13 changed files with 88 additions and 119 deletions

View File

@@ -1,11 +1,18 @@
from .crawl import crawl_tool
from .python_repl import python_repl_tool
from .search import tavily_tool
from .bash_tool import bash_tool
from .search import tavily_search_tool, duckduckgo_search_tool
from src.config import SELECTED_SEARCH_ENGINE, SearchEngine
# Map search engine names to their respective tools
search_tool_mappings = {
SearchEngine.TAVILY.value: tavily_search_tool,
SearchEngine.DUCKDUCKGO.value: duckduckgo_search_tool,
}
web_search_tool = search_tool_mappings.get(SELECTED_SEARCH_ENGINE, tavily_search_tool)
__all__ = [
"bash_tool",
"crawl_tool",
"tavily_tool",
"web_search_tool",
"python_repl_tool",
]

View File

@@ -1,49 +0,0 @@
import logging
import subprocess
from typing import Annotated
from langchain_core.tools import tool
from .decorators import log_io
# Initialize logger
logger = logging.getLogger(__name__)
@tool
@log_io
def bash_tool(
cmd: Annotated[str, "The bash command to be executed."],
timeout: Annotated[
int, "Maximum time in seconds for the command to complete."
] = 120,
):
"""Use this to execute bash command and do necessary operations."""
logger.info(f"Executing Bash Command: {cmd} with timeout {timeout}s")
try:
# Execute the command and capture output
result = subprocess.run(
cmd, shell=True, check=True, text=True, capture_output=True, timeout=timeout
)
# Return stdout as the result
return result.stdout
except subprocess.CalledProcessError as e:
# If command fails, return error information
error_message = f"Command failed with exit code {
e.returncode}.\nStdout: {
e.stdout}\nStderr: {
e.stderr}"
logger.error(error_message)
return error_message
except subprocess.TimeoutExpired:
# Handle timeout exception
error_message = f"Command '{cmd}' timed out after {timeout}s."
logger.error(error_message)
return error_message
except Exception as e:
# Catch any other exceptions
error_message = f"Error executing command: {str(e)}"
logger.error(error_message)
return error_message
if __name__ == "__main__":
print(bash_tool.invoke("ls -all"))

View File

@@ -1,10 +1,18 @@
import logging
from langchain_community.tools.tavily_search import TavilySearchResults
from src.config import TAVILY_MAX_RESULTS
from langchain_community.tools import DuckDuckGoSearchResults
from src.config import SEARCH_MAX_RESULTS
from .decorators import create_logged_tool
logger = logging.getLogger(__name__)
# Initialize Tavily search tool with logging
LoggedTavilySearch = create_logged_tool(TavilySearchResults)
tavily_tool = LoggedTavilySearch(name="tavily_search", max_results=TAVILY_MAX_RESULTS)
tavily_search_tool = LoggedTavilySearch(
name="web_search", max_results=SEARCH_MAX_RESULTS
)
LoggedDuckDuckGoSearch = create_logged_tool(DuckDuckGoSearchResults)
duckduckgo_search_tool = LoggedDuckDuckGoSearch(
name="web_search", max_results=SEARCH_MAX_RESULTS
)