Mohammad Wasil
commited on
Commit
·
6117f75
1
Parent(s):
a760e19
Fix frontend connection: use relative API path
Browse files- Dockerfile +3 -1
- rag_with_memory.py +1 -1
- tools.py +100 -44
Dockerfile
CHANGED
|
@@ -26,7 +26,9 @@ COPY --chown=appuser:appuser . .
|
|
| 26 |
EXPOSE 7860
|
| 27 |
|
| 28 |
USER appuser
|
| 29 |
-
RUN chmod -R 777 /app/data && chmod -R 777 /app/chroma_db
|
|
|
|
|
|
|
| 30 |
# Update Healthcheck in Dockerfile
|
| 31 |
HEALTHCHECK --interval=30s --timeout=3s \
|
| 32 |
CMD curl --fail http://localhost:7860/health || exit 1
|
|
|
|
| 26 |
EXPOSE 7860
|
| 27 |
|
| 28 |
USER appuser
|
| 29 |
+
# RUN chmod -R 777 /app/data && chmod -R 777 /app/chroma_db
|
| 30 |
+
COPY ./data /app/data
|
| 31 |
+
RUN chmod -R 777 /app/data
|
| 32 |
# Update Healthcheck in Dockerfile
|
| 33 |
HEALTHCHECK --interval=30s --timeout=3s \
|
| 34 |
CMD curl --fail http://localhost:7860/health || exit 1
|
rag_with_memory.py
CHANGED
|
@@ -39,7 +39,7 @@ class MemoryRAG:
|
|
| 39 |
|
| 40 |
# 1. Load and chunk documents
|
| 41 |
loader = DirectoryLoader(docs_path, glob="**/*.md",
|
| 42 |
-
loader_cls=TextLoader,
|
| 43 |
docs = loader.load()
|
| 44 |
logger.info(f"RAG DATABASE STATUS: Loaded {len(docs)} documents from {docs_path}")
|
| 45 |
if not docs:
|
|
|
|
| 39 |
|
| 40 |
# 1. Load and chunk documents
|
| 41 |
loader = DirectoryLoader(docs_path, glob="**/*.md",
|
| 42 |
+
loader_cls=TextLoader, silent_errors=False)
|
| 43 |
docs = loader.load()
|
| 44 |
logger.info(f"RAG DATABASE STATUS: Loaded {len(docs)} documents from {docs_path}")
|
| 45 |
if not docs:
|
tools.py
CHANGED
|
@@ -1,60 +1,116 @@
|
|
| 1 |
-
"""
|
| 2 |
-
|
| 3 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
import os
|
| 6 |
from langchain_core.tools import tool
|
| 7 |
from pydantic import BaseModel, Field
|
| 8 |
from rag_with_memory import MemoryRAG
|
| 9 |
-
import glob
|
| 10 |
from loguru import logger
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
except Exception as e:
|
| 35 |
-
logger.exception(f"Failed to initialize MemoryRAG: {e}")
|
| 36 |
-
rag_engine = None
|
| 37 |
|
| 38 |
class KnowledgeBaseInput(BaseModel):
|
| 39 |
-
query: str = Field(description="
|
| 40 |
|
| 41 |
@tool(args_schema=KnowledgeBaseInput, return_direct=True)
|
| 42 |
def knowledge_base_search(query: str) -> str:
|
| 43 |
-
"""Search
|
|
|
|
| 44 |
|
| 45 |
-
if
|
| 46 |
-
logger.
|
| 47 |
-
return "I'm sorry,
|
| 48 |
|
| 49 |
try:
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
if not answer or "error" in answer.lower():
|
| 54 |
-
return "I couldn't find specific information about that in the knowledge base."
|
| 55 |
-
|
| 56 |
-
return answer
|
| 57 |
-
|
| 58 |
except Exception as e:
|
| 59 |
-
logger.error(f"
|
| 60 |
-
return "I encountered
|
|
|
|
| 1 |
+
# """
|
| 2 |
+
# This tools working correctly
|
| 3 |
+
# """
|
| 4 |
+
|
| 5 |
+
# import os
|
| 6 |
+
# from langchain_core.tools import tool
|
| 7 |
+
# from pydantic import BaseModel, Field
|
| 8 |
+
# from rag_with_memory import MemoryRAG
|
| 9 |
+
# import glob
|
| 10 |
+
# from loguru import logger
|
| 11 |
+
|
| 12 |
+
# possible_paths = [
|
| 13 |
+
# "/app/data/knowledge_base",
|
| 14 |
+
# "./data/knowledge_base",
|
| 15 |
+
# "./backend/data/knowledge_base"
|
| 16 |
+
# ]
|
| 17 |
+
|
| 18 |
+
# KNOWLEDGE_BASE_PATH = None
|
| 19 |
+
# for p in possible_paths:
|
| 20 |
+
# # Check if path exists AND contains .md files
|
| 21 |
+
# if os.path.exists(p) and glob.glob(os.path.join(p, "*.md")):
|
| 22 |
+
# KNOWLEDGE_BASE_PATH = p
|
| 23 |
+
# break
|
| 24 |
+
|
| 25 |
+
# if not KNOWLEDGE_BASE_PATH:
|
| 26 |
+
# logger.critical("No .md files found in any knowledge base path!")
|
| 27 |
+
# rag_engine = None
|
| 28 |
+
|
| 29 |
+
# else:
|
| 30 |
+
# logger.info(f"Knowledge Base detected at: {KNOWLEDGE_BASE_PATH}")
|
| 31 |
+
# try:
|
| 32 |
+
# rag_engine = MemoryRAG(docs_path=KNOWLEDGE_BASE_PATH)
|
| 33 |
+
# logger.success("RAG Engine initialized successfully.")
|
| 34 |
+
# except Exception as e:
|
| 35 |
+
# logger.exception(f"Failed to initialize MemoryRAG: {e}")
|
| 36 |
+
# rag_engine = None
|
| 37 |
+
|
| 38 |
+
# class KnowledgeBaseInput(BaseModel):
|
| 39 |
+
# query: str = Field(description="User's question about coffee products, resets, warranty, installation safety, maintenance procedures, or troubleshooting guide.")
|
| 40 |
+
|
| 41 |
+
# @tool(args_schema=KnowledgeBaseInput, return_direct=True)
|
| 42 |
+
# def knowledge_base_search(query: str) -> str:
|
| 43 |
+
# """Search product documentation and FAQs to provide accurate answers about company products."""
|
| 44 |
+
|
| 45 |
+
# if not rag_engine:
|
| 46 |
+
# logger.warning(f"Search attempted but RAG engine is None. Query: {query}")
|
| 47 |
+
# return "I'm sorry, my internal knowledge base is currently offline."
|
| 48 |
+
|
| 49 |
+
# try:
|
| 50 |
+
# result = rag_engine.query(query, session_id="agent_tool_session")
|
| 51 |
+
|
| 52 |
+
# answer = result.get("answer", "")
|
| 53 |
+
# if not answer or "error" in answer.lower():
|
| 54 |
+
# return "I couldn't find specific information about that in the knowledge base."
|
| 55 |
+
|
| 56 |
+
# return answer
|
| 57 |
+
|
| 58 |
+
# except Exception as e:
|
| 59 |
+
# logger.error(f"Error during RAG query: {e}")
|
| 60 |
+
# return "I encountered a technical error while searching documents."
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
|
| 69 |
import os
|
| 70 |
from langchain_core.tools import tool
|
| 71 |
from pydantic import BaseModel, Field
|
| 72 |
from rag_with_memory import MemoryRAG
|
|
|
|
| 73 |
from loguru import logger
|
| 74 |
|
| 75 |
+
# Global variable to hold the engine
|
| 76 |
+
_rag_engine = None
|
| 77 |
+
|
| 78 |
+
def get_rag_engine():
|
| 79 |
+
global _rag_engine
|
| 80 |
+
if _rag_engine is None:
|
| 81 |
+
# ABSOLUTE PATH for Docker 2026
|
| 82 |
+
target_path = "/app/data/knowledge_base"
|
| 83 |
+
|
| 84 |
+
# Verify folder exists and isn't empty
|
| 85 |
+
if not os.path.exists(target_path) or not os.listdir(target_path):
|
| 86 |
+
logger.error(f"KNOWLEDGE BASE MISSING at {target_path}")
|
| 87 |
+
return None
|
| 88 |
+
|
| 89 |
+
try:
|
| 90 |
+
logger.info("Initializing MemoryRAG engine...")
|
| 91 |
+
_rag_engine = MemoryRAG(docs_path=target_path)
|
| 92 |
+
logger.success("MemoryRAG engine is now ONLINE.")
|
| 93 |
+
except Exception as e:
|
| 94 |
+
logger.error(f"MemoryRAG Initialization failed: {e}")
|
| 95 |
+
return None
|
| 96 |
+
return _rag_engine
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
class KnowledgeBaseInput(BaseModel):
|
| 99 |
+
query: str = Field(description="The search query.")
|
| 100 |
|
| 101 |
@tool(args_schema=KnowledgeBaseInput, return_direct=True)
|
| 102 |
def knowledge_base_search(query: str) -> str:
|
| 103 |
+
"""Search the company knowledge base for answers."""
|
| 104 |
+
engine = get_rag_engine()
|
| 105 |
|
| 106 |
+
if engine is None:
|
| 107 |
+
logger.error("Tool called but RAG engine remains None.")
|
| 108 |
+
return "I'm sorry, I'm currently unable to access my internal documentation."
|
| 109 |
|
| 110 |
try:
|
| 111 |
+
# LOGIC FIX: Always return a string for the Agent to read
|
| 112 |
+
result = engine.query(query, session_id="tool_session")
|
| 113 |
+
return result.get("answer", "No information found.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
except Exception as e:
|
| 115 |
+
logger.error(f"Tool execution failed: {e}")
|
| 116 |
+
return "I encountered an error while searching."
|