Skip to content

Phase 5 retrieval - #5

Merged
ShehanSuraweera merged 3 commits into
mainfrom
phase-5-retrieval
Oct 1, 2026
Merged

ShehanSuraweera merged 3 commits into
mainfrom
phase-5-retrieval

Conversation

@ShehanSuraweera

Copy link
Copy Markdown
Owner

This pull request introduces a comprehensive retrieval system to the AI service, enabling similar-issue search and suggested resolutions grounded in past issues. It adds vector search capabilities using PostgreSQL with the pgvector extension, local embeddings via fastembed, and a robust tenant isolation model. The documentation, configuration, and testing infrastructure have all been significantly updated to reflect and support these new features.

Retrieval and Vector Search System

  • Added a vector search subsystem for similar-issue retrieval and resolution suggestions, using PostgreSQL + pgvector for storage and fastembed for local embeddings. New endpoints support indexing, deletion, listing, and searching of issue documents, with strong tenant and product isolation enforced at multiple layers. (ai-service/README.md [1] [2] [3]; ai-service/.env.example [4]

Database and Migrations

  • Introduced Alembic migrations for the new ai schema, including the creation of the issue_documents table with appropriate indexes and constraints for company and product isolation. Alembic configuration and migration scripts are provided. (ai-service/alembic.ini [1] ai-service/migrations/env.py [2] ai-service/migrations/versions/0001_issue_documents.py [3] ai-service/migrations/script.py.mako [4]

Configuration and Dependencies

  • Updated environment variables and the example .env to support retrieval configuration, embedding provider/model, and connection details for the vector database. Added new dependencies for Alembic, fastembed, pgvector, and psycopg. (ai-service/.env.example [1] ai-service/pyproject.toml [2] [3]

API and Dependency Injection

  • Extended FastAPI dependency modules to provide the vector store and embedder, with error handling for unavailable retrieval. New error types and dependency annotations are introduced for clean API integration. (ai-service/src/ai_service/api/deps.py [1] [2]

Documentation and Testing

  • Thoroughly revised the README to document the retrieval feature, new endpoints, tenant isolation strategy, database setup, design decisions, and expanded testing (including database security and retrieval correctness). Test infrastructure now spins up throwaway PostgreSQL containers and covers retrieval, isolation, and embedding logic. (ai-service/README.md [1] [2] [3] [4] [5] [6] [7] [8] [9]; ai-service/pyproject.toml [10]

These changes collectively enable robust, secure, and efficient retrieval-based AI assistance in the service.

Resolved issues are embedded locally (fastembed, bge-small-en-v1.5) and
stored with pgvector in the application's PostgreSQL, in an `ai` schema
owned by a restricted ai_service role that has no access to the public
schema.

- ai-db bootstrap creates the role, schema and extension (idempotent);
  ai-db migrate applies Alembic migrations as ai_service.
- Endpoints to index and remove documents, find similar issues, and
  suggest a resolution. Every query is scoped to company_id in SQL and
  can be narrowed to the products the viewer may open.
- The title and description are embedded; staff resolution notes are
  stored for suggestions but not embedded.
- Suggested resolutions may only cite retrieved tickets and must be
  consistent; past issues are wrapped in delimiters like client text.
  No LLM call is made when nothing clears the similarity threshold.
- Retrieval degrades to 503 RETRIEVAL_UNAVAILABLE if its database or
  model can't start; triage and sentiment are unaffected.
- ai-serve runs uvicorn on a selector event loop, which psycopg's async
  mode needs on Windows. Connections have a 10s connect timeout.
- Shared LLM call validation (llm_call.py) for every feature.
- 75 new tests: one contract for the in-memory and Postgres stores,
  database-enforced isolation, citation checks and injection handling.
A developer's local .env filled in variables that tests deliberately
removed, so the AI_SERVICE_TOKEN startup check passed or failed
depending on the machine. Tests now never read .env.
- index_issue jobs keep the similarity index in step with resolved
  issues: queued on resolve, reopen, edit while resolved and staff
  comments while resolved, at most one at a time. The worker reads the
  current state and indexes (staff notes as resolution) or removes.
- GET /issues/:id/ai/similar and POST/GET /issues/:id/ai/resolution,
  scoped to the issue's company and the products the viewer can open.
  Every result is re-checked against the viewer's tenancy, which also
  drops issues reopened or deleted since indexing.
- Suggested resolutions are stored with model, prompt version and
  sources; helpful/not-helpful feedback is recorded once. Call cost is
  logged, including for failed generations.
- npm run ai:reindex rebuilds the index and removes deleted issues.
- 17 new tests, including a post-filter test where the AI service
  returns issues from another company, another product, a reopened
  issue and a deleted one.
@ShehanSuraweera
ShehanSuraweera merged commit 05b6af5 into main Oct 1, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant