Skip to content

Enhance backend persistence and cleanup LLM service#7

Open
maheshshinde9100 wants to merge 4 commits into
ayush-kumar-24:mainfrom
maheshshinde9100:fix/persistence-and-refactor
Open

Enhance backend persistence and cleanup LLM service#7
maheshshinde9100 wants to merge 4 commits into
ayush-kumar-24:mainfrom
maheshshinde9100:fix/persistence-and-refactor

Conversation

@maheshshinde9100

@maheshshinde9100 maheshshinde9100 commented May 14, 2026

Copy link
Copy Markdown

Description

This PR improves backend persistence, refactors the LLM service, and introduces chat management enhancements to improve production readiness and maintainability of the NyayaSetu-AI application.

The update resolves issues related to in-memory chat storage, duplicated LLM logic, inconsistent frontend API handling, and missing chat cleanup functionality.

Closes #6


Changes

Backend

Memory & Persistence

  • Integrated app/services/database.py with app/services/memory.py
  • Enabled persistent chat history storage in legal_ai.db
  • Preserved chat sessions across backend restarts

LLM Refactoring

  • Refactored app/services/llm.py
  • Removed duplicated API request logic
  • Standardized RAG context handling and prompt structure
  • Improved maintainability and response consistency

Chat Management Endpoint

  • Added:

    DELETE /chat/{session_id}
  • Supports clearing/deleting chat history via API

Startup Validation

  • Updated app/main.py

  • Added startup validation for:

    • GROQ_API_KEY
    • HF_TOKEN
  • Improved configuration error visibility during application startup


Frontend

API Service Improvements

  • Updated frontend/src/services/api.js

  • Added proper handling for:

    • session_id
    • authorization token

Chat History Support

  • Added clearChatHistory() helper method
  • Improved frontend-backend session synchronization

ChatWindow Integration

  • Updated ChatWindow.jsx
  • Ensured active chatId is passed correctly to backend requests

Benefits

  • Persistent chat history across server restarts
  • Cleaner and more maintainable LLM service code
  • Improved API consistency between frontend and backend
  • Added support for deleting/clearing chat sessions
  • Better startup diagnostics and environment validation
  • Improved foundation for future chat/session management features

Testing

  • Verified messages persist in legal_ai.db
  • Verified chat history remains after backend restart
  • Tested DELETE /chat/{session_id} endpoint
  • Confirmed frontend correctly passes session_id
  • Confirmed warning logs appear when required environment variables are missing

Files Updated

  • app/services/memory.py
  • app/services/database.py
  • app/services/llm.py
  • app/api/chat.py
  • app/main.py
  • frontend/src/services/api.js
  • frontend/src/components/ChatWindow.jsx

- add Dockerfile for FastAPI backend with Python 3.10 slim
- add multi-stage Dockerfile for React/Vite frontend with Nginx
- add docker-compose configuration for full-stack orchestration
- add .dockerignore files to optimize Docker build context
- update frontend API service to use VITE_API_URL env variable
- update README with Docker setup instructions and corrected project structure
- Link memory service to SQLite database for message persistence
- De-duplicate code in llm.py and improve RAG context handling
- Add DELETE /chat/{session_id} endpoint for history management
- Update frontend API service to handle tokens and session IDs correctly
- Add environment variable validation on backend startup
@Karanjot786

Copy link
Copy Markdown

Hey @maheshshinde9100! Saw your work on GSSoC 2026.

We are building TermUI, a TypeScript terminal UI framework with React-style hooks and JSX, rendered entirely in the terminal.

We have 67 unassigned GSSoC issues open. 19 are marked good first issue. Your JavaScript background transfers directly.

Karanjot, TermUI maintainer

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.

feat: improve persistence, cleanup LLM service, and add chat management endpoints

2 participants