Embed on LangChain

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_community.embeddings import CohereEmbeddings
2cohere_embeddings = CohereEmbeddings(cohere_api_key="{API_KEY}",
3 model="embed-english-light-v3.0")
4text = "This is a test document."
5query_result = cohere_embeddings.embed_query(text)
6print(query_result)
7doc_result = cohere_embeddings.embed_documents([text])
8print(doc_result)

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.

PYTHON
1from langchain.retrievers import ContextualCompressionRetriever, CohereRagRetriever
2from langchain.retrievers.document_compressors import CohereRerank
3from langchain_community.embeddings import CohereEmbeddings
4from langchain_community.chat_models import ChatCohere
5from langchain.text_splitter import CharacterTextSplitter
6from langchain_community.document_loaders import TextLoader
7from langchain_community.vectorstores import Chroma
8
9user_query = "When was Cohere started?"
10# Create cohere's chat model and embeddings objects
11cohere_chat_model = ChatCohere(cohere_api_key="{API-KEY}")
12cohere_embeddings = CohereEmbeddings(cohere_api_key="{API-KEY}")
13# Load text files and split into chunks, you can also use data gathered elsewhere in your application
14raw_documents = TextLoader('test.txt').load()
15text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
16documents = text_splitter.split_documents(raw_documents)
17# Create a vector store from the documents
18db = Chroma.from_documents(documents, cohere_embeddings)
19input_docs = db.as_retriever().get_relevant_documents(user_query)
20
21# Create the cohere rag retriever using the chat model
22rag = CohereRagRetriever(llm=cohere_chat_model)
23docs = rag.get_relevant_documents(
24 user_query,
25 source_documents=input_docs,
26)
27# Print the documents
28for doc in docs[:-1]:
29 print(doc.metadata)
30 print("\n\n" + doc.page_content)
31 print("\n\n" + "-" * 30 + "\n\n")
32# Print the final generation
33answer = docs[-1].page_content
34print(answer)
35# Print the final citations
36citations = docs[-1].metadata['citations']
37print(citations)

Cohere with LangChain and Bedrock

Prerequisite

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

Cohere Embeddings with LangChain and Amazon Bedrock

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

PYTHON
1from langchain_community.embeddings 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)
9embeddings.embed_query("This is a content of the document")