-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.py
More file actions
37 lines (30 loc) · 1.01 KB
/
vector.py
File metadata and controls
37 lines (30 loc) · 1.01 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
from langchain_ollama import OllamaEmbeddings
from langchain_chroma import Chroma
from langchain_core.documents import Document
import os
import pandas as pd
df = pd.read_csv("realistic_restaurant_reviews.csv")
embeddings = OllamaEmbeddings(model="mxbai-embed-large")
db_location = "./chrome_langchain_db"
add_documents = not os.path.exists(db_location)
if add_documents:
documents = []
ids = []
for i, row in df.iterrows():
document = Document(
page_content=row["Title"] + " " + row["Review"],
metadata={"rating": row["Rating"], "date": row["Date"]},
id=str(i)
)
ids.append(str(i))
documents.append(document)
vector_store = Chroma(
collection_name="restaurant_reviews",
persist_directory=db_location,
embedding_function=embeddings
)
if add_documents:
vector_store.add_documents(documents=documents, ids=ids)
retriever = vector_store.as_retriever(
search_kwargs={"k": 5}
)