Spaces:
Running
Running
Update app/rag.py
Browse files- app/rag.py +47 -66
app/rag.py
CHANGED
@@ -1,13 +1,10 @@
|
|
1 |
import os
|
2 |
from dotenv import load_dotenv
|
3 |
-
import ollama
|
4 |
import logging
|
5 |
-
import time
|
6 |
import sys
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
# Configure logging for stdout only
|
11 |
logging.basicConfig(
|
12 |
level=logging.INFO,
|
13 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
@@ -25,56 +22,30 @@ Context from transcript:
|
|
25 |
User Question: {question}
|
26 |
|
27 |
Please provide a clear, concise answer based only on the information given in the context. If the context doesn't contain enough information to fully answer the question, acknowledge this in your response.
|
28 |
-
|
29 |
-
Guidelines:
|
30 |
-
1. Use only information from the provided context
|
31 |
-
2. Be specific and direct in your answer
|
32 |
-
3. If context is insufficient, say so
|
33 |
-
4. Maintain accuracy and avoid speculation
|
34 |
-
5. Use natural, conversational language
|
35 |
""".strip()
|
36 |
|
37 |
class RAGSystem:
|
38 |
def __init__(self, data_processor):
|
39 |
self.data_processor = data_processor
|
40 |
-
self.model =
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
def check_ollama_service(self):
|
48 |
-
try:
|
49 |
-
ollama.list()
|
50 |
-
logger.info("Ollama service is accessible.")
|
51 |
-
self.pull_model()
|
52 |
-
except Exception as e:
|
53 |
-
logger.error(f"An error occurred while connecting to Ollama: {e}")
|
54 |
-
logger.error(f"Please ensure Ollama is running and accessible at {self.ollama_host}")
|
55 |
|
56 |
-
def
|
57 |
try:
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
60 |
except Exception as e:
|
61 |
-
logger.error(f"Error
|
62 |
-
|
63 |
-
def generate(self, prompt):
|
64 |
-
for attempt in range(self.max_retries):
|
65 |
-
try:
|
66 |
-
response = ollama.chat(
|
67 |
-
model=self.model,
|
68 |
-
messages=[{"role": "user", "content": prompt}]
|
69 |
-
)
|
70 |
-
print("Printing the response from OLLAMA: "+response['message']['content'])
|
71 |
-
return response['message']['content']
|
72 |
-
except Exception as e:
|
73 |
-
logger.error(f"Error generating response on attempt {attempt + 1}: {e}")
|
74 |
-
if attempt == self.max_retries - 1:
|
75 |
-
logger.error("All retries exhausted. Unable to generate response.")
|
76 |
-
return None
|
77 |
-
time.sleep(2 ** attempt) # Exponential backoff
|
78 |
|
79 |
def get_prompt(self, user_query, relevant_docs):
|
80 |
context = "\n".join([doc['content'] for doc in relevant_docs])
|
@@ -88,43 +59,53 @@ class RAGSystem:
|
|
88 |
if not index_name:
|
89 |
raise ValueError("No index name provided. Please select a video and ensure it has been processed.")
|
90 |
|
91 |
-
relevant_docs = self.data_processor.search(
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
if not relevant_docs:
|
94 |
logger.warning("No relevant documents found for the query.")
|
95 |
return "I couldn't find any relevant information to answer your query.", ""
|
96 |
|
97 |
prompt = self.get_prompt(user_query, relevant_docs)
|
|
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
)
|
103 |
-
|
104 |
-
answer = response['message']['content']
|
105 |
return answer, prompt
|
|
|
106 |
except Exception as e:
|
107 |
logger.error(f"An error occurred in the RAG system: {e}")
|
108 |
return f"An error occurred: {str(e)}", ""
|
109 |
|
110 |
def rewrite_cot(self, query):
|
111 |
-
prompt = f"""
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
|
|
|
|
|
|
116 |
response = self.generate(prompt)
|
117 |
if response:
|
118 |
return response, prompt
|
119 |
-
return query, prompt
|
120 |
|
121 |
def rewrite_react(self, query):
|
122 |
-
prompt = f"""
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
|
|
|
|
|
|
127 |
response = self.generate(prompt)
|
128 |
if response:
|
129 |
return response, prompt
|
130 |
-
return query, prompt
|
|
|
1 |
import os
|
2 |
from dotenv import load_dotenv
|
|
|
3 |
import logging
|
|
|
4 |
import sys
|
5 |
+
from transformers import pipeline
|
6 |
|
7 |
+
# Configure logging
|
|
|
|
|
8 |
logging.basicConfig(
|
9 |
level=logging.INFO,
|
10 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
22 |
User Question: {question}
|
23 |
|
24 |
Please provide a clear, concise answer based only on the information given in the context. If the context doesn't contain enough information to fully answer the question, acknowledge this in your response.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
""".strip()
|
26 |
|
27 |
class RAGSystem:
|
28 |
def __init__(self, data_processor):
|
29 |
self.data_processor = data_processor
|
30 |
+
self.model = pipeline(
|
31 |
+
"text-generation",
|
32 |
+
model="google/flan-t5-base", # Using a smaller model suitable for Spaces
|
33 |
+
device=-1 # Use CPU
|
34 |
+
)
|
35 |
+
logger.info("Initialized RAG system with flan-t5-base model")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
def generate(self, prompt):
|
38 |
try:
|
39 |
+
response = self.model(
|
40 |
+
prompt,
|
41 |
+
max_length=512,
|
42 |
+
min_length=64,
|
43 |
+
num_return_sequences=1
|
44 |
+
)[0]['generated_text']
|
45 |
+
return response
|
46 |
except Exception as e:
|
47 |
+
logger.error(f"Error generating response: {e}")
|
48 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
def get_prompt(self, user_query, relevant_docs):
|
51 |
context = "\n".join([doc['content'] for doc in relevant_docs])
|
|
|
59 |
if not index_name:
|
60 |
raise ValueError("No index name provided. Please select a video and ensure it has been processed.")
|
61 |
|
62 |
+
relevant_docs = self.data_processor.search(
|
63 |
+
user_query,
|
64 |
+
num_results=3,
|
65 |
+
method=search_method,
|
66 |
+
index_name=index_name
|
67 |
+
)
|
68 |
|
69 |
if not relevant_docs:
|
70 |
logger.warning("No relevant documents found for the query.")
|
71 |
return "I couldn't find any relevant information to answer your query.", ""
|
72 |
|
73 |
prompt = self.get_prompt(user_query, relevant_docs)
|
74 |
+
answer = self.generate(prompt)
|
75 |
|
76 |
+
if not answer:
|
77 |
+
return "I encountered an error generating the response.", prompt
|
78 |
+
|
|
|
|
|
|
|
79 |
return answer, prompt
|
80 |
+
|
81 |
except Exception as e:
|
82 |
logger.error(f"An error occurred in the RAG system: {e}")
|
83 |
return f"An error occurred: {str(e)}", ""
|
84 |
|
85 |
def rewrite_cot(self, query):
|
86 |
+
prompt = f"""
|
87 |
+
Think through this step by step:
|
88 |
+
1. Original query: {query}
|
89 |
+
2. What are the key components of this query?
|
90 |
+
3. How can we break this down into a clearer question?
|
91 |
+
|
92 |
+
Rewritten query:
|
93 |
+
"""
|
94 |
response = self.generate(prompt)
|
95 |
if response:
|
96 |
return response, prompt
|
97 |
+
return query, prompt
|
98 |
|
99 |
def rewrite_react(self, query):
|
100 |
+
prompt = f"""
|
101 |
+
Let's approach this step-by-step:
|
102 |
+
1. Question: {query}
|
103 |
+
2. What information do we need?
|
104 |
+
3. What's the best way to structure this query?
|
105 |
+
|
106 |
+
Rewritten query:
|
107 |
+
"""
|
108 |
response = self.generate(prompt)
|
109 |
if response:
|
110 |
return response, prompt
|
111 |
+
return query, prompt
|