LangChain 是用于构建 LLM 支持的应用程序的最广泛使用的 Python 框架。将实时网络搜索添加到 LangChain 应用程序中,可以将其从依赖静态训练截止转变为访问实时信息。 langchain-savio 包提供了一个即插即用的 ScavioSearch 工具,兼容 LangChain 的工具接口、LCEL 链和代理执行器。本教程涵盖安装、配置和三种常见集成模式:独立工具调用、LCEL 链和 ReAct 代理。
前置条件
- Python 3.10 或更高版本
- pip install langchain langchain-scavio langchain-openai
- Scavio API 密钥
- 兼容 LangChain 的 LLM API 密钥
操作指南
步骤 1: 安装 langchain-scavio
安装集成包。它提供 ScavioSearch 作为 BaseTool 子类,具有可配置的平台、国家/地区和结果计数。
pip install langchain langchain-scavio langchain-openai步骤 2: 使用 ScadioSearch 作为独立工具
无需代理即可直接调用 ScavioSearch 来验证集成并检查返回的数据。
from langchain_scavio import ScavioSearch
tool = ScavioSearch(api_key="your_scavio_api_key", platform="google", country_code="us")
result = tool.invoke("latest LLM releases 2026")
print(result[:500]) # Returns formatted string of top results步骤 3: 添加到 LCEL 链
将 ScavioSearch 绑定为 LCEL 链中的上下文检索器,在生成答案之前获取搜索结果。
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
llm = ChatOpenAI(model="gpt-4o")
prompt = ChatPromptTemplate.from_template("Answer using this context:\n{context}\n\nQuestion: {question}")
chain = ({"context": tool, "question": lambda x: x}) | prompt | llm | StrOutputParser()
result = chain.invoke("What are the best Python AI libraries in 2026?")步骤 4: 绑定到工具调用代理
向工具调用代理注册 ScadioSearch,以便法学硕士可以决定何时进行搜索。
llm_with_tools = ChatOpenAI(model="gpt-4o").bind_tools([tool])
response = llm_with_tools.invoke("What is the current price of gold?")
print(response.tool_calls)Python 示例
import os
from langchain_scavio import ScavioSearch
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
os.environ["OPENAI_API_KEY"] = "your_openai_key"
tool = ScavioSearch(api_key=os.environ["SCAVIO_API_KEY"], platform="google", country_code="us", max_results=5)
llm = ChatOpenAI(model="gpt-4o", temperature=0)
prompt = ChatPromptTemplate.from_template(
"Use the following search results to answer the question.\nResults: {context}\nQuestion: {question}"
)
chain = ({"context": tool, "question": lambda x: x}) | prompt | llm | StrOutputParser()
if __name__ == "__main__":
answer = chain.invoke("What are the top Python libraries for building AI agents in 2026?")
print(answer)JavaScript 示例
const { ChatOpenAI } = require("@langchain/openai");
const { DynamicTool } = require("@langchain/core/tools");
const API_KEY = process.env.SCAVIO_API_KEY;
// langchain-scavio is Python-only; use DynamicTool in JS
const scavioTool = new DynamicTool({
name: "scavio_google_search",
description: "Search Google for current information. Returns top organic results.",
func: async (query) => {
const res = await fetch("https://api.scavio.dev/api/v1/search", {
method: "POST",
headers: { "x-api-key": API_KEY, "Content-Type": "application/json" },
body: JSON.stringify({ query, country_code: "us" })
});
const data = await res.json();
return (data.organic_results || []).slice(0, 5)
.map(r => `${r.title}: ${r.snippet || ""}`).join("\n");
}
});
async function main() {
const result = await scavioTool.invoke("best Python AI libraries 2026");
console.log(result);
}
main().catch(console.error);预期输出
Answer: Based on 2026 search results, the top Python AI libraries include:
1. LangChain — agent orchestration and LLM chains
2. LlamaIndex — data framework for LLM apps
3. CrewAI — multi-agent coordination
4. Haystack — production RAG pipelines
5. Pydantic AI — structured output agents