add file
This commit is contained in:
+9
-23
@@ -1,36 +1,22 @@
|
|||||||
from qdrant_client import QdrantClient
|
from chromadb import Client as ChromaClient
|
||||||
from qdrant_client.http import models
|
|
||||||
from langchain_ollama import OllamaEmbeddings
|
from langchain_ollama import OllamaEmbeddings
|
||||||
|
|
||||||
class QdrantStore:
|
class ChromaStore:
|
||||||
def __init__(self, url="http://localhost:6333", collection_name="rag_collection"):
|
def __init__(self, collection_name="rag_collection"):
|
||||||
self.client = QdrantClient(url=url)
|
self.client = ChromaClient()
|
||||||
self.collection_name = collection_name
|
self.collection_name = collection_name
|
||||||
self._ensure_collection()
|
self._ensure_collection()
|
||||||
|
|
||||||
def _ensure_collection(self):
|
def _ensure_collection(self):
|
||||||
if self.collection_name not in self.client.get_collections().collections:
|
if self.collection_name not in [c.name for c in self.client.get_collections()]:
|
||||||
self.client.recreate_collection(
|
self.client.create_collection(name=self.collection_name, metadata={})
|
||||||
collection_name=self.collection_name,
|
|
||||||
vectors_config=models.VectorParams(size=384, distance=models.Distance.COSINE),
|
|
||||||
)
|
|
||||||
|
|
||||||
def add_documents(self, documents, titles):
|
def add_documents(self, documents, titles):
|
||||||
embeddings = OllamaEmbeddings(model="nomic-embed-text")
|
embeddings = OllamaEmbeddings(model="nomic-embed-text")
|
||||||
vectors = embeddings.embed_documents(documents)
|
vectors = embeddings.embed_documents(documents)
|
||||||
payload = [{"title": t} for t in titles]
|
payload = [{"title": t} for t in titles]
|
||||||
self.client.upsert(
|
self.client.upsert(collection_name=self.collection_name, documents=documents, ids=[str(i) for i in range(len(documents))], metadatas=payload)
|
||||||
collection_name=self.collection_name,
|
|
||||||
points=models.Batch(points=[models.PointStruct(id=i, vector=v, payload=p) for i, (v, p) in enumerate(zip(vectors, payload))])
|
|
||||||
)
|
|
||||||
|
|
||||||
def search(self, query, limit=5):
|
def search(self, query, limit=5):
|
||||||
embeddings = OllamaEmbeddings(model="nomic-embed-text")
|
results = self.client.query(collection_name=self.collection_name, query_text=query, n_results=limit, include_metadata=True)
|
||||||
query_vector = embeddings.embed_query(query)
|
return [r['metadata'] for r in results]
|
||||||
results = self.client.search(
|
|
||||||
collection_name=self.collection_name,
|
|
||||||
query_vector=query_vector,
|
|
||||||
limit=limit,
|
|
||||||
with_payload=True,
|
|
||||||
)
|
|
||||||
return [r.payload for r in results]
|
|
||||||
|
|||||||
Reference in New Issue
Block a user