Spaces:
Sleeping
Sleeping
viraj commited on
Commit ·
0cb2687
1
Parent(s): 8529242
chat questions and answer retains.
Browse files- app.py +32 -2
- rag_pipeline.py +11 -10
app.py
CHANGED
|
@@ -1,5 +1,12 @@
|
|
| 1 |
from rag_pipeline import process_file, answer_query
|
| 2 |
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
class QueryRequest(BaseModel):
|
| 5 |
file_id: str
|
|
@@ -22,7 +29,6 @@ load_dotenv()
|
|
| 22 |
CHROMA_DIR = "./chroma_db"
|
| 23 |
embedding_model = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 24 |
|
| 25 |
-
|
| 26 |
app = FastAPI()
|
| 27 |
BASE_DIR = "files"
|
| 28 |
app.add_middleware(
|
|
@@ -32,6 +38,10 @@ app.add_middleware(
|
|
| 32 |
allow_headers=["*"],
|
| 33 |
)
|
| 34 |
file_store = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
@app.get("/test")
|
| 36 |
async def test():
|
| 37 |
return {"message": "hello world!"}
|
|
@@ -104,9 +114,25 @@ async def query_endpoint(request = Body(...)):
|
|
| 104 |
for context in contexts
|
| 105 |
)
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
# Get the answer using the enhanced context
|
| 108 |
answer = answer_query(question, formatted_context, explain_like_5)
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
return {
|
| 111 |
"answer": answer,
|
| 112 |
"context_used": formatted_context # Optionally return context for debugging
|
|
@@ -146,6 +172,10 @@ async def delete_file(file_id: str):
|
|
| 146 |
except Exception as e:
|
| 147 |
print(f"Error deleting file {filename}: {str(e)}")
|
| 148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
if not matching_files and not os.path.exists(chroma_path):
|
| 150 |
raise HTTPException(
|
| 151 |
status_code=404,
|
|
@@ -153,7 +183,7 @@ async def delete_file(file_id: str):
|
|
| 153 |
)
|
| 154 |
|
| 155 |
return {
|
| 156 |
-
"message": "File and
|
| 157 |
"deleted_files": matching_files,
|
| 158 |
"embeddings_deleted": os.path.exists(chroma_path)
|
| 159 |
}
|
|
|
|
| 1 |
from rag_pipeline import process_file, answer_query
|
| 2 |
from pydantic import BaseModel
|
| 3 |
+
from typing import List, Dict, Optional
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
class ChatMessage(BaseModel):
|
| 7 |
+
question: str
|
| 8 |
+
answer: str
|
| 9 |
+
timestamp: datetime
|
| 10 |
|
| 11 |
class QueryRequest(BaseModel):
|
| 12 |
file_id: str
|
|
|
|
| 29 |
CHROMA_DIR = "./chroma_db"
|
| 30 |
embedding_model = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 31 |
|
|
|
|
| 32 |
app = FastAPI()
|
| 33 |
BASE_DIR = "files"
|
| 34 |
app.add_middleware(
|
|
|
|
| 38 |
allow_headers=["*"],
|
| 39 |
)
|
| 40 |
file_store = {}
|
| 41 |
+
|
| 42 |
+
# Add chat memory store
|
| 43 |
+
chat_memory: Dict[str, List[ChatMessage]] = {}
|
| 44 |
+
|
| 45 |
@app.get("/test")
|
| 46 |
async def test():
|
| 47 |
return {"message": "hello world!"}
|
|
|
|
| 114 |
for context in contexts
|
| 115 |
)
|
| 116 |
|
| 117 |
+
# Add chat history to context if it exists
|
| 118 |
+
if file_id in chat_memory and chat_memory[file_id]:
|
| 119 |
+
chat_history = "\n\nPrevious Conversation:\n"
|
| 120 |
+
for msg in chat_memory[file_id][-3:]: # Include last 3 exchanges
|
| 121 |
+
chat_history += f"Q: {msg.question}\nA: {msg.answer}\n\n"
|
| 122 |
+
formatted_context = chat_history + formatted_context
|
| 123 |
+
|
| 124 |
# Get the answer using the enhanced context
|
| 125 |
answer = answer_query(question, formatted_context, explain_like_5)
|
| 126 |
|
| 127 |
+
# Store the Q&A in chat memory
|
| 128 |
+
if file_id not in chat_memory:
|
| 129 |
+
chat_memory[file_id] = []
|
| 130 |
+
chat_memory[file_id].append(ChatMessage(
|
| 131 |
+
question=question,
|
| 132 |
+
answer=answer,
|
| 133 |
+
timestamp=datetime.now()
|
| 134 |
+
))
|
| 135 |
+
|
| 136 |
return {
|
| 137 |
"answer": answer,
|
| 138 |
"context_used": formatted_context # Optionally return context for debugging
|
|
|
|
| 172 |
except Exception as e:
|
| 173 |
print(f"Error deleting file {filename}: {str(e)}")
|
| 174 |
|
| 175 |
+
# 3. Clear chat memory for this file
|
| 176 |
+
if file_id in chat_memory:
|
| 177 |
+
del chat_memory[file_id]
|
| 178 |
+
|
| 179 |
if not matching_files and not os.path.exists(chroma_path):
|
| 180 |
raise HTTPException(
|
| 181 |
status_code=404,
|
|
|
|
| 183 |
)
|
| 184 |
|
| 185 |
return {
|
| 186 |
+
"message": "File, embeddings, and chat history deleted successfully",
|
| 187 |
"deleted_files": matching_files,
|
| 188 |
"embeddings_deleted": os.path.exists(chroma_path)
|
| 189 |
}
|
rag_pipeline.py
CHANGED
|
@@ -91,18 +91,19 @@ def answer_query(question, context, explain_like_5=False):
|
|
| 91 |
context = "\n\n".join(str(c) for c in context)
|
| 92 |
|
| 93 |
system_prompt = (
|
| 94 |
-
"You are a helpful assistant answering user queries based
|
| 95 |
"IMPORTANT RULES:\n"
|
| 96 |
-
"1.
|
| 97 |
-
"2. If the answer cannot be fully derived from the context, say 'I cannot answer this question based on the provided
|
| 98 |
"3. If you're unsure about any part of the answer, acknowledge the uncertainty.\n"
|
| 99 |
-
"4. Do not make assumptions beyond what's explicitly stated in the context.\n"
|
| 100 |
-
"5. Quote relevant parts of the context to support your answers when possible."
|
|
|
|
| 101 |
)
|
| 102 |
|
| 103 |
if explain_like_5:
|
| 104 |
-
system_prompt += "\nExplain the answer in a simple way, like you're talking to a 5-year-old, but still only use information from the context."
|
| 105 |
-
|
| 106 |
try:
|
| 107 |
# Send to LLM with formatted prompt
|
| 108 |
response = client.chat.completions.create(
|
|
@@ -110,10 +111,10 @@ def answer_query(question, context, explain_like_5=False):
|
|
| 110 |
messages=[
|
| 111 |
{"role": "system", "content": system_prompt},
|
| 112 |
{"role": "user", "content": (
|
| 113 |
-
f"Context:\n{context}\n\n"
|
| 114 |
f"Question: {question}\n\n"
|
| 115 |
-
"Remember to answer
|
| 116 |
-
"If you cannot find the answer in
|
| 117 |
)}
|
| 118 |
],
|
| 119 |
temperature=0.3 # Lower temperature for more focused answers
|
|
|
|
| 91 |
context = "\n\n".join(str(c) for c in context)
|
| 92 |
|
| 93 |
system_prompt = (
|
| 94 |
+
"You are a helpful assistant answering user queries based on the provided document chunks and conversation history.\n"
|
| 95 |
"IMPORTANT RULES:\n"
|
| 96 |
+
"1. Use information from both the document context and previous conversation history.\n"
|
| 97 |
+
"2. If the answer cannot be fully derived from the context or conversation history, say 'I cannot answer this question based on the provided information.'\n"
|
| 98 |
"3. If you're unsure about any part of the answer, acknowledge the uncertainty.\n"
|
| 99 |
+
"4. Do not make assumptions beyond what's explicitly stated in the context or conversation history.\n"
|
| 100 |
+
"5. Quote relevant parts of the context to support your answers when possible.\n"
|
| 101 |
+
"6. When referencing previous conversation, be clear about which information came from where."
|
| 102 |
)
|
| 103 |
|
| 104 |
if explain_like_5:
|
| 105 |
+
system_prompt += "\nExplain the answer in a simple way, like you're talking to a 5-year-old, but still only use information from the context and conversation history."
|
| 106 |
+
|
| 107 |
try:
|
| 108 |
# Send to LLM with formatted prompt
|
| 109 |
response = client.chat.completions.create(
|
|
|
|
| 111 |
messages=[
|
| 112 |
{"role": "system", "content": system_prompt},
|
| 113 |
{"role": "user", "content": (
|
| 114 |
+
f"Context and Conversation History:\n{context}\n\n"
|
| 115 |
f"Question: {question}\n\n"
|
| 116 |
+
"Remember to answer based on both the document context and conversation history above. "
|
| 117 |
+
"If you cannot find the answer in either, say so explicitly."
|
| 118 |
)}
|
| 119 |
],
|
| 120 |
temperature=0.3 # Lower temperature for more focused answers
|