Spaces:
Sleeping
Sleeping
feat: add custom Swagger docs endpoint and import get_swagger_ui_html
Browse files
api.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
|
|
|
|
|
|
| 4 |
from environment import CustomerSupportEnv
|
| 5 |
|
| 6 |
-
app = FastAPI(title="Customer Support AI
|
| 7 |
|
| 8 |
app.add_middleware(
|
| 9 |
CORSMiddleware,
|
|
@@ -23,59 +26,69 @@ class StepRequest(BaseModel):
|
|
| 23 |
action: str
|
| 24 |
|
| 25 |
|
| 26 |
-
def obs_to_dict(obs) -> dict:
|
| 27 |
-
step = obs.current_step
|
| 28 |
-
return {
|
| 29 |
-
"task_id": obs.task_id,
|
| 30 |
-
"task_description": obs.task_description,
|
| 31 |
-
"current_customer_message": obs.current_customer_message,
|
| 32 |
-
"current_step": {
|
| 33 |
-
"step_id": str(step.step_id),
|
| 34 |
-
"label": str(step.label),
|
| 35 |
-
"description": str(step.description),
|
| 36 |
-
},
|
| 37 |
-
"progress": obs.progress,
|
| 38 |
-
"step_number": int(obs.step_number),
|
| 39 |
-
"total_steps": int(obs.total_steps),
|
| 40 |
-
"max_steps": int(obs.max_steps),
|
| 41 |
-
"metadata": obs.metadata,
|
| 42 |
-
"conversation_history": obs.conversation_history,
|
| 43 |
-
"context_memory": obs.context_memory,
|
| 44 |
-
"steps_completed": list(obs.steps_completed),
|
| 45 |
-
"consecutive_failures": int(obs.consecutive_failures),
|
| 46 |
-
"episode_status": str(obs.episode_status),
|
| 47 |
-
"fail_conditions": list(obs.fail_conditions),
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
|
| 51 |
@app.get("/")
|
| 52 |
-
def
|
| 53 |
-
return {"status": "ok", "message": "Customer Support AI
|
| 54 |
|
| 55 |
|
| 56 |
@app.post("/reset")
|
| 57 |
def reset_env(body: ResetRequest = None):
|
| 58 |
global _env
|
| 59 |
-
task =
|
| 60 |
-
|
| 61 |
-
task = "easy"
|
| 62 |
-
_env = CustomerSupportEnv(task=task)
|
| 63 |
obs = _env.reset()
|
| 64 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
|
| 67 |
@app.post("/step")
|
| 68 |
def step_env(body: StepRequest):
|
| 69 |
global _env
|
| 70 |
if _env is None:
|
| 71 |
-
return {"error": "Call /reset first"}
|
| 72 |
result = _env.step(body.action)
|
| 73 |
-
|
| 74 |
return {
|
| 75 |
-
"observation":
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
}
|
| 80 |
|
| 81 |
|
|
@@ -83,7 +96,7 @@ def step_env(body: StepRequest):
|
|
| 83 |
def get_state():
|
| 84 |
global _env
|
| 85 |
if _env is None:
|
| 86 |
-
return {"error": "Call /reset first"}
|
| 87 |
return _env.state()
|
| 88 |
|
| 89 |
|
|
@@ -96,6 +109,7 @@ def observation_space():
|
|
| 96 |
"current_customer_message": "str",
|
| 97 |
"current_step": "StepInfo",
|
| 98 |
"progress": "str",
|
|
|
|
| 99 |
"episode_status": "str",
|
| 100 |
}
|
| 101 |
}
|
|
@@ -105,6 +119,9 @@ def observation_space():
|
|
| 105 |
def action_space():
|
| 106 |
return {
|
| 107 |
"type": "Text",
|
| 108 |
-
"description": "Agent free-text reply",
|
| 109 |
"min_words": 6,
|
| 110 |
}
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi.openapi.docs import get_swagger_ui_html
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from pydantic import BaseModel
|
| 5 |
+
from typing import Optional
|
| 6 |
+
from dataclasses import asdict
|
| 7 |
from environment import CustomerSupportEnv
|
| 8 |
|
| 9 |
+
app = FastAPI(title="Customer Support AI Environment")
|
| 10 |
|
| 11 |
app.add_middleware(
|
| 12 |
CORSMiddleware,
|
|
|
|
| 26 |
action: str
|
| 27 |
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
@app.get("/")
|
| 30 |
+
def health_check():
|
| 31 |
+
return {"status": "ok", "message": "Customer Support AI Environment"}
|
| 32 |
|
| 33 |
|
| 34 |
@app.post("/reset")
|
| 35 |
def reset_env(body: ResetRequest = None):
|
| 36 |
global _env
|
| 37 |
+
task = body.task if body else "easy"
|
| 38 |
+
_env = CustomerSupportEnv(task=task.lower())
|
|
|
|
|
|
|
| 39 |
obs = _env.reset()
|
| 40 |
+
return {
|
| 41 |
+
"observation": {
|
| 42 |
+
"task_id": obs.task_id,
|
| 43 |
+
"task_description": obs.task_description,
|
| 44 |
+
"current_customer_message": obs.current_customer_message,
|
| 45 |
+
"current_step": {
|
| 46 |
+
"step_id": obs.current_step.step_id,
|
| 47 |
+
"label": obs.current_step.label,
|
| 48 |
+
"description": obs.current_step.description,
|
| 49 |
+
},
|
| 50 |
+
"progress": obs.progress,
|
| 51 |
+
"step_number": obs.step_number,
|
| 52 |
+
"total_steps": obs.total_steps,
|
| 53 |
+
"max_steps": obs.max_steps,
|
| 54 |
+
"metadata": obs.metadata,
|
| 55 |
+
"conversation_history": obs.conversation_history,
|
| 56 |
+
"context_memory": obs.context_memory,
|
| 57 |
+
"episode_status": obs.episode_status,
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
|
| 61 |
|
| 62 |
@app.post("/step")
|
| 63 |
def step_env(body: StepRequest):
|
| 64 |
global _env
|
| 65 |
if _env is None:
|
| 66 |
+
return {"error": "Environment not initialized. Call /reset first."}
|
| 67 |
result = _env.step(body.action)
|
| 68 |
+
obs = result.observation
|
| 69 |
return {
|
| 70 |
+
"observation": {
|
| 71 |
+
"task_id": obs.task_id,
|
| 72 |
+
"task_description": obs.task_description,
|
| 73 |
+
"current_customer_message": obs.current_customer_message,
|
| 74 |
+
"current_step": {
|
| 75 |
+
"step_id": obs.current_step.step_id,
|
| 76 |
+
"label": obs.current_step.label,
|
| 77 |
+
"description": obs.current_step.description,
|
| 78 |
+
},
|
| 79 |
+
"progress": obs.progress,
|
| 80 |
+
"step_number": obs.step_number,
|
| 81 |
+
"total_steps": obs.total_steps,
|
| 82 |
+
"max_steps": obs.max_steps,
|
| 83 |
+
"metadata": obs.metadata,
|
| 84 |
+
"conversation_history": obs.conversation_history,
|
| 85 |
+
"context_memory": obs.context_memory,
|
| 86 |
+
"episode_status": obs.episode_status,
|
| 87 |
+
"steps_completed": obs.steps_completed,
|
| 88 |
+
},
|
| 89 |
+
"reward": result.reward,
|
| 90 |
+
"done": result.done,
|
| 91 |
+
"info": result.info,
|
| 92 |
}
|
| 93 |
|
| 94 |
|
|
|
|
| 96 |
def get_state():
|
| 97 |
global _env
|
| 98 |
if _env is None:
|
| 99 |
+
return {"error": "Environment not initialized. Call /reset first."}
|
| 100 |
return _env.state()
|
| 101 |
|
| 102 |
|
|
|
|
| 109 |
"current_customer_message": "str",
|
| 110 |
"current_step": "StepInfo",
|
| 111 |
"progress": "str",
|
| 112 |
+
"conversation_history": "List[Dict]",
|
| 113 |
"episode_status": "str",
|
| 114 |
}
|
| 115 |
}
|
|
|
|
| 119 |
def action_space():
|
| 120 |
return {
|
| 121 |
"type": "Text",
|
| 122 |
+
"description": "Agent free-text reply to customer",
|
| 123 |
"min_words": 6,
|
| 124 |
}
|
| 125 |
+
@app.get("/docs", include_in_schema=False)
|
| 126 |
+
def custom_docs():
|
| 127 |
+
return get_swagger_ui_html(openapi_url="/openapi.json", title="API Docs")
|