Quickstart
In this guide, we will go over the basic ways to create Chains and Agents that call Tools. Tools can be just about anything — APIs, functions, databases, etc. Tools allow us to extend the capabilities of a model beyond just outputting text/messages. The key to using models with tools is correctly prompting a model and parsing its response so that it chooses the right tools and provides the right inputs for them.
Setup
We’ll need to install the following packages for this guide:
%pip install --upgrade --quiet langchain langchain-openai
And set these environment variables:
import getpass
import os
os.environ["OPENAI_API_KEY"] = getpass.getpass()
# If you'd like to use LangSmith, uncomment the below
# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()
Create a tool
First, we need to create a tool to call. For this example, we will create a custom tool from a function. For more information on creating custom tools, please see this guide.
from langchain_core.tools import tool
@tool
def multiply(first_int: int, second_int: int) -> int:
"""Multiply two integers together."""
return first_int * second_int
print(multiply.name)
print(multiply.description)
print(multiply.args)
multiply
multiply(first_int: int, second_int: int) -> int - Multiply two integers together.
{'first_int': {'title': 'First Int', 'type': 'integer'}, 'second_int': {'title': 'Second Int', 'type': 'integer'}}
multiply.invoke({"first_int": 4, "second_int": 5})
20
Chains
If we know that we only need to use a tool a fixed number of times, we can create a chain for doing so. Let’s create a simple chain that just multiplies user-specified numbers.
Function calling
One of the most reliable ways to use tools with LLMs is with function calling APIs (also sometimes called tool calling or parallel function calling). This only works with models that explicitly support function calling, like OpenAI models. To learn more head to the function calling guide.
First we’ll define our model and tools. We’ll start with just a single
tool, multiply
.
from langchain_openai.chat_models import ChatOpenAI
model = ChatOpenAI(model="gpt-3.5-turbo-1106")
Next we’ll convert our LangChain Tool to an OpenAI format JSONSchema
function, and bind this as the tools
argument to be passed to all
ChatOpenAI calls. Since we only have a single Tool and in this initial
chain we want to make sure it’s always used, we’ll also specify
tool_choice
. See the OpenAI chat API
reference
for more on these parameters:
model_with_tools = model.bind_tools([multiply], tool_choice="multiply")
model_with_tools.kwargs["tools"]
[{'type': 'function',
'function': {'name': 'multiply',
'description': 'multiply(first_int: int, second_int: int) -> int - Multiply two integers together.',
'parameters': {'type': 'object',
'properties': {'first_int': {'type': 'integer'},
'second_int': {'type': 'integer'}},
'required': ['first_int', 'second_int']}}}]
model_with_tools.kwargs["tool_choice"]
{'type': 'function', 'function': {'name': 'multiply'}}
Now we’ll compose out tool-calling model with a JsonOutputToolsParser
,
a built-in LangChain output parser that converts an OpenAI
function-calling response to a list of
{"type": "TOOL_NAME", "args": {...}}
dicts with the tools to invoke
and arguments to invoke them with.
from langchain.output_parsers import JsonOutputToolsParser
chain = model_with_tools | JsonOutputToolsParser()
chain.invoke("What's four times 23")
[{'type': 'multiply', 'args': {'first_int': 4, 'second_int': 23}}]
Since we know we’re always invoking the multiply
tool, we can simplify
our output a bit to return only the args for the multiply
tool using
the JsonoutputKeyToolsParser
. To further simplify we’ll specify
return_single=True
, so that instead of a list of tool invocations our
output parser returns only the first tool invocation.
from langchain.output_parsers import JsonOutputKeyToolsParser
chain = model_with_tools | JsonOutputKeyToolsParser(
key_name="multiply", return_single=True
)
chain.invoke("What's four times 23")
{'first_int': 4, 'second_int': 23}
Invoking the tool
Great! We’re able to generate tool invocations. But what if we want to actually call the tool? To do that we just need to pass them to the tool:
from operator import itemgetter
# Note: the `.map()` at the end of `multiply` allows us to pass in a list of `multiply` arguments instead of a single one.
chain = (
model_with_tools
| JsonOutputKeyToolsParser(key_name="multiply", return_single=True)
| multiply
)
chain.invoke("What's four times 23")
92
Agents
Chains are great when we know the specific sequence of tool usage needed for any user input. But for certain use cases, how many times we use tools depends on the input. In these cases, we want to let the model itself decide how many times to use tools and in what order. Agents let us do just this.
LangChain comes with a number of built-in agents that are optimized for different use cases. Read about all the agent types here.
As an example, let’s try out the OpenAI tools agent, which makes use of the new OpenAI tool-calling API (this is only available in the latest OpenAI models, and differs from function-calling in that the model can return multiple function invocations at once)
from langchain import hub
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain_openai import ChatOpenAI
# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/openai-tools-agent")
prompt.messages
[SystemMessagePromptTemplate(prompt=PromptTemplate(input_variables=[], template='You are a helpful assistant')),
MessagesPlaceholder(variable_name='chat_history', optional=True),
HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['input'], template='{input}')),
MessagesPlaceholder(variable_name='agent_scratchpad')]
Agents are also great because they make it easy to use multiple tools. To learn how to build Chains that use multiple tools, check out the Chains with multiple tools page.
@tool
def add(first_int: int, second_int: int) -> int:
"Add two integers."
return first_int + second_int
@tool
def exponentiate(base: int, exponent: int) -> int:
"Exponentiate the base to the exponent power."
return base**exponent
tools = [multiply, add, exponentiate]
# Choose the LLM that will drive the agent
# Only certain models support this
model = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0)
# Construct the OpenAI Tools agent
agent = create_openai_tools_agent(model, tools, prompt)
# Create an agent executor by passing in the agent and tools
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
With an agent, we can ask questions that require arbitrarily-many uses of our tools:
agent_executor.invoke(
{
"input": "Take 3 to the fifth power and multiply that by the sum of twelve and three, then square the whole result"
}
)
> Entering new AgentExecutor chain...
Invoking: `exponentiate` with `{'base': 3, 'exponent': 5}`
243
Invoking: `add` with `{'first_int': 12, 'second_int': 3}`
15
Invoking: `multiply` with `{'first_int': 243, 'second_int': 15}`
3645
Invoking: `exponentiate` with `{'base': 3645, 'exponent': 2}`
13286025The result of raising 3 to the fifth power and multiplying that by the sum of twelve and three, then squaring the whole result is 13,286,025.
> Finished chain.
{'input': 'Take 3 to the fifth power and multiply that by the sum of twelve and three, then square the whole result',
'output': 'The result of raising 3 to the fifth power and multiplying that by the sum of twelve and three, then squaring the whole result is 13,286,025.'}
Next steps
Here we’ve gone over the basic ways to use Tools with Chains and Agents. We recommend the following sections to explore next:
- Agents: Everything related to Agents.
- Choosing between multiple tools: How to make tool chains that select from multiple tools.
- Prompting for tool use: How to make tool chains that prompt models directly, without using function-calling APIs.
- Parallel tool use: How to make tool chains that invoke multiple tools at once.