Spaces:
Runtime error
Runtime error
Commit ·
de48ca2
1
Parent(s): 3c733fa
updated memory and semantic cache
Browse files- rag_pipeline.py +42 -27
- requirements.txt +0 -0
- semantic_cache_db/67c1dac6-5c62-4b53-a9ab-cf380440cd3e/data_level0.bin +3 -0
- semantic_cache_db/67c1dac6-5c62-4b53-a9ab-cf380440cd3e/header.bin +3 -0
- semantic_cache_db/67c1dac6-5c62-4b53-a9ab-cf380440cd3e/length.bin +3 -0
- semantic_cache_db/67c1dac6-5c62-4b53-a9ab-cf380440cd3e/link_lists.bin +0 -0
- semantic_cache_db/chroma.sqlite3 +3 -0
- vector_db/26c2daa6-1313-4de4-ba60-f695238ff587/data_level0.bin +1 -1
- vector_db/26c2daa6-1313-4de4-ba60-f695238ff587/length.bin +1 -1
- vector_db/chroma.sqlite3 +1 -1
rag_pipeline.py
CHANGED
|
@@ -21,6 +21,7 @@ load_dotenv()
|
|
| 21 |
|
| 22 |
# Constants
|
| 23 |
CHROMA_PATH = "vector_db"
|
|
|
|
| 24 |
DATA_PATH = "dataset"
|
| 25 |
|
| 26 |
# Embeddings Function
|
|
@@ -47,7 +48,6 @@ def build_vector_db():
|
|
| 47 |
md_docs = md_loader.load()
|
| 48 |
|
| 49 |
documents = pdf_docs + md_docs
|
| 50 |
-
# --------------------------------------
|
| 51 |
|
| 52 |
if not documents:
|
| 53 |
print("⚠️ Koi documents nahi mile. Empty DB return kar raha hoon.")
|
|
@@ -90,7 +90,7 @@ def get_rag_chain():
|
|
| 90 |
|
| 91 |
history_aware_retriever = create_history_aware_retriever(llm, retriever, contextualize_q_prompt)
|
| 92 |
|
| 93 |
-
#
|
| 94 |
system_prompt = (
|
| 95 |
"You are the exclusive AI Assistant for Hammas Shahzad Shani, an AI/ML Engineer. "
|
| 96 |
"Your objective is to provide precise, professional, and context-grounded information regarding Hammas Shahzad Shani's professional profile, experience, projects, skills, certifications, education, achievements, and work. "
|
|
@@ -115,19 +115,13 @@ def get_rag_chain():
|
|
| 115 |
" - Never use external knowledge.\n"
|
| 116 |
" - Never make assumptions.\n"
|
| 117 |
" - Never infer missing information.\n"
|
| 118 |
-
" - Never fabricate projects, experience, skills, achievements, education,
|
| 119 |
"\n"
|
| 120 |
"3. NO HALLUCINATION:\n"
|
| 121 |
" - If information is not present in the CONTEXT, immediately return the appropriate refusal message.\n"
|
| 122 |
" - Do not guess.\n"
|
| 123 |
-
" - Do not generate estimated answers.\n"
|
| 124 |
"\n"
|
| 125 |
-
"4. NO
|
| 126 |
-
" - Under NO circumstances generate source code, scripts, SQL queries, commands, APIs, configuration files, pseudocode, or code snippets.\n"
|
| 127 |
-
" - Even if the user asks for code as an example of Hammas Shahzad Shani's work, skills, or projects, immediately refuse.\n"
|
| 128 |
-
" - You may only describe projects at a high level if the information exists in the CONTEXT.\n"
|
| 129 |
-
"\n"
|
| 130 |
-
"5. NO GENERAL TECHNOLOGY EXPLANATIONS:\n"
|
| 131 |
" - Never explain technologies such as FastAPI, LangChain, YOLO, TensorFlow, Python, or any other technology in general.\n"
|
| 132 |
" - You may only explain how Hammas Shahzad Shani used a technology in a project explicitly mentioned in the CONTEXT.\n"
|
| 133 |
"\n\n"
|
|
@@ -137,18 +131,13 @@ def get_rag_chain():
|
|
| 137 |
"- Avoid greetings, filler phrases, small talk, and unnecessary explanations.\n"
|
| 138 |
"- Keep responses focused only on the user's question and the available CONTEXT.\n"
|
| 139 |
"\n\n"
|
| 140 |
-
"
|
| 141 |
-
"
|
| 142 |
-
"
|
| 143 |
-
"
|
| 144 |
-
"\n
|
| 145 |
-
"
|
| 146 |
-
"
|
| 147 |
-
"- Is the answer explicitly present in the CONTEXT?\n"
|
| 148 |
-
"- Does the response avoid code generation?\n"
|
| 149 |
-
"- Does the response mirror the user's language?\n"
|
| 150 |
-
"- Does the response avoid assumptions and hallucinations?\n"
|
| 151 |
-
"- If ANY answer is NO, return the appropriate refusal message.\n"
|
| 152 |
"\n\n"
|
| 153 |
"Context:\n{context}"
|
| 154 |
)
|
|
@@ -164,14 +153,37 @@ def get_rag_chain():
|
|
| 164 |
|
| 165 |
return rag_chain
|
| 166 |
|
| 167 |
-
#
|
| 168 |
-
def get_semantic_answer(rag_chain, user_input, chat_history=[]):
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
response = rag_chain.invoke({
|
| 171 |
"input": user_input,
|
| 172 |
"chat_history": chat_history
|
| 173 |
})
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
# Verification Block
|
| 177 |
if __name__ == "__main__":
|
|
@@ -179,5 +191,8 @@ if __name__ == "__main__":
|
|
| 179 |
if db:
|
| 180 |
print("\n✅ System is Operational.")
|
| 181 |
chain = get_rag_chain()
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
| 183 |
print(f"🤖 AI Test: {test_response}")
|
|
|
|
| 21 |
|
| 22 |
# Constants
|
| 23 |
CHROMA_PATH = "vector_db"
|
| 24 |
+
CACHE_PATH = "semantic_cache_db" # 🟢 Cache Path Add Kar Diya Hai
|
| 25 |
DATA_PATH = "dataset"
|
| 26 |
|
| 27 |
# Embeddings Function
|
|
|
|
| 48 |
md_docs = md_loader.load()
|
| 49 |
|
| 50 |
documents = pdf_docs + md_docs
|
|
|
|
| 51 |
|
| 52 |
if not documents:
|
| 53 |
print("⚠️ Koi documents nahi mile. Empty DB return kar raha hoon.")
|
|
|
|
| 90 |
|
| 91 |
history_aware_retriever = create_history_aware_retriever(llm, retriever, contextualize_q_prompt)
|
| 92 |
|
| 93 |
+
# Strict System Prompt + Emergency Code Stop
|
| 94 |
system_prompt = (
|
| 95 |
"You are the exclusive AI Assistant for Hammas Shahzad Shani, an AI/ML Engineer. "
|
| 96 |
"Your objective is to provide precise, professional, and context-grounded information regarding Hammas Shahzad Shani's professional profile, experience, projects, skills, certifications, education, achievements, and work. "
|
|
|
|
| 115 |
" - Never use external knowledge.\n"
|
| 116 |
" - Never make assumptions.\n"
|
| 117 |
" - Never infer missing information.\n"
|
| 118 |
+
" - Never fabricate projects, experience, skills, achievements, education, timelines, or personal information.\n"
|
| 119 |
"\n"
|
| 120 |
"3. NO HALLUCINATION:\n"
|
| 121 |
" - If information is not present in the CONTEXT, immediately return the appropriate refusal message.\n"
|
| 122 |
" - Do not guess.\n"
|
|
|
|
| 123 |
"\n"
|
| 124 |
+
"4. NO GENERAL TECHNOLOGY EXPLANATIONS:\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
" - Never explain technologies such as FastAPI, LangChain, YOLO, TensorFlow, Python, or any other technology in general.\n"
|
| 126 |
" - You may only explain how Hammas Shahzad Shani used a technology in a project explicitly mentioned in the CONTEXT.\n"
|
| 127 |
"\n\n"
|
|
|
|
| 131 |
"- Avoid greetings, filler phrases, small talk, and unnecessary explanations.\n"
|
| 132 |
"- Keep responses focused only on the user's question and the available CONTEXT.\n"
|
| 133 |
"\n\n"
|
| 134 |
+
"FINAL VALIDATION AND EMERGENCY STOP (CRITICAL):\n"
|
| 135 |
+
"Before you output anything, run this internal check:\n"
|
| 136 |
+
"1. Does the user's prompt ask for code, scripts, algorithms (like TwoSum, Fibonacci), or programming help?\n"
|
| 137 |
+
"2. Does your planned response contain ANY code block (e.g., ```python), functions, syntax, or pseudo-code?\n"
|
| 138 |
+
"IF YES TO EITHER: YOU MUST IMMEDIATELY ABORT. DO NOT explain anything. DO NOT apologize. JUST output exactly this sentence and nothing else:\n"
|
| 139 |
+
"Refusal: 'Main sirf Hammas Shahzad Shani ke professional background aur projects ke baare mein maloomat de sakta hoon. Main programming code generate nahi kar sakta.'\n"
|
| 140 |
+
"3. Does the planned response contain information outside the CONTEXT? If YES, use the standard refusal.\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
"\n\n"
|
| 142 |
"Context:\n{context}"
|
| 143 |
)
|
|
|
|
| 153 |
|
| 154 |
return rag_chain
|
| 155 |
|
| 156 |
+
# --- 🧠 SMART SEMANTIC CACHE LOGIC ---
|
| 157 |
+
def get_semantic_answer(rag_chain, user_input, chat_history=[], threshold=0.4):
|
| 158 |
+
embeddings = get_embeddings_function()
|
| 159 |
+
semantic_cache = Chroma(persist_directory=CACHE_PATH, embedding_function=embeddings)
|
| 160 |
+
|
| 161 |
+
# RULE: Cache sirf tab check karein jab chat_history khali ho (Pehla sawal ho)
|
| 162 |
+
# Taake "us k ilawa" jaise follow-up questions puranay cache se galat jawab na uthayen.
|
| 163 |
+
if not chat_history:
|
| 164 |
+
results = semantic_cache.similarity_search_with_score(user_input, k=1)
|
| 165 |
+
if results:
|
| 166 |
+
best_match, distance = results[0]
|
| 167 |
+
if distance < threshold:
|
| 168 |
+
print(f"\n🟢 CACHE HIT! Score: {distance:.4f} - Fetching instantly from DB...")
|
| 169 |
+
return best_match.metadata["answer"]
|
| 170 |
+
|
| 171 |
+
# Agar Cache mein nahi hai ya Chat History mojood hai, toh Groq API hit karein
|
| 172 |
+
print("\n🔴 CACHE MISS! Hitting Groq API with context & memory...")
|
| 173 |
response = rag_chain.invoke({
|
| 174 |
"input": user_input,
|
| 175 |
"chat_history": chat_history
|
| 176 |
})
|
| 177 |
+
answer = response["answer"]
|
| 178 |
+
|
| 179 |
+
# RULE: Naya jawab cache mein sirf tab save karein jab yeh standalone sawal ho
|
| 180 |
+
if not chat_history:
|
| 181 |
+
semantic_cache.add_texts(
|
| 182 |
+
texts=[user_input],
|
| 183 |
+
metadatas=[{"answer": answer}]
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
return answer
|
| 187 |
|
| 188 |
# Verification Block
|
| 189 |
if __name__ == "__main__":
|
|
|
|
| 191 |
if db:
|
| 192 |
print("\n✅ System is Operational.")
|
| 193 |
chain = get_rag_chain()
|
| 194 |
+
|
| 195 |
+
test_q = "Hi, Who is Hammas?"
|
| 196 |
+
print(f"\n👤 User: {test_q}")
|
| 197 |
+
test_response = get_semantic_answer(chain, test_q, [])
|
| 198 |
print(f"🤖 AI Test: {test_response}")
|
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|
semantic_cache_db/67c1dac6-5c62-4b53-a9ab-cf380440cd3e/data_level0.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:699e3c6b7da103a1ca94aa949a42167439996a67454b8fc2998ea00704976db5
|
| 3 |
+
size 167600
|
semantic_cache_db/67c1dac6-5c62-4b53-a9ab-cf380440cd3e/header.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a0e81c3b22454233bc12d0762f06dcca48261a75231cf87c79b75e69a6c00150
|
| 3 |
+
size 100
|
semantic_cache_db/67c1dac6-5c62-4b53-a9ab-cf380440cd3e/length.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:36a32280ad42bec1ec45ee0de76eb28e83ead12166132277574d200ba6b4b857
|
| 3 |
+
size 400
|
semantic_cache_db/67c1dac6-5c62-4b53-a9ab-cf380440cd3e/link_lists.bin
ADDED
|
File without changes
|
semantic_cache_db/chroma.sqlite3
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:aad8a7016ed424f3800780bf3c92c040cd44b759dfa2c921522ece98903dff8c
|
| 3 |
+
size 188416
|
vector_db/26c2daa6-1313-4de4-ba60-f695238ff587/data_level0.bin
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 167600
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5261a249dcbf70d0f43bb90d948f06634089ed76cccec833c02bd1eb8b8d8370
|
| 3 |
size 167600
|
vector_db/26c2daa6-1313-4de4-ba60-f695238ff587/length.bin
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 400
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7a12e561363385e9dfeeab326368731c030ed4b374e7f5897ac819159d2884c5
|
| 3 |
size 400
|
vector_db/chroma.sqlite3
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 331776
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2109d111a65d6522602ba860762a26c00729d49b7396f1429e05d29fff1875b0
|
| 3 |
size 331776
|