Spaces:
Sleeping
Sleeping
File size: 12,763 Bytes
33340fe | 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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | # Application: REST API Server (FastAPI)
**Code**: `adsp/app/api_server.py`
## Purpose
Expose the Application layer over HTTP so external clients (frontend/UI, tools, other services) can interact with:
- authentication
- persona discovery
- persona chat (Q&A)
- ingestion (upload)
- reports
The server is designed for local development and demos. It wires existing in-process services (`adsp/app/*`) and returns typed JSON responses.
## Key technologies
- **FastAPI**: REST framework + OpenAPI generation
- **Pydantic**: request/response validation (reuses `adsp/core/types.py`)
- **Uvicorn**: ASGI server
Dependencies are declared in `requirements.txt` (`fastapi`, `uvicorn[standard]`).
## Runtime architecture (in-process)
On startup, the API server creates and stores service singletons:
- `AuthService` (`adsp/app/auth_service.py`)
- `QAService` (`adsp/app/qa_service.py`) → uses Core orchestrator with local RAG + persona loading
- `IngestionService` (`adsp/app/ingestion_service.py`) → writes to `object_store` shim
- `ReportService` (`adsp/app/report_service.py`) → writes markdown to `reports/api/` by default
- `SurveyManager` (`adsp/core/survey/survey_manager.py`) → stores survey definitions
- `GroupGenerator` (`adsp/core/survey/group_generator.py`) → creates respondent groups
- `ResponseSynthesizer` (`adsp/core/survey/response_synthesizer.py`) → runs survey simulations and exports results
These are accessible to endpoints via a FastAPI dependency (`get_services()`).
## Authentication model (dev)
If `ADSP_REQUIRE_AUTH=true`, protected endpoints require:
- `X-User: <user>`
- `X-Token: <token>`
Register tokens via:
- `POST /v1/auth/register`
When `ADSP_REQUIRE_AUTH` is unset/false, endpoints are open for local demo convenience.
## Swagger / OpenAPI
- Swagger UI: `GET /docs`
- OpenAPI JSON: `GET /openapi.json`
- ReDoc: `GET /redoc`
## Endpoints
### System
- `GET /health`
- Response fields:
- `status`: API health indicator (`ok` when service is up)
- `version`: API version string
- `qa_status`: QA service warmup status (`idle|warming|ready|error`)
- Example:
```bash
curl -X GET "http://localhost:8000/health"
```
### Auth
- `POST /v1/auth/register`
- Request fields:
- `user`: user identifier
- `token`: API token assigned to this user
- Response fields:
- `status`: registration result (`ok`)
- Example:
```bash
curl -X POST "http://localhost:8000/v1/auth/register" \
-H "Content-Type: application/json" \
-d '{"user":"demo-user","token":"demo-token-123"}'
```
- `POST /v1/auth/validate`
- Request fields:
- `user`: user identifier to validate
- `token`: token to validate
- Response fields:
- `authorized`: `true` when user/token pair matches registry
- Example:
```bash
curl -X POST "http://localhost:8000/v1/auth/validate" \
-H "Content-Type: application/json" \
-d '{"user":"demo-user","token":"demo-token-123"}'
```
### Personas
- `GET /v1/personas`
- Response fields:
- `personas`: array of persona summaries
- `persona_id`: stable persona key
- `persona_name`: human-readable name
- `summary_bio`: short profile summary
- Example:
```bash
curl -X GET "http://localhost:8000/v1/personas"
```
- `GET /v1/personas/{persona_id}/profile`
- Path fields:
- `persona_id`: persona to retrieve
- Response fields:
- Full `PersonaProfileModel` payload (`adsp/data_pipeline/schema.py`)
- Example:
```bash
curl -X GET "http://localhost:8000/v1/personas/curious-connoisseurs/profile"
```
- `GET /v1/personas/{persona_id}/system-prompt`
- Path fields:
- `persona_id`: persona to retrieve
- Response fields:
- `system_prompt`: generated prompt string used by the orchestrator
- Example:
```bash
curl -X GET "http://localhost:8000/v1/personas/curious-connoisseurs/system-prompt"
```
### Chat
- `POST /v1/chat`
- Request fields (`ChatRequest`):
- `persona_id`: target persona id
- `query`: user question
- `session_id`: optional session key
- `persona_display_name`: optional persona label
- `attachments`: optional list of attachment objects (`type`, `name`, `mime_type`, `payload`)
- `top_k`: retrieval depth
- `use_tools`: enable/disable tool use
- Response fields (`response: ChatResponse`):
- `persona_id`: persona used to answer
- `answer`: generated response text
- `context`: assembled retrieval context
- `citations`: evidence list
- `tool_calls`: tool execution trace
- Example:
```bash
curl -X POST "http://localhost:8000/v1/chat" \
-H "Content-Type: application/json" \
-d '{
"persona_id":"curious-connoisseurs",
"query":"What matters most when buying coffee?",
"top_k":5,
"use_tools":false
}'
```
### Ingestion
- `POST /v1/ingestion/upload`
- Request fields:
- `filename`: target object key
- `content_base64`: Base64-encoded file payload
- `bucket`: optional destination bucket override
- Response fields:
- `bucket`: storage bucket used
- `key`: object key written
- `size_bytes`: decoded payload size
- Example:
```bash
curl -X POST "http://localhost:8000/v1/ingestion/upload" \
-H "Content-Type: application/json" \
-d '{
"filename":"notes.txt",
"content_base64":"SGVsbG8gTGF2YXp6YSBBSSBQZXJzb25hcyE=",
"bucket":"uploads"
}'
```
### Reports
- `POST /v1/reports/{persona_id}`
- Path fields:
- `persona_id`: persona key used to name output report
- Request fields:
- `insights`: free-form insights object rendered to markdown
- Response fields:
- `path`: generated report path
- Example:
```bash
curl -X POST "http://localhost:8000/v1/reports/curious-connoisseurs" \
-H "Content-Type: application/json" \
-d '{
"insights":{
"top_drivers":["taste","price","brand trust"],
"summary":"Segment values consistent quality and practical pricing."
}
}'
```
### Survey Simulation
- `GET /api/surveys`
- Query fields:
- `page`: 1-based page number (default `1`)
- `page_size`: items per page (default `20`, max `200`)
- Response fields:
- `items`: paged survey list (`survey_id`, `title`, `description`, `created_at`, `questions`)
- `paging`: metadata (`page`, `page_size`, `total_items`, `total_pages`)
- Example:
```bash
curl -X GET "http://localhost:8000/api/surveys?page=1&page_size=20"
```
- `POST /api/surveys`
- Request fields:
- `survey_id`: optional id (auto-generated if omitted)
- `title`: survey title
- `description`: survey objective
- `questions`: question array (`question_id`, `text`, `type`, optional `options`, optional `metadata`)
- Response fields:
- `survey_id`, `title`, `description`, `created_at`, `questions`
- Example:
```bash
curl -X POST "http://localhost:8000/api/surveys" \
-H "Content-Type: application/json" \
-d '{
"survey_id":"coffee-pref-2026-q1",
"title":"Coffee Preference Survey",
"description":"Capture coffee purchase preferences.",
"questions":[
{
"question_id":"q1",
"text":"Which product claim matters most?",
"type":"multiple_choice",
"options":["Great taste","Affordable price","Sustainable sourcing"]
},
{
"question_id":"q2",
"text":"How likely are you to try a new blend?",
"type":"rating",
"metadata":{"scale":"1-5"}
}
]
}'
```
- `GET /api/surveys/{survey_id}`
- Path fields:
- `survey_id`: survey identifier
- Response fields:
- `survey_id`, `title`, `description`, `created_at`, `questions`
- `simulations`: all simulation runs for this survey
- `simulation_id`, `group_id`, `status`, `started_at`, `completed_at`
- `progress` (`completed`, `pending`)
- `responses_count`, `responses`, `statistics_ready`
- Example:
```bash
curl -X GET "http://localhost:8000/api/surveys/coffee-pref-2026-q1"
```
- `POST /api/groups`
- Request fields:
- `composition`: list of group parts (`persona_id`, `count`)
- `mode`: `random` or `llm`
- `sampling_ratio`: trait sampling ratio
- `include_names`: generate synthetic names flag
- `countries`: optional list of allowed countries (for example `["italy","france"]`)
- `seed`: optional deterministic seed
- Response fields:
- `group_id`, `created_at`, `total_respondents`, `composition`, `generation_method`, `sample_profiles`
- each generated profile includes `name`, `country`, `gender`, and `ethnicity`
- Example:
```bash
curl -X POST "http://localhost:8000/api/groups" \
-H "Content-Type: application/json" \
-d '{
"composition":[
{"persona_id":"curious-connoisseurs","count":25},
{"persona_id":"conscious-explorers","count":15}
],
"mode":"random",
"sampling_ratio":0.7,
"include_names":true,
"countries":["italy","france"],
"seed":42
}'
```
- `GET /api/groups/countries`
- Purpose:
- Lists every country id supported by `POST /api/groups` in `countries`
- Response fields:
- `countries[]`: `country_id`, `display_name`, `description`
- Example:
```bash
curl -X GET "http://localhost:8000/api/groups/countries"
```
- `GET /api/groups`
- Query fields:
- `page`: 1-based page number (default `1`)
- `page_size`: items per page (default `20`, max `200`)
- Response fields:
- `items`: paged group list (`group_id`, `created_at`, `total_respondents`, `composition`, `generation_method`, `sample_profiles`)
- `paging`: metadata (`page`, `page_size`, `total_items`, `total_pages`)
- Example:
```bash
curl -X GET "http://localhost:8000/api/groups?page=1&page_size=20"
```
- `GET /api/groups/{group_id}`
- Path fields:
- `group_id`: group identifier
- Query fields:
- `include_full_profiles`: `true` to include full respondent payloads
- Response fields:
- `group_id`, `created_at`, `total_respondents`, `composition`, `generation_method`, `respondents`, `sample_profiles`
- Example:
```bash
curl -X GET "http://localhost:8000/api/groups/group-ab12cd34?include_full_profiles=false"
```
- `POST /api/surveys/{survey_id}/simulate`
- Path fields:
- `survey_id`: target survey id
- Query fields:
- `background`: when `true` (default), starts async run and returns immediately
- Request fields:
- `group_id`: source respondent group
- `batch_size`: processing batch size
- `format`: output format hint
- Response fields:
- `simulation_id`, `status`, `total_respondents`, `progress`, `estimated_time_minutes`
- `status` is `processing` while running and becomes `completed` after finish
- Example:
```bash
curl -X POST "http://localhost:8000/api/surveys/coffee-pref-2026-q1/simulate?background=true" \
-H "Content-Type: application/json" \
-d '{
"group_id":"group-ab12cd34",
"batch_size":10,
"format":"json"
}'
```
- `GET /api/surveys/{survey_id}/responses`
- Path fields:
- `survey_id`: target survey id
- Query fields:
- `simulation_id`: optional simulation id (latest when omitted)
- `format`: `csv` or `json`
- Response:
- File download (`text/csv` for `format=csv`, `application/json` for `format=json`)
- Example:
```bash
curl -L -X GET "http://localhost:8000/api/surveys/coffee-pref-2026-q1/responses?format=csv" -o responses.csv
```
- `GET /api/surveys/{survey_id}/statistics`
- Path fields:
- `survey_id`: target survey id
- Query fields:
- `simulation_id`: optional simulation id (latest when omitted)
- Notes:
- Statistics are computed only for `completed` simulations.
- The first computation is stored as cache per simulation and reused on subsequent calls.
- Response fields:
- `survey_id`, `simulation_id`, `total_respondents`, `total_responses`, `computed_at`, `questions`
- Per-question fields:
- `question_id`, `text`, `type`, `answered_count`, `response_rate`
- `choice_distribution` (`multiple_choice`)
- `rating_distribution`, `rating_average`, `rating_min`, `rating_max` (`rating`)
- `text_response_count`, `sample_text_responses` (`open_ended`)
- Example:
```bash
curl -X GET "http://localhost:8000/api/surveys/coffee-pref-2026-q1/statistics?simulation_id=sim-1234abcd"
```
## How to run locally
```bash
make install
python scripts/run_api.py
```
Then open:
- `http://localhost:8000/docs`
## Configuration
Environment variables (see `.env.example`):
- API:
- `ADSP_API_HOST` (default `0.0.0.0`)
- `ADSP_API_PORT` (default `8000`)
- `ADSP_API_RELOAD` (default `true`)
- `ADSP_REQUIRE_AUTH` (default `false`)
- Data paths:
- `ADSP_PERSONAS_DIR`
- `ADSP_PERSONA_TRAITS_DIR`
- Runtime LLM backend (optional):
- `ADSP_LLM_BACKEND=stub|openai`
- `ADSP_LLM_BASE_URL`, `ADSP_LLM_MODEL`, `ADSP_LLM_API_KEY`
|