Skip to main content

DuckDuckGoSearch

DuckDuckGoSearch offers a privacy-focused search API designed for LLM Agents. It provides seamless integration with a wide range of data sources, prioritizing user privacy and relevant search results.

Setup

Install the @langchain/community package, along with the duck-duck-scrape dependency:

npm install @langchain/community
npm install duck-duck-scrape

Usage

You can call .invoke on DuckDuckGoSearch to search for a query:

import { DuckDuckGoSearch } from "@langchain/community/tools/duckduckgo_search";

// Instantiate the DuckDuckGoSearch tool.
const tool = new DuckDuckGoSearch({ maxResults: 1 });

// Get the results of a query by calling .invoke on the tool.
const result = await tool.invoke(
"What is Anthropic's estimated revenue for 2024?"
);

console.log(result);
/*
[{
"title": "Anthropic forecasts more than $850 mln in annualized revenue rate by ...",
"link": "https://www.reuters.com/technology/anthropic-forecasts-more-than-850-mln-annualized-revenue-rate-by-2024-end-report-2023-12-26/",
"snippet": "Dec 26 (Reuters) - Artificial intelligence startup <b>Anthropic</b> has projected it will generate more than $850 million in annualized <b>revenue</b> by the end of <b>2024</b>, the Information reported on Tuesday ..."
}]
*/

API Reference:

tip

See the LangSmith trace here

With an agent

import { DuckDuckGoSearch } from "@langchain/community/tools/duckduckgo_search";
import { ChatOpenAI } from "@langchain/openai";
import type { ChatPromptTemplate } from "@langchain/core/prompts";

import { pull } from "langchain/hub";
import { AgentExecutor, createOpenAIFunctionsAgent } from "langchain/agents";

// Define the tools the agent will have access to.
const tools = [new DuckDuckGoSearch({ maxResults: 1 })];

// Get the prompt to use - you can modify this!
// If you want to see the prompt in full, you can at:
// https://smith.langchain.com/hub/hwchase17/openai-functions-agent
const prompt = await pull<ChatPromptTemplate>(
"hwchase17/openai-functions-agent"
);
const llm = new ChatOpenAI({
model: "gpt-4-turbo-preview",
temperature: 0,
});
const agent = await createOpenAIFunctionsAgent({
llm,
tools,
prompt,
});
const agentExecutor = new AgentExecutor({
agent,
tools,
});
const result = await agentExecutor.invoke({
input: "What is Anthropic's estimated revenue for 2024?",
});

console.log(result);
/*
{
input: "What is Anthropic's estimated revenue for 2024?",
output: 'Anthropic has projected that it will generate more than $850 million in annualized revenue by the end of 2024. For more details, you can refer to the [Reuters article](https://www.reuters.com/technology/anthropic-forecasts-more-than-850-mln-annualized-revenue-rate-by-2024-end-report-2023-12-26/).'
}
*/

API Reference:

tip

See the LangSmith trace for the Agent example here


Was this page helpful?


You can also leave detailed feedback on GitHub.