DuckDuckGoSearch
This notebook provides a quick overview for getting started with DuckDuckGoSearch. For detailed documentation of all DuckDuckGoSearch features and configurations head to the API reference.
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.
Overviewβ
Integration detailsβ
Class | Package | Serializable | PY support | Package latest |
---|---|---|---|---|
DuckDuckGoSearch | @langchain/community | beta | β |
Setupβ
The integration lives in the @langchain/community
package, along with
the duck-duck-scrape
dependency:
- npm
- yarn
- pnpm
npm i @langchain/community duck-duck-scrape
yarn add @langchain/community duck-duck-scrape
pnpm add @langchain/community duck-duck-scrape
Credentialsβ
Itβs also helpful (but not needed) to set up LangSmith for best-in-class observability:
process.env.LANGCHAIN_TRACING_V2 = "true";
process.env.LANGCHAIN_API_KEY = "your-api-key";
Instantiationβ
Here we show how to insatiate an instance of the DuckDuckGoSearch
tool, with
import { DuckDuckGoSearch } from "@langchain/community/tools/duckduckgo_search";
const tool = new DuckDuckGoSearch({ maxResults: 1 });
Invocationβ
Invoke directly with argsβ
await tool.invoke("What is Anthropic's estimated revenue for 2024?");
[{"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 ..."}]
Invoke with ToolCallβ
We can also invoke the tool with a model-generated ToolCall
, in which
case a ToolMessage
will be returned:
// This is usually generated by a model, but we'll create a tool call directly for demo purposes.
const modelGeneratedToolCall = {
args: {
input: "What is Anthropic's estimated revenue for 2024?",
},
id: "tool_call_id",
name: tool.name,
type: "tool_call",
};
await tool.invoke(modelGeneratedToolCall);
ToolMessage {
"content": "[{\"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 ...\"}]",
"name": "duckduckgo-search",
"additional_kwargs": {},
"response_metadata": {},
"tool_call_id": "tool_call_id"
}
Chainingβ
We can use our tool in a chain by first binding it to a tool-calling model and then calling it:
Pick your chat model:
- OpenAI
- Anthropic
- FireworksAI
- MistralAI
- Groq
- VertexAI
Install dependencies
- npm
- yarn
- pnpm
npm i @langchain/openai
yarn add @langchain/openai
pnpm add @langchain/openai
Add environment variables
OPENAI_API_KEY=your-api-key
Instantiate the model
import { ChatOpenAI } from "@langchain/openai";
const llm = new ChatOpenAI({
model: "gpt-4o-mini",
temperature: 0
});
Install dependencies
- npm
- yarn
- pnpm
npm i @langchain/anthropic
yarn add @langchain/anthropic
pnpm add @langchain/anthropic
Add environment variables
ANTHROPIC_API_KEY=your-api-key
Instantiate the model
import { ChatAnthropic } from "@langchain/anthropic";
const llm = new ChatAnthropic({
model: "claude-3-5-sonnet-20240620",
temperature: 0
});
Install dependencies
- npm
- yarn
- pnpm
npm i @langchain/community
yarn add @langchain/community
pnpm add @langchain/community
Add environment variables
FIREWORKS_API_KEY=your-api-key
Instantiate the model
import { ChatFireworks } from "@langchain/community/chat_models/fireworks";
const llm = new ChatFireworks({
model: "accounts/fireworks/models/llama-v3p1-70b-instruct",
temperature: 0
});
Install dependencies
- npm
- yarn
- pnpm
npm i @langchain/mistralai
yarn add @langchain/mistralai
pnpm add @langchain/mistralai
Add environment variables
MISTRAL_API_KEY=your-api-key
Instantiate the model
import { ChatMistralAI } from "@langchain/mistralai";
const llm = new ChatMistralAI({
model: "mistral-large-latest",
temperature: 0
});
Install dependencies
- npm
- yarn
- pnpm
npm i @langchain/groq
yarn add @langchain/groq
pnpm 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: "mixtral-8x7b-32768",
temperature: 0
});
Install dependencies
- npm
- yarn
- pnpm
npm i @langchain/google-vertexai
yarn add @langchain/google-vertexai
pnpm add @langchain/google-vertexai
Add environment variables
GOOGLE_APPLICATION_CREDENTIALS=credentials.json
Instantiate the model
import { ChatVertexAI } from "@langchain/google-vertexai";
const llm = new ChatVertexAI({
model: "gemini-1.5-flash",
temperature: 0
});
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { RunnableConfig } from "@langchain/core/runnables";
import { AIMessage } from "@langchain/core/messages";
const prompt = ChatPromptTemplate.fromMessages([
["system", "You are a helpful assistant."],
["human", "{user_input}"],
["placeholder", "{messages}"],
]);
// specifying tool_choice will force the model to call this tool.
const llmWithTools = llm.bindTools([tool], {
tool_choice: tool.name,
});
const llmChain = prompt.pipe(llmWithTools);
const toolChain = async (
userInput: string,
config?: RunnableConfig
): Promise<AIMessage> => {
const input_ = { user_input: userInput };
const aiMsg = await llmChain.invoke(input_, config);
const toolMsgs = await tool.batch(aiMsg.tool_calls, config);
return llmChain.invoke({ ...input_, messages: [aiMsg, ...toolMsgs] }, config);
};
const toolChainResult = await toolChain(
"What is Anthropic's estimated revenue for 2024?"
);
const { tool_calls, content } = toolChainResult;
console.log(
"AIMessage",
JSON.stringify(
{
tool_calls,
content,
},
null,
2
)
);
AIMessage {
"tool_calls": [
{
"name": "duckduckgo-search",
"args": {
"input": "Anthropic revenue 2024 forecast"
},
"type": "tool_call",
"id": "call_E22L1T1bI6xPrMtL8wrKW5C5"
}
],
"content": ""
}
With an agentβ
We can also pass the DuckDuckGoSearch
tool to 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 toolsForAgent = [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 promptForAgent = await pull<ChatPromptTemplate>(
"hwchase17/openai-functions-agent"
);
const llmForAgent = new ChatOpenAI({
model: "gpt-4-turbo-preview",
temperature: 0,
});
const agent = await createOpenAIFunctionsAgent({
llm: llmForAgent,
tools: toolsForAgent,
prompt: promptForAgent,
});
const agentExecutor = new AgentExecutor({
agent,
tools: toolsForAgent,
});
const agentResult = await agentExecutor.invoke({
input: "What is Anthropic's estimated revenue for 2024?",
});
console.log(agentResult);
{
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.'
}
API referenceβ
For detailed documentation of all DuckDuckGoSearch features and configurations head to the API reference