-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.py
More file actions
54 lines (43 loc) · 1.49 KB
/
vector.py
File metadata and controls
54 lines (43 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from langchain_core.documents import Document
import pandas as pd
import os
from langchain_community.embeddings import OllamaEmbeddings
from langchain_qdrant import QdrantVectorStore
from qdrant_client import QdrantClient
from qdrant_client.http.models import Distance, VectorParams
# Charger les données du CSV
df = pd.read_csv("realistic_restaurant_reviews.csv")
# Initialisation du modèle d'embedding
embeddings = OllamaEmbeddings(model="mxbai-embed-large") # Ce modèle retourne des vecteurs de taille 1024
# Connexion au Qdrant local via URL
client = QdrantClient(url="http://localhost:6333")
# Nom de la collection
collection_name = "restaurant_reviews"
# Forcer la recréation de la collection pour correspondre à la taille 1024
client.recreate_collection(
collection_name=collection_name,
vectors_config=VectorParams(size=1024, distance=Distance.COSINE)
)
# Créer les documents
documents = []
ids = []
for i, row in df.iterrows():
document = Document(
page_content=row["Title"] + " " + row["Review"],
metadata={
"rating": row["Rating"],
"date": row["Date"]
}
)
documents.append(document)
ids.append(str(i))
# Créer le vector store
vector_store = QdrantVectorStore(
client=client,
collection_name=collection_name,
embedding=embeddings
)
# Ajouter les documents
vector_store.add_documents(documents=documents, ids=ids)
# Optionnel : Créer un retriever
retriever = vector_store.as_retriever(search_kwargs={"k": 5})