Update app.py
Browse files
app.py
CHANGED
|
@@ -1,151 +1,76 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
-
import
|
| 4 |
-
from typing import Any, Dict
|
| 5 |
-
|
| 6 |
-
from
|
| 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 |
-
transparency = clamp01(req_data.get("transparency", getattr(req, "transparency", 0.5)), 0.5)
|
| 78 |
-
|
| 79 |
-
incoming_help_mode = req_data.get("help_mode", getattr(req, "help_mode", None))
|
| 80 |
-
explicit_intent = req_data.get("intent", getattr(req, "intent", None))
|
| 81 |
-
intent = explicit_intent or detect_intent(actual_user_message or full_text, incoming_help_mode)
|
| 82 |
-
help_mode = intent_to_help_mode(intent)
|
| 83 |
-
|
| 84 |
-
result = engine.generate_response(
|
| 85 |
-
raw_user_text=actual_user_message or full_text,
|
| 86 |
-
tone=tone,
|
| 87 |
-
verbosity=verbosity,
|
| 88 |
-
transparency=transparency,
|
| 89 |
-
intent=intent,
|
| 90 |
-
help_mode=help_mode,
|
| 91 |
-
chat_history=getattr(req, "chat_history", None) or getattr(req, "history", None) or [],
|
| 92 |
-
question_text=question_text,
|
| 93 |
-
options_text=options_text,
|
| 94 |
-
category=category,
|
| 95 |
-
)
|
| 96 |
-
|
| 97 |
-
return JSONResponse(
|
| 98 |
-
{
|
| 99 |
-
"reply": result.reply,
|
| 100 |
-
"meta": {
|
| 101 |
-
"domain": result.domain,
|
| 102 |
-
"solved": result.solved,
|
| 103 |
-
"help_mode": result.help_mode,
|
| 104 |
-
"answer_letter": result.answer_letter,
|
| 105 |
-
"answer_value": result.answer_value,
|
| 106 |
-
"topic": result.topic,
|
| 107 |
-
"used_retrieval": result.used_retrieval,
|
| 108 |
-
"used_generator": result.used_generator,
|
| 109 |
-
},
|
| 110 |
-
}
|
| 111 |
-
)
|
| 112 |
-
except Exception as e:
|
| 113 |
-
return JSONResponse(
|
| 114 |
-
{
|
| 115 |
-
"error": type(e).__name__,
|
| 116 |
-
"detail": str(e),
|
| 117 |
-
},
|
| 118 |
-
status_code=500,
|
| 119 |
-
)
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
@app.post("/log/session/start")
|
| 123 |
-
def log_session_start(payload: SessionStartRequest) -> Dict[str, Any]:
|
| 124 |
-
return store.start_session(payload.session_id, payload.user_id, payload.condition, payload.metadata)
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
@app.post("/log/event")
|
| 128 |
-
def log_event(payload: EventLogRequest) -> Dict[str, Any]:
|
| 129 |
-
return store.log_event(payload.session_id, payload.event_type, payload.payload, payload.timestamp)
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
@app.post("/log/session/finalize")
|
| 133 |
-
def log_session_finalize(payload: SessionFinalizeRequest) -> Dict[str, Any]:
|
| 134 |
-
return store.finalize_session(payload.session_id, payload.summary)
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
@app.get("/research/sessions")
|
| 138 |
-
def research_sessions() -> Dict[str, Any]:
|
| 139 |
-
return {"sessions": store.list_sessions()}
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
@app.get("/research/session/{session_id}")
|
| 143 |
-
def research_session(session_id: str) -> Dict[str, Any]:
|
| 144 |
-
return store.get_session(session_id)
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
if __name__ == "__main__":
|
| 148 |
-
import uvicorn
|
| 149 |
-
|
| 150 |
-
port = int(os.getenv("PORT", "7860"))
|
| 151 |
-
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
+
from dataclasses import dataclass, field
|
| 4 |
+
from typing import Any, Dict, List, Optional
|
| 5 |
+
|
| 6 |
+
from pydantic import BaseModel
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class ChatRequest(BaseModel):
|
| 10 |
+
message: Optional[str] = None
|
| 11 |
+
prompt: Optional[str] = None
|
| 12 |
+
query: Optional[str] = None
|
| 13 |
+
text: Optional[str] = None
|
| 14 |
+
user_message: Optional[str] = None
|
| 15 |
+
input: Optional[str] = None
|
| 16 |
+
content: Optional[str] = None
|
| 17 |
+
|
| 18 |
+
tone: Optional[float] = 0.5
|
| 19 |
+
verbosity: Optional[float] = 0.5
|
| 20 |
+
transparency: Optional[float] = 0.5
|
| 21 |
+
|
| 22 |
+
help_mode: Optional[str] = None
|
| 23 |
+
intent: Optional[str] = None
|
| 24 |
+
category: Optional[str] = None
|
| 25 |
+
chat_history: Optional[List[Dict[str, Any]]] = None
|
| 26 |
+
history: Optional[List[Dict[str, Any]]] = None
|
| 27 |
+
|
| 28 |
+
question_text: Optional[str] = None
|
| 29 |
+
options_text: Optional[List[str]] = None
|
| 30 |
+
question_id: Optional[str] = None
|
| 31 |
+
session_id: Optional[str] = None
|
| 32 |
+
user_id: Optional[str] = None
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class SessionStartRequest(BaseModel):
|
| 36 |
+
session_id: str
|
| 37 |
+
user_id: Optional[str] = None
|
| 38 |
+
condition: Optional[str] = None
|
| 39 |
+
metadata: Optional[Dict[str, Any]] = None
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class EventLogRequest(BaseModel):
|
| 43 |
+
session_id: str
|
| 44 |
+
event_type: str
|
| 45 |
+
timestamp: Optional[str] = None
|
| 46 |
+
payload: Optional[Dict[str, Any]] = None
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
class SessionFinalizeRequest(BaseModel):
|
| 50 |
+
session_id: str
|
| 51 |
+
summary: Optional[Dict[str, Any]] = None
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
@dataclass
|
| 55 |
+
class RetrievedChunk:
|
| 56 |
+
text: str
|
| 57 |
+
topic: str = "general"
|
| 58 |
+
source: str = "local"
|
| 59 |
+
score: float = 0.0
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
@dataclass
|
| 63 |
+
class SolverResult:
|
| 64 |
+
reply: str = ""
|
| 65 |
+
domain: str = "fallback"
|
| 66 |
+
solved: bool = False
|
| 67 |
+
help_mode: str = "answer"
|
| 68 |
+
answer_letter: Optional[str] = None
|
| 69 |
+
answer_value: Optional[str] = None
|
| 70 |
+
topic: Optional[str] = None
|
| 71 |
+
used_retrieval: bool = False
|
| 72 |
+
used_generator: bool = False
|
| 73 |
+
internal_answer: Optional[str] = None
|
| 74 |
+
steps: List[str] = field(default_factory=list)
|
| 75 |
+
teaching_chunks: List[RetrievedChunk] = field(default_factory=list)
|
| 76 |
+
meta: Dict[str, Any] = field(default_factory=dict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|