Spaces:
Sleeping
Sleeping
Upload 59 files
Browse files- .env +0 -3
- README.md +1 -11
- TASKS.md +182 -0
- app/__pycache__/main.cpython-312.pyc +0 -0
- app/agent/__pycache__/graph.cpython-312.pyc +0 -0
- app/agent/__pycache__/state.cpython-312.pyc +0 -0
- app/db/__pycache__/neo4j.cpython-312.pyc +0 -0
- app/db/neo4j.py +1 -2
- app/memory/__pycache__/redis_checkpointer.cpython-312.pyc +0 -0
- app/services/__pycache__/nlp_client.cpython-312.pyc +0 -0
- app/tools/__pycache__/hybrid_tool.cpython-312.pyc +0 -0
- requirements.txt +0 -0
.env
CHANGED
|
@@ -11,9 +11,6 @@ POSTGRES_READONLY_URL=postgresql://chat_ro_user:strong_password@hayabusa.proxy.r
|
|
| 11 |
NEO4J_URI=neo4j+s://b55eb74c.databases.neo4j.io
|
| 12 |
NEO4J_USER=b55eb74c
|
| 13 |
NEO4J_PASSWORD=ba7mRDw67kwIDDquVXp_e10msUFvaOz0ipH7_nE-a4Y
|
| 14 |
-
NEO4J_DATABASE=b55eb74c
|
| 15 |
-
AURA_INSTANCEID=b55eb74c
|
| 16 |
-
AURA_INSTANCENAME=KG
|
| 17 |
|
| 18 |
# --- Redis (reuse existing instance, separate DB index) ---
|
| 19 |
REDIS_URL=redis://default:UzTsAxeHPgOQUKPjDSvlxkdMcRVoJalU@hayabusa.proxy.rlwy.net:56307
|
|
|
|
| 11 |
NEO4J_URI=neo4j+s://b55eb74c.databases.neo4j.io
|
| 12 |
NEO4J_USER=b55eb74c
|
| 13 |
NEO4J_PASSWORD=ba7mRDw67kwIDDquVXp_e10msUFvaOz0ipH7_nE-a4Y
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# --- Redis (reuse existing instance, separate DB index) ---
|
| 16 |
REDIS_URL=redis://default:UzTsAxeHPgOQUKPjDSvlxkdMcRVoJalU@hayabusa.proxy.rlwy.net:56307
|
README.md
CHANGED
|
@@ -1,11 +1 @@
|
|
| 1 |
-
-
|
| 2 |
-
title: Chat Service
|
| 3 |
-
emoji: π₯
|
| 4 |
-
colorFrom: pink
|
| 5 |
-
colorTo: pink
|
| 6 |
-
sdk: docker
|
| 7 |
-
pinned: false
|
| 8 |
-
short_description: Chat Service
|
| 9 |
-
---
|
| 10 |
-
|
| 11 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
+
chat-service
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TASKS.md
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# TASKS β Conversational Analytics Assistant (chat-service)
|
| 2 |
+
|
| 3 |
+
Step-by-step build checklist, derived from the implementation plan. Check items off as you go. Order matters β each milestone assumes the previous one works.
|
| 4 |
+
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
## Milestone 0 β Repo & environment setup
|
| 8 |
+
|
| 9 |
+
- [x] Create `chat-service/` folder structure
|
| 10 |
+
- [x] `git init`, set `origin` remote
|
| 11 |
+
- [x] Create virtual environment (`python -m venv venv`)
|
| 12 |
+
- [x] Activate venv
|
| 13 |
+
- [x] Create `.env.example` with all vars from plan Β§10 (placeholders)
|
| 14 |
+
- [x] Create local `.env` (real values, gitignored)
|
| 15 |
+
- [x] Add `.gitignore` (venv, `.env`, `__pycache__`, `*.pyc`)
|
| 16 |
+
- [x] Initial commit ("chore: project skeleton")
|
| 17 |
+
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
## Milestone 1 β Service skeleton (boots + Groq round-trip)
|
| 21 |
+
|
| 22 |
+
- [x] Add core deps to `requirements.txt`: `fastapi`, `uvicorn[standard]`, `pydantic-settings`, `groq`, `python-dotenv`
|
| 23 |
+
- [x] `pip install -r requirements.txt`
|
| 24 |
+
- [x] `app/config.py` β `Settings(BaseSettings)` class reading env vars
|
| 25 |
+
- [x] `app/agent/llm.py` β thin Groq client wrapper (single `ask(prompt: str) -> str` function)
|
| 26 |
+
- [x] `app/main.py`:
|
| 27 |
+
- [x] `GET /health` β `{"status": "ok"}`
|
| 28 |
+
- [x] Temporary `POST /chat` β calls Groq wrapper directly, no LangGraph yet, just to prove the chain works
|
| 29 |
+
- [x] Run locally: `uvicorn app.main:app --reload --port 8002`
|
| 30 |
+
- [x] Test `/health` with curl/Postman
|
| 31 |
+
- [x] Test `/chat` with curl/Postman β confirm real Groq response comes back
|
| 32 |
+
- [x] Write `Dockerfile` (Python 3.12 base, matches existing services)
|
| 33 |
+
- [x] Build + run container locally, re-test `/health` and `/chat` inside Docker
|
| 34 |
+
- [x] Commit ("feat: service skeleton with Groq round-trip")
|
| 35 |
+
|
| 36 |
+
**Done when:** `/health` and `/chat` both work locally and in Docker.
|
| 37 |
+
|
| 38 |
+
---
|
| 39 |
+
|
| 40 |
+
## Milestone 2 β First real tool + LangGraph agent (`sql_query_tool`)
|
| 41 |
+
|
| 42 |
+
- [x] Add deps: `langgraph`, `langchain-groq`, `sqlalchemy`, `psycopg[binary]` (or `asyncpg`)
|
| 43 |
+
- [x] Create **read-only Postgres role** (`chat_ro_user`) β DB side, not app code
|
| 44 |
+
- [x] `app/db/postgres.py` β read-only connection pool using `POSTGRES_READONLY_URL`
|
| 45 |
+
- [x] Confirm exact schema fields needed (resolve plan Β§12.1: `ArticleFakeNews`, `ArticleTopic`, etc. field names)
|
| 46 |
+
- [x] `app/tools/sql_tool.py`:
|
| 47 |
+
- [x] Define `metric` enum: `fake_news_count`, `articles_by_topic`, `sentiment_breakdown`, `propaganda_count`, `hate_speech_count`, `article_lookup`, `dialect_breakdown`
|
| 48 |
+
- [x] Write one parametrized SQL query per metric
|
| 49 |
+
- [x] Return shape: `{"rows": [...], "sql_used": "...", "source_refs": [...]}`
|
| 50 |
+
- [x] Write unit tests for each metric in `tests/test_sql_tool.py` (against a test/staging DB)
|
| 51 |
+
- [x] `app/agent/state.py` β `AgentState` TypedDict (`messages`, `sources`, `session_id`, `user_id`)
|
| 52 |
+
- [x] `app/agent/prompts.py` β system prompt (identity, tool-preference rule, citation rule, language-matching rule, no-fabrication rule)
|
| 53 |
+
- [x] `app/agent/graph.py` β LangGraph ReAct-style agent wired to `sql_query_tool` only
|
| 54 |
+
- [x] Wire `/chat` in `main.py` to call the LangGraph agent instead of the raw Groq wrapper
|
| 55 |
+
- [x] `app/schemas/request.py` / `app/schemas/response.py` β formalize `ChatRequest` / `ChatResponse` models per plan Β§4
|
| 56 |
+
- [x] Manually test: "How many fake news articles were detected last month?" β correct tool call + correct answer
|
| 57 |
+
- [x] Commit ("feat: sql_query_tool + LangGraph agent")
|
| 58 |
+
|
| 59 |
+
**Done when:** agent correctly answers at least 3 different stat questions using real DB data, citing article IDs.
|
| 60 |
+
|
| 61 |
+
---
|
| 62 |
+
|
| 63 |
+
## Milestone 3 β Django proxy endpoint
|
| 64 |
+
|
| 65 |
+
- [x] In Django: add `CHAT_SERVICE_URL` and `CHAT_SERVICE_INTERNAL_TOKEN` to settings/env
|
| 66 |
+
- [x] New view: `POST /api/v1/chat/`
|
| 67 |
+
- [x] Verify JWT (reuse existing auth)
|
| 68 |
+
- [ ] Apply DRF throttling/rate limit (no need for now)
|
| 69 |
+
- [x] Forward `{session_id, message, user_id}` to chat service with internal token header
|
| 70 |
+
- [x] Return chat service's JSON response unchanged
|
| 71 |
+
- [x] Confirm chat service rejects requests without the internal token
|
| 72 |
+
- [x] Test end-to-end: frontend-style request β Django β chat service β Groq β back
|
| 73 |
+
- [x] Commit on Django repo ("feat: chat proxy endpoint")
|
| 74 |
+
|
| 75 |
+
**Done when:** a JWT-authenticated request through Django reaches the chat service and gets a real answer.
|
| 76 |
+
|
| 77 |
+
---
|
| 78 |
+
|
| 79 |
+
## Milestone 4 β `graph_query_tool` (Neo4j)
|
| 80 |
+
|
| 81 |
+
- [x] Confirm current Neo4j label/relationship conventions used by `kg_sync` (resolve plan Β§12.2)
|
| 82 |
+
- [x] ~~Create read-only Neo4j role/user~~ β N/A, Aura Free has no RBAC; enforcement moved to app-level (read-only transactions + no write Cypher in tool code)
|
| 83 |
+
- [x] `app/db/neo4j.py` β driver wrapper using `NEO4J_*` vars, enforcing read-only via explicit read transactions
|
| 84 |
+
- [x] `app/tools/graph_tool.py`:
|
| 85 |
+
- [x] Define `query_type` enum (7 total): `entity_connections`, `entity_mentions`, `shared_entities_between_articles`, `most_connected_entities`, `article_verdict`, `claims_for_article`, `analysis_for_article`
|
| 86 |
+
- [x] Write one parametrized Cypher template per query_type
|
| 87 |
+
- [x] Return shape: `{"rows": [...], "source_refs": [...]}`
|
| 88 |
+
- [x] Add `graph_query_tool` to the LangGraph agent's tool list
|
| 89 |
+
- [x] Unit tests in `tests/test_graph_tool.py`
|
| 90 |
+
- [x] Manually test: "Who is connected to [entity]?" β correct Cypher template used, correct answer
|
| 91 |
+
- [x] Commit ("feat: graph_query_tool")
|
| 92 |
+
|
| 93 |
+
**Done when:** agent correctly answers at least 2 relationship questions using real graph data.
|
| 94 |
+
|
| 95 |
+
---
|
| 96 |
+
|
| 97 |
+
## Milestone 5 β Memory (multi-turn)
|
| 98 |
+
|
| 99 |
+
- [x] Add dep: Redis client (`redis` / `redis[hiredis]`)
|
| 100 |
+
- [x] `app/memory/redis_checkpointer.py` β LangGraph checkpointer backed by Redis, key prefix `chat:checkpoint:{session_id}`
|
| 101 |
+
- [x] Wire checkpointer into `app/agent/graph.py` compilation
|
| 102 |
+
- [x] Set `CHAT_SESSION_TTL_SECONDS` TTL on session keys
|
| 103 |
+
- [x] Implement history trimming: keep last `CHAT_HISTORY_MAX_TURNS` verbatim, summarize older turns beyond that
|
| 104 |
+
- [x] Manual test: ask a question, then a follow-up ("and what about last month?") using the same `session_id` β confirm context carries over
|
| 105 |
+
- [x] Manual test: new `session_id` β confirm no memory leaks across sessions
|
| 106 |
+
- [x] Commit ("feat: Redis-backed multi-turn memory")
|
| 107 |
+
|
| 108 |
+
**Done when:** follow-up questions correctly resolve using prior turn context, and TTL/trimming work as expected.
|
| 109 |
+
|
| 110 |
+
---
|
| 111 |
+
|
| 112 |
+
## Milestone 6 β Citations end-to-end
|
| 113 |
+
|
| 114 |
+
- [x] `app/tools/article_tool.py` β `get_article_detail(article_id)` for resolving full title/snippet
|
| 115 |
+
- [x] Confirm every tool (`sql_query_tool`, `graph_query_tool`) consistently returns `source_refs`
|
| 116 |
+
- [x] Agent post-processing step: merge all `source_refs` collected during the turn into final `sources[]` in the response
|
| 117 |
+
- [x] Update system prompt to explicitly require citing every ID it used
|
| 118 |
+
- [x] Manual test: ask a question, verify `sources[]` in the JSON response matches what was actually used
|
| 119 |
+
- [x] Commit ("feat: citation resolution")
|
| 120 |
+
|
| 121 |
+
**Done when:** every factual answer includes a non-empty, accurate `sources[]` array.
|
| 122 |
+
|
| 123 |
+
---
|
| 124 |
+
|
| 125 |
+
## Milestone 7 β `hybrid_search_tool`
|
| 126 |
+
|
| 127 |
+
- [x] Confirm reuse path for `_embed` (call existing Django/analysis embedding function - the function is in app/services/nlp_client.py)
|
| 128 |
+
- [x] `app/tools/hybrid_tool.py`:
|
| 129 |
+
- [x] Step 1: embed query text
|
| 130 |
+
- [x] Step 2: pgvector cosine similarity search over `search_vector`
|
| 131 |
+
- [x] Step 3: for each top article, pull mentioned entities from Neo4j
|
| 132 |
+
- [x] Step 4: merge into single ranked result set with combined `source_refs`
|
| 133 |
+
- [x] Add tool to agent's tool list
|
| 134 |
+
- [ ] Manual test: "Articles about the election, and who's mentioned" β correct combined results
|
| 135 |
+
- [x] Commit ("feat: hybrid_search_tool")
|
| 136 |
+
|
| 137 |
+
**Done when:** at least one combined semantic+graph question is answered correctly with merged citations.
|
| 138 |
+
|
| 139 |
+
---
|
| 140 |
+
|
| 141 |
+
## Milestone 8 β Frontend chat widget
|
| 142 |
+
|
| 143 |
+
- [ ] Generate/persist `session_id` client-side (e.g. `localStorage`, created on first load)
|
| 144 |
+
- [ ] Chat UI component: message list, input box, send button
|
| 145 |
+
- [ ] Call `POST /api/v1/chat/` with `{session_id, message}`
|
| 146 |
+
- [ ] Render `answer` text
|
| 147 |
+
- [ ] Render `sources[]` as clickable links (article/entity)
|
| 148 |
+
- [ ] Loading state while waiting for response
|
| 149 |
+
- [ ] Basic error state (service down / timeout)
|
| 150 |
+
- [ ] Commit ("feat: chat widget")
|
| 151 |
+
|
| 152 |
+
**Done when:** a real user can open the widget, ask a question, get an answer with clickable citations, and ask a follow-up.
|
| 153 |
+
|
| 154 |
+
---
|
| 155 |
+
|
| 156 |
+
## Milestone 9 β Tests, docs, hardening
|
| 157 |
+
|
| 158 |
+
- [ ] Unit tests for all tools (`sql_tool`, `graph_tool`, `hybrid_tool`) with mocked/test DB data
|
| 159 |
+
- [ ] `tests/test_agent_e2e.py` β at least 3 end-to-end scenarios (stat question, relationship question, follow-up question)
|
| 160 |
+
- [ ] `chat-service/README.md` β setup instructions, env vars, how to run locally + in Docker
|
| 161 |
+
- [ ] Verify `.env.example` is complete and matches plan Β§10
|
| 162 |
+
- [ ] Security checklist review (plan Β§8) β confirm every box is actually true in the running system:
|
| 163 |
+
- [ ] Read-only DB roles confirmed (test that a write attempt fails)
|
| 164 |
+
- [ ] No raw SQL/Cypher ever comes from the LLM β code review confirms only enum params reach tools
|
| 165 |
+
- [ ] Internal token required and enforced
|
| 166 |
+
- [ ] Rate limiting active on Django proxy
|
| 167 |
+
- [ ] Groq key not logged anywhere
|
| 168 |
+
- [ ] Tool calls logged (name + params, not raw rows)
|
| 169 |
+
- [ ] Add `chat_service` to `docker-compose.yml` (no published host port in prod config)
|
| 170 |
+
- [ ] Final walkthrough / demo run-through for defense
|
| 171 |
+
- [ ] Tag/commit ("chore: v1 complete")
|
| 172 |
+
|
| 173 |
+
**Done when:** everything above is checked and you can demo the full flow live without surprises.
|
| 174 |
+
|
| 175 |
+
---
|
| 176 |
+
|
| 177 |
+
## Open items to resolve before/while coding (carry over from plan Β§12)
|
| 178 |
+
|
| 179 |
+
- [ ] Confirm exact Postgres field names per metric
|
| 180 |
+
- [ ] Confirm Neo4j label/relationship naming conventions
|
| 181 |
+
- [ ] Decide: authenticated-only sessions, or also support anonymous/demo sessions for defense
|
| 182 |
+
- [ ] Decide: citation links deep-link into real frontend routes, or raw IDs for now
|
app/__pycache__/main.cpython-312.pyc
CHANGED
|
Binary files a/app/__pycache__/main.cpython-312.pyc and b/app/__pycache__/main.cpython-312.pyc differ
|
|
|
app/agent/__pycache__/graph.cpython-312.pyc
CHANGED
|
Binary files a/app/agent/__pycache__/graph.cpython-312.pyc and b/app/agent/__pycache__/graph.cpython-312.pyc differ
|
|
|
app/agent/__pycache__/state.cpython-312.pyc
CHANGED
|
Binary files a/app/agent/__pycache__/state.cpython-312.pyc and b/app/agent/__pycache__/state.cpython-312.pyc differ
|
|
|
app/db/__pycache__/neo4j.cpython-312.pyc
CHANGED
|
Binary files a/app/db/__pycache__/neo4j.cpython-312.pyc and b/app/db/__pycache__/neo4j.cpython-312.pyc differ
|
|
|
app/db/neo4j.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
-
import os
|
| 3 |
|
| 4 |
from neo4j import AsyncGraphDatabase, AsyncDriver, AsyncTransaction
|
| 5 |
|
|
@@ -47,4 +46,4 @@ async def run_read_query(query: str, **params) -> list[dict]:
|
|
| 47 |
return await result.data()
|
| 48 |
|
| 49 |
async with driver.session() as session:
|
| 50 |
-
return await session.execute_read(_tx_func)
|
|
|
|
| 1 |
from __future__ import annotations
|
|
|
|
| 2 |
|
| 3 |
from neo4j import AsyncGraphDatabase, AsyncDriver, AsyncTransaction
|
| 4 |
|
|
|
|
| 46 |
return await result.data()
|
| 47 |
|
| 48 |
async with driver.session() as session:
|
| 49 |
+
return await session.execute_read(_tx_func)
|
app/memory/__pycache__/redis_checkpointer.cpython-312.pyc
CHANGED
|
Binary files a/app/memory/__pycache__/redis_checkpointer.cpython-312.pyc and b/app/memory/__pycache__/redis_checkpointer.cpython-312.pyc differ
|
|
|
app/services/__pycache__/nlp_client.cpython-312.pyc
CHANGED
|
Binary files a/app/services/__pycache__/nlp_client.cpython-312.pyc and b/app/services/__pycache__/nlp_client.cpython-312.pyc differ
|
|
|
app/tools/__pycache__/hybrid_tool.cpython-312.pyc
ADDED
|
Binary file (6.8 kB). View file
|
|
|
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|