$ show voicecomm-ai/ai-vocsagex-backend
This is a production-grade backend service for an enterprise AI platform, VoiceSageX, providing Retrieval-Augmented Generation (RAG), agent-based conversational AI, multi-agent orchestration, long-term memory, and model inference. Built with FastAPI, it exposes numerous RESTful endpoints under `/Voicecomm/VoiceSageX/` for knowledge base management (document parsing, chunk editing, vector indexing), agent chat (single and multi-agent with LangGraph), MCP tool integration, and model invocation. The architecture follows a modular, pipeline-oriented design: the RAG subsystem implements a clear stage-based flow (extraction, cleaning, splitting, vectorization, retrieval, reranking) with strategy and factory patterns, while the agent subsystem uses a class hierarchy (BaseAgent → HistoryAgent → MemoryAgent) and supports streaming. The system relies on PostgreSQL with pgvector for vector storage, external sandbox for code execution, and multiple model providers (OpenAI, Azure, SiliconFlow, Ollama). Configuration is environment-aware (dev/qa/prod) and loaded from JSON files. The codebase demonstrates solid engineering practices with async patterns, Pydantic models, and consistent error handling, though there is some duplication across similar API endpoints and potential dependency drift risks from langchain and langgraph versions.
The LLM identified 5 files worth highlighting for reuse:
Implements a provider registration and instantiation system for multiple model backends (OpenAI, Azure, Ollama, etc.). Uses a `register_model_provider` decorator and a manager that returns appropriate `ModelInstance` subclasses based on configuration. Clean separation of concerns; could be extracted and reused for any service needing pluggable model backends.
Orchestrates retrieval and reranking by composing multiple retriever strategies (vector, fulltext, hybrid) and reranker strategies (model-based, weight-based). Decouples retrieval logic from the rest of the RAG pipeline. Demonstrates clean composition and could be reused in any RAG system needing pluggable retrieval/reranking.
Defines the base ReAct agent with streaming and non-streaming invocation, tool binding, and history summarization. Provides a template method pattern where subclasses (HistoryAgent, MemoryAgent) override specific behaviors. Well-structured and reusable for any LLM-based agent workflow that requires tool use and conversation history.
Factory that creates database instances based on configuration, with async connection pool management. Currently supports PostgreSQL, but the abstraction allows adding new backends. Clean separation from business logic; could be extracted for any project needing database abstraction with async support.
Contains utility functions `create_generator_handler` and `create_node_handler` that standardize the creation of streaming and non-streaming API endpoints. Reduces boilerplate across many similar endpoints. Demonstrates a reusable pattern for FastAPI projects with consistent response handling.