组件接入 · LANGCHAIN
LangChain 搜索工具接入指南
在 LangChain Python 与 TypeScript 中接入 Annolux 中英双语搜索 API:自带来源归属与明确抓取时间。
Python 接入方式
使用 LangChain 标准 tool 方法封装 Annolux 搜索接口。每条结果都带有 URL、排序、干净摘要与明确抓取时间。
import os
import requests
from langchain_core.tools import tool
@tool
def annolux_search(query: str, limit: int = 10) -> str:
"""Search Annolux curated English and Chinese index with explicit fetched_at timestamps."""
api_key = os.environ.get("ANNOLUX_API_KEY", "")
res = requests.post(
"https://api.annolux.com/api/v1/search",
headers={"Authorization": f"Bearer {api_key}"},
json={"query": query, "limit": limit, "deduplicate": True},
timeout=30,
)
if not res.ok:
return f"Search error: {res.status_code}"
return res.text
TypeScript 接入方式
在 TypeScript 与 Node.js 工作流中,使用 @langchain/core 与 Zod 定义结构化工具。
import { tool } from "@langchain/core/tools";
import { z } from "zod";
export const annoluxSearchTool = tool(
async ({ query, limit = 10 }) => {
const res = await fetch("https://api.annolux.com/api/v1/search", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${process.env.ANNOLUX_API_KEY}`
},
body: JSON.stringify({ query, limit, deduplicate: true })
});
return await res.text();
},
{
name: "annolux_search",
description: "Search Annolux curated English and Chinese index.",
schema: z.object({
query: z.string().describe("Search query"),
limit: z.number().optional().default(10)
})
}
);
在 Agent 中使用搜索
将工具绑定到 LangGraph 或 React Agent。模型发起检索时会得到带来源归属的结果,避免引用的时间产生错误。
边界与点数
search_web 查询精选中英索引。只有成功的 2xx 搜索扣 1 点,错误和超时不扣。新账户有 1,000 免费点数。