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
from langchain_cohere import CohereEmbeddings
# Define the Cohere embedding model
embeddings = CohereEmbeddings(
cohere_api_key="COHERE_API_KEY", model="embed-v4.0"
)
# Embed a document
text = "This is a test document."
query_result = embeddings.embed_query(text)
print(query_result[:5], "...")
doc_result = embeddings.embed_documents([text])
print(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
from langchain_cohere import ChatCohere, CohereEmbeddings
from langchain_text_splitters import CharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain_community.document_loaders import WebBaseLoader
user_query = "what is Cohere Toolkit?"
llm = ChatCohere(
cohere_api_key="COHERE_API_KEY",
model="command-a-03-2025",
temperature=0,
)
embeddings = CohereEmbeddings(
cohere_api_key="COHERE_API_KEY", model="embed-v4.0"
)
# Load text and split into chunks, you can also use data gathered elsewhere in your application
raw_documents = WebBaseLoader(
"https://docs.cohere.com/docs/cohere-toolkit"
).load()
text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0)
documents = text_splitter.split_documents(raw_documents)
# Create a vector store from the documents and retrieve the most relevant chunks
db = Chroma.from_documents(documents, embeddings)
input_docs = db.as_retriever().invoke(user_query)
# Ground the answer in the retrieved documents
response = llm.invoke(user_query, documents=input_docs)
# Print the answer
print("Answer:")
print(response.content)
# Print the citations that ground the answer in the documents
print("Citations:")
print(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
from langchain_aws import BedrockEmbeddings
# Replace the profile name with the one created in the setup.
embeddings = BedrockEmbeddings(
credentials_profile_name="{PROFILE-NAME}",
region_name="us-east-1",
model_id="cohere.embed-english-v3",
)
embeddings.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
llm = CohereEmbeddings(
base_url="<YOUR_DEPLOYMENT_URL>",
cohere_api_key="COHERE_API_KEY",
model="MODEL_NAME",
)