File size: 5,923 Bytes
bbb4f78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4a09bf
bbb4f78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4a09bf
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# `app/api` – REST API Layer for Multi-LLM Chatbot

This module defines the complete FastAPI-based HTTP interface for all backend features, including chat, session management, RAG operations, provider switching, and document interaction.

Each file in this directory defines route groups (`APIRouter`) to modularize functionality.

---

## API Directory Layout

| File | Purpose |
|------|---------|
| `auth.py` | Handles user authentication (login, signup, token validation) |
| `chat.py` | Core routes for LLM-backed chat, reply-to-advisor, and multi-turn flow |
| `chat_sessions.py` | Stores user conversations and provides access to saved history |
| `debug.py` | Developer tools: debug personas, RAG tests, ranking advisor responses |
| `documents.py` | Upload, parse, index, and query documents via RAG |
| `provider.py` | Switch between Gemini and Ollama providers |
| `root.py` | Root `/` endpoint for heartbeat and versioning |
| `sessions.py` | Tracks and resets session-specific in-memory context |
| `utils.py` | Helpers used by multiple routers (e.g. session ID management) |

---

## `auth.py` – User Authentication API

| Endpoint | Method | Description |
|----------|--------|-------------|
| `/signup` | `POST` | Register a new user |
| `/login` | `POST` | Authenticate user and return access token |
| `/me` | `GET` | Return current logged-in user |
| `/healthcheck` | `GET` | Ping endpoint to check login status |

Uses JWT-based Bearer token auth via FastAPI dependencies.

---

## `chat.py` – Chat Interaction

| Endpoint | Method | Description |
|----------|--------|-------------|
| `/chat-stream` | `POST` | Stream advisor responses as newline-delimited JSON |
| `/reply-to-advisor` | `POST` | Ask a question to a specific advisor/persona |

These routes handle:
- Message routing via `ImprovedChatOrchestrator`
- Persona-wise response generation
- Embedding document-aware context
- Returning consistent message structure

---

## `chat_sessions.py` – Persistent Storage of Conversations

| Endpoint | Method | Description |
|----------|--------|-------------|
| `/chat-sessions` | `GET` | List all saved chat sessions |
| `/chat-sessions/{id}` | `GET` | Retrieve specific chat session |
| `/chat-sessions/{id}` | `DELETE` | Soft-delete a chat session |
| `/chat-sessions/save` | `POST` | Save in-memory session to MongoDB |

Saves message history, metadata, and uploaded files.

---

## `debug.py` – Developer Tools

| Endpoint | Method | Description |
|----------|--------|-------------|
| `/debug/personas` | `GET` | List current personas, prompts, keywords |
| `/debug/ranked-personas` | `GET` | Return top advisors for current session |
| `/debug/rag-status` | `GET` | Run sample RAG query + return health info |

Provides insight into:
- Persona prompt preview
- RAG test queries and indexed documents
- Session size + truncation status

---

## `documents.py` – Document Upload and RAG

| Endpoint | Method | Description |
|----------|--------|-------------|
| `/upload-document` | `POST` | Upload and parse a document for semantic search |
| `/search-documents` | `POST` | RAG search using text query and persona context |
| `/document-stats` | `GET` | Overview of documents uploaded to session |
| `/uploaded-files` | `GET` | Return list of uploaded file names |
| `/document-insights/{filename}` | `GET` | Get detailed metadata for a document |
| `/export-chat` | `GET` | Export current or stored chat session (PDF, TXT, DOCX) |
| `/chat-summary` | `GET` | Export summary generated by LLM (multi-format) |

Supports file parsing (`PDF`, `DOCX`, `TXT`), chunking, embedding, and export.

---

## `provider.py` – LLM Provider Control

| Endpoint | Method | Description |
|----------|--------|-------------|
| `/current-provider` | `GET` | Return currently active provider and model |
| `/switch-provider` | `POST` | Dynamically switch between `gemini` and `ollama` |
| `/current-model` | `GET` | Get currently loaded model name |
| `/switch-model` | `POST` | Alias for switching based on model name |

Changes are propagated by:
- Creating new LLM client
- Re-registering all personas

---

## `sessions.py` – In-Memory Session Management

| Endpoint | Method | Description |
|----------|--------|-------------|
| `/context` | `GET` | Return current session context (messages, documents, stats) |
| `/reset-session` | `POST` | Reset in-memory session or specific chat context |
| `/session-stats` | `GET` | Return stats like message count, file size, timestamps |
| `/active-sessions` | `GET` | Return list of all active in-memory sessions |
| `/cleanup-sessions` | `POST` | Manually trigger expired session cleanup |

Supports ephemeral sessions and reusable chat contexts (e.g. for documents).

---

## `utils.py` – Route-Level Utilities

Defines shared helper:

- `get_or_create_session_for_request(request)`  
- `get_or_create_session_for_request_async(request)`  

These parse session cookies or generate new session IDs, crucial for maintaining separation across:
- In-memory ephemeral sessions
- Document-linked long-term sessions

---

## `root.py` – API Healthcheck

| Endpoint | Method | Description |
|----------|--------|-------------|
| `/` | `GET` | Return version + feature list |

Simple heartbeat endpoint used for readiness probes and sanity checks.

---

## Auth Flow Integration

Most routes use:

```python
Depends(get_current_active_user)
```

This ensures only logged-in users can:
- Upload and retrieve files
- Export summaries
- Save or delete chat sessions

JWT tokens are passed via the `Authorization: Bearer ...` header.

---

## High-Level Flow

```text
Frontend → /chat-stream → orchestrator → personas → RAG + LLM → response[]
        ↘ /upload-document → extractor → RAG chunks → indexed
        ↘ /context or /reset-session → session_manager
        ↘ /export-chat or /chat-summary → utils + formatter
```

---