-
Notifications
You must be signed in to change notification settings - Fork 1
/
vectorstore_retriever.py
44 lines (37 loc) · 1.38 KB
/
vectorstore_retriever.py
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
from langchain_community.vectorstores import Chroma
from embeddings import CustomOllamaEmbeddings
class VectorStoreRetriever:
"""
Creates a vector store from a list of documents and provides a retriever.
Args:
documents (list): A list of documents to be indexed.
collection_name (str): The name of the Chroma collection.
embedding_model (str): The name of the embedding model to use.
embedding_url (str, optional): The URL of the embedding model service. Defaults to "http://localhost:11434".
"""
def __init__(
self,
documents,
collection_name,
embedding_model,
embedding_url="http://localhost:11434",
):
self.documents = documents
self.collection_name = collection_name
self.embedding = CustomOllamaEmbeddings(
model=embedding_model, base_url=embedding_url
)
self.vectorstore = Chroma.from_documents(
documents=self.documents,
collection_name=self.collection_name,
embedding=self.embedding,
)
def get_retriever(self):
"""
Returns a retriever for the vector store.
Returns:
langchain.vectorstores.base.VectorStoreRetriever: A retriever object.
"""
return self.vectorstore.as_retriever(
search_type="similarity", search_kwargs={"k": 2}
)