Cohere Embed 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 different Cohere embeddings with LangChain.

Prerequisites

Running Cohere embeddings with LangChain doesn’t require many prerequisites, consult the top-level document for more information.

Cohere Embeddings with LangChain

To use Cohere’s Embeddings with LangChain, create a CohereEmbedding object as follows (the available cohere embedding models are listed here):

PYTHON
1from langchain_cohere import CohereEmbeddings
2
3# Define the Cohere embedding model
4embeddings = CohereEmbeddings(
5 cohere_api_key="COHERE_API_KEY", model="embed-v4.0"
6)
7
8# Embed a document
9text = "This is a test document."
10query_result = embeddings.embed_query(text)
11print(query_result[:5], "...")
12doc_result = embeddings.embed_documents([text])
13print(doc_result[0][:5], "...")

To use these embeddings with Cohere’s RAG functionality, you will need to use one of the vector DBs from this list. In this example we use chroma, so in order to run it you will need to install chroma using pip install chromadb. We retrieve the most relevant chunks from the vector store and pass them to ChatCohere through its documents argument to get a grounded answer with citations.

PYTHON
1from langchain_cohere import ChatCohere, CohereEmbeddings
2from langchain_text_splitters import CharacterTextSplitter
3from langchain_community.vectorstores import Chroma
4from langchain_community.document_loaders import WebBaseLoader
5
6user_query = "what is Cohere Toolkit?"
7
8llm = ChatCohere(
9 cohere_api_key="COHERE_API_KEY",
10 model="command-a-03-2025",
11 temperature=0,
12)
13
14embeddings = CohereEmbeddings(
15 cohere_api_key="COHERE_API_KEY", model="embed-v4.0"
16)
17
18# Load text and split into chunks, you can also use data gathered elsewhere in your application
19raw_documents = WebBaseLoader(
20 "https://docs.cohere.com/docs/cohere-toolkit"
21).load()
22
23text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0)
24documents = text_splitter.split_documents(raw_documents)
25
26# Create a vector store from the documents and retrieve the most relevant chunks
27db = Chroma.from_documents(documents, embeddings)
28input_docs = db.as_retriever().invoke(user_query)
29
30# Ground the answer in the retrieved documents
31response = llm.invoke(user_query, documents=input_docs)
32
33# Print the answer
34print("Answer:")
35print(response.content)
36# Print the citations that ground the answer in the documents
37print("Citations:")
38print(response.additional_kwargs.get("citations"))

Cohere with LangChain and Bedrock

Prerequisite

In addition to the prerequisites above, integrating Cohere with LangChain on Amazon Bedrock also requires:

  • The LangChain AWS package. To install it, run pip install langchain-aws.
  • AWS Python SDK. To install it, run pip install boto3. You can find more details here .
  • Configured authentication credentials for AWS. For more details, see this document.

Cohere Embeddings with LangChain and Amazon Bedrock

In this example, we create embeddings for a query using Bedrock and LangChain:

PYTHON
1from langchain_aws import BedrockEmbeddings
2
3# Replace the profile name with the one created in the setup.
4embeddings = BedrockEmbeddings(
5 credentials_profile_name="{PROFILE-NAME}",
6 region_name="us-east-1",
7 model_id="cohere.embed-english-v3",
8)
9
10embeddings.embed_query("This is a content of the document")

Using LangChain on Private Deployments

You can use LangChain with privately deployed Cohere models. To use it, specify your model deployment URL in the base_url parameter.

PYTHON
1llm = CohereEmbeddings(
2 base_url="<YOUR_DEPLOYMENT_URL>",
3 cohere_api_key="COHERE_API_KEY",
4 model="MODEL_NAME",
5)