bsmith3715 commited on
Commit
65a2085
·
verified ·
1 Parent(s): c628574

Upload rag_processor_v0.py

Browse files
Files changed (1) hide show
  1. rag_processor_v0.py +64 -0
rag_processor_v0.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import List, Generator
3
+ import openai
4
+ from sentence_transformers import SentenceTransformer
5
+ import faiss
6
+ import numpy as np
7
+ from dotenv import load_dotenv
8
+
9
+ load_dotenv()
10
+
11
+ class RAGProcessor:
12
+ def __init__(self, model_name: str = "bsmith3715/legal-ft-demo_final"):
13
+ self.model = SentenceTransformer(model_name)
14
+ self.index = None
15
+ self.documents = []
16
+ self.openai_client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
17
+
18
+ def add_documents(self, documents: List[str]):
19
+ """Add documents to the RAG system."""
20
+ self.documents = documents
21
+ embeddings = self.model.encode(documents)
22
+
23
+ # Create FAISS index
24
+ dimension = embeddings.shape[1]
25
+ self.index = faiss.IndexFlatL2(dimension)
26
+ self.index.add(embeddings.astype('float32'))
27
+
28
+ def retrieve_relevant_context(self, query: str, k: int = 3) -> List[str]:
29
+ """Retrieve relevant documents for a given query."""
30
+ if not self.index:
31
+ return []
32
+
33
+ query_embedding = self.model.encode([query])
34
+ distances, indices = self.index.search(query_embedding.astype('float32'), k)
35
+
36
+ return [self.documents[i] for i in indices[0]]
37
+
38
+ async def generate_response(self, query: str) -> AsyncGenerator[str, None]:
39
+ """Generate a streaming response using OpenAI API with retrieved context."""
40
+ relevant_docs = self.retrieve_relevant_context(query)
41
+ context = "\n".join(relevant_docs)
42
+
43
+ prompt = f"""Context information is below.
44
+ ---------------------
45
+ {context}
46
+ ---------------------
47
+ Given the context information, please answer the following question. If the context doesn't contain relevant information, say so.
48
+ Question: {query}
49
+ Answer:"""
50
+
51
+ stream = self.openai_client.chat.completions.create(
52
+ model="gpt-40",
53
+ messages=[
54
+ {"role": "system", "content": "You are a helpful Pilates instructor assistant. Use the provided context to answer questions accurately."},
55
+ {"role": "user", "content": prompt}
56
+ ],
57
+ temperature=0.1,
58
+ max_tokens=1000,
59
+ stream=True
60
+ )
61
+
62
+ for chunk in stream:
63
+ if chunk.choices[0].delta.content is not None:
64
+ yield chunk.choices[0].delta.content