Skip to content

Ask AI

Powered by Blaze

Answers come from E2E Cloud documentation. AI can make mistakes.

Use Qdrant with LangChain

View as Markdown

LangChain builds applications on top of language models. Use your Qdrant database as its vector store to embed documents and retrieve the ones closest to a question.

The script runs on Python 3.8 or later against a Running database and reads your documents from a text file. It uses the database’s Endpoint URL and API Key; see Connect to a vector database.

pip install langchain langchain-community qdrant-client

<your-endpoint-url> is the Endpoint URL, and <your-api-key> is your API key. <your-file> is your text file, and <your-query> is the question to search for.

from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import CharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import Qdrant
# Load and chunk documents
loader = TextLoader("<your-file>")
documents = loader.load()
docs = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0).split_documents(documents)
# Embed and insert into Qdrant
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
qdrant = Qdrant.from_documents(
docs, embeddings,
host="<your-endpoint-url>", port=6333,
api_key="<your-api-key>",
collection_name="my_documents",
)
# Similarity search
found_docs = qdrant.similarity_search_with_score("<your-query>")
document, score = found_docs[0]
# MMR search (diverse results)
found_docs = qdrant.max_marginal_relevance_search("<your-query>", k=2, fetch_k=10)

The first search returns the closest document and its score. The MMR search returns two results chosen to differ from each other.

For more, see the Qdrant LangChain documentation. To create the embeddings with the TIR GenAI API, see Generate vector embeddings. If a connection fails, see Troubleshooting Vector Database.

Was this page helpful?