Spaces:
Running
Running
Update blaxel_main.py
Browse files- blaxel_main.py +20 -14
blaxel_main.py
CHANGED
|
@@ -1,39 +1,45 @@
|
|
| 1 |
-
# blaxel_main.py (
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from sse_starlette.sse import EventSourceResponse
|
| 5 |
import os
|
| 6 |
import uvicorn
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
from agent_logic import StrategicSelectorAgent
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
class ProblemRequest(BaseModel):
|
| 14 |
problem: str
|
| 15 |
-
|
| 16 |
-
# --- 2. Initialize MudabbirAI (The Brain) ---
|
| 17 |
-
# We create one instance when the server starts.
|
| 18 |
-
mudabbir_ai = StrategicSelectorAgent()
|
| 19 |
-
|
| 20 |
|
| 21 |
@app.post("/solve_problem")
|
| 22 |
async def solve_problem(request: ProblemRequest):
|
| 23 |
"""
|
| 24 |
-
This endpoint now
|
| 25 |
-
and
|
|
|
|
| 26 |
"""
|
| 27 |
-
|
| 28 |
-
# --- 3. Call the agent's .solve() generator ---
|
| 29 |
-
# We are returning the agent's generator directly.
|
| 30 |
-
# FastAPI and EventSourceResponse will handle streaming it.
|
| 31 |
async def stream_solution():
|
| 32 |
try:
|
|
|
|
|
|
|
|
|
|
| 33 |
async for status_update in mudabbir_ai.solve(request.problem):
|
| 34 |
yield status_update
|
| 35 |
except Exception as e:
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
return EventSourceResponse(stream_solution())
|
| 39 |
|
|
|
|
| 1 |
+
# blaxel_main.py (New Secure Version)
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from sse_starlette.sse import EventSourceResponse
|
| 5 |
import os
|
| 6 |
import uvicorn
|
| 7 |
+
from typing import Dict, Optional
|
| 8 |
|
| 9 |
+
# 1. Import our "Brain"
|
| 10 |
from agent_logic import StrategicSelectorAgent
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
+
# 2. Update Pydantic model to accept keys (some are optional)
|
| 15 |
+
class ApiKeys(BaseModel):
|
| 16 |
+
google: str
|
| 17 |
+
anthropic: Optional[str] = None
|
| 18 |
+
sambanova: Optional[str] = None
|
| 19 |
+
|
| 20 |
class ProblemRequest(BaseModel):
|
| 21 |
problem: str
|
| 22 |
+
keys: ApiKeys
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
@app.post("/solve_problem")
|
| 25 |
async def solve_problem(request: ProblemRequest):
|
| 26 |
"""
|
| 27 |
+
This endpoint now creates a *new* agent for every request,
|
| 28 |
+
and passes the user's API keys to it.
|
| 29 |
+
This is stateless and secure.
|
| 30 |
"""
|
| 31 |
+
|
|
|
|
|
|
|
|
|
|
| 32 |
async def stream_solution():
|
| 33 |
try:
|
| 34 |
+
# 3. Pass keys to the agent's constructor
|
| 35 |
+
mudabbir_ai = StrategicSelectorAgent(api_keys=request.keys.dict())
|
| 36 |
+
|
| 37 |
async for status_update in mudabbir_ai.solve(request.problem):
|
| 38 |
yield status_update
|
| 39 |
except Exception as e:
|
| 40 |
+
# This will catch errors (like invalid keys)
|
| 41 |
+
yield f"AGENT ERROR: {e}"
|
| 42 |
+
print(f"AGENT ERROR: {e}") # Also log to backend
|
| 43 |
|
| 44 |
return EventSourceResponse(stream_solution())
|
| 45 |
|