Files
langchain/docs/versioned_docs/version-0.2.x/how_to/tools_parallel.ipynb
Harrison Chase 66b2ac62eb cr
2024-04-24 17:07:56 -07:00

216 lines
5.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "95982bf1-7d9d-4dd6-a4ad-9de0719fe17f",
"metadata": {},
"source": [
"# How to call tools in parallel\n",
"\n",
"In the [Chains with multiple tools](/docs/use_cases/tool_use/multiple_tools) guide we saw how to build function-calling chains that select between multiple tools. Some models, like the OpenAI models released in Fall 2023, also support parallel function calling, which allows you to invoke multiple functions (or the same function multiple times) in a single model call. Our previous chain from the multiple tools guides actually already supports this."
]
},
{
"cell_type": "markdown",
"id": "3fafec38-443a-42ad-a913-5be7667e3734",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"We'll need to install the following packages for this guide:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "78411bf1-0117-4f33-a3d7-f3d77a97bb78",
"metadata": {},
"outputs": [],
"source": [
"%pip install --upgrade --quiet langchain-core"
]
},
{
"cell_type": "markdown",
"id": "59d08fd0-ddd9-4c74-bcea-a5ca3a86e542",
"metadata": {},
"source": [
"If you'd like to trace your runs in [LangSmith](/docs/langsmith/) uncomment and set the following environment variables:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4185e74b-0500-4cad-ace0-bac37de466ac",
"metadata": {},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
"# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass()"
]
},
{
"cell_type": "markdown",
"id": "d28159f5-b7d0-4385-aa44-4cd1b64507bb",
"metadata": {},
"source": [
"## Tools"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e13ec98c-8521-4d63-b521-caf92da87b70",
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.tools import tool\n",
"\n",
"\n",
"@tool\n",
"def multiply(first_int: int, second_int: int) -> int:\n",
" \"\"\"Multiply two integers together.\"\"\"\n",
" return first_int * second_int\n",
"\n",
"\n",
"@tool\n",
"def add(first_int: int, second_int: int) -> int:\n",
" \"Add two integers.\"\n",
" return first_int + second_int\n",
"\n",
"\n",
"@tool\n",
"def exponentiate(base: int, exponent: int) -> int:\n",
" \"Exponentiate the base to the exponent power.\"\n",
" return base**exponent"
]
},
{
"cell_type": "markdown",
"id": "119d419c-1c61-4e0d-834a-5dabb72f5514",
"metadata": {},
"source": [
"# Chain\n",
"\n",
"```{=mdx}\n",
"import ChatModelTabs from \"@theme/ChatModelTabs\";\n",
"\n",
"<ChatModelTabs customVarName=\"llm\" hideGoogle=\"true\"/>\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "f67d91d8-cc38-4065-8f80-901e079954dd",
"metadata": {},
"outputs": [],
"source": [
"# | echo: false\n",
"# | output: false\n",
"\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c35359ae-a740-48c5-b5e7-1a377fb25aa2",
"metadata": {},
"outputs": [],
"source": [
"from operator import itemgetter\n",
"from typing import Dict, List, Union\n",
"\n",
"from langchain_core.messages import AIMessage\n",
"from langchain_core.runnables import (\n",
" Runnable,\n",
" RunnableLambda,\n",
" RunnableMap,\n",
" RunnablePassthrough,\n",
")\n",
"\n",
"tools = [multiply, exponentiate, add]\n",
"llm_with_tools = llm.bind_tools(tools)\n",
"tool_map = {tool.name: tool for tool in tools}\n",
"\n",
"\n",
"def call_tools(msg: AIMessage) -> Runnable:\n",
" \"\"\"Simple sequential tool calling helper.\"\"\"\n",
" tool_map = {tool.name: tool for tool in tools}\n",
" tool_calls = msg.tool_calls.copy()\n",
" for tool_call in tool_calls:\n",
" tool_call[\"output\"] = tool_map[tool_call[\"name\"]].invoke(tool_call[\"args\"])\n",
" return tool_calls\n",
"\n",
"\n",
"chain = llm_with_tools | call_tools"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "ea6dbb32-ec9b-4c70-a90f-a2db93978cf1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'name': 'multiply',\n",
" 'args': {'first_int': 23, 'second_int': 7},\n",
" 'id': 'call_22tgOrsVLyLMsl2RLbUhtycw',\n",
" 'output': 161},\n",
" {'name': 'multiply',\n",
" 'args': {'first_int': 5, 'second_int': 18},\n",
" 'id': 'call_EbKHEG3TjqBhEwb7aoxUtgzf',\n",
" 'output': 90},\n",
" {'name': 'add',\n",
" 'args': {'first_int': 1000000, 'second_int': 1000000000},\n",
" 'id': 'call_LUhu2IT3vINxlTc5fCVY6Nhi',\n",
" 'output': 1001000000},\n",
" {'name': 'exponentiate',\n",
" 'args': {'base': 37, 'exponent': 3},\n",
" 'id': 'call_bnCZIXelOKkmcyd4uGXId9Ct',\n",
" 'output': 50653}]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chain.invoke(\n",
" \"What's 23 times 7, and what's five times 18 and add a million plus a billion and cube thirty-seven\"\n",
")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}