docs: update documentation for React frontend and cache mode
Update all documentation to reflect recent changes: - Replace Streamlit dashboard references with React TypeScript dashboard - Update dashboard port from 8501 to 8080 - Add auth.py and database.py to architecture section - Change USE_DATABASE terminology to USE_CACHE - Add JWT_SECRET to environment variables reference - Document default admin credentials and user seeding 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
+58
-45
@@ -1,16 +1,19 @@
|
||||
# Database Mode for Testing and Analytics
|
||||
# Database Storage and Caching
|
||||
|
||||
This document explains how to use SPARC's database mode for storing LLM messages for testing and analytics purposes.
|
||||
This document explains how SPARC uses PostgreSQL for storing LLM messages, enabling response caching and analytics.
|
||||
|
||||
## Overview
|
||||
|
||||
SPARC supports two modes of operation:
|
||||
SPARC stores all LLM interactions in PostgreSQL, providing:
|
||||
|
||||
1. **API Mode** (default): Messages are sent to OpenRouter's API and you receive real LLM responses
|
||||
2. **Database Mode**: Messages are stored in a PostgreSQL database without making API calls, useful for:
|
||||
- Testing the application without consuming API credits
|
||||
- Collecting analytics on message patterns and usage
|
||||
- Development and debugging
|
||||
- **Response Caching**: Avoid redundant API calls for previously analyzed patents
|
||||
- **Analytics**: Track usage patterns, token consumption, and analysis history
|
||||
- **Persistence**: Maintain analysis history across sessions
|
||||
|
||||
SPARC supports two cache modes:
|
||||
|
||||
1. **Cache Mode** (default, `USE_CACHE=true`): Check database for cached responses before making API calls
|
||||
2. **Fresh Mode** (`USE_CACHE=false`): Always make fresh API calls (still stores results in database)
|
||||
|
||||
## Setup
|
||||
|
||||
@@ -45,43 +48,43 @@ cp .env.example .env
|
||||
Edit `.env` and set:
|
||||
|
||||
```env
|
||||
# For database mode (testing/analytics)
|
||||
USE_DATABASE=true
|
||||
# Database connection (required)
|
||||
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/sparc
|
||||
|
||||
# For API mode (production)
|
||||
USE_DATABASE=false
|
||||
# Cache mode: use cached responses when available
|
||||
USE_CACHE=true
|
||||
|
||||
# API key for fresh LLM calls
|
||||
OPENROUTER_API_KEY=your_openrouter_key_here
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Running in Database Mode
|
||||
### Running with Cache Mode (Default)
|
||||
|
||||
Set `USE_DATABASE=true` in your `.env` file, then run the application normally:
|
||||
Set `USE_CACHE=true` in your `.env` file, then run the application normally:
|
||||
|
||||
```bash
|
||||
python main.py
|
||||
```
|
||||
|
||||
Instead of sending messages to OpenRouter, the application will:
|
||||
- Store all prompts in the database
|
||||
- Return a placeholder response
|
||||
- Log metadata (company name, analysis type, timestamps)
|
||||
The application will:
|
||||
- Check the database for cached responses matching the request
|
||||
- If found, return the cached response (no API call)
|
||||
- If not found, make an API call and store the response for future use
|
||||
|
||||
### Running in API Mode
|
||||
### Running with Fresh Mode
|
||||
|
||||
Set `USE_DATABASE=false` in your `.env` file, then run the application normally:
|
||||
Set `USE_CACHE=false` in your `.env` file to always get fresh responses:
|
||||
|
||||
```bash
|
||||
python main.py
|
||||
```
|
||||
|
||||
The application will send messages to OpenRouter and return real LLM responses.
|
||||
|
||||
### Hybrid Mode (Optional)
|
||||
|
||||
You can also enable database logging while still using the API by initializing the database client in your code. The `LLMAnalyzer` will automatically log all API calls to the database if a database client is available.
|
||||
The application will:
|
||||
- Always send messages to OpenRouter for real LLM responses
|
||||
- Store all responses in the database
|
||||
- Useful when you need the latest analysis or want to refresh cached data
|
||||
|
||||
## Viewing Analytics
|
||||
|
||||
@@ -195,16 +198,16 @@ docker-compose down -v
|
||||
|
||||
## Toggling Between Modes
|
||||
|
||||
You can easily switch between modes by changing the `USE_DATABASE` environment variable:
|
||||
You can easily switch between modes by changing the `USE_CACHE` environment variable:
|
||||
|
||||
### Quick Toggle (temporary, for testing)
|
||||
### Quick Toggle (temporary)
|
||||
|
||||
```bash
|
||||
# Run in database mode
|
||||
USE_DATABASE=true python main.py
|
||||
# Run with caching enabled
|
||||
USE_CACHE=true python main.py
|
||||
|
||||
# Run in API mode
|
||||
USE_DATABASE=false python main.py
|
||||
# Run with fresh API calls
|
||||
USE_CACHE=false python main.py
|
||||
```
|
||||
|
||||
### Persistent Toggle
|
||||
@@ -212,38 +215,48 @@ USE_DATABASE=false python main.py
|
||||
Edit your `.env` file:
|
||||
|
||||
```env
|
||||
# For testing/analytics
|
||||
USE_DATABASE=true
|
||||
# Use cached responses when available (recommended for most use)
|
||||
USE_CACHE=true
|
||||
|
||||
# For production use
|
||||
USE_DATABASE=false
|
||||
# Always make fresh API calls
|
||||
USE_CACHE=false
|
||||
```
|
||||
|
||||
## Use Cases
|
||||
|
||||
### Testing Without API Costs
|
||||
### Cost Optimization with Caching
|
||||
|
||||
During development, enable database mode to test the full application flow without consuming API credits:
|
||||
Cache mode reduces API costs by reusing previous analysis results:
|
||||
|
||||
```bash
|
||||
USE_DATABASE=true python main.py
|
||||
USE_CACHE=true python main.py
|
||||
```
|
||||
|
||||
If the same company/patent combination was analyzed before, the cached response is returned instantly.
|
||||
|
||||
### Fresh Analysis
|
||||
|
||||
When you need the latest LLM analysis (e.g., after model updates):
|
||||
|
||||
```bash
|
||||
USE_CACHE=false python main.py
|
||||
```
|
||||
|
||||
### Collecting Usage Analytics
|
||||
|
||||
Enable database mode in a test environment to collect analytics on:
|
||||
The database stores all interactions, enabling analytics on:
|
||||
- Which companies are analyzed most frequently
|
||||
- Types of analyses performed
|
||||
- Prompt patterns and lengths
|
||||
- Usage over time
|
||||
- Token usage and costs over time
|
||||
- Response caching hit rates
|
||||
|
||||
### Development and Debugging
|
||||
|
||||
Database mode is useful for:
|
||||
- Testing patent parsing logic without API calls
|
||||
Database storage is useful for:
|
||||
- Reviewing actual prompts sent to the LLM
|
||||
- Analyzing response patterns
|
||||
- Debugging the full pipeline end-to-end
|
||||
- Collecting sample prompts for optimization
|
||||
- Understanding token usage patterns (when in API mode with logging)
|
||||
- Understanding token usage patterns
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
|
||||
Reference in New Issue
Block a user