How sopanmunde/trishul_ai works
1. Top-level shape
- This is a two-tier monorepo: a Next.js frontend in frontend/ and a FastAPI/Python backend in api/.
- The architecture boundary is clear: the frontend handles presentation, routing, and chat UX; the backend owns authentication, persistence, retrieval, and LLM orchestration.
2. Frontend subsystem
- The frontend is a Next.js 15 app with route groups for landing, login, dashboard, and settings.
- frontend/app/page.tsx and supporting marketing components render the public product site, while app/login, app/dashboard, and app/setting drive the authenticated experience.
- middleware.ts likely guards protected routes and redirects unauthenticated users.
- The component set (ChatPane, Composer, Sidebar, Message, modals, etc.) shows a chat-product UI with conversation navigation, templates, folders, profile settings, and responsive interaction patterns.
3. Backend API subsystem
- The backend is a FastAPI service exposed through api/index.py.
- It is responsible for the application’s core business logic: login/signup/auth, chat request handling, streaming responses, and data access.
- api/auth.py, api/models.py, and api/db/database.py indicate a layered backend where authentication, schemas/models, and database connectivity are separated.
4. Authentication and user state
- Authentication is implemented with JWTs and hashed passwords, as described in the README and supported by auth.py.
- User-facing state such as users, conversations, folders, and templates is persisted in MongoDB via the async driver mentioned in the README.
- This makes MongoDB the system-of-record for mutable application data, while the vector store is used only for retrieval.
5. RAG retrieval subsystem
- The assistant uses a Retrieval-Augmented Generation pipeline rather than answering directly from the model.
- Incoming questions are embedded with a Hugging Face sentence-transformer model (all-MiniLM-L6-v2 per README), then matched against a Pinecone vector index.
- The repo’s api/data/Medical_book.pdf and store_index.py show an offline ingestion path that turns source documents into indexed chunks.
6. Generation subsystem
- Retrieved chunks are injected into prompt templates from api/src/prompts.py, with helper logic in api/src/helper.py.
- The backend then calls a Google GenAI / Gemini model for response generation.
- The design is explicitly safety-oriented: the system prompt constrains the assistant to non-diagnostic, medically cautious answers.
7. Streaming response flow
- Chat responses are streamed back to the client, not returned as a single blocking payload.
- api/test_stream.py suggests SSE or similar streaming behavior was developed and tested separately.
- This streaming boundary matters for the diagram: the backend emits incremental tokens/events, and the frontend chat pane updates in real time.
8. Conversation and UI state management
- The frontend maintains rich local interaction state around conversations, folders, templates, search, settings, and user profile.
- Components like Sidebar, ConversationRow, FolderRow, TemplateRow, SearchModal, and SettingsPopover indicate that the dashboard is more than a simple chat box: it is a multi-entity workspace.
- The chat UI is also designed to render markdown and code-like content cleanly, based on the README’s markdown/highlight dependencies.
9. Supporting scripts and operational boundaries
- store_index.py is the offline indexing pipeline entrypoint, separate from runtime chat serving.
- list_models.py suggests a utility for inspecting available model options or provider configuration.
- render.yaml and vercel.json indicate deployment concerns are split between backend hosting and frontend hosting, which reinforces the service boundary.
10. What to draw as the main data flows
- User flow: browser → Next.js UI → FastAPI auth/chat endpoints → MongoDB / Pinecone / Gemini → streamed response back to browser.
- Ingestion flow: PDF/source docs in api/data/ → chunking/indexing via store_index.py → Pinecone vector index.
- Persistence flow: authenticated user actions in the dashboard → backend APIs → MongoDB for durable chat/app metadata.
- The key architectural distinction is that MongoDB stores app state, Pinecone stores semantic knowledge, and Gemini generates the final answer using both retrieved context and prompt policy.
Main entry points: index.py API service · app Next app · page.tsx Dashboard · helper.py Orchestration
Components 24
Frontend
Next.js app and chat product UI
- Next app · Routes, layouts, and page shells for the web client
frontend/app - Landing · Public homepage and product marketing surfaces
frontend/app/page.tsx - Login · Sign-in and authentication entrypoint
frontend/app/login/page.tsx - Dashboard · Authenticated chat workspace and conversation shell
frontend/app/dashboard/page.tsx - Settings · User settings and preference screens
frontend/app/setting/page.tsx - Route guard · Protects routes and redirects unauthenticated users
frontend/middleware.ts - App shell · Global layout, loading state, and styling foundation
frontend/app/layout.tsx - Marketing UI · Hero, pricing, testimonials, FAQ, and footer sections
frontend/components/hero.tsx - Chat UI · Chat pane, composer, message rendering, and assistant wrapper
frontend/components/ChatPane.jsx - Workspace nav · Sidebar, conversations, folders, templates, search, and profile controls
frontend/components/Sidebar.jsx - UI kit · Reusable primitives and interaction components
frontend/components/ui/button.tsx
Backend
FastAPI service, auth, persistence, and RAG pipeline
- API service · HTTP entrypoint for auth, chat, and streaming endpoints
api/index.py - Auth layer · Login, signup, password hashing, and token handling
api/auth.py - Schemas · Application models and request/response shapes
api/models.py - Mongo layer · Async MongoDB connection and persistence helpers
api/db/database.py - Prompting · System and response prompts for model orchestration
api/src/prompts.py - Orchestration · Retrieval, prompt assembly, and response helper logic
api/src/helper.py - Indexer · Document chunking and vector index build entrypoint
api/store_index.py - Streaming · Standalone streaming behavior and endpoint testing
api/test_stream.py - Source docs · Input document used for retrieval indexing
api/data/Medical_book.pdf
External systems
Hosted services used by the backend
- MongoDB · System of record for users, chats, folders, and templates
- Pinecone · Semantic retrieval store for embedded document chunks
- Gemini · Generates answers from retrieved context and policy prompts
- Browser · End-user entry point for the web application
Connections 26
- Browser → Next app: open app · Users load the Next.js client in the browser
- Route guard → Dashboard: protect · Redirects unauthenticated users away from protected routes
- Route guard → Settings: protect · Applies access control to settings views
- Next app → Landing: render · Serves public marketing pages
- Next app → Login: render · Serves the login flow
- Next app → Dashboard: render · Serves the main chat workspace
- Next app → Settings: render · Serves account and preference pages
- Next app → App shell: wraps · Provides global layout and styling
- Landing → Marketing UI: compose · Landing page is built from marketing sections
- Dashboard → Chat UI: compose · Dashboard centers on the chat experience
- Dashboard → Workspace nav: compose · Dashboard includes conversation and workspace navigation
- Chat UI → UI kit: use · Chat UI relies on shared primitives and inputs
- Workspace nav → UI kit: use · Sidebar and dialogs use common UI building blocks
- Login → API service: login · Auth flow calls backend endpoints
- Dashboard → API service: chat · Workspace sends chat and persistence requests
- API service → Auth layer: delegate · API routes use auth helpers for credential handling
- API service → Schemas: validate · Request and response payloads are shaped by models
- API service → Mongo layer: store · Persists application state in MongoDB
- API service → Orchestration: orchestrate · Coordinates retrieval and generation logic
- Orchestration → Prompting: build prompt · Assembles system and response prompts
- Orchestration → Pinecone: retrieve · Fetches semantically relevant chunks
- Orchestration → Gemini: generate · Calls the LLM to produce the final answer
- Mongo layer → MongoDB: persist · Reads and writes durable app state
- Indexer → Source docs: ingest · Indexes the source PDF corpus
- Indexer → Pinecone: index · Writes embedded chunks to the vector store
- Streaming → API service: verify · Exercises streaming response behavior