Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

feat: support baidu ai search

yugasun6b18b205e926
diff --git a/.env.example b/.env.example
index a22ba0a68..a805f382f 100644
--- a/.env.example
+++ b/.env.example
@@ -69,10 +69,27 @@ TTS_API_KEY=
# Web Search Configuration
# ============================================================================
# Configure external search APIs for research features.
+
+# Search provider to use for web search
+# Options: perplexity, baidu
+# Default: perplexity
+SEARCH_PROVIDER=perplexity
+
+# ----------------------------------------------------------------------------
+# Perplexity AI Search Configuration
+# ----------------------------------------------------------------------------
# Perplexity API key for web search functionality
# Get your API key at: https://www.perplexity.ai/settings/api
PERPLEXITY_API_KEY=
+# ----------------------------------------------------------------------------
+# Baidu AI Search Configuration (百度AI搜索)
+# ----------------------------------------------------------------------------
+# Baidu API Key for intelligent search and generation
+# Get your API key at: https://console.bce.baidu.com/ai_apaas/resource
+# Format: bce-v3/ALTAK-xxx/xxx or the full API key
+BAIDU_API_KEY=
+
# ============================================================================
# Logging Configuration
# ============================================================================
diff --git a/README.md b/README.md
index 05360b43e..758303cf7 100644
--- a/README.md
+++ b/README.md
@@ -262,7 +262,9 @@ cp .env.example .env
| `BACKEND_PORT` | No | Backend port (default: `8001`) |
| `FRONTEND_PORT` | No | Frontend port (default: `3782`) |
| `TTS_*` | No | Text-to-Speech settings |
-| `PERPLEXITY_API_KEY` | No | For web search |
+| `SEARCH_PROVIDER` | No | Search provider (options: `perplexity`, `baidu`, default: `perplexity`) |
+| `PERPLEXITY_API_KEY` | No | For Perplexity web search |
+| `BAIDU_API_KEY` | No | For Baidu AI search |
</details>
diff --git a/src/tools/web_search.py b/src/tools/web_search.py
index c0d5a710e..a88c78201 100644
--- a/src/tools/web_search.py
+++ b/src/tools/web_search.py
@@ -1,12 +1,15 @@
#!/usr/bin/env python
"""
-Web Search Tool - Network search using Perplexity API
+Web Search Tool - Network search using Perplexity API or Baidu AI Search API
"""
from datetime import datetime
+from enum import Enum
import json
import os
+import requests
+
try:
from perplexity import Perplexity
@@ -16,123 +19,378 @@ except ImportError:
Perplexity = None
-def web_search(query: str, output_dir: str | None = None, verbose: bool = False) -> dict:
+class SearchProvider(str, Enum):
+ """Supported search providers"""
+
+ PERPLEXITY = "perplexity"
+ BAIDU = "baidu"
+
+
+class BaiduAISearch:
+ """Baidu AI Search client for intelligent search and generation"""
+
+ BASE_URL = "https://qianfan.baidubce.com/v2/ai_search/chat/completions"
+
+ def __init__(self, api_key: str):
+ """
+ Initialize Baidu AI Search client
+
+ Args:
+ api_key: Baidu Qianfan API Key (format: bce-v3/xxx or Bearer token)
+ """
+ self.api_key = api_key
+ self.headers = {
+ "Content-Type": "application/json",
+ "Authorization": f"Bearer {api_key}" if not api_key.startswith("Bearer ") else api_key,
+ }
+
+ def search(
+ self,
+ query: str,
+ model: str = "ernie-4.5-turbo-32k",
+ search_source: str = "baidu_search_v2",
+ stream: bool = False,
+ enable_deep_search: bool = False,
+ enable_corner_markers: bool = True,
+ enable_followup_queries: bool = False,
+ temperature: float = 0.11,
+ top_p: float = 0.55,
+ search_mode: str = "auto",
+ search_recency_filter: str | None = None,
+ instruction: str = "",
+ ) -> dict:
+ """
+ Perform intelligent search using Baidu AI Search API
+
+ Args:
+ query: Search query
+ model: Model to use for generation (default: ernie-4.5-turbo-32k)
+ search_source: Search engine version (baidu_search_v1 or baidu_search_v2)
+ stream: Whether to use streaming response
+ enable_deep_search: Enable deep search for more comprehensive results
+ enable_corner_markers: Enable corner markers for reference citations
+ enable_followup_queries: Enable follow-up query suggestions
+ temperature: Model sampling temperature (0, 1]
+ top_p: Model sampling top_p (0, 1]
+ search_mode: Search mode (auto, required, disabled)
+ search_recency_filter: Filter by recency (week, month, semiyear, year)
+ instruction: System instruction for response style
+ max_completion_tokens: Maximum output tokens
+
+ Returns:
+ dict: API response containing search results and generated answer
+ """
+ payload = {
+ "messages": [{"role": "user", "content": query}],
+ "model": model,
+ "search_source": search_source,
+ "stream": stream,
+ "enable_deep_search": enable_deep_search,
+ "enable_corner_markers": enable_corner_markers,
+ "enable_followup_queries": enable_followup_queries,
+ "temperature": temperature,
+ "top_p": top_p,
+ "search_mode": search_mode,
+ }
+
+ if search_recency_filter:
+ payload["search_recency_filter"] = search_recency_filter
+
+ if instruction:
+ payload["instruction"] = instruction
+
+ response = requests.post(self.BASE_URL, headers=self.headers, json=payload, timeout=120)
+
+ if response.status_code != 200:
+ error_data = response.json() if response.text else {}
+ raise Exception(
+ f"Baidu AI Search API error: {response.status_code} - "
+ f"{error_data.get('message', response.text)}"
+ )
+
+ return response.json()
+
+
+def _search_with_baidu(
+ query: str,
+ model: str = "ernie-4.5-turbo-32k",
+ enable_deep_search: bool = False,
+ search_recency_filter: str | None = None,
+ verbose: bool = False,
+) -> dict:
"""
- Perform network search using Perplexity API and return results
+ Perform search using Baidu AI Search API
Args:
query: Search query
- output_dir: Output directory (optional, if provided will save results)
+ model: Model to use for generation
+ enable_deep_search: Enable deep search
+ search_recency_filter: Filter by recency
verbose: Whether to print detailed information
Returns:
- dict: Dictionary containing search results
- {
- "query": str,
- "answer": str,
- "result_file": str (if file was saved)
+ dict: Standardized search result
+ """
+ api_key = os.environ.get("BAIDU_API_KEY")
+ if not api_key:
+ raise ValueError(
+ "BAIDU_API_KEY environment variable is not set. "
+ "Please get your API key from https://console.bce.baidu.com/ai_apaas/resource"
+ )
+
+ client = BaiduAISearch(api_key=api_key)
+
+ response = client.search(
+ query=query,
+ model=model,
+ enable_deep_search=enable_deep_search,
+ search_recency_filter=search_recency_filter,
+ )
+
+ # Extract answer from response
+ answer = ""
+ if response.get("choices"):
+ choice = response["choices"][0]
+ if choice.get("message"):
+ answer = choice["message"].get("content", "")
+
+ # Build standardized result
+ result = {
+ "timestamp": datetime.now().isoformat(),
+ "query": query,
+ "model": model,
+ "provider": "baidu",
+ "answer": answer,
+ "response": {
+ "content": answer,
+ "role": "assistant",
+ "finish_reason": response.get("choices", [{}])[0].get("finish_reason", ""),
+ },
+ "usage": {},
+ "citations": [],
+ "search_results": [],
+ "is_safe": response.get("is_safe", True),
+ "request_id": response.get("request_id", ""),
+ }
+
+ # Extract usage information
+ if response.get("usage"):
+ usage = response["usage"]
+ result["usage"] = {
+ "prompt_tokens": usage.get("prompt_tokens", 0),
+ "completion_tokens": usage.get("completion_tokens", 0),
+ "total_tokens": usage.get("total_tokens", 0),
+ }
+
+ # Extract references/citations
+ if response.get("references"):
+ for i, ref in enumerate(response["references"], 1):
+ citation_data = {
+ "id": ref.get("id", i),
+ "reference": f"[{ref.get('id', i)}]",
+ "url": ref.get("url", ""),
+ "title": ref.get("title", ""),
+ "snippet": ref.get("content", ""),
+ "date": ref.get("date", ""),
+ "type": ref.get("type", "web"),
+ "icon": ref.get("icon", ""),
+ "website": ref.get("website", ""),
+ "web_anchor": ref.get("web_anchor", ""),
}
+ result["citations"].append(citation_data)
- Raises:
- ImportError: If perplexity module is not installed
- ValueError: If PERPLEXITY_API_KEY environment variable is not set
- Exception: If API call fails
+ # Also add to search_results for compatibility
+ search_result = {
+ "title": ref.get("title", ""),
+ "url": ref.get("url", ""),
+ "date": ref.get("date", ""),
+ "snippet": ref.get("content", ""),
+ "source": ref.get("web_anchor", ""),
+ }
+ result["search_results"].append(search_result)
+
+ # Extract follow-up queries if available
+ if response.get("followup_queries"):
+ result["followup_queries"] = response["followup_queries"]
+
+ if verbose:
+ print(f"[Baidu AI Search] Query: {query}")
+ print(f"[Baidu AI Search] Model: {model}")
+ print(f"[Baidu AI Search] References count: {len(result['citations'])}")
+
+ return result
+
+
+def _search_with_perplexity(query: str, verbose: bool = False) -> dict:
+ """
+ Perform search using Perplexity API
+
+ Args:
+ query: Search query
+ verbose: Whether to print detailed information
+
+ Returns:
+ dict: Standardized search result
"""
- # Check if perplexity module is available
if not PERPLEXITY_AVAILABLE:
raise ImportError(
- "perplexity module is not installed. To use web search functionality, please install the corresponding package.\n"
- "Note: This is an optional feature and does not affect the use of other modules."
+ "perplexity module is not installed. To use Perplexity search, please install: "
+ "pip install perplexity"
)
- # Check API key
api_key = os.environ.get("PERPLEXITY_API_KEY")
if not api_key:
raise ValueError("PERPLEXITY_API_KEY environment variable is not set")
- # Initialize client
- client = Perplexity(api_key=api_key)
+ if Perplexity is None:
+ raise ImportError("Perplexity module is not available")
- try:
- # Call API
- completion = client.chat.completions.create(
- model="sonar",
- messages=[
- {
- "role": "system",
- "content": "You are a helpful AI assistant. Provide detailed and accurate answers based on web search results.",
- },
- {"role": "user", "content": query},
- ],
- )
+ client = Perplexity(api_key=api_key)
- # Extract response content
- answer = completion.choices[0].message.content
-
- # Build result dictionary
- result = {
- "timestamp": datetime.now().isoformat(),
- "query": query,
- "model": completion.model,
- "answer": answer,
- "response": {
- "content": answer,
- "role": completion.choices[0].message.role,
- "finish_reason": completion.choices[0].finish_reason,
- },
- "usage": {
- "prompt_tokens": completion.usage.prompt_tokens,
- "completion_tokens": completion.usage.completion_tokens,
- "total_tokens": completion.usage.total_tokens,
- "cost": {
- "total_cost": completion.usage.cost.total_cost,
- "input_tokens_cost": completion.usage.cost.input_tokens_cost,
- "output_tokens_cost": completion.usage.cost.output_tokens_cost,
- },
+ completion = client.chat.completions.create(
+ model="sonar",
+ messages=[
+ {
+ "role": "system",
+ "content": "You are a helpful AI assistant. Provide detailed and accurate answers based on web search results.",
},
- "citations": [],
- "search_results": [],
+ {"role": "user", "content": query},
+ ],
+ )
+
+ answer = completion.choices[0].message.content
+
+ # Build usage info with safe attribute access
+ usage_info: dict = {}
+ if hasattr(completion, "usage") and completion.usage is not None:
+ usage = completion.usage
+ usage_info = {
+ "prompt_tokens": getattr(usage, "prompt_tokens", 0),
+ "completion_tokens": getattr(usage, "completion_tokens", 0),
+ "total_tokens": getattr(usage, "total_tokens", 0),
}
+ if hasattr(usage, "cost") and usage.cost is not None:
+ cost = usage.cost
+ usage_info["cost"] = {
+ "total_cost": getattr(cost, "total_cost", 0),
+ "input_tokens_cost": getattr(cost, "input_tokens_cost", 0),
+ "output_tokens_cost": getattr(cost, "output_tokens_cost", 0),
+ }
+
+ result = {
+ "timestamp": datetime.now().isoformat(),
+ "query": query,
+ "model": completion.model,
+ "provider": "perplexity",
+ "answer": answer,
+ "response": {
+ "content": answer,
+ "role": completion.choices[0].message.role,
+ "finish_reason": completion.choices[0].finish_reason,
+ },
+ "usage": usage_info,
+ "citations": [],
+ "search_results": [],
+ }
+
+ # Extract citation links
+ if hasattr(completion, "citations") and completion.citations:
+ for i, citation_url in enumerate(completion.citations, 1):
+ citation_data = {
+ "id": i,
+ "reference": f"[{i}]",
+ "url": citation_url,
+ "title": "",
+ "snippet": "",
+ }
+
+ for search_item in result.get("search_results", []):
+ if search_item.get("url") == citation_url:
+ citation_data["title"] = search_item.get("title", "")
+ citation_data["snippet"] = search_item.get("snippet", "")
+ break
+
+ result["citations"].append(citation_data)
+
+ # Extract search result details
+ if hasattr(completion, "search_results") and completion.search_results:
+ for search_item in completion.search_results:
+ search_result = {
+ "title": search_item.title,
+ "url": search_item.url,
+ "date": search_item.date,
+ "last_updated": search_item.last_updated,
+ "snippet": search_item.snippet,
+ "source": search_item.source,
+ }
+ result["search_results"].append(search_result)
+
+ if verbose:
+ print(f"[Perplexity] Query: {query}")
+ print(f"[Perplexity] Model: {completion.model}")
+
+ return result
- # Extract citation links (improved: includes complete metadata)
- if hasattr(completion, "citations") and completion.citations:
- for i, citation_url in enumerate(completion.citations, 1):
- citation_data = {
- "id": i,
- "reference": f"[{i}]",
- "url": citation_url,
- "title": "",
- "snippet": "",
- }
-
- # Try to match from search_results
- for search_item in result.get("search_results", []):
- if search_item.get("url") == citation_url:
- citation_data["title"] = search_item.get("title", "")
- citation_data["snippet"] = search_item.get("snippet", "")
- break
-
- result["citations"].append(citation_data)
-
- # Extract search result details
- if hasattr(completion, "search_results") and completion.search_results:
- for search_item in completion.search_results:
- search_result = {
- "title": search_item.title,
- "url": search_item.url,
- "date": search_item.date,
- "last_updated": search_item.last_updated,
- "snippet": search_item.snippet,
- "source": search_item.source,
- }
- result["search_results"].append(search_result)
+
+def web_search(
+ query: str,
+ output_dir: str | None = None,
+ verbose: bool = False,
+ # Baidu-specific options
+ baidu_model: str = "ernie-4.5-turbo-32k",
+ baidu_enable_deep_search: bool = False,
+ baidu_search_recency_filter: str = "week",
+) -> dict:
+ """
+ Perform network search using specified search provider and return results
+
+ Args:
+ query: Search query
+ output_dir: Output directory (optional, if provided will save results)
+ verbose: Whether to print detailed information
+ baidu_model: Model to use for Baidu AI Search (default: ernie-4.5-turbo-32k)
+ baidu_enable_deep_search: Enable deep search for Baidu (more comprehensive results)
+ baidu_search_recency_filter: Filter by recency for Baidu (week, month, semiyear, year)
+
+ Returns:
+ dict: Dictionary containing search results
+ {
+ "query": str,
+ "answer": str,
+ "provider": str,
+ "result_file": str (if file was saved)
+ }
+
+ Raises:
+ ImportError: If required module is not installed
+ ValueError: If required environment variable is not set
+ Exception: If API call fails
+ """
+ # Get search provider from environment variable
+ provider = os.environ.get("SEARCH_PROVIDER", "perplexity").lower()
+
+ try:
+ if provider == "baidu":
+ result = _search_with_baidu(
+ query=query,
+ model=baidu_model,
+ enable_deep_search=baidu_enable_deep_search,
+ search_recency_filter=baidu_search_recency_filter,
+ verbose=verbose,
+ )
+ elif provider == "perplexity":
+ result = _search_with_perplexity(query=query, verbose=verbose)
+ else:
+ raise ValueError(
+ f"Unsupported search provider: {provider}. Use 'perplexity' or 'baidu'."
+ )
# If output directory provided, save results
result_file = None
if output_dir:
os.makedirs(output_dir, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
- output_filename = f"search_{timestamp}.json"
+ output_filename = f"search_{provider}_{timestamp}.json"
output_path = os.path.join(output_dir, output_filename)
with open(output_path, "w", encoding="utf-8") as f:
@@ -148,13 +406,15 @@ def web_search(query: str, output_dir: str | None = None, verbose: bool = False)
result["result_file"] = result_file
if verbose:
+ answer = result.get("answer", "")
print(f"Query: {query}")
- print(f"Answer: {answer[:200]}..." if len(answer) > 200 else answer)
+ if answer:
+ print(f"Answer: {answer[:200]}..." if len(answer) > 200 else f"Answer: {answer}")
return result
except Exception as e:
- raise Exception(f"Perplexity API call failed: {e!s}")
+ raise Exception(f"{provider.capitalize()} API call failed: {e!s}")
if __name__ == "__main__":
@@ -165,8 +425,18 @@ if __name__ == "__main__":
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
- # Test
- result = web_search("What is a diffusion model?", output_dir="./test_output", verbose=True)
+ # Test with different providers
+ # Default: Perplexity
+ # result = web_search("What is a diffusion model?", output_dir="./test_output", verbose=True)
+
+ # Test with Baidu AI Search
+ result = web_search(
+ "What is a diffusion model?",
+ output_dir="./test_output",
+ verbose=True,
+ )
print("\nSearch completed!")
+ print(f"Provider: {result.get('provider', 'unknown')}")
print(f"Query: {result['query']}")
- print(f"Answer: {result['answer'][:300]}...")
+ answer = result.get("answer", "")
+ print(f"Answer: {answer[:300]}..." if len(answer) > 300 else f"Answer: {answer}")