-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector.py
More file actions
42 lines (32 loc) · 996 Bytes
/
vector.py
File metadata and controls
42 lines (32 loc) · 996 Bytes
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
from langchain_ollama import OllamaEmbeddings
from langchain_chroma import Chroma
from langchain_core.documents import Document
import os
import pandas as pd
script_dir = os.path.dirname(os.path.abspath(__file__))
csv_path = os.path.join(script_dir, "data", "cleaned_dadjokes.csv")
df = pd.read_csv(csv_path)
embeddings = OllamaEmbeddings(model="mxbai-embed-large")
db_location = "./chroma_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['dadjoke'],
metadata={"upvotes": row['upvotes']},
id=str(i)
)
ids.append(str(i))
documents.append(document)
vector_store = Chroma(
collection_name = "dad-jokes",
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}
)