+35
-18
@@ -1,26 +1,42 @@
|
|||||||
from langchain_community.vectorstores import Chroma
|
from langchain_qdrant import QdrantVectorStore
|
||||||
from langchain_ollama import OllamaEmbeddings
|
from langchain_ollama import OllamaEmbeddings
|
||||||
from langchain.schema import Document
|
from langchain_core.documents import Document
|
||||||
|
from qdrant_client import QdrantClient
|
||||||
|
from qdrant_client.models import Distance, VectorParams
|
||||||
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
||||||
|
import uuid
|
||||||
|
|
||||||
COLLECTION_NAME = "rag_collection"
|
COLLECTION_NAME = "knowledge_base"
|
||||||
EMBEDDING_MODEL = "nomic-embed-text"
|
EMBEDDING_MODEL = "nomic-embed-text"
|
||||||
CHROMA_PERSIST_DIR = "./chroma_db"
|
VECTOR_SIZE = 768
|
||||||
|
|
||||||
|
|
||||||
class ChromaStore:
|
def get_embeddings():
|
||||||
def __init__(self, collection_name: str = COLLECTION_NAME):
|
return OllamaEmbeddings(model=EMBEDDING_MODEL)
|
||||||
self.client = Chroma(
|
|
||||||
embedding_function=OllamaEmbeddings(model=EMBEDDING_MODEL),
|
|
||||||
collection_name=collection_name,
|
def get_qdrant_client():
|
||||||
persist_directory=CHROMA_PERSIST_DIR,
|
return QdrantClient(host="localhost", port=6333)
|
||||||
|
|
||||||
|
|
||||||
|
def init_collection(client: QdrantClient):
|
||||||
|
collections = [c.name for c in client.get_collections().collections]
|
||||||
|
if COLLECTION_NAME not in collections:
|
||||||
|
client.create_collection(
|
||||||
|
collection_name=COLLECTION_NAME,
|
||||||
|
vectors_config=VectorParams(size=VECTOR_SIZE, distance=Distance.COSINE),
|
||||||
)
|
)
|
||||||
|
|
||||||
def add_documents(self, docs: list[Document]) -> None:
|
|
||||||
self.client.add_documents(docs)
|
|
||||||
|
|
||||||
def search(self, query: str, limit: int = 5) -> list[Document]:
|
def get_vector_store() -> QdrantVectorStore:
|
||||||
return self.client.similarity_search(query, k=limit)
|
client = get_qdrant_client()
|
||||||
|
init_collection(client)
|
||||||
|
embeddings = get_embeddings()
|
||||||
|
return QdrantVectorStore(
|
||||||
|
client=client,
|
||||||
|
collection_name=COLLECTION_NAME,
|
||||||
|
embedding=embeddings,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def add_documents(content: str, title: str) -> int:
|
def add_documents(content: str, title: str) -> int:
|
||||||
@@ -37,20 +53,21 @@ def add_documents(content: str, title: str) -> int:
|
|||||||
)
|
)
|
||||||
for i, chunk in enumerate(chunks)
|
for i, chunk in enumerate(chunks)
|
||||||
]
|
]
|
||||||
store = ChromaStore()
|
store = get_vector_store()
|
||||||
store.add_documents(docs)
|
store.add_documents(docs)
|
||||||
return len(docs)
|
return len(docs)
|
||||||
|
|
||||||
|
|
||||||
def search_documents(query: str, max_results: int = 5) -> list[dict]:
|
def search_documents(query: str, max_results: int = 5) -> list[dict]:
|
||||||
store = ChromaStore()
|
store = get_vector_store()
|
||||||
results = store.search(query, limit=max_results)
|
results = store.similarity_search_with_relevance_scores(query, k=max_results)
|
||||||
output = []
|
output = []
|
||||||
for doc in results:
|
for doc, score in results:
|
||||||
output.append(
|
output.append(
|
||||||
{
|
{
|
||||||
"content": doc.page_content,
|
"content": doc.page_content,
|
||||||
"metadata": doc.metadata,
|
"metadata": doc.metadata,
|
||||||
|
"score": round(score, 4),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
return output
|
return output
|
||||||
Reference in New Issue
Block a user