43 lines
1.6 KiB
Python
43 lines
1.6 KiB
Python
"""
|
||
Vector store initialization using Qdrant in-memory.
|
||
|
||
The vector store is used by the search tool to perform semantic similarity search.
|
||
"""
|
||
import os
|
||
from pathlib import Path
|
||
from langchain_qdrant import QdrantVectorStore
|
||
from qdrant_client import QdrantClient
|
||
from qdrant_client.models import Distance, VectorParams
|
||
from langchain_openai import OpenAIEmbeddings
|
||
|
||
# Embedding model compatible with OpenRouter API (used by BroJS LLM)
|
||
embeddings = OpenAIEmbeddings(
|
||
model="text-embedding-3-small",
|
||
base_url="https://openrouter.ai/api/v1",
|
||
api_key=os.getenv("OPENAI_API_KEY"),
|
||
)
|
||
|
||
# In-memory Qdrant client – no external server required
|
||
client = QdrantClient(":memory:")
|
||
client.create_collection(
|
||
"knowledge",
|
||
vectors_config=VectorParams(size=1536, distance=Distance.COSINE),
|
||
)
|
||
vector_store = QdrantVectorStore(client=client, collection_name="knowledge", embedding=embeddings)
|
||
|
||
# Helper to add documents – used in examples
|
||
from langchain_core.documents import Document
|
||
|
||
def add_documents(docs: list[Document]):
|
||
"""Add a list of :class:`~langchain_core.documents.Document` objects to the store."""
|
||
vector_store.add_documents(docs)
|
||
|
||
# Example documents – can be extended by users
|
||
if __name__ == "__main__":
|
||
docs = [
|
||
Document(page_content="LangChain is a framework for building applications powered by language models.", metadata={"title": "LangChain Overview"}),
|
||
Document(page_content="Qdrant is an open-source vector database that stores embeddings and performs similarity search efficiently.", metadata={"title": "Qdrant Documentation"}),
|
||
]
|
||
add_documents(docs)
|
||
print("Added example documents to Qdrant in-memory store")
|