Update main.py
Browse files
main.py
CHANGED
|
@@ -1,107 +1,106 @@
|
|
| 1 |
-
import uvicorn
|
| 2 |
-
from fastapi import FastAPI, HTTPException
|
| 3 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
-
from pydantic import BaseModel
|
| 5 |
-
from typing import List, Optional
|
| 6 |
-
from graph import app_graph
|
| 7 |
-
|
| 8 |
-
app = FastAPI(title="Clause.ai Backend")
|
| 9 |
-
|
| 10 |
-
# --- CORS SETUP (LOCKED DOWN) ---
|
| 11 |
-
origins = [
|
| 12 |
-
"https://clause-ai-nbu8.vercel.app"
|
| 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 |
-
if __name__ == "__main__":
|
| 107 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 1 |
+
import uvicorn
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from typing import List, Optional
|
| 6 |
+
from graph import app_graph
|
| 7 |
+
|
| 8 |
+
app = FastAPI(title="Clause.ai Backend")
|
| 9 |
+
|
| 10 |
+
# --- CORS SETUP (LOCKED DOWN) ---
|
| 11 |
+
origins = [
|
| 12 |
+
"https://clause-ai-nbu8.vercel.app"
|
| 13 |
+
]
|
| 14 |
+
app.add_middleware(
|
| 15 |
+
CORSMiddleware,
|
| 16 |
+
allow_origins=["*"], # Allow ANYONE to connect
|
| 17 |
+
allow_credentials=True,
|
| 18 |
+
allow_methods=["*"],
|
| 19 |
+
allow_headers=["*"],
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# --- API MODELS ---
|
| 23 |
+
class DraftRequest(BaseModel):
|
| 24 |
+
query: str
|
| 25 |
+
|
| 26 |
+
class AgentResponse(BaseModel):
|
| 27 |
+
status: str
|
| 28 |
+
phase: str
|
| 29 |
+
message: str
|
| 30 |
+
missing_info: List[str] = []
|
| 31 |
+
draft: Optional[str] = None
|
| 32 |
+
reference: Optional[str] = None
|
| 33 |
+
|
| 34 |
+
@app.get("/")
|
| 35 |
+
def home():
|
| 36 |
+
return {"status": "Clause.ai Brain is Online"}
|
| 37 |
+
|
| 38 |
+
@app.post("/draft", response_model=AgentResponse)
|
| 39 |
+
async def generate_clause(request: DraftRequest):
|
| 40 |
+
try:
|
| 41 |
+
# Initialize the state
|
| 42 |
+
initial_state = {
|
| 43 |
+
"query": request.query,
|
| 44 |
+
"messages": [],
|
| 45 |
+
"context": "",
|
| 46 |
+
"reference_clause": "",
|
| 47 |
+
"final_draft": "",
|
| 48 |
+
"phase": "",
|
| 49 |
+
"missing_info": [],
|
| 50 |
+
"clarification_question": ""
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# Run the LangGraph Agent
|
| 54 |
+
result = app_graph.invoke(initial_state)
|
| 55 |
+
|
| 56 |
+
phase = result.get("phase", "")
|
| 57 |
+
|
| 58 |
+
# --- SCENARIO 1: Guardrail stopped (general question/greeting/injection) ---
|
| 59 |
+
if phase == "stopped":
|
| 60 |
+
return {
|
| 61 |
+
"status": "general_response",
|
| 62 |
+
"phase": "stopped",
|
| 63 |
+
"message": result.get("final_draft", ""),
|
| 64 |
+
"missing_info": [],
|
| 65 |
+
"draft": None,
|
| 66 |
+
"reference": None
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
# --- SCENARIO 2: Triage needs clarification ---
|
| 70 |
+
if phase == "planning":
|
| 71 |
+
return {
|
| 72 |
+
"status": "needs_info",
|
| 73 |
+
"phase": "planning",
|
| 74 |
+
"message": result.get("clarification_question", "Please provide more details."),
|
| 75 |
+
"missing_info": result.get("missing_info", []),
|
| 76 |
+
"draft": None,
|
| 77 |
+
"reference": None
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
# --- SCENARIO 3: Draft completed successfully ---
|
| 81 |
+
if phase == "drafting" or result.get("final_draft"):
|
| 82 |
+
return {
|
| 83 |
+
"status": "success",
|
| 84 |
+
"phase": "drafting",
|
| 85 |
+
"message": "Draft generated successfully.",
|
| 86 |
+
"missing_info": [],
|
| 87 |
+
"draft": result.get("final_draft", ""),
|
| 88 |
+
"reference": result.get("reference_clause", "")
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
# --- FALLBACK: Unknown state ---
|
| 92 |
+
return {
|
| 93 |
+
"status": "error",
|
| 94 |
+
"phase": "unknown",
|
| 95 |
+
"message": "Unable to process your request. Please try again.",
|
| 96 |
+
"missing_info": [],
|
| 97 |
+
"draft": None,
|
| 98 |
+
"reference": None
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
except Exception as e:
|
| 102 |
+
print(f"❌ Error: {e}")
|
| 103 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
|
|
|
| 106 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|