Upload src/eva_chatbot.py with huggingface_hub
Browse files- src/eva_chatbot.py +38 -24
src/eva_chatbot.py
CHANGED
|
@@ -3,8 +3,22 @@ EVA - Enterprise Virtual Assistant
|
|
| 3 |
Complete chatbot: greeting handling + router + memory + hybrid retrieval + extraction
|
| 4 |
"""
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def is_small_talk(query, groq_client, generate_with_groq_fn):
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
rather than an actual question needing information lookup? Answer ONLY "yes" or "no".
|
| 9 |
|
| 10 |
Message: {query}"""
|
|
@@ -14,7 +28,7 @@ Message: {query}"""
|
|
| 14 |
|
| 15 |
def handle_small_talk(query, groq_client, generate_with_groq_fn, bot_name="EVA"):
|
| 16 |
prompt = f"""You are {bot_name}, a friendly enterprise knowledge assistant chatbot for HR, Legal, Finance, and IT questions.
|
| 17 |
-
Respond naturally and briefly to this casual message. If it
|
| 18 |
|
| 19 |
Message: {query}
|
| 20 |
|
|
@@ -24,42 +38,42 @@ Response:"""
|
|
| 24 |
|
| 25 |
def ask_eva(query, conversation_history, embedding_model, index, all_chunks, groq_client,
|
| 26 |
router_model, tokenizer, label_encoder, domain_indices, domain_chunks_map,
|
| 27 |
-
qa_model, qa_tokenizer, classify_query_fn, retrieve_hybrid_fn, generate_with_groq_fn,
|
| 28 |
extract_exact_answer_fn, top_k=5, hallucination_threshold=0.95, bot_name="EVA"):
|
| 29 |
-
|
| 30 |
if is_small_talk(query, groq_client, generate_with_groq_fn):
|
| 31 |
answer = handle_small_talk(query, groq_client, generate_with_groq_fn, bot_name)
|
| 32 |
-
conversation_history.append({
|
| 33 |
-
return {
|
| 34 |
-
|
| 35 |
domain, confidence_or_probs = classify_query_fn(query, router_model, tokenizer, label_encoder)
|
| 36 |
-
|
| 37 |
history_text = ""
|
| 38 |
if conversation_history:
|
| 39 |
-
history_text = "\n".join([f"User: {h['question']}\nAssistant: {h['answer']}"
|
| 40 |
for h in conversation_history[-3:]])
|
| 41 |
-
|
| 42 |
rewrite_prompt = f"""Given this conversation history:
|
| 43 |
{history_text}
|
| 44 |
|
| 45 |
-
Rewrite the NEW question to be a clear, standalone, explicit search query — resolving any references
|
| 46 |
to earlier parts of the conversation. Keep it short. Only output the rewritten question.
|
| 47 |
|
| 48 |
New question: {query}
|
| 49 |
|
| 50 |
Rewritten question:"""
|
| 51 |
rewritten = generate_with_groq_fn(rewrite_prompt, groq_client).strip()
|
| 52 |
-
|
| 53 |
-
results = retrieve_hybrid_fn(rewritten, domain, embedding_model, domain_indices, domain_chunks_map,
|
| 54 |
index, all_chunks, top_k=top_k)
|
| 55 |
-
|
| 56 |
best_distance = results[0][0]
|
| 57 |
if best_distance > hallucination_threshold:
|
| 58 |
-
answer = f"I
|
| 59 |
-
conversation_history.append({
|
| 60 |
-
return {
|
| 61 |
-
|
| 62 |
-
context_text = "\n\n".join([f"[Source: {c['domain']} - {c['title']}]\n{c['text']}" for dist, c in results])
|
| 63 |
prompt = f"""You are {bot_name}, an enterprise knowledge assistant having an ongoing conversation.
|
| 64 |
|
| 65 |
Conversation so far:
|
|
@@ -74,8 +88,8 @@ New question: {query}
|
|
| 74 |
|
| 75 |
Answer:"""
|
| 76 |
answer = generate_with_groq_fn(prompt, groq_client)
|
| 77 |
-
|
| 78 |
-
exact_quote, _ = extract_exact_answer_fn(rewritten, results[0][1][
|
| 79 |
-
|
| 80 |
-
conversation_history.append({
|
| 81 |
-
return {
|
|
|
|
| 3 |
Complete chatbot: greeting handling + router + memory + hybrid retrieval + extraction
|
| 4 |
"""
|
| 5 |
|
| 6 |
+
# Fast, instant-match greetings — checked BEFORE any LLM call.
|
| 7 |
+
GREETINGS = {
|
| 8 |
+
"hi", "hello", "hey", "yo", "sup",
|
| 9 |
+
"hi eva", "hello eva", "hey eva",
|
| 10 |
+
"good morning", "good afternoon", "good evening",
|
| 11 |
+
"thanks", "thank you", "thanks eva", "thank you eva",
|
| 12 |
+
"bye", "goodbye", "see you", "ok", "okay", "cool",
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
|
| 16 |
def is_small_talk(query, groq_client, generate_with_groq_fn):
|
| 17 |
+
normalized = query.lower().strip().strip("!?.,")
|
| 18 |
+
if normalized in GREETINGS:
|
| 19 |
+
return True
|
| 20 |
+
|
| 21 |
+
prompt = f"""Is this message small talk/greeting/casual conversation (like "hi", "hello", "how are you", "thanks", "bye")
|
| 22 |
rather than an actual question needing information lookup? Answer ONLY "yes" or "no".
|
| 23 |
|
| 24 |
Message: {query}"""
|
|
|
|
| 28 |
|
| 29 |
def handle_small_talk(query, groq_client, generate_with_groq_fn, bot_name="EVA"):
|
| 30 |
prompt = f"""You are {bot_name}, a friendly enterprise knowledge assistant chatbot for HR, Legal, Finance, and IT questions.
|
| 31 |
+
Respond naturally and briefly to this casual message. If it is a greeting, introduce yourself briefly and invite them to ask a question.
|
| 32 |
|
| 33 |
Message: {query}
|
| 34 |
|
|
|
|
| 38 |
|
| 39 |
def ask_eva(query, conversation_history, embedding_model, index, all_chunks, groq_client,
|
| 40 |
router_model, tokenizer, label_encoder, domain_indices, domain_chunks_map,
|
| 41 |
+
qa_model, qa_tokenizer, classify_query_fn, retrieve_hybrid_fn, generate_with_groq_fn,
|
| 42 |
extract_exact_answer_fn, top_k=5, hallucination_threshold=0.95, bot_name="EVA"):
|
| 43 |
+
|
| 44 |
if is_small_talk(query, groq_client, generate_with_groq_fn):
|
| 45 |
answer = handle_small_talk(query, groq_client, generate_with_groq_fn, bot_name)
|
| 46 |
+
conversation_history.append({"question": query, "answer": answer})
|
| 47 |
+
return {"answer": answer, "exact_quote": None, "source": None, "domain": None}
|
| 48 |
+
|
| 49 |
domain, confidence_or_probs = classify_query_fn(query, router_model, tokenizer, label_encoder)
|
| 50 |
+
|
| 51 |
history_text = ""
|
| 52 |
if conversation_history:
|
| 53 |
+
history_text = "\n".join([f"User: {h[\'question\']}\nAssistant: {h[\'answer\']}"
|
| 54 |
for h in conversation_history[-3:]])
|
| 55 |
+
|
| 56 |
rewrite_prompt = f"""Given this conversation history:
|
| 57 |
{history_text}
|
| 58 |
|
| 59 |
+
Rewrite the NEW question to be a clear, standalone, explicit search query — resolving any references
|
| 60 |
to earlier parts of the conversation. Keep it short. Only output the rewritten question.
|
| 61 |
|
| 62 |
New question: {query}
|
| 63 |
|
| 64 |
Rewritten question:"""
|
| 65 |
rewritten = generate_with_groq_fn(rewrite_prompt, groq_client).strip()
|
| 66 |
+
|
| 67 |
+
results = retrieve_hybrid_fn(rewritten, domain, embedding_model, domain_indices, domain_chunks_map,
|
| 68 |
index, all_chunks, top_k=top_k)
|
| 69 |
+
|
| 70 |
best_distance = results[0][0]
|
| 71 |
if best_distance > hallucination_threshold:
|
| 72 |
+
answer = f"I am {bot_name}, and I could not find information about this in my current knowledge base. Could you rephrase, or ask about HR, Legal, Finance, or IT topics?"
|
| 73 |
+
conversation_history.append({"question": query, "answer": answer})
|
| 74 |
+
return {"answer": answer, "exact_quote": None, "source": None, "domain": domain}
|
| 75 |
+
|
| 76 |
+
context_text = "\n\n".join([f"[Source: {c[\'domain\']} - {c[\'title\']}]\n{c[\'text\']}" for dist, c in results])
|
| 77 |
prompt = f"""You are {bot_name}, an enterprise knowledge assistant having an ongoing conversation.
|
| 78 |
|
| 79 |
Conversation so far:
|
|
|
|
| 88 |
|
| 89 |
Answer:"""
|
| 90 |
answer = generate_with_groq_fn(prompt, groq_client)
|
| 91 |
+
|
| 92 |
+
exact_quote, _ = extract_exact_answer_fn(rewritten, results[0][1]["text"], qa_model, qa_tokenizer)
|
| 93 |
+
|
| 94 |
+
conversation_history.append({"question": query, "answer": answer})
|
| 95 |
+
return {"answer": answer, "exact_quote": exact_quote, "source": results[0][1]["title"], "domain": domain}
|