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.
Before you begin
Section titled “Before you begin”You need a Running database with its Endpoint URL and API Key; see Connect to a vector database. You need Python 3.8 or later and a text file of documents.
pip install langchain langchain-community qdrant-clientEmbed documents and search
Section titled “Embed documents and search”<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 TextLoaderfrom langchain_text_splitters import CharacterTextSplitterfrom langchain.embeddings import HuggingFaceEmbeddingsfrom langchain_community.vectorstores import Qdrant
# Load and chunk documentsloader = TextLoader("<your-file>")documents = loader.load()docs = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0).split_documents(documents)
# Embed and insert into Qdrantembeddings = 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 searchfound_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.
What to do next
Section titled “What to do next”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.