LP_2-test / TODO.md
DocUA's picture
Clean deployment without large index files
461adca

A newer version of the Gradio SDK is available: 6.6.0

Upgrade

TODO: Refactoring Legal Position AI Analyzer

Status: Phase 2 COMPLETED ✅ + Prompt Editing Feature ✅

Останнє оновлення: 2025-12-28


✨ НОВЕ: Prompt Editing Feature (COMPLETED ✅)

Реалізовано 2025-12-28:

  • Розширено UserSessionState з полем custom_prompts
  • Додано методи get_prompt(), set_prompt(), reset_prompts()
  • Оновлено серіалізацію (to_dict/from_dict)
  • Інтегровано Session Manager з Gradio інтерфейсом
  • Додано вкладку "⚙️ Налаштування" з редакторами промптів
  • Реалізовано save_custom_prompts() для збереження
  • Реалізовано reset_prompts_to_default() для скидання
  • Реалізовано load_session_prompts() для завантаження
  • Оновлено generate_legal_position() для підтримки кастомних промптів
  • Оновлено всі AI провайдери (OpenAI, Anthropic, Gemini, DeepSeek)
  • Написано повну документацію (PROMPT_EDITING.md)
  • Написано швидкий старт (QUICK_START_PROMPTS.md)
  • Створено архітектурну схему (ARCHITECTURE.md)
  • Оновлено README.md з детальною інформацією
  • Створено CHANGES.md з повним changelog

Можливі покращення в майбутньому:

  • Експорт/імпорт промптів у JSON/YAML формат
  • Бібліотека готових шаблонів промптів
  • Версіонування промптів (історія змін)
  • A/B тестування різних промптів з метриками
  • Адміністративна панель для глобальних промптів
  • Можливість шерингу промптів між користувачами
  • Автоматичне збереження вдалих промптів
  • Рекомендації по покращенню промптів на основі AI

Phase 1: YAML Configuration (HIGH PRIORITY)

1.1 Create configuration structure

  • Create config/ directory
  • Create config/init.py
  • Create config/environments/ directory
  • Create config/environments/default.yaml
  • Create config/environments/development.yaml
  • Create config/environments/production.yaml

1.2 Pydantic models

  • Create config/settings.py with Pydantic models
  • AppConfig - general app settings
  • AWSConfig - AWS/S3 settings
  • LlamaIndexConfig - LlamaIndex settings
  • ModelConfig - models configuration
  • SessionConfig - session settings
  • LoggingConfig - logging settings
  • Settings - main configuration class

1.3 Configuration loader

  • Create config/loader.py
  • ConfigLoader class with load_yaml() method
  • merge_configs() method (default + environment)
  • validate_config() method
  • Support environment variables in YAML

1.4 Validator

  • Create config/validator.py
  • Validate required fields
  • Validate API keys
  • Validate file paths
  • Validate models

1.5 Refactor config.py

  • Remove hardcoded values
  • Keep only Enum classes
  • Add get_settings() function
  • Update validate_environment()
  • Update imports in other files

Phase 2: Session Management (HIGH PRIORITY)

2.1 Create session structure

  • Create src/ directory
  • Create src/session/ directory
  • Create src/session/init.py

2.2 Session manager

  • Create src/session/manager.py
  • SessionManager class with get_session() method
  • cleanup_session() method
  • cleanup_expired_sessions() background task
  • Thread-safe operations with asyncio.Lock

2.3 Session state

  • Create src/session/state.py
  • UserSessionState dataclass
  • Fields: session_id, legal_position_json, search_nodes
  • Timestamps: created_at, last_activity
  • update_activity() method

2.4 Session storage

  • Create src/session/storage.py
  • BaseStorage abstract class
  • MemoryStorage implementation
  • RedisStorage implementation (optional)
  • Storage factory

2.5 Update interface.py

  • Add SessionManager initialization
  • Add session_id State for each user
  • Update all handlers to use session-based state
  • Remove global state variables
  • Add session cleanup on disconnect

Phase 3: Refactor main.py

3.1 Create LLM providers structure

  • Create src/llm/ directory
  • Create src/llm/init.py

3.2 Base LLM provider

  • Create src/llm/base.py
  • BaseLLMProvider abstract class
  • analyze() abstract method
  • generate() abstract method
  • Common error handling

3.3 Specific providers

  • Create src/llm/openai.py - OpenAIProvider
  • Create src/llm/anthropic.py - AnthropicProvider
  • Create src/llm/gemini.py - GeminiProvider
  • Create src/llm/deepseek.py - DeepSeekProvider

3.4 LLM factory

  • Create src/llm/factory.py
  • LLMFactory class
  • create_provider() method
  • Provider registry

3.5 Create services

  • Create src/services/ directory
  • Create src/services/init.py
  • Create src/services/generation.py - GenerationService
  • Create src/services/search.py - SearchService
  • Create src/services/analysis.py - AnalysisService

3.6 Create workflows

  • Create src/workflows/ directory
  • Create src/workflows/init.py
  • Move PrecedentAnalysisWorkflow to src/workflows/precedent_analysis.py

3.7 Create storage

  • Create src/storage/ directory
  • Create src/storage/init.py
  • Create src/storage/s3.py - S3Storage
  • Create src/storage/local.py - LocalStorage

3.8 Update main.py

  • Remove LLMAnalyzer class (moved to providers)
  • Remove PrecedentAnalysisWorkflow (moved to workflows)
  • Remove generate_legal_position (moved to services)
  • Remove search functions (moved to services)
  • Remove analyze_action (moved to services)
  • Keep only initialization and app launch

Phase 4: Error Handling and Logging

4.1 Custom exceptions

  • Create src/exceptions.py
  • LegalPositionError base exception
  • ConfigurationError
  • LLMProviderError
  • SearchError
  • SessionError

4.2 Logging configuration

  • Create src/logging_config.py
  • Setup logging from YAML config
  • Add file and console handlers
  • Add log rotation

4.3 Middleware

  • Create src/middleware/ directory
  • Create src/middleware/init.py
  • Create src/middleware/error_handler.py
  • Create src/middleware/rate_limiter.py

4.4 Update all modules

  • Add logging to all services
  • Add proper error handling
  • Add try-except blocks with custom exceptions

Phase 5: Additional Improvements

5.1 Validation

  • Add Pydantic models for input validation
  • Validate user inputs in interface.py
  • Sanitize outputs

5.2 Testing

  • Create tests/ directory
  • Add pytest configuration
  • Write unit tests for services
  • Write integration tests
  • Add test coverage reporting

5.3 Documentation

  • Update README.md with new structure
  • Add docstrings to all classes and methods
  • Create API documentation
  • Add usage examples

5.4 Hugging Face optimization

  • Add health check endpoint
  • Optimize memory usage
  • Add graceful shutdown
  • Add performance monitoring
  • Test on Hugging Face Spaces

5.5 CI/CD

  • Create .github/workflows/ directory
  • Add GitHub Actions for testing
  • Add linting (flake8, mypy)
  • Add automatic deployment to Hugging Face

Dependencies to add

  • pyyaml - for YAML configuration
  • pydantic - for configuration validation
  • pydantic-settings - for settings management
  • redis (optional) - for session storage
  • pytest - for testing
  • pytest-asyncio - for async tests
  • pytest-cov - for coverage
  • mypy - for type checking
  • flake8 - for linting

Notes

  • Start with Phase 1 (YAML Configuration)
  • Then Phase 2 (Session Management) - critical for Hugging Face
  • Phase 3 can be done incrementally
  • Phases 4-5 are lower priority but important for production

Current Progress

  • Project analysis completed
  • Refactoring plan created
  • Phase 1: YAML Configuration - COMPLETED ✅