初次提交

This commit is contained in:
lzy
2025-05-09 14:16:33 +08:00
commit 3a50afeec4
56 changed files with 9224 additions and 0 deletions

View File

View File

@@ -0,0 +1,78 @@
"""
Search Online Module
This module provides functions for searching information on the web.
"""
from ...core.llm_tools import llm_tool
from ...core.config import general_config
import asyncio
import os
from typing import Annotated, Any, Dict, List, Union
from langchain_community.utilities import SearxSearchWrapper
import mcp.types as types
@llm_tool(name="search_online_searxng", description="Search scientific information online using searxng")
async def search_online_searxng(
query: Annotated[str, "Search term"],
num_results: Annotated[int, "Number of results"] = 5
) -> str:
"""
Searches for scientific information online and returns results as a formatted string.
Args:
query: Search term for scientific content
num_results: Number of results to return
Returns:
Formatted string with search results (titles, snippets, links)
"""
# lzy: 此部分到正式发布时可能要删除因为searxng 已在本地部署,因此本地调试时无需设置代理
os.environ['HTTP_PROXY'] = ''
os.environ['HTTPS_PROXY'] = ''
try:
max_results = min(int(num_results), general_config.SEARXNG_MAX_RESULTS)
search = SearxSearchWrapper(
searx_host=general_config.SEARXNG_HOST,
categories=["science",],
k=num_results
)
# Execute search in a separate thread to avoid blocking the event loop
# since SearxSearchWrapper doesn't have native async support
loop = asyncio.get_event_loop()
raw_results = await loop.run_in_executor(
None,
lambda: search.results(query, language=['en','zh'], num_results=max_results)
)
# Transform results into structured format
formatted_results = []
for result in raw_results:
formatted_results.append({
"title": result.get("title", ""),
"snippet": result.get("snippet", ""),
"link": result.get("link", ""),
"source": result.get("source", "")
})
# Format results into a readable Markdown string
result_str = f"Search Results for '{query}' ({len(formatted_results)} items):\n\n"
if len(formatted_results) > 0:
for i, res in enumerate(formatted_results):
title = res.get("title", "No Title")
snippet = res.get("snippet", "No Snippet")
link = res.get("link", "No Link")
source = res.get("source", "No Source")
result_str += f"{i + 1}. **{title}**\n"
result_str += f" - Snippet: {snippet}\n"
result_str += f" - Link: [{link}]({link})\n"
result_str += f" - Source: {source}\n\n"
else:
result_str += "No results found.\n"
return result_str
except Exception as e:
return f"Error: {str(e)}"