Help me creating a RAG class: vectorstore should be reusable at the second call #28111
jsmith173
announced in
Ask Dosu (Archived)
Replies: 1 comment 5 replies
-
To create a reusable RAG (Retrieval-Augmented Generation) class in Python, you can structure your code to initialize the necessary components only once and reuse them for subsequent calls. Here's a basic example using the specified functions: from some_module import WebBaseLoader, RecursiveCharacterTextSplitter, OllamaEmbeddings, Chroma
class RAG:
_vectorstore = None
_embeddings = None
def __init__(self, documents):
if RAG._vectorstore is None or RAG._embeddings is None:
self.loader = WebBaseLoader()
self.text_splitter = RecursiveCharacterTextSplitter()
self.embeddings = OllamaEmbeddings()
self.vectorstore = Chroma.from_documents(documents, self.embeddings)
RAG._vectorstore = self.vectorstore
RAG._embeddings = self.embeddings
else:
self.vectorstore = RAG._vectorstore
self.embeddings = RAG._embeddings
def __del__(self):
# Clean up class variables when the instance is deleted
RAG._vectorstore = None
RAG._embeddings = None
def query(self, query_text):
# Implement your query logic here
pass
# Example usage
documents = ["Document 1 text", "Document 2 text"]
rag_instance = RAG(documents)
result = rag_instance.query("Your query here") Key Points:
This structure allows you to reuse the vector store and embeddings without reinitializing them for every new instance of the class. Adjust the |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Create me a python code (using class) for using RAG.
Use the following functions: WebBaseLoader, RecursiveCharacterTextSplitter, OllamaEmbeddings, Chroma.from_documents
But this important when a second call is coming I don't want run again the RAG create process so store this variables in a class variable and make sure that they are free upon deleting the class instance
Beta Was this translation helpful? Give feedback.
All reactions