Cohere Tools on LangChain (Integration Guide)
Cohere Tools on LangChain (Integration Guide)
Cohere supports various integrations with LangChain, a large language model (LLM) framework which allows you to quickly create applications based on Cohere’s models. This doc will guide you through how to leverage Cohere tools with LangChain.
Prerequisites
Running Cohere tools with LangChain doesn’t require many prerequisites, consult the top-level document for more information.
Multi-Step Tool Use
The idiomatic way to build a multi-step agent with LangChain v1 is create_agent from langchain.agents (it ships with langchain, so no extra install is needed). The agent can call tools repeatedly, reasoning across multiple steps before returning a final answer. Here we give it an internet search tool (Tavily); install it with pip install langchain-tavily, set a TAVILY_API_KEY environment variable to run it, and swap in any other LangChain tool you like. You can steer the agent’s behavior with a system instruction by passing it to create_agent via the system_prompt argument.
Single-Step Tool Use
Single-step tool use lets the model decide which tools to call for a query without executing them. Bind your tools to the model and read the chosen tool calls from the response’s .tool_calls attribute. Provide the routing instruction as a SystemMessage at the start of the conversation.
SQL Agent
You can build an agent that interacts with a SQL database by giving create_agent the tools from LangChain’s SQLDatabaseToolkit.
CSV Agent
You can build an agent that answers questions about a CSV file by loading it into a pandas dataframe and giving create_agent a Python REPL tool with the dataframe in scope, so the agent can answer arbitrary questions about the data by writing and running pandas code (install pip install langchain-experimental pandas).
The Python REPL tool runs model-generated code, so only use it with data and queries you trust.
Streaming for Tool Calling
When tools are called in a streaming context, message chunks will be populated with tool call chunk objects in a list via the .tool_call_chunks attribute.
LangGraph Agents
LangGraph is a stateful, orchestration framework that brings added control to agent workflows.
To use LangGraph with Cohere, you need to install the LangGraph package. To install it, run pip install langgraph.
Basic Chatbot
This simple chatbot example will illustrate the core concepts of building with LangGraph.
Enhancing the Chatbot with Tools
To handle queries our chatbot can’t answer “from memory”, we’ll integrate a web search tool. Our bot can use this tool to find relevant information and provide better responses.