fix: harden AI analysis and seed defaults
This commit is contained in:
@@ -350,13 +350,14 @@ GOOGLE_GENERATIVE_AI_API_KEY=...
|
||||
ANTHROPIC_API_KEY=sk-ant-...
|
||||
```
|
||||
|
||||
## Scenario: Elder AI Analysis MVP With LangChain And PostgreSQL JSONB Embeddings
|
||||
## Scenario: Elder AI Analysis MVP With LangChain And Keyword Knowledge Retrieval
|
||||
|
||||
### 1. Scope / Trigger
|
||||
|
||||
- Trigger: AI features that inspect resident, care, health, family, admission, alert, incident, or knowledge data.
|
||||
- Current project contract: the elder analysis MVP uses LangChain on the server with an OpenAI-compatible provider, not the Vercel AI SDK runtime path.
|
||||
- Current project contract: the elder analysis MVP uses LangChain on the server with an OpenAI-compatible chat provider, not the Vercel AI SDK runtime path.
|
||||
- Keep provider calls under `modules/ai/server/*`; feature modules and API routes must not instantiate model clients directly.
|
||||
- Knowledge retrieval is local keyword scoring over persisted chunks; it must not require an embedding provider or `pgvector`.
|
||||
|
||||
### 2. Signatures
|
||||
|
||||
@@ -369,13 +370,13 @@ ANTHROPIC_API_KEY=sk-ant-...
|
||||
- `DELETE /api/ai/knowledge/[id]`
|
||||
- DB:
|
||||
- `ai_knowledge_entries`
|
||||
- `ai_knowledge_chunks` with `jsonb` `embedding` arrays
|
||||
- `ai_knowledge_chunks` with text `content`; the legacy JSONB `embedding` column remains defaulted for compatibility and is not read or written by retrieval.
|
||||
- `elder_ai_analyses`
|
||||
- Runtime env:
|
||||
- `AI_API_KEY` required for generation and embedding.
|
||||
- `AI_API_KEY` required for chat generation only.
|
||||
- `AI_BASE_URL` optional OpenAI-compatible endpoint.
|
||||
- `AI_CHAT_MODEL` optional, defaults in `modules/ai/server/config.ts`.
|
||||
- `AI_EMBEDDING_MODEL` optional, defaults in `modules/ai/server/config.ts`.
|
||||
- Do not introduce `AI_EMBEDDING_MODEL`; knowledge retrieval must work without embedding config.
|
||||
|
||||
### 3. Contracts
|
||||
|
||||
@@ -394,7 +395,8 @@ ANTHROPIC_API_KEY=sk-ant-...
|
||||
- `errorCategory` / `errorReason`: sanitized only for failed rows
|
||||
- Failed rows must not store prompts, full resident context snapshots, API keys, or raw provider errors.
|
||||
- Knowledge retrieval may return enabled platform knowledge and enabled active-organization knowledge only.
|
||||
- Knowledge retrieval first filters enabled platform / active-organization chunks in SQL, then computes cosine similarity in `modules/ai/server/knowledge.ts`; this keeps the MVP deployable on plain PostgreSQL without the `vector` extension.
|
||||
- Knowledge retrieval first filters enabled platform / active-organization chunks in SQL, then scores chunks with local lexical overlap in `modules/ai/server/knowledge.ts`; this keeps the MVP deployable on plain PostgreSQL without the `vector` extension or embedding API calls.
|
||||
- Completed elder analysis `resultJson.modelSummary` must include `{ provider: "openai-compatible", chatModel, knowledgeRetrieval: "keyword" }`.
|
||||
- Completed elder analysis `resultJson.citations` must be derived from citation IDs actually referenced by `keyFindings[].citationIds` and `recommendations[].citationIds`; do not append unused available citations.
|
||||
|
||||
### 4. Validation & Error Matrix
|
||||
@@ -421,7 +423,7 @@ ANTHROPIC_API_KEY=sk-ant-...
|
||||
### 6. Tests Required
|
||||
|
||||
- Permission seeding assertions for `ai:*` and `knowledge:*` default role grants.
|
||||
- Knowledge retrieval tests for platform shared, own organization, other organization, disabled entry filtering, and cosine-score ordering from JSONB embeddings.
|
||||
- Knowledge retrieval tests for platform shared, own organization, other organization, disabled entry filtering, keyword-score ordering, nonmatching chunk exclusion, and no embedding-provider calls.
|
||||
- Analysis tests for missing config, schema validation failure, failed-history sanitization, and data-scope redaction.
|
||||
- Analysis tests for unknown citation rejection and referenced-only citation persistence.
|
||||
- Route tests for auth/permission failures and standard response shape.
|
||||
|
||||
Reference in New Issue
Block a user