Documentation Index
Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
Use this file to discover all available pages before exploring further.
本笔记本提供了快速入门 DuckDuckGoSearch 工具 的概述。有关 DuckDuckGoSearch 所有功能和配置的详细文档,请参阅 API 参考。
DuckDuckGoSearch 提供了一个专注于隐私的搜索 API,专为 LLM 智能体设计。它能够无缝集成多种数据源,同时优先考虑用户隐私和相关的搜索结果。
集成详情
该集成位于 @langchain/community 包中,同时需要 duck-duck-scrape 依赖:
npm install @langchain/community @langchain/core duck-duck-scrape
设置 LangSmith 以获得最佳的观测能力也很有帮助(但不是必需的):
process.env.LANGSMITH_TRACING="true"
process.env.LANGSMITH_API_KEY="your-api-key"
实例化
你可以像这样实例化一个 DuckDuckGoSearch 工具:
import { DuckDuckGoSearch } from "@langchain/community/tools/duckduckgo_search"
const tool = new DuckDuckGoSearch({ maxResults: 1 })
await tool.invoke("what is the current weather in sf?")
[{"title":"San Francisco, CA Current Weather | AccuWeather","link":"https://www.accuweather.com/en/us/san-francisco/94103/current-weather/347629","snippet":"<b>Current</b> <b>weather</b> <b>in</b> San Francisco, CA. Check <b>current</b> conditions in San Francisco, CA with radar, hourly, and more."}]
我们也可以使用模型生成的 ToolCall 来调用工具,此时将返回一个 ToolMessage:
// 这通常由模型生成,但为了演示目的,我们直接创建一个工具调用。
const modelGeneratedToolCall = {
args: {
input: "what is the current weather in sf?"
},
id: "tool_call_id",
name: tool.name,
type: "tool_call",
}
await tool.invoke(modelGeneratedToolCall)
ToolMessage {
"content": "[{\"title\":\"San Francisco, CA Weather Conditions | Weather Underground\",\"link\":\"https://www.wunderground.com/weather/us/ca/san-francisco\",\"snippet\":\"San Francisco <b>Weather</b> Forecasts. <b>Weather</b> Underground provides local & long-range <b>weather</b> forecasts, weatherreports, maps & tropical <b>weather</b> conditions for the San Francisco area.\"}]",
"name": "duckduckgo-search",
"additional_kwargs": {},
"response_metadata": {},
"tool_call_id": "tool_call_id"
}
链式调用
我们可以通过先将工具绑定到一个 工具调用模型,然后在链中使用它:
// @lc-docs-hide-cell
import { ChatOpenAI } from "@langchain/openai"
const llm = new ChatOpenAI({
model: "gpt-4.1-mini",
})
import { HumanMessage } from "@langchain/core/messages";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { RunnableLambda } from "@langchain/core/runnables";
const prompt = ChatPromptTemplate.fromMessages(
[
["system", "You are a helpful assistant."],
["placeholder", "{messages}"],
]
)
const llmWithTools = llm.bindTools([tool]);
const chain = prompt.pipe(llmWithTools);
const toolChain = RunnableLambda.from(
async (userInput: string, config) => {
const humanMessage = new HumanMessage(userInput,);
const aiMsg = await chain.invoke({
messages: [new HumanMessage(userInput)],
}, config);
const toolMsgs = await tool.batch(aiMsg.tool_calls, config);
return chain.invoke({
messages: [humanMessage, aiMsg, ...toolMsgs],
}, config);
}
);
const toolChainResult = await toolChain.invoke("how many people have climbed mount everest?");
const { tool_calls, content } = toolChainResult;
console.log("AIMessage", JSON.stringify({
tool_calls,
content,
}, null, 2));
AIMessage {
"tool_calls": [],
"content": "As of December 2023, a total of 6,664 different people have reached the summit of Mount Everest."
}
智能体
有关如何在智能体中使用 LangChain 工具的指南,请参阅 LangGraph.js 文档。
API 参考
有关 DuckDuckGoSearch 所有功能和配置的详细文档,请参阅 API 参考