Martechsol commited on
Commit Β·
efff587
1
Parent(s): 759466a
Fix bot behavior: handle greetings, strip reasoning monologues, and sanitize history
Browse files- app/services/llm.py +17 -8
- app/ui_gradio.py +2 -1
app/services/llm.py
CHANGED
|
@@ -4,15 +4,16 @@ import httpx
|
|
| 4 |
# MASTER SYSTEM PROMPT - Consolidating all user feedback for "Perfection"
|
| 5 |
# MASTER SYSTEM PROMPT - Upgraded for "ChatGPT-level" Depth but STRICT Accuracy
|
| 6 |
# MASTER SYSTEM PROMPT - HTML Precision Protocol
|
| 7 |
-
SYSTEM_PROMPT = """You are Martechsol Assistant, a precision data relay. Your SOLE purpose is to extract and present facts EXACTLY as they appear in the provided Expert Data.
|
| 8 |
|
| 9 |
STRICT FORMATTING & CONTENT RULES:
|
| 10 |
-
1.
|
| 11 |
-
2.
|
| 12 |
-
3.
|
| 13 |
-
4.
|
| 14 |
-
5.
|
| 15 |
-
6.
|
|
|
|
| 16 |
|
| 17 |
TONE: Silent, literal, precision relay of facts."""
|
| 18 |
|
|
@@ -150,4 +151,12 @@ class LLMService:
|
|
| 150 |
logging.getLogger(__name__).error(f"Groq API Error {resp.status_code}: {resp.text}")
|
| 151 |
resp.raise_for_status()
|
| 152 |
data = resp.json()
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
# MASTER SYSTEM PROMPT - Consolidating all user feedback for "Perfection"
|
| 5 |
# MASTER SYSTEM PROMPT - Upgraded for "ChatGPT-level" Depth but STRICT Accuracy
|
| 6 |
# MASTER SYSTEM PROMPT - HTML Precision Protocol
|
| 7 |
+
SYSTEM_PROMPT = """You are Martechsol Assistant, a precision data relay. Your SOLE purpose is to extract and present facts EXACTLY as they appear in the provided Expert Data.
|
| 8 |
|
| 9 |
STRICT FORMATTING & CONTENT RULES:
|
| 10 |
+
1. GREETINGS: If the user says "hi", "hello", or similar, respond politely and invite them to ask about company policies.
|
| 11 |
+
2. HTML BOLDING ONLY: Use <b>...</b> for bolding. NEVER use markdown **.
|
| 12 |
+
3. NO HEADERS: Do not use headers, titles, or bolded category labels.
|
| 13 |
+
4. CONDITIONAL BULLET POINTS: Use bullet points ONLY if the source data naturally contains a list or multiple distinct points. Otherwise, provide a concise paragraph.
|
| 14 |
+
5. NO INTERNAL MONOLOGUE: You are FORBIDDEN from outputting "thinking" blocks, internal monologues, or "Okay, let's see" text. Output ONLY the final answer.
|
| 15 |
+
6. ZERO HALLUCINATION POLICY: You are FORBIDDEN from adding any information, steps, benefits, or meetings that are not explicitly written in the Expert Data. If the Expert Data does not mention it, it DOES NOT EXIST.
|
| 16 |
+
7. NO FILLER: Answer directly without filler. Do not add introductions like "Here is the information".
|
| 17 |
|
| 18 |
TONE: Silent, literal, precision relay of facts."""
|
| 19 |
|
|
|
|
| 151 |
logging.getLogger(__name__).error(f"Groq API Error {resp.status_code}: {resp.text}")
|
| 152 |
resp.raise_for_status()
|
| 153 |
data = resp.json()
|
| 154 |
+
content = data["choices"][0]["message"]["content"].strip()
|
| 155 |
+
|
| 156 |
+
# ββ Post-Processing: Strip thinking blocks if model leaks them ββ
|
| 157 |
+
import re
|
| 158 |
+
content = re.sub(r'<think>.*?</think>', '', content, flags=re.DOTALL).strip()
|
| 159 |
+
# Also strip any leading monologues like "Okay, let's see..."
|
| 160 |
+
content = re.sub(r'^(Okay|Let\'s see|Alright|I will|Sure).*?\n', '', content, flags=re.IGNORECASE | re.MULTILINE).strip()
|
| 161 |
+
|
| 162 |
+
return content
|
app/ui_gradio.py
CHANGED
|
@@ -193,7 +193,8 @@ async def chat_fn(message: str, chat_history: List[Dict[str, str]], session_id:
|
|
| 193 |
await asyncio.sleep(random.uniform(0.2, 0.4))
|
| 194 |
|
| 195 |
# ββ Fire-and-forget: persist to session store (never blocks the UI) βββ
|
| 196 |
-
|
|
|
|
| 197 |
asyncio.create_task(_ss.save_message(session_id, message, displayed))
|
| 198 |
|
| 199 |
# Re-enable the input box at the end
|
|
|
|
| 193 |
await asyncio.sleep(random.uniform(0.2, 0.4))
|
| 194 |
|
| 195 |
# ββ Fire-and-forget: persist to session store (never blocks the UI) βββ
|
| 196 |
+
# We only save successful responses, not error messages
|
| 197 |
+
if session_id and not displayed.startswith("β οΈ"):
|
| 198 |
asyncio.create_task(_ss.save_message(session_id, message, displayed))
|
| 199 |
|
| 200 |
# Re-enable the input box at the end
|