Spaces:
Build error
Build error
Create rag.py
Browse files
rag.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 2 |
+
from langchain.vectorstores import Chroma
|
| 3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
+
from langchain.document_loaders import TextLoader
|
| 5 |
+
|
| 6 |
+
# We'll assume you have a documentation text file. If not, we can use some sample Python docs.
|
| 7 |
+
# Let's create a sample if the file doesn't exist, or load it.
|
| 8 |
+
|
| 9 |
+
def load_documents():
|
| 10 |
+
# Load the documents from a file (or multiple files)
|
| 11 |
+
# For demonstration, we'll create a sample document if it doesn't exist.
|
| 12 |
+
doc_path = "python_docs.txt"
|
| 13 |
+
if not os.path.exists(doc_path):
|
| 14 |
+
# Create a sample documentation about Python functions
|
| 15 |
+
with open(doc_path, 'w') as f:
|
| 16 |
+
f.write("""
|
| 17 |
+
Functions in Python are defined using the def keyword.
|
| 18 |
+
For example: def hello_world(): print("Hello, world!")
|
| 19 |
+
Functions can take parameters and return values.
|
| 20 |
+
""")
|
| 21 |
+
loader = TextLoader(doc_path)
|
| 22 |
+
documents = loader.load()
|
| 23 |
+
return documents
|
| 24 |
+
|
| 25 |
+
def create_vector_store(documents):
|
| 26 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
| 27 |
+
texts = text_splitter.split_documents(documents)
|
| 28 |
+
embeddings = OpenAIEmbeddings()
|
| 29 |
+
vectorstore = Chroma.from_documents(documents=texts, embedding=embeddings)
|
| 30 |
+
return vectorstore
|
| 31 |
+
|
| 32 |
+
def retrieve_relevant_docs(vectorstore, query, k=3):
|
| 33 |
+
"""
|
| 34 |
+
Retrieve relevant documents for the query.
|
| 35 |
+
"""
|
| 36 |
+
docs = vectorstore.similarity_search(query, k=k)
|
| 37 |
+
return "\n".join([doc.page_content for doc in docs])
|
| 38 |
+
|
| 39 |
+
# Initialize the vector store once (for performance)
|
| 40 |
+
documents = load_documents()
|
| 41 |
+
vectorstore = create_vector_store(documents)
|
| 42 |
+
|
| 43 |
+
def get_rag_context(query):
|
| 44 |
+
return retrieve_relevant_docs(vectorstore, query)
|