Skip to content

Commit

Permalink
Add Tavily template, update template (#6416)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 authored Aug 6, 2024
1 parent d528235 commit f31cabc
Show file tree
Hide file tree
Showing 5 changed files with 378 additions and 160 deletions.
5 changes: 5 additions & 0 deletions docs/core_docs/docs/integrations/tools/aiplugin-tool.mdx
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
---
hide_table_of_contents: true
sidebar_class_name: hidden
---

import CodeBlock from "@theme/CodeBlock";
import Example from "@examples/agents/aiplugin-tool.ts";

# ChatGPT Plugins

:::warning
OpenAI has [deprecated plugins](https://openai.com/index/chatgpt-plugins/).
:::

This example shows how to use ChatGPT Plugins within LangChain abstractions.

Note 1: This currently only works for plugins with no auth.
Expand Down
138 changes: 34 additions & 104 deletions docs/core_docs/docs/integrations/tools/duckduckgo_search.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
"\n",
"### Integration details\n",
"\n",
"| Class | Package | Serializable | [PY support](https://python.langchain.com/docs/integrations/tools/ddg/) | Package latest |\n",
"| Class | Package | [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) | ❌ | ✅ | ![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://www.npmjs.com/package/@langchain/community) | ✅ | ![NPM - Version](https://img.shields.io/npm/v/@langchain/community?style=flat-square&label=%20&) |\n",
"\n",
"## Setup\n",
"\n",
Expand Down Expand Up @@ -65,7 +65,7 @@
"source": [
"## Instantiation\n",
"\n",
"Here we show how to insatiate an instance of the `DuckDuckGoSearch` tool, with "
"You can instantiate an instance of the `DuckDuckGoSearch` tool like this:"
]
},
{
Expand Down Expand Up @@ -100,12 +100,12 @@
"name": "stdout",
"output_type": "stream",
"text": [
"[{\"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"
"[{\"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.\"}]\n"
]
}
],
"source": [
"await tool.invoke(\"What is Anthropic's estimated revenue for 2024?\")"
"await tool.invoke(\"what is the current weather in sf?\")"
]
},
{
Expand All @@ -129,7 +129,7 @@
"output_type": "stream",
"text": [
"ToolMessage {\n",
" \"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 ...\\\"}]\",\n",
" \"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.\\\"}]\",\n",
" \"name\": \"duckduckgo-search\",\n",
" \"additional_kwargs\": {},\n",
" \"response_metadata\": {},\n",
Expand All @@ -142,7 +142,7 @@
"// This is usually generated by a model, but we'll create a tool call directly for demo purposes.\n",
"const modelGeneratedToolCall = {\n",
" args: {\n",
" input: \"What is Anthropic's estimated revenue for 2024?\"\n",
" input: \"what is the current weather in sf?\"\n",
" },\n",
" id: \"tool_call_id\",\n",
" name: tool.name,\n",
Expand Down Expand Up @@ -185,43 +185,45 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 10,
"id": "fdbf35b5-3aaf-4947-9ec6-48c21533fb95",
"metadata": {},
"outputs": [],
"source": [
"import { ChatPromptTemplate } from \"@langchain/core/prompts\"\n",
"import { RunnableConfig } from \"@langchain/core/runnables\"\n",
"import { AIMessage } from \"@langchain/core/messages\"\n",
"import { HumanMessage } from \"@langchain/core/messages\";\n",
"import { ChatPromptTemplate } from \"@langchain/core/prompts\";\n",
"import { RunnableLambda } from \"@langchain/core/runnables\";\n",
"\n",
"const prompt = ChatPromptTemplate.fromMessages(\n",
" [\n",
" [\"system\", \"You are a helpful assistant.\"],\n",
" [\"human\", \"{user_input}\"],\n",
" [\"placeholder\", \"{messages}\"],\n",
" ]\n",
")\n",
"\n",
"// specifying tool_choice will force the model to call this tool.\n",
"const llmWithTools = llm.bindTools([tool], {\n",
" tool_choice: tool.name\n",
"})\n",
"const llmWithTools = llm.bindTools([tool]);\n",
"\n",
"const llmChain = prompt.pipe(llmWithTools);\n",
"const chain = prompt.pipe(llmWithTools);\n",
"\n",
"const toolChain = async (userInput: string, config?: RunnableConfig): Promise<AIMessage> => {\n",
" const input_ = { user_input: userInput };\n",
" const aiMsg = await llmChain.invoke(input_, config);\n",
" const toolMsgs = await tool.batch(aiMsg.tool_calls, config);\n",
" return llmChain.invoke({ ...input_, messages: [aiMsg, ...toolMsgs] }, config);\n",
"};\n",
"const toolChain = RunnableLambda.from(\n",
" async (userInput: string, config) => {\n",
" const humanMessage = new HumanMessage(userInput,);\n",
" const aiMsg = await chain.invoke({\n",
" messages: [new HumanMessage(userInput)],\n",
" }, config);\n",
" const toolMsgs = await tool.batch(aiMsg.tool_calls, config);\n",
" return chain.invoke({\n",
" messages: [humanMessage, aiMsg, ...toolMsgs],\n",
" }, config);\n",
" }\n",
");\n",
"\n",
"const toolChainResult = await toolChain(\"What is Anthropic's estimated revenue for 2024?\");"
"const toolChainResult = await toolChain.invoke(\"how many people have climbed mount everest?\");"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 11,
"id": "28448fe2",
"metadata": {},
"outputs": [
Expand All @@ -230,17 +232,8 @@
"output_type": "stream",
"text": [
"AIMessage {\n",
" \"tool_calls\": [\n",
" {\n",
" \"name\": \"duckduckgo-search\",\n",
" \"args\": {\n",
" \"input\": \"Anthropic revenue 2024 forecast\"\n",
" },\n",
" \"type\": \"tool_call\",\n",
" \"id\": \"call_E22L1T1bI6xPrMtL8wrKW5C5\"\n",
" }\n",
" ],\n",
" \"content\": \"\"\n",
" \"tool_calls\": [],\n",
" \"content\": \"As of December 2023, a total of 6,664 different people have reached the summit of Mount Everest.\"\n",
"}\n"
]
}
Expand All @@ -250,81 +243,18 @@
"\n",
"console.log(\"AIMessage\", JSON.stringify({\n",
" tool_calls,\n",
" content\n",
"}, null, 2))"
" content,\n",
"}, null, 2));"
]
},
{
"cell_type": "markdown",
"id": "71ceafa7",
"metadata": {},
"source": [
"## With an agent\n",
"\n",
"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": 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",
"id": "570f4662",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[\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": [
"const exampleQuery = \"What is Anthropic's estimated revenue for 2024?\"\n",
"\n",
"const events = await agentExecutor.stream(\n",
" { messages: [[\"user\", exampleQuery]]},\n",
" { streamMode: \"values\", }\n",
")\n",
"## Agents\n",
"\n",
"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",
"}"
"For guides on how to use LangChain tools in agents, see the [LangGraph.js](https://langchain-ai.github.io/langgraphjs/) docs."
]
},
{
Expand Down
Loading

0 comments on commit f31cabc

Please sign in to comment.