-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapi_server.py
More file actions
564 lines (480 loc) · 19 KB
/
api_server.py
File metadata and controls
564 lines (480 loc) · 19 KB
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import Dict, List, Optional, Any
import uuid
from datetime import datetime
import logging
from pathlib import Path
import os
# Import existing AnnaAgent components
from src.anna_agent.backbone import configure
from fixed_ms_patient import FixedMsPatient
from src.anna_agent.dataset_loader import DatasetLoader
from src.anna_agent.complaint_chain import gen_complaint_chain
# Initialize AnnaAgent configuration
configure(Path("."))
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# FastAPI app
app = FastAPI(
title="AnnaAgent API",
description="FastAPI wrapper for AnnaAgent psychological counseling system",
version="0.1.0"
)
# Enable CORS for frontend integration
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Configure appropriately for production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class SafeMsPatientWrapper:
"""现在使用FixedMsPatient,已经内置了强健的错误处理"""
def __init__(self, portrait, report, previous_conversations,chain,prompt):
# 为缺省/空的 chain 提供安全默认
safe_chain = chain if chain else gen_complaint_chain(portrait)
# prompt 允许为空字符串,不阻断
safe_prompt = prompt if (prompt is not None) else ""
self.ms_patient = FixedMsPatient(
portrait, report, previous_conversations, safe_chain, safe_prompt
)
def chat(self, message: str):
"""调用底层并始终返回 (response, emotion, complaint) 三元组。"""
try:
result = self.ms_patient.chat(message)
if isinstance(result, tuple) and len(result) == 3:
response, emotion, complaint = result
elif isinstance(result, str):
response, emotion, complaint = result, "neutral", "unknown"
else:
response, emotion, complaint = (
"抱歉,我刚才走神了...最近工作太忙,脑子有点乱。你刚才说什么?",
"neutral",
"unknown",
)
if not response or (isinstance(response, str) and response.strip() == ""):
logger.warning(
"FixedMsPatient returned empty response - this should not happen"
)
return (
"抱歉,我刚才走神了...最近工作太忙,脑子有点乱。你刚才说什么?",
emotion or "neutral",
complaint or "unknown",
)
return response, emotion, complaint
except Exception as e:
logger.error(f"Unexpected error in SafeMsPatientWrapper: {str(e)}")
return (
"不好意思,我在想工作的事情...能再说一遍吗?",
"neutral",
"unknown",
)
# 完美代理所有其他属性,保持AnnaAgent功能完整性
def __getattr__(self, name):
return getattr(self.ms_patient, name)
# In-memory session storage (use database in production)
sessions: Dict[str, SafeMsPatientWrapper] = {}
session_metadata: Dict[str, dict] = {}
# Dataset loader (with mtime-based hot reload)
DATASET_PATH = Path(os.getenv("MERGED_DATA_PATH", "ref/merged_data.json"))
dataset_loader = DatasetLoader(DATASET_PATH)
# Pydantic models for API
class PatientProfile(BaseModel):
age: str
gender: str
occupation: str
martial_status: str
symptoms: str
class Report(BaseModel):
title: str = "默认咨询案例"
content: Optional[str] = None
class Conversation(BaseModel):
role: str # "Counselor" or "Seeker"
content: str
class CreateSessionRequest(BaseModel):
id: str
conversation: Optional[List[Conversation]] = None
report: Optional[Report] = None
portrait: PatientProfile
seeker_prompt: Optional[str] = None
chain: Optional[List[str]] = None
class CreateSessionByIdRequest(BaseModel):
patient_id: str
class CreateSessionRequest(BaseModel):
profile: PatientProfile
report: Optional[Dict[str, Any]] = None
previous_conversations: Optional[List[Dict[str, str]]] = None
seeker_prompt: Optional[str] = None
chain: Optional[List[str]] = None
class ChatRequest(BaseModel):
message: str
class ChatResponse(BaseModel):
response: str
emotion: str
complaint: str
session_id: str
timestamp: str
message_count: Optional[int] = None
complaint_stage: Optional[int] = None
class SessionResponse(BaseModel):
session_id: str
created_at: str
profile: PatientProfile
status: str = "active"
class SimpleResponse(BaseModel):
response: str
emotion: str
complaint: str
timestamp: str
class PatientSummary(BaseModel):
id: str
name: str
age: str
gender: str
occupation: str
symptoms: List[str]
case_title: str
difficulty: str = "中级"
description: str
class PatientDetail(BaseModel):
id: str
profile: PatientProfile
report: Dict[str, Any]
conversation_preview: List[Dict[str, str]]
seeker_prompt: Optional[str] = None
chain: List[Dict[str, Any]]
total_messages: int
class PatientListResponse(BaseModel):
patients: List[PatientSummary]
total: int
page: int
page_size: int
# Default patient profile for simple chat
DEFAULT_PROFILE = {
"age": "28",
"gender": "男",
"occupation": "软件工程师",
"martial_status": "未婚",
"symptoms": "工作焦虑,失眠"
}
DEFAULT_REPORT = {"案例标题": "工作压力咨询"}
@app.get("/")
async def root():
return {
"message": "AnnaAgent API Server",
"version": "0.1.0",
"endpoints": {
"patients_list": "/api/patients",
"patient_detail": "/api/patients/{patient_id}",
"create_session": "/api/sessions",
"create_session_by_id": "/api/sessions/by_id",
"session_chat": "/api/sessions/{session_id}/chat",
"get_sessions": "/api/sessions",
"get_session_detail": "/api/sessions/{session_id}",
"end_session": "/api/sessions/{session_id}",
"list_patient_ids": "/api/patients/ids"
}
}
@app.get("/api/patients", response_model=PatientListResponse)
async def get_patients(page: int = 1, page_size: int = 10, random_order: bool = False):
"""
获取病人列表,支持分页和随机排序
"""
try:
ids = dataset_loader.list_ids()
total = len(ids)
# 如果需要随机排序
if random_order:
import random
ids = random.sample(ids, len(ids))
# 分页处理
start_idx = (page - 1) * page_size
end_idx = start_idx + page_size
page_ids = ids[start_idx:end_idx]
patients = []
for patient_id in page_ids:
try:
record = dataset_loader.get_by_id(patient_id)
portrait, report, conversation, _, chain = dataset_loader.try_map_to_components(record)
# 解析症状列表
symptoms_str = portrait.get("symptoms", "")
symptoms = [s.strip() for s in symptoms_str.split(";") if s.strip()] if symptoms_str else []
# 生成病人摘要
case_title = report.get("案例标题", "心理咨询案例")
# 根据症状和案例类型判断难度
difficulty = "初级"
if "抑郁" in symptoms_str or "焦虑" in symptoms_str:
difficulty = "中级"
if "精神" in symptoms_str or "幻听" in symptoms_str or "双相" in symptoms_str:
difficulty = "高级"
# 生成描述
case_categories = report.get("案例类别", [])
description = f"{portrait.get('age')}岁{portrait.get('gender')}性,{portrait.get('occupation')},主要涉及{' '.join(case_categories[:2]) if case_categories else '心理健康'}问题"
patient_summary = PatientSummary(
id=patient_id,
name=f"{portrait.get('gender')}性求助者", # 保护隐私,不使用真实姓名
age=portrait.get("age", "28"),
gender=portrait.get("gender", "男"),
occupation=portrait.get("occupation", "未知"),
symptoms=symptoms,
case_title=case_title,
difficulty=difficulty,
description=description
)
patients.append(patient_summary)
except Exception as e:
logger.warning(f"Error processing patient {patient_id}: {str(e)}")
continue
return PatientListResponse(
patients=patients,
total=total,
page=page,
page_size=page_size
)
except Exception as e:
logger.error(f"Error getting patients list: {str(e)}")
raise HTTPException(status_code=500, detail=f"获取病人列表时出错: {str(e)}")
@app.get("/api/patients/{patient_id}", response_model=PatientDetail)
async def get_patient_detail(patient_id: str):
"""
获取指定病人的详细信息
"""
try:
record = dataset_loader.get_by_id(patient_id)
portrait, report, conversation, seeker_prompt, chain = dataset_loader.try_map_to_components(record)
# 处理对话预览(前几条消息)
conversation_preview = conversation[:6] if conversation else []
# 处理chain格式
formatted_chain = []
if isinstance(chain, list):
for i, item in enumerate(chain):
if isinstance(item, dict):
formatted_chain.append({
"stage": item.get("stage", i + 1),
"content": item.get("content", str(item))
})
else:
formatted_chain.append({
"stage": i + 1,
"content": str(item)
})
return PatientDetail(
id=patient_id,
profile=PatientProfile(**portrait),
report=report,
conversation_preview=conversation_preview,
seeker_prompt=seeker_prompt,
chain=formatted_chain,
total_messages=len(conversation)
)
except KeyError:
raise HTTPException(status_code=404, detail=f"病人ID {patient_id} 不存在")
except Exception as e:
logger.error(f"Error getting patient detail: {str(e)}")
raise HTTPException(status_code=500, detail=f"获取病人详情时出错: {str(e)}")
@app.get("/api/patients/ids")
async def list_patient_ids():
"""
获取所有病人ID列表(向后兼容)
"""
try:
ids = dataset_loader.list_ids()
return {"ids": ids, "count": len(ids)}
except Exception as e:
logger.error(f"Error listing patient ids: {str(e)}")
raise HTTPException(status_code=500, detail=f"获取患者ID列表时出错: {str(e)}")
@app.post("/api/sessions", response_model=SessionResponse)
async def create_session(request: CreateSessionRequest):
"""
Create a new patient session with custom profile (for frontend compatibility).
Returns session ID for subsequent conversations.
"""
try:
session_id = str(uuid.uuid4())
# Convert frontend profile to internal format
portrait = {
"age": request.profile.age,
"gender": request.profile.gender,
"occupation": request.profile.occupation,
"martial_status": request.profile.martial_status,
"symptoms": request.profile.symptoms
}
report = request.report or {"title": "自定义咨询案例"}
previous_conversations = request.previous_conversations or []
seeker_prompt = request.seeker_prompt or ""
chain = request.chain or []
# Create MsPatient instance
patient = SafeMsPatientWrapper(
portrait=portrait,
report=report,
previous_conversations=previous_conversations,
chain=chain,
prompt=seeker_prompt
)
# Store session
sessions[session_id] = patient
session_metadata[session_id] = {
"created_at": datetime.now(),
"profile": portrait,
"message_count": 0,
"status": "active"
}
logger.info(f"Created session {session_id} with custom profile")
return SessionResponse(
session_id=session_id,
created_at=session_metadata[session_id]["created_at"].isoformat(),
profile=PatientProfile(**portrait),
status="active"
)
except Exception as e:
logger.error(f"Error creating session: {str(e)}")
raise HTTPException(status_code=500, detail=f"创建会话时出错: {str(e)}")
@app.post("/api/sessions/by_id", response_model=SessionResponse)
async def create_session_by_id(request: CreateSessionByIdRequest):
"""
Create a new patient session by loading from dataset by patient ID.
Returns session ID for subsequent conversations.
"""
try:
session_id = str(uuid.uuid4())
# Load from dataset by id
try:
record = dataset_loader.get_by_id(request.patient_id)
except KeyError:
raise HTTPException(status_code=404, detail=f"未找到患者ID: {request.patient_id}")
portrait, report, previous, seek_prompt, chain = dataset_loader.try_map_to_components(record)
# Create MsPatient instance
patient = SafeMsPatientWrapper(
portrait=portrait,
report=report,
previous_conversations=previous,
chain=chain,
prompt=seek_prompt
)
# Store session
sessions[session_id] = patient
session_metadata[session_id] = {
"created_at": datetime.now(),
"profile": portrait,
"message_count": 0,
"status": "active"
}
logger.info(f"Created session {session_id} for patient {request.patient_id}")
return SessionResponse(
session_id=session_id,
created_at=session_metadata[session_id]["created_at"].isoformat(),
profile=PatientProfile(**portrait),
status="active"
)
except Exception as e:
logger.error(f"Error creating session: {str(e)}")
raise HTTPException(status_code=500, detail=f"创建会话时出错: {str(e)}")
@app.post("/api/sessions/{session_id}/chat", response_model=ChatResponse)
async def session_chat(session_id: str, request: ChatRequest):
"""
Send message to specific session and get response.
Maintains conversation state within the MsPatient instance.
"""
try:
# Check if session exists
if session_id not in sessions:
raise HTTPException(status_code=404, detail=f"会话 {session_id} 不存在")
# Check if session is active
if session_metadata[session_id]["status"] != "active":
raise HTTPException(status_code=400, detail=f"会话 {session_id} 已结束")
# Get patient instance
patient = sessions[session_id]
# Get response
response, emotion, complaint = patient.chat(request.message)
# Update metadata
session_metadata[session_id]["message_count"] += 1
# Build response with additional info
chat_response = ChatResponse(
response=response,
emotion=emotion,
complaint=complaint,
session_id=session_id,
timestamp=datetime.now().isoformat(),
message_count=session_metadata[session_id]["message_count"],
complaint_stage=patient.chain_index
)
logger.info(f"Session {session_id}: message #{chat_response.message_count}")
return chat_response
except HTTPException:
raise
except Exception as e:
logger.error(f"Error in session chat: {str(e)}")
raise HTTPException(status_code=500, detail=f"处理对话时出错: {str(e)}")
@app.get("/api/sessions")
async def list_sessions():
"""
List all active sessions.
"""
try:
session_list = []
for session_id, metadata in session_metadata.items():
session_list.append({
"session_id": session_id,
"created_at": metadata["created_at"].isoformat(),
"message_count": metadata["message_count"],
"status": metadata["status"],
"profile": metadata["profile"]
})
return {"sessions": session_list, "total": len(session_list)}
except Exception as e:
logger.error(f"Error listing sessions: {str(e)}")
raise HTTPException(status_code=500, detail=f"获取会话列表时出错: {str(e)}")
@app.get("/api/sessions/{session_id}")
async def get_session(session_id: str):
"""
Get session details and conversation history.
"""
try:
if session_id not in sessions:
raise HTTPException(status_code=404, detail=f"会话 {session_id} 不存在")
patient = sessions[session_id]
metadata = session_metadata[session_id]
return {
"session_id": session_id,
"metadata": {
"created_at": metadata["created_at"].isoformat(),
"message_count": metadata["message_count"],
"status": metadata["status"],
"profile": metadata["profile"]
},
"conversation": patient.conversation,
"complaint_stage": patient.chain_index,
"status_summary": patient.status
}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error getting session: {str(e)}")
raise HTTPException(status_code=500, detail=f"获取会话详情时出错: {str(e)}")
@app.delete("/api/sessions/{session_id}")
async def end_session(session_id: str):
"""
End a session (mark as inactive).
"""
try:
if session_id not in sessions:
raise HTTPException(status_code=404, detail=f"会话 {session_id} 不存在")
# Mark as inactive instead of deleting to preserve history
session_metadata[session_id]["status"] = "ended"
session_metadata[session_id]["ended_at"] = datetime.now()
return {"message": f"会话 {session_id} 已结束", "session_id": session_id}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error ending session: {str(e)}")
raise HTTPException(status_code=500, detail=f"结束会话时出错: {str(e)}")
# Health check endpoint
@app.get("/health")
async def health_check():
return {"status": "healthy", "timestamp": datetime.now().isoformat()}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8080)