Spaces:
Sleeping
Sleeping
File size: 4,429 Bytes
bbb4f78 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | # `app/models` – Data Models & Persona Configuration
This module defines the **core data structures** for users, chat sessions, and AI advisor personas in the Multi-LLM Chatbot Backend.
It plays a foundational role in ensuring that:
- User data and session state are **structured, validated, and serializable**
- Persona behavior is **configurable, injectable, and extensible**
---
## Persona Model (`persona.py`)
### `class Persona`
Represents a single AI advisor with its own personality, tone, and domain of expertise.
| Attribute | Description |
|----------------|-------------|
| `id` | Unique identifier for the persona |
| `name` | Human-readable display name |
| `system_prompt`| The persona’s default LLM instruction |
| `llm` | Instance of the LLM client (Gemini/Ollama) |
| `temperature` | Controls creativity level (0–10 scale, converted to 0.0–1.0 internally) |
### `respond()` method
This asynchronous method generates a persona-specific reply using the provided context and desired `response_length` (short, medium, long). It uses a **system prompt + user messages** + length-based instructions.
```python
await persona.respond(context=messages, response_length="medium")
```
---
## Persona Registry (`default_personas.py`)
Defines and registers **all built-in personas** using detailed `system_prompt` templates and metadata.
> These prompts define the tone, response style, formatting rules, document behavior, and epistemological approach of each advisor.
### Available Personas
- `methodologist`: Research methods and design expert
- `theorist`: Theoretical frameworks and philosophy of science
- `pragmatist`: Action-oriented coach with a focus on task execution
- `socratic`: Socratic questioning mentor
- `motivator`: Psychology-focused coach to build momentum
- `critic`: Constructive reviewer with sharp academic critique
- `storyteller`: Communication and storytelling specialist
- `minimalist`: Minimal guidance, maximum clarity
- `visionary`: Long-term strategy and innovation
- `empathetic`: Emotionally aware advisor for mental health & motivation
### Registry Functions
| Function | Description |
|---------|-------------|
| `get_default_personas(llm)` | Returns a list of `Persona` instances with LLM injected |
| `get_default_persona_prompt(pid)` | Returns only the `system_prompt` of a persona |
| `is_valid_persona_id(pid)` | Checks if ID exists in registry |
| `list_available_personas()` | Lists all persona IDs |
---
## User & Session Models (`user.py`)
### `UserCreate` / `UserLogin`
Pydantic models for request payloads during signup/login.
### `User`
Persistent user object, mapped to MongoDB using `_id` aliasing.
| Field | Description |
|-------|-------------|
| `id` (`_id`) | MongoDB ObjectId |
| `email`, `hashed_password` | Auth fields |
| `academicStage`, `researchArea` | Optional metadata |
| `created_at`, `last_login` | Timestamps |
| `is_active` | Soft-deletion or block flag |
### `UserResponse`
Serialized user profile returned to frontend after login/token validation.
---
### `ChatSession`
Stores a **single multi-turn conversation**. Used for RAG context, memory, and export.
| Field | Description |
|-------|-------------|
| `id` | MongoDB `_id` |
| `user_id` | Owner user’s ID |
| `title` | Human-readable title |
| `messages` | List of exchanged messages |
| `created_at`, `updated_at` | Session lifecycle tracking |
| `is_active` | Whether it is a deleted/inactive session |
### `ChatSessionResponse`
Returned when listing past sessions (lightweight response).
---
### `Token`
Used as the unified login response structure:
```json
{
"access_token": "...",
"token_type": "bearer",
"user": { ... }
}
```
---
## Design Principles
- All models are **fully compatible with FastAPI + Pydantic**
- MongoDB integration uses `bson.ObjectId` support and aliases
- Persona logic is **decoupled** from orchestration — easy to extend
- System prompts are rich, structured, and **frontend-format aware** (markdown rules enforced)
---
## Next Steps
This module is used by:
- `core/improved_orchestrator.py` – Persona routing
- `routes/chat.py` – Sequential chat + replies
- `auth.py` – Token generation and validation
- `documents.py` – Document-enhanced message generation
> Add a new persona? Just extend `DEFAULT_PERSONAS` and restart the backend. |