Commit cf9601d
committed
feat(v0.3.0): LLM-augmented chat, validation, and follow-up Q&A
Adds an optional LLM layer on top of the deterministic SCI engine.
The engine still works without an LLM key (graceful degradation).
New modules under src/sci_case_tracker/:
- llm_client.py provider-agnostic httpx wrapper (Groq / OpenAI /
Anthropic / OpenRouter via env vars LLM_PROVIDER,
LLM_MODEL, *_API_KEY)
- validator.py LLM-based reply validator. Catches mismatches like
'user asked about case X but reply is about case Y'.
Fail-open: passes the original reply through if LLM
is down or response is unparseable.
- memory.py per-user SQLite store. Keeps last 10 conversation
turns + last uploaded report (24h TTL).
- router.py message classifier + chat fallback + report-followup
Q&A. Heuristic classify(), then LLM dispatch.
Bot wiring (bot/telegram_bot.py):
- on_text now decides: sci_lookup | report_followup | chat
- SCI lookups go through validator before sending
- 'hi' / 'hello' / general questions get a friendly LLM reply
- 'from this report list cases where ...' answers from the cached
last upload (no re-fetch from SCI)
- on_document caches the enriched rows in memory for follow-ups
Config (in .env, all optional):
LLM_PROVIDER=groq (default: groq)
LLM_MODEL=llama-3.3-70b-versatile
GROQ_API_KEY=gsk_...
LLM_VALIDATION_ENABLED=true
LLM_CHAT_FALLBACK_ENABLED=true
LLM_MEMORY_TURNS=10
LLM_MEMORY_REPORT_TTL_HOURS=24
Tests:
- 20 new tests (test_router, test_memory, test_validator)
- All 48 tests pass (mock LLM via unittest.mock.patch)
Live integration verified with Groq llama-3.3-70b:
- 'hi' returns a friendly Hindi/English greeting
- 'CA 7395/2024' returns SCI data, validator says 'ok'
- intentional mismatch correctly flagged
- Diary-vs-CaseNumber alias collision NOT flagged (false-positive
fix in validator system prompt)
Provider-agnostic: swap to Claude/OpenAI by changing LLM_PROVIDER and
the matching *_API_KEY env var. No code changes needed.1 parent 0ff628f commit cf9601d
11 files changed
Lines changed: 156772 additions & 13 deletions
File tree
- bot
- logs
- src/sci_case_tracker
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| |||
159 | 160 | | |
160 | 161 | | |
161 | 162 | | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
171 | 211 | | |
172 | | - | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
173 | 221 | | |
174 | | - | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
175 | 239 | | |
176 | 240 | | |
177 | 241 | | |
| |||
224 | 288 | | |
225 | 289 | | |
226 | 290 | | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
227 | 297 | | |
228 | 298 | | |
229 | 299 | | |
| |||
0 commit comments