Spaces:
Running
Running
Create pipeline/RAGnarok.py
Browse files- pipeline/RAGnarok.py +57 -0
pipeline/RAGnarok.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from chromadb.config import Settings
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
from pytz import timezone
|
| 7 |
+
|
| 8 |
+
# Set up environment and paths
|
| 9 |
+
load_dotenv()
|
| 10 |
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
| 11 |
+
|
| 12 |
+
# Import from project structure
|
| 13 |
+
from agents.llm import wake_llm
|
| 14 |
+
from vector_stores.L_vecdB import LongTermDatabase
|
| 15 |
+
from vector_stores.S_vecdB import ShortTermDatabase
|
| 16 |
+
from langchain_core.exceptions import OutputParserException
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# # Initialize vector DBs
|
| 20 |
+
# longdb = LongTermDatabase(persist_directory="longterm_db")
|
| 21 |
+
# shortdb = ShortTermDatabase(client_settings=Settings(persist_directory="shortterm_db"))
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class RAGnarok:
|
| 25 |
+
def __init__(self, longdb, shortdb, model="deepseek-r1-distill-llama-70b"):
|
| 26 |
+
self.llm_agent = wake_llm(longdb, shortdb, model=model)
|
| 27 |
+
|
| 28 |
+
def invoke(self, query: str) -> str:
|
| 29 |
+
try:
|
| 30 |
+
current_time = datetime.now(timezone('Asia/Kolkata')).strftime('%A, %Y-%m-%d %H:%M:%S')
|
| 31 |
+
# Combine current_time into the input key
|
| 32 |
+
response = self.llm_agent.invoke({"input": f"{query} (Current time: {current_time})"})
|
| 33 |
+
|
| 34 |
+
# Response could be a string or a dict
|
| 35 |
+
if isinstance(response, dict):
|
| 36 |
+
if "output" in response:
|
| 37 |
+
return response["output"]
|
| 38 |
+
return str(response)
|
| 39 |
+
return str(response)
|
| 40 |
+
|
| 41 |
+
except OutputParserException as e:
|
| 42 |
+
raw_output = getattr(e, "llm_output", "Unavailable")
|
| 43 |
+
return f"[❌ Parsing Error] {str(e)}\n[Raw Output]: {raw_output}"
|
| 44 |
+
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"[❌ Error] {str(e)}"
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
# CLI interface
|
| 51 |
+
ragnarok = RAGnarok(longdb, shortdb)
|
| 52 |
+
query = input("Enter your query: ")
|
| 53 |
+
response = ragnarok.invoke(query)
|
| 54 |
+
|
| 55 |
+
print("\n🧠 RAGnarok Response:")
|
| 56 |
+
print(response)
|
| 57 |
+
print("\n--- End of Response ---")
|