跳至主要內容

網頁搜尋整合

為任何 LLM 提供者啟用透明的伺服器端網路搜尋執行。LiteLLM 會自動攔截網路搜尋工具呼叫,並使用您設定的搜尋提供者(Perplexity、Tavily 等)來執行。

快速開始

1. 設定網路搜尋攔截

加入到您的 config.yaml

model_list:
- model_name: gpt-4o
litellm_params:
model: openai/gpt-4o
api_key: os.environ/OPENAI_API_KEY

litellm_settings:
callbacks: ["websearch_interception"]
websearch_interception_params:
enabled_providers:
- openai
- minimax
- anthropic
search_tool_name: perplexity-search # Optional

search_tools:
- search_tool_name: perplexity-search
litellm_params:
search_provider: perplexity
api_key: os.environ/PERPLEXITY_API_KEY

2. 搭配任何提供者使用

import litellm

response = await litellm.acompletion(
model="gpt-4o",
messages=[
{"role": "user", "content": "What's the weather in San Francisco today?"}
],
tools=[
{
"type": "function",
"function": {
"name": "litellm_web_search",
"description": "Search the web for information",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query"}
},
"required": ["query"]
}
}
}
]
)

# Response includes search results automatically!
print(response.choices[0].message.content)

運作方式

當模型發出網路搜尋工具呼叫時,LiteLLM 會:

  1. 偵測回應中的 litellm_web_search 工具呼叫
  2. 使用您設定的搜尋提供者執行搜尋
  3. 發出後續請求並帶上搜尋結果
  4. 最終答案回傳給使用者

結果:使用者一次 API 呼叫 → 含搜尋結果的完整答案

支援的提供者

網路搜尋整合適用於使用以下方式的所有提供者

  • Base HTTP Handler (BaseLLMHTTPHandler)
  • OpenAI Completion Handler (OpenAIChatCompletion)

使用 Base HTTP Handler 的提供者

提供者狀態備註
OpenAI✅ 支援GPT-4、GPT-3.5 等
Anthropic✅ 支援透過 HTTP handler 的 Claude 模型
MiniMax✅ 支援所有 MiniMax 模型
Mistral✅ 支援Mistral AI 模型
Cohere✅ 支援Command 模型
Fireworks AI✅ 支援所有 Fireworks 模型
Together AI✅ 支援所有 Together AI 模型
Groq✅ 支援所有 Groq 模型
Perplexity✅ 支援Perplexity 模型
DeepSeek✅ 支援DeepSeek 模型
xAI✅ 支援Grok 模型
Hugging Face✅ 支援推論 API 模型
OCI✅ 支援Oracle Cloud 模型
Vertex AI✅ 支援Google Vertex AI 模型
Bedrock✅ 支援AWS Bedrock 模型(converse_like 路由)
Azure OpenAI✅ 支援Azure 託管的 OpenAI 模型
Sagemaker✅ 支援AWS Sagemaker 模型
Databricks✅ 支援Databricks 模型
DataRobot✅ 支援DataRobot 模型
Hosted VLLM✅ 支援自架 VLLM
Heroku✅ 支援Heroku 託管的模型
RAGFlow✅ 支援RAGFlow 模型
Compactif✅ 支援Compactif 模型
Cometapi✅ 支援Comet API 模型
A2A✅ 支援Agent-to-Agent 模型
Bytez✅ 支援Bytez 模型

使用 OpenAI Handler 的提供者

提供者狀態備註
OpenAI✅ 支援原生 OpenAI API
Azure OpenAI✅ 支援Azure 託管的 OpenAI
OpenAI-Compatible✅ 支援任何相容 OpenAI 的 API

設定

WebSearch 攔截參數

參數類型必填說明範例
enabled_providersList[String]要啟用網路搜尋的提供者清單[openai, minimax, anthropic]
search_tool_nameString來自 search_tools 設定的特定搜尋工具。若未設定,則使用第一個可用項目。perplexity-search

提供者值

enabled_providers 中使用這些值:

提供者提供者
OpenAIopenaiAnthropicanthropic
MiniMaxminimaxMistralmistral
CoherecohereFireworks AIfireworks_ai
Together AItogether_aiGroqgroq
PerplexityperplexityDeepSeekdeepseek
xAIxaiHugging Facehuggingface
OCIociVertex AIvertex_ai
BedrockbedrockAzureazure
Sagemakersagemaker_chatDatabricksdatabricks
DataRobotdatarobotVLLMhosted_vllm
HerokuherokuRAGFlowragflow
CompactifcompactifCometapicometapi
A2Aa2aBytezbytez

搜尋提供者

設定要使用的搜尋提供者。LiteLLM 支援多個搜尋提供者:

提供者search_provider環境變數
Perplexity AIperplexityPERPLEXITYAI_API_KEY
TavilytavilyTAVILY_API_KEY
Exa AIexa_aiEXA_API_KEY
Brave SearchbraveBRAVE_API_KEY
Parallel AIparallel_aiPARALLEL_AI_API_KEY
Google PSEgoogle_pseGOOGLE_PSE_API_KEY, GOOGLE_PSE_ENGINE_ID
DataForSEOdataforseoDATAFORSEO_LOGIN, DATAFORSEO_PASSWORD
FirecrawlfirecrawlFIRECRAWL_API_KEY
SearXNGsearxngSEARXNG_API_BASE(必填)
LinkuplinkupLINKUP_API_KEY
SerperserperSERPER_API_KEY
SearchAPI.iosearchapiSEARCHAPI_API_KEY

請參閱 搜尋提供者文件 以取得詳細設定說明。

完整設定範例

model_list:
# OpenAI
- model_name: gpt-4o
litellm_params:
model: openai/gpt-4o
api_key: os.environ/OPENAI_API_KEY

# MiniMax
- model_name: minimax
litellm_params:
model: minimax/MiniMax-M2.1
api_key: os.environ/MINIMAX_API_KEY

# Anthropic
- model_name: claude
litellm_params:
model: anthropic/claude-sonnet-4-5
api_key: os.environ/ANTHROPIC_API_KEY

# Azure OpenAI
- model_name: azure-gpt4
litellm_params:
model: azure/gpt-4
api_base: https://my-azure.openai.azure.com
api_key: os.environ/AZURE_API_KEY

litellm_settings:
callbacks: ["websearch_interception"]
websearch_interception_params:
enabled_providers:
- openai
- minimax
- anthropic
- azure
search_tool_name: perplexity-search

search_tools:
- search_tool_name: perplexity-search
litellm_params:
search_provider: perplexity
api_key: os.environ/PERPLEXITY_API_KEY

- search_tool_name: tavily-search
litellm_params:
search_provider: tavily
api_key: os.environ/TAVILY_API_KEY

使用範例

Python SDK

import litellm

# Configure callbacks
litellm.callbacks = ["websearch_interception"]

# Make completion with web search tool
response = await litellm.acompletion(
model="gpt-4o",
messages=[
{"role": "user", "content": "What are the latest AI news?"}
],
tools=[
{
"type": "function",
"function": {
"name": "litellm_web_search",
"description": "Search the web for current information",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Search query"
}
},
"required": ["query"]
}
}
}
]
)

print(response.choices[0].message.content)

Proxy 伺服器

# Start proxy with config
litellm --config config.yaml

# Make request
curl http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "What is the weather in San Francisco?"}
],
"tools": [
{
"type": "function",
"function": {
"name": "litellm_web_search",
"description": "Search the web",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string"}
},
"required": ["query"]
}
}
}
]
}'

搜尋工具選擇的運作方式

  1. 如果指定了 search_tool_name → 使用該特定搜尋工具
  2. 如果未指定 search_tool_name → 使用 search_tools 清單中的第一個搜尋工具
search_tools:
- search_tool_name: perplexity-search # ← This will be used if no search_tool_name specified
litellm_params:
search_provider: perplexity
api_key: os.environ/PERPLEXITY_API_KEY

- search_tool_name: tavily-search
litellm_params:
search_provider: tavily
api_key: os.environ/TAVILY_API_KEY

疑難排解

網路搜尋無法運作

  1. 檢查已啟用提供者

    enabled_providers:
    - openai # Make sure your provider is in this list
  2. 確認已設定搜尋工具

    search_tools:
    - search_tool_name: perplexity-search
    litellm_params:
    search_provider: perplexity
    api_key: os.environ/PERPLEXITY_API_KEY
  3. 檢查 API 金鑰是否已設定

    export PERPLEXITY_API_KEY=your-key
  4. 啟用除錯記錄

    litellm.set_verbose = True

常見問題

問題:模型回傳 tool_calls 而不是最終答案

  • 原因:提供者不在 enabled_providers 清單中
  • 解決方案:將提供者加入 enabled_providers

問題:"No search tool configured" 錯誤

  • 原因search_tools 設定中沒有搜尋工具
  • 解決方案:至少新增一個搜尋工具設定

問題:"Invalid function arguments json string" 錯誤(MiniMax)

  • 原因:已在最新版修正-arguments 未正確序列化為 JSON
  • 解決方案:更新至最新 LiteLLM 版本

技術細節

架構

網路搜尋整合是以自訂回呼(WebSearchInterceptionLogger)實作,該回呼會:

  1. 請求前 Hook:將原生網路搜尋工具轉換為 LiteLLM 標準格式
  2. 回應後 Hook:偵測回應中的網路搜尋工具呼叫
  3. 代理式迴圈:自動執行搜尋並發出後續請求

支援的 API

  • Chat Completions API(OpenAI 格式)
  • Anthropic Messages API(Anthropic 格式)
  • 串流(自動轉換)
  • 非串流

回應格式偵測

處理器會自動偵測回應格式:

  • OpenAI 格式:assistant 訊息中的 tool_calls
  • Anthropic 格式:content 中的 tool_use 區塊

效能

  • 延遲:增加一次額外的 LLM 呼叫(帶有搜尋結果的後續請求)
  • 快取:搜尋結果可被快取(取決於搜尋提供者)
  • 平行搜尋:多個搜尋查詢可平行執行

貢獻

發現錯誤或想要新增對新提供者的支援?請參閱我們的 貢獻指南