Skip to main content

DuckDuckGo搜索

本笔记本提供了快速入门 DuckDuckGo 搜索的快速概览。如需查看所有 DuckDuckGo 搜索功能和配置的详细文档,请前往API 参考

DuckDuckGo 搜索提供了一款以隐私为中心的搜索 API,专为 LLM 代理设计。它能够与广泛的数据源无缝集成,在提供相关搜索结果的同时优先保护用户隐私。

概述

集成详情

类别PY 支持最新包
DuckDuckGo 搜索@langchain/communityNPM - 版本

安装设置

该集成位于@langchain/community包中,并依赖于duck-duck-scrape包:

:::提示 请参阅安装集成包的一般说明部分。 :::

yarn add @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

// This is usually generated by a model, but we'll create a tool call directly for demo purposes.
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"
}

链式调用

我们可以通过首先将工具绑定到一个工具调用模型,然后调用它,以实现链式调用:

Pick your chat model:

Install dependencies

yarn add @langchain/groq 

Add environment variables

GROQ_API_KEY=your-api-key

Instantiate the model

import { ChatGroq } from "@langchain/groq";

const llm = new ChatGroq({
model: "llama-3.3-70b-versatile",
temperature: 0
});
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 参考文档


Was this page helpful?


You can also leave detailed feedback on GitHub.