Skip to main content
Cohere is a Canadian startup that provides natural language processing models that help companies improve human-machine interactions. This will help you getting started with ChatCohere chat models. For detailed documentation of all ChatCohere features and configurations head to the API reference.

Overview

Integration details

ClassPackageSerializablePY supportDownloadsVersion
ChatCohere@langchain/coherebetaNPM - DownloadsNPM - Version

Model features

See the links in the table headers below for guides on how to use specific features.

Setup

In order to use the LangChain.js Cohere integration you’ll need an API key. You can sign up for a Cohere account and create an API key. You’ll first need to install the @langchain/cohere package.

Credentials

Head to Cohere’s website to sign up to Cohere and generate an API key. Once you’ve done this set the COHERE_API_KEY environment variable:
If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

Installation

The LangChain ChatCohere integration lives in the @langchain/cohere package:

Instantiation

Now we can instantiate our model object and generate chat completions:

Custom client for Cohere on Azure, Cohere on AWS Bedrock, and standalone Cohere instance

We can instantiate a custom CohereClient and pass it to the ChatCohere constructor. Note: If a custom client is provided both COHERE_API_KEY environment variable and apiKey parameter in the constructor will be ignored.

Invocation

RAG

Cohere also comes out of the box with RAG support. You can pass in documents as context to the API request and Cohere’s models will use them when generating responses.

Connectors

The API also allows for other connections which are not static documents. An example of this is their web-search connector which allows you to pass in a query and the API will search the web for relevant documents. The example below demonstrates how to use this feature.
We can see in the additional_kwargs object that the API request did a few things:
  • Performed a search query, storing the result data in the searchQueries and searchResults fields. In the searchQueries field we see they rephrased our query for better results.
  • Generated three documents from the search query.
  • Generated a list of citations
  • Generated a final response based on the above actions & content.

API reference

For detailed documentation of all ChatCohere features and configurations head to the API reference.