Spaces:
Sleeping
Sleeping
Update query_index.py
Browse files- query_index.py +52 -38
query_index.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
-
|
| 2 |
import os
|
| 3 |
import logging
|
| 4 |
from typing import Dict, List, Optional
|
| 5 |
from dotenv import load_dotenv
|
|
|
|
| 6 |
|
| 7 |
from llama_index.core import (
|
| 8 |
StorageContext,
|
|
@@ -43,7 +43,10 @@ class MultimodalRAGSystem:
|
|
| 43 |
self.config = MultimodalRAGConfig()
|
| 44 |
self.index = None
|
| 45 |
self.query_engine = None
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
self._initialize_system()
|
| 48 |
|
| 49 |
def _initialize_system(self):
|
|
@@ -87,48 +90,47 @@ class MultimodalRAGSystem:
|
|
| 87 |
|
| 88 |
logger.info(f"System Ready! Model: {self.config.LLM_MODEL}")
|
| 89 |
|
| 90 |
-
def
|
| 91 |
"""
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
"""
|
| 95 |
-
if not
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
history_text = "\n".join(
|
| 99 |
-
f"{turn['role'].capitalize()}: {turn['content']}"
|
| 100 |
-
for turn in chat_history[-4:] # last 2 turns
|
| 101 |
-
)
|
| 102 |
|
| 103 |
-
|
| 104 |
-
You are rewriting a follow-up question into a standalone question.
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
|
|
|
| 114 |
|
| 115 |
-
rewritten = self.query_engine._llm.complete(prompt)
|
| 116 |
-
return rewritten.text.strip()
|
| 117 |
-
|
| 118 |
-
def ask(self, question: str, chat_history: Optional[List[Dict[str, str]]] = None) -> Dict:
|
| 119 |
-
"""
|
| 120 |
-
Ask a question and return answer + source images
|
| 121 |
-
"""
|
| 122 |
-
if not self.query_engine:
|
| 123 |
-
raise RuntimeError("Query engine not initialized")
|
| 124 |
-
|
| 125 |
-
logger.info(f"Original question: {question}")
|
| 126 |
-
|
| 127 |
-
if chat_history:
|
| 128 |
-
standalone_question = self._rewrite_question(question, chat_history)
|
| 129 |
logger.info(f"Rewritten question: {standalone_question}")
|
| 130 |
-
else:
|
| 131 |
-
standalone_question = question
|
| 132 |
|
| 133 |
response = self.query_engine.query(standalone_question)
|
| 134 |
|
|
@@ -196,12 +198,24 @@ class MultimodalRAGSystem:
|
|
| 196 |
def main():
|
| 197 |
try:
|
| 198 |
rag = MultimodalRAGSystem()
|
|
|
|
| 199 |
while True:
|
| 200 |
q = input("Query (q to quit): ")
|
| 201 |
if q.lower() == 'q': break
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
except Exception as e:
|
| 204 |
print(f"Error: {e}")
|
| 205 |
|
| 206 |
if __name__ == "__main__":
|
| 207 |
-
main()
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
from typing import Dict, List, Optional
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
+
from llama_index.llms.openai import OpenAI
|
| 6 |
|
| 7 |
from llama_index.core import (
|
| 8 |
StorageContext,
|
|
|
|
| 43 |
self.config = MultimodalRAGConfig()
|
| 44 |
self.index = None
|
| 45 |
self.query_engine = None
|
| 46 |
+
self.rewrite_llm = OpenAI(
|
| 47 |
+
model="gpt-4o-mini",
|
| 48 |
+
temperature=0.0
|
| 49 |
+
)
|
| 50 |
self._initialize_system()
|
| 51 |
|
| 52 |
def _initialize_system(self):
|
|
|
|
| 90 |
|
| 91 |
logger.info(f"System Ready! Model: {self.config.LLM_MODEL}")
|
| 92 |
|
| 93 |
+
def ask(self, query_str: str, chat_history: Optional[List[Dict[str, str]]] = None) -> Dict:
|
| 94 |
"""
|
| 95 |
+
Query the RAG system with optional chat history for context.
|
| 96 |
+
|
| 97 |
+
Args:
|
| 98 |
+
query_str: The user's question
|
| 99 |
+
chat_history: List of dicts with 'role' and 'content' keys
|
| 100 |
+
|
| 101 |
+
Returns:
|
| 102 |
+
Dict with 'answer', 'images', and 'texts' keys
|
| 103 |
"""
|
| 104 |
+
if not self.query_engine:
|
| 105 |
+
raise RuntimeError("Query engine not initialized")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
logger.info(f"Original question: {query_str}")
|
|
|
|
| 108 |
|
| 109 |
+
# Rewrite follow-up into standalone question if history exists
|
| 110 |
+
standalone_question = query_str
|
| 111 |
|
| 112 |
+
if chat_history and len(chat_history) > 0:
|
| 113 |
+
# Convert chat history to context string
|
| 114 |
+
history_text = "\n".join(
|
| 115 |
+
f"{turn['role'].capitalize()}: {turn['content']}"
|
| 116 |
+
for turn in chat_history[-4:] # last 2 turns (4 messages)
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
rewrite_prompt = (
|
| 120 |
+
"Given the previous conversation and the follow-up question, "
|
| 121 |
+
"rewrite the follow-up question as a standalone question that includes all necessary context.\n\n"
|
| 122 |
+
f"Conversation history:\n{history_text}\n\n"
|
| 123 |
+
f"Follow-up question:\n{query_str}\n\n"
|
| 124 |
+
"Rewrite this as a standalone question that can be understood without the conversation history. "
|
| 125 |
+
"Only output the rewritten question, nothing else.\n\n"
|
| 126 |
+
"Standalone question:"
|
| 127 |
+
)
|
| 128 |
|
| 129 |
+
standalone_question = self.rewrite_llm.complete(
|
| 130 |
+
rewrite_prompt
|
| 131 |
+
).text.strip()
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
logger.info(f"Rewritten question: {standalone_question}")
|
|
|
|
|
|
|
| 134 |
|
| 135 |
response = self.query_engine.query(standalone_question)
|
| 136 |
|
|
|
|
| 198 |
def main():
|
| 199 |
try:
|
| 200 |
rag = MultimodalRAGSystem()
|
| 201 |
+
chat_hist = []
|
| 202 |
while True:
|
| 203 |
q = input("Query (q to quit): ")
|
| 204 |
if q.lower() == 'q': break
|
| 205 |
+
|
| 206 |
+
result = rag.ask(q, chat_history=chat_hist)
|
| 207 |
+
print(f"\nAnswer: {result['answer']}\n")
|
| 208 |
+
|
| 209 |
+
# Update history
|
| 210 |
+
chat_hist.append({"role": "user", "content": q})
|
| 211 |
+
chat_hist.append({"role": "assistant", "content": result['answer']})
|
| 212 |
+
|
| 213 |
+
# Keep history reasonable
|
| 214 |
+
if len(chat_hist) > 6:
|
| 215 |
+
chat_hist = chat_hist[-6:]
|
| 216 |
+
|
| 217 |
except Exception as e:
|
| 218 |
print(f"Error: {e}")
|
| 219 |
|
| 220 |
if __name__ == "__main__":
|
| 221 |
+
main()
|