Skip to content

Commit

Permalink
docs[patch]: Refactor duckduckgo doc to use langgraph agent (#6415)
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul authored Aug 6, 2024
1 parent 4877190 commit 3c69464
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 42 deletions.
2 changes: 0 additions & 2 deletions docs/core_docs/docs/integrations/toolkits/openapi.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,6 @@
"source": [
"import { createReactAgent } from \"@langchain/langgraph/prebuilt\"\n",
"\n",
"import { ChatOpenAI } from \"@langchain/openai\";\n",
"\n",
"const agentExecutor = createReactAgent({ llm, tools });"
]
},
Expand Down
89 changes: 49 additions & 40 deletions docs/core_docs/docs/integrations/tools/duckduckgo_search.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"\n",
"| Class | Package | Serializable | [PY support](https://python.langchain.com/docs/integrations/tools/ddg/) | Package latest |\n",
"| :--- | :--- | :---: | :---: | :---: |\n",
"| [DuckDuckGoSearch](https://api.js.langchain.com/classes/langchain_community_tools_duckduckgo_search.DuckDuckGoSearch.html) | [@langchain/community](https://api.js.langchain.com/modules/langchain_community_tools_duckduckgo_search.html) | beta | ✅ | ![NPM - Version](https://img.shields.io/npm/v/@langchain/community?style=flat-square&label=%20&) |\n",
"| [DuckDuckGoSearch](https://api.js.langchain.com/classes/langchain_community_tools_duckduckgo_search.DuckDuckGoSearch.html) | [@langchain/community](https://api.js.langchain.com/modules/langchain_community_tools_duckduckgo_search.html) | | ✅ | ![NPM - Version](https://img.shields.io/npm/v/@langchain/community?style=flat-square&label=%20&) |\n",
"\n",
"## Setup\n",
"\n",
Expand Down Expand Up @@ -169,7 +169,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 1,
"id": "af3123ad-7a02-40e5-b58e-7d56e23e5830",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -261,61 +261,70 @@
"source": [
"## With an agent\n",
"\n",
"We can also pass the `DuckDuckGoSearch` tool to an agent:"
"We can also pass the `DuckDuckGoSearch` tool to an agent. First, ensure you have the LangGraph package installed.\n",
"\n",
"```{=mdx}\n",
"<Npm2Yarn>\n",
" @langchain/langgraph\n",
"</Npm2Yarn>\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 3,
"id": "77a05a61",
"metadata": {},
"outputs": [],
"source": [
"import { DuckDuckGoSearch } from \"@langchain/community/tools/duckduckgo_search\";\n",
"import { createReactAgent } from \"@langchain/langgraph/prebuilt\";\n",
"\n",
"// Define the tools the agent will have access to.\n",
"const toolsForAgent = [new DuckDuckGoSearch({ maxResults: 1 })];\n",
"\n",
"const agentExecutor = createReactAgent({ llm, tools: toolsForAgent });"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "3130e547",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" input: \"What is Anthropic's estimated revenue for 2024?\",\n",
" output: 'Anthropic has projected that it will generate more than $850 million in annualized revenue by the end of 2024.'\n",
"}\n"
"[\n",
" {\n",
" name: 'duckduckgo-search',\n",
" args: { input: 'Anthropic estimated revenue 2024' },\n",
" type: 'tool_call',\n",
" id: 'call_eZwbyemMAu8tgQw4VqJs65hF'\n",
" }\n",
"]\n",
"[{\"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 ...\"}]\n",
"Anthropic is projected to generate more than $850 million in annualized revenue by the end of 2024.\n"
]
}
],
"source": [
"import { DuckDuckGoSearch } from \"@langchain/community/tools/duckduckgo_search\";\n",
"import { ChatOpenAI } from \"@langchain/openai\";\n",
"import type { ChatPromptTemplate } from \"@langchain/core/prompts\";\n",
"\n",
"import { pull } from \"langchain/hub\";\n",
"import { AgentExecutor, createOpenAIFunctionsAgent } from \"langchain/agents\";\n",
"const exampleQuery = \"What is Anthropic's estimated revenue for 2024?\"\n",
"\n",
"// Define the tools the agent will have access to.\n",
"const toolsForAgent = [new DuckDuckGoSearch({ maxResults: 1 })];\n",
"const events = await agentExecutor.stream(\n",
" { messages: [[\"user\", exampleQuery]]},\n",
" { streamMode: \"values\", }\n",
")\n",
"\n",
"// Get the prompt to use - you can modify this!\n",
"// If you want to see the prompt in full, you can at:\n",
"// https://smith.langchain.com/hub/hwchase17/openai-functions-agent\n",
"const promptForAgent = await pull<ChatPromptTemplate>(\n",
" \"hwchase17/openai-functions-agent\"\n",
");\n",
"const llmForAgent = new ChatOpenAI({\n",
" model: \"gpt-4-turbo-preview\",\n",
" temperature: 0,\n",
"});\n",
"const agent = await createOpenAIFunctionsAgent({\n",
" llm: llmForAgent,\n",
" tools: toolsForAgent,\n",
" prompt: promptForAgent,\n",
"});\n",
"const agentExecutor = new AgentExecutor({\n",
" agent,\n",
" tools: toolsForAgent,\n",
"});\n",
"const agentResult = await agentExecutor.invoke({\n",
" input: \"What is Anthropic's estimated revenue for 2024?\",\n",
"});\n",
"\n",
"console.log(agentResult);"
"for await (const event of events) {\n",
" const lastMsg = event.messages[event.messages.length - 1];\n",
" if (lastMsg.tool_calls?.length) {\n",
" console.dir(lastMsg.tool_calls, { depth: null });\n",
" } else if (lastMsg.content) {\n",
" console.log(lastMsg.content);\n",
" }\n",
"}"
]
},
{
Expand Down

0 comments on commit 3c69464

Please sign in to comment.