Create config.py
Browse files
config.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import logging
|
3 |
+
import asyncio
|
4 |
+
from fastapi import FastAPI
|
5 |
+
from txagent.txagent import TxAgent
|
6 |
+
from db.mongo import get_mongo_client
|
7 |
+
|
8 |
+
# Logging
|
9 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
10 |
+
logger = logging.getLogger("TxAgentAPI")
|
11 |
+
|
12 |
+
# Globals
|
13 |
+
agent = None
|
14 |
+
patients_collection = None
|
15 |
+
analysis_collection = None
|
16 |
+
alerts_collection = None
|
17 |
+
users_collection = None
|
18 |
+
|
19 |
+
# JWT settings (must match CPS-API)
|
20 |
+
SECRET_KEY = os.getenv("SECRET_KEY", "your-secret-key")
|
21 |
+
ALGORITHM = "HS256"
|
22 |
+
|
23 |
+
def setup_app(app: FastAPI):
|
24 |
+
global agent, patients_collection, analysis_collection, alerts_collection, users_collection
|
25 |
+
|
26 |
+
@app.on_event("startup")
|
27 |
+
async def startup_event():
|
28 |
+
global agent, patients_collection, analysis_collection, alerts_collection, users_collection
|
29 |
+
|
30 |
+
agent = TxAgent(
|
31 |
+
model_name="mims-harvard/TxAgent-T1-Llama-3.1-8B",
|
32 |
+
rag_model_name="mims-harvard/ToolRAG-T1-GTE-Qwen2-1.5B",
|
33 |
+
enable_finish=True,
|
34 |
+
enable_rag=False,
|
35 |
+
force_finish=True,
|
36 |
+
enable_checker=True,
|
37 |
+
step_rag_num=4,
|
38 |
+
seed=42
|
39 |
+
)
|
40 |
+
agent.chat_prompt = (
|
41 |
+
"You are a clinical assistant AI. Analyze the patient's data and provide clear clinical recommendations."
|
42 |
+
)
|
43 |
+
agent.init_model()
|
44 |
+
logger.info("✅ TxAgent initialized")
|
45 |
+
|
46 |
+
db = get_mongo_client()["cps_db"]
|
47 |
+
users_collection = db["users"]
|
48 |
+
patients_collection = db["patients"]
|
49 |
+
analysis_collection = db["patient_analysis_results"]
|
50 |
+
alerts_collection = db["clinical_alerts"]
|
51 |
+
logger.info("📡 Connected to MongoDB")
|
52 |
+
|
53 |
+
asyncio.create_task(analyze_all_patients())
|
54 |
+
|
55 |
+
async def analyze_all_patients():
|
56 |
+
patients = await patients_collection.find({}).to_list(length=None)
|
57 |
+
for patient in patients:
|
58 |
+
await analyze_patient(patient)
|
59 |
+
await asyncio.sleep(0.1)
|