-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
593 lines (535 loc) · 22.8 KB
/
api.py
File metadata and controls
593 lines (535 loc) · 22.8 KB
1
2
3
4
5
6
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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
from fastapi import FastAPI, HTTPException, Header
from fastapi.responses import StreamingResponse
import asyncio
import json
import time
from pydantic import BaseModel, Field
from typing import Any, Dict, List, Optional, Union
import logging
from pipeline import Pipeline
from config import Config
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Initialize FastAPI app
app = FastAPI()
# Add CORS middleware
try:
from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
except Exception:
pass
# Global pipeline registry
pipelines: Dict[str, Pipeline] = {}
# Startup initialization to register default pipelines
@app.on_event("startup")
async def _init_default_pipelines() -> None:
try:
from agent_pipeline_declarations import create_prediction_pipeline, create_research_pipeline
prediction = create_prediction_pipeline()
pipelines[prediction.name] = prediction
pipelines.setdefault("prediction_pipeline", prediction)
research = create_research_pipeline()
pipelines[research.name] = research
logger.info(f"Initialized default pipelines: {list(pipelines.keys())}")
except Exception as e:
logger.error(f"Failed to initialize default pipelines: {e}")
# Pydantic models for API
class ResponseCreateRequest(BaseModel):
model: Optional[str] = None
input: Any
pipeline_name: Optional[str] = None
class PipelineResponse(BaseModel):
success: bool
data: Any
error: Optional[str] = None
execution_time: Optional[float] = None
stage_results: Optional[List[Dict[str, Any]]] = None
class CreatePipelineRequest(BaseModel):
name: str = Field(..., description="Name of the pipeline")
description: Optional[str] = Field("", description="Description of the pipeline")
stages: List[Dict[str, Any]] = Field(..., description="List of stage configurations")
class ToolConfig(BaseModel):
name: str
description: str
parameters: Dict[str, Any]
class ReasoningConfigRequest(BaseModel):
reasoning_effort: Optional[str] = None # "low", "medium", "high", "maximum"
temperature: Optional[float] = None # None -> use server default
max_output_tokens: Optional[int] = None
top_p: Optional[float] = None
stop: Optional[List[str]] = None
tools: Optional[List[Union[str, ToolConfig]]] = None # built-in tool strings or custom function tools
tool_choice: Optional[Union[str, Dict[str, Any]]] = None # e.g., "auto"
verbosity: Optional[str] = None # optional application-level control
text_format_type: Optional[str] = None # "text" or "json_schema"
text_json_schema: Optional[Dict[str, Any]] = None
include: Optional[List[str]] = None
max_tool_calls: Optional[int] = None
parallel_tool_calls: Optional[bool] = None
extra_request_kwargs: Optional[Dict[str, Any]] = None
timeout_seconds: Optional[int] = None
class StageConfig(BaseModel):
name: str
type: str = "gpt" # Currently only supports GPT agents
system_prompt: str
model: str = "gpt-5"
reasoning_config: Optional[ReasoningConfigRequest] = None
# -------- Minimal OpenAI-compatible endpoint (/v1/chat/completions) --------
class ChatMessage(BaseModel):
role: str
content: str
class ChatCompletionsRequest(BaseModel):
model: Optional[str] = None
messages: List[ChatMessage]
temperature: Optional[float] = None
max_tokens: Optional[int] = None
pipeline_name: Optional[str] = Config.DEFAULT_PIPELINE_NAME
stream: Optional[bool] = False
diagnostics: Optional[bool] = False
# -------- Simple SSE test endpoint (no model calls) --------
class StreamTestRequest(BaseModel):
duration_sec: Optional[int] = 360 # default 6 minutes
@app.post("/v1/chat/completions")
async def chat_completions(req: ChatCompletionsRequest, authorization: Optional[str] = Header(default=None)):
# Require Bearer auth only when a server API key is configured
if Config.SERVER_AUTH_ENABLED:
if not authorization or not authorization.startswith("Bearer "):
raise HTTPException(status_code=401, detail="Missing Authorization header")
token = authorization.split(" ", 1)[1]
if token != Config.SERVER_API_KEY:
raise HTTPException(status_code=401, detail="Invalid API key")
# Join all user/assistant content as input; prefer latest user message
user_text = "\n".join(m.content for m in req.messages if m.role == "user") or (req.messages[-1].content if req.messages else "")
pipeline_name = req.pipeline_name or Config.DEFAULT_PIPELINE_NAME
pipeline = pipelines.get(pipeline_name)
if not pipeline:
raise HTTPException(status_code=503, detail="pipeline not ready")
if req.stream:
async def event_stream():
request_id = f"cmpl-local-{int(time.time()*1000)}"
model_id = req.model or "gpt-5"
# Initial role chunk
first = {
"id": request_id,
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": model_id,
"choices": [{"index": 0, "delta": {"role": "assistant"}, "finish_reason": None}],
}
yield f"data: {json.dumps(first)}\n\n"
# Comment keep-alive to force flush through intermediaries
yield ": start\n\n"
await asyncio.sleep(0)
task = asyncio.create_task(pipeline.execute(user_text, None))
# Heartbeat every ~30s to keep long streams alive when desired
heartbeat_interval_seconds = 30
diagnostics_enabled = bool(req.diagnostics)
hb_count = 0
start_ts = time.time()
while not task.done():
await asyncio.sleep(heartbeat_interval_seconds)
hb_count += 1
hb = {
"id": request_id,
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": model_id,
"choices": [{"index": 0, "delta": {}, "finish_reason": None}],
}
yield f"data: {json.dumps(hb)}\n\n"
# Extra comment heartbeat to defeat proxy buffering
yield ": heartbeat\n\n"
if diagnostics_enabled:
diag = {
"ts": int(time.time()),
"elapsed_sec": int(time.time() - start_ts),
"heartbeats": hb_count,
"status": "running"
}
yield f": diag {json.dumps(diag)}\n\n"
await asyncio.sleep(0)
result = await task
if not result.success:
# Emit final error info, then DONE
try:
stage_summaries = []
for i, sr in enumerate(result.stage_results or []):
meta = sr.metadata or {}
stage_summaries.append({
"index": i + 1,
"name": meta.get("agent_name"),
"success": bool(sr.success),
"error": sr.error,
})
debug_text = f"Pipeline failed: {result.error or 'unknown'}\n\nStages: {json.dumps(stage_summaries)}"
except Exception:
debug_text = f"Pipeline failed: {result.error or 'unknown'}"
chunk = {
"id": request_id,
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": model_id,
"choices": [{"index": 0, "delta": {"content": debug_text}, "finish_reason": "error"}],
}
yield f"data: {json.dumps(chunk)}\n\n"
yield "data: [DONE]\n\n"
return
text = str(result.data or "")
out = {
"id": request_id,
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": model_id,
"choices": [{"index": 0, "delta": {"content": text}, "finish_reason": "stop"}],
}
yield f"data: {json.dumps(out)}\n\n"
yield "data: [DONE]\n\n"
return StreamingResponse(event_stream(), media_type="text/event-stream", headers={
"Cache-Control": "no-cache, no-transform",
"Connection": "keep-alive",
"X-Accel-Buffering": "no",
})
# Non-stream path
result = await pipeline.execute(user_text, None)
if not result.success:
raise HTTPException(status_code=500, detail=result.error or "pipeline_failed")
text = str(result.data or "")
return {
"id": "cmpl-local-1",
"object": "chat.completion",
"created": 0,
"model": req.model or "gpt-5",
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": text},
"finish_reason": "stop"
}
]
}
@app.post("/v1/chat/completions_nohb")
async def chat_completions_no_heartbeat(req: ChatCompletionsRequest, authorization: Optional[str] = Header(default=None)):
# Require Bearer auth only when a server API key is configured
if Config.SERVER_AUTH_ENABLED:
if not authorization or not authorization.startswith("Bearer "):
raise HTTPException(status_code=401, detail="Missing Authorization header")
token = authorization.split(" ", 1)[1]
if token != Config.SERVER_API_KEY:
raise HTTPException(status_code=401, detail="Invalid API key")
# Join all user/assistant content as input; prefer latest user message
user_text = "\n".join(m.content for m in req.messages if m.role == "user") or (req.messages[-1].content if req.messages else "")
pipeline_name = req.pipeline_name or Config.DEFAULT_PIPELINE_NAME
pipeline = pipelines.get(pipeline_name)
if not pipeline:
raise HTTPException(status_code=503, detail="pipeline not ready")
if req.stream:
async def event_stream():
request_id = f"cmpl-local-{int(time.time()*1000)}"
model_id = req.model or "gpt-5"
# Initial role chunk
first = {
"id": request_id,
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": model_id,
"choices": [{"index": 0, "delta": {"role": "assistant"}, "finish_reason": None}],
}
yield f"data: {json.dumps(first)}\n\n"
# Optional single comment to help initial flush; no periodic heartbeats
yield ": start\n\n"
await asyncio.sleep(0)
result = await pipeline.execute(user_text, None)
if not result.success:
try:
stage_summaries = []
for i, sr in enumerate(result.stage_results or []):
meta = sr.metadata or {}
stage_summaries.append({
"index": i + 1,
"name": meta.get("agent_name"),
"success": bool(sr.success),
"error": sr.error,
})
debug_text = f"Pipeline failed: {result.error or 'unknown'}\n\nStages: {json.dumps(stage_summaries)}"
except Exception:
debug_text = f"Pipeline failed: {result.error or 'unknown'}"
chunk = {
"id": request_id,
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": model_id,
"choices": [{"index": 0, "delta": {"content": debug_text}, "finish_reason": "error"}],
}
yield f"data: {json.dumps(chunk)}\n\n"
yield "data: [DONE]\n\n"
return
text = str(result.data or "")
out = {
"id": request_id,
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": model_id,
"choices": [{"index": 0, "delta": {"content": text}, "finish_reason": "stop"}],
}
yield f"data: {json.dumps(out)}\n\n"
yield "data: [DONE]\n\n"
return StreamingResponse(event_stream(), media_type="text/event-stream", headers={
"Cache-Control": "no-cache, no-transform",
"Connection": "keep-alive",
"X-Accel-Buffering": "no",
})
# Non-stream path
result = await pipeline.execute(user_text, None)
if not result.success:
raise HTTPException(status_code=500, detail=result.error or "pipeline_failed")
text = str(result.data or "")
return {
"id": "cmpl-local-1",
"object": "chat.completion",
"created": 0,
"model": req.model or "gpt-5",
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": text},
"finish_reason": "stop"
}
]
}
@app.post("/v1/stream/test/asgi")
async def stream_test_asgi(req: StreamTestRequest, authorization: Optional[str] = Header(default=None)):
# Require Bearer auth to match other endpoints when enabled
if Config.SERVER_AUTH_ENABLED:
if not authorization or not authorization.startswith("Bearer "):
raise HTTPException(status_code=401, detail="Missing Authorization header")
token = authorization.split(" ", 1)[1]
if token != Config.SERVER_API_KEY:
raise HTTPException(status_code=401, detail="Invalid API key")
async def event_stream():
request_id = f"test-{int(time.time()*1000)}"
model_id = "stream-test"
# Initial chunk ASAP
first = {
"id": request_id,
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": model_id,
"choices": [{"index": 0, "delta": {"role": "assistant"}, "finish_reason": None}],
}
yield f"data: {json.dumps(first)}\n\n"
# Heartbeats for duration
remaining = max(1, int(req.duration_sec or 360))
start = time.time()
while time.time() - start < remaining:
await asyncio.sleep(5)
hb = {
"id": request_id,
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": model_id,
"choices": [{"index": 0, "delta": {}, "finish_reason": None}],
}
yield f"data: {json.dumps(hb)}\n\n"
# Final content
out = {
"id": request_id,
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": model_id,
"choices": [{"index": 0, "delta": {"content": "stream-test-complete"}, "finish_reason": "stop"}],
}
yield f"data: {json.dumps(out)}\n\n"
yield "data: [DONE]\n\n"
return StreamingResponse(event_stream(), media_type="text/event-stream", headers={
"Cache-Control": "no-cache, no-transform",
"Pragma": "no-cache",
"Connection": "keep-alive",
"Content-Encoding": "identity",
"X-Accel-Buffering": "no",
})
@app.get("/")
async def root():
"""Root endpoint with API information"""
return {
"message": "Agent Framework API",
"version": "1.0.0",
"available_pipelines": list(pipelines.keys())
}
@app.get("/pipelines")
async def list_pipelines():
"""List all available pipelines"""
payload = {
"pipelines": [
{
"name": name,
"description": pipeline.description,
"stages": pipeline.get_stage_names()
}
for name, pipeline in pipelines.items()
]
}
if Config.DEBUG:
payload["debug"] = {"count": len(pipelines)}
return payload
@app.post("/pipelines")
async def create_pipeline(request: CreatePipelineRequest):
"""Create a new pipeline"""
try:
from agent import GPTAgent, ReasoningConfig, ToolDefinition
# Create pipeline
pipeline = Pipeline(request.name, request.description or "")
# Add stages
for stage_config in request.stages:
if stage_config["type"] == "gpt":
# Create reasoning config if provided
reasoning_config = None
if "reasoning_config" in stage_config and stage_config["reasoning_config"]:
rc_data = stage_config["reasoning_config"]
# Convert tool configs: allow built-in tool strings or function tools
tools: List[Union[str, ToolDefinition]] = []
if rc_data.get("tools"):
for t in rc_data["tools"]:
if isinstance(t, str):
tools.append(t)
else:
tool = ToolDefinition(
name=t["name"],
description=t["description"],
parameters=t["parameters"]
)
tools.append(tool)
reasoning_config = ReasoningConfig(
reasoning_effort=rc_data.get("reasoning_effort"),
temperature=rc_data.get("temperature"),
max_output_tokens=rc_data.get("max_output_tokens"),
top_p=rc_data.get("top_p"),
stop=rc_data.get("stop"),
tools=tools,
tool_choice=rc_data.get("tool_choice"),
verbosity=rc_data.get("verbosity"),
text_format_type=rc_data.get("text_format_type"),
text_json_schema=rc_data.get("text_json_schema"),
include=rc_data.get("include"),
max_tool_calls=rc_data.get("max_tool_calls"),
parallel_tool_calls=rc_data.get("parallel_tool_calls"),
extra_request_kwargs=rc_data.get("extra_request_kwargs"),
timeout_seconds=rc_data.get("timeout_seconds")
)
agent = GPTAgent(
name=stage_config["name"],
system_prompt=stage_config["system_prompt"],
model=stage_config.get("model", "gpt-5"),
reasoning_config=reasoning_config
)
pipeline.add_stage(agent)
else:
raise HTTPException(
status_code=400,
detail=f"Unsupported stage type: {stage_config['type']}"
)
# Register pipeline
pipelines[request.name] = pipeline
return {
"message": f"Pipeline '{request.name}' created successfully",
"pipeline": {
"name": request.name,
"description": request.description,
"stages": pipeline.get_stage_names()
}
}
except Exception as e:
logger.error(f"Failed to create pipeline: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/responses")
async def responses_create(request: ResponseCreateRequest):
"""Create a response. If pipeline_name is supplied, execute that pipeline. Minimal surface."""
try:
if not request.pipeline_name:
raise HTTPException(status_code=400, detail="pipeline_name is required")
if request.pipeline_name not in pipelines:
raise HTTPException(status_code=404, detail=f"Pipeline '{request.pipeline_name}' not found")
pipeline = pipelines[request.pipeline_name]
result = await pipeline.execute(request.input, None)
response_body = {
"object": "response",
"status": "completed" if result.success else "failed",
"output": [{
"type": "message",
"role": "assistant",
"content": [{"type": "output_text", "text": str(result.data or "")}]
}]
}
try:
logger.info(f"[CLIENT] Response body: {response_body}")
except Exception:
pass
return response_body
# Convert stage results to dict format
stage_results = []
if result.stage_results:
for stage_result in result.stage_results:
stage_results.append({
"success": stage_result.success,
"data": stage_result.data,
"error": stage_result.error,
"metadata": stage_result.metadata
})
resp = PipelineResponse(
success=result.success,
data=result.data,
error=result.error,
execution_time=result.execution_time,
stage_results=stage_results
)
if Config.DEBUG:
logger.info(f"[DEBUG] Execute payload stages={len(stage_results)} success={result.success}")
return resp
except HTTPException:
raise
except Exception as e:
logger.error(f"Pipeline execution failed: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/pipelines/{pipeline_name}")
async def get_pipeline(pipeline_name: str):
"""Get pipeline details"""
if pipeline_name not in pipelines:
raise HTTPException(
status_code=404,
detail=f"Pipeline '{pipeline_name}' not found"
)
pipeline = pipelines[pipeline_name]
payload = {
"name": pipeline.name,
"description": pipeline.description,
"stages": pipeline.get_stage_names()
}
if Config.DEBUG:
payload["debug"] = True
return payload
@app.delete("/pipelines/{pipeline_name}")
async def delete_pipeline(pipeline_name: str):
"""Delete a pipeline"""
if pipeline_name not in pipelines:
raise HTTPException(
status_code=404,
detail=f"Pipeline '{pipeline_name}' not found"
)
del pipelines[pipeline_name]
return {"message": f"Pipeline '{pipeline_name}' deleted successfully"}
if __name__ == "__main__":
import uvicorn
from config import Config
# Validate configuration
try:
Config.validate()
except ValueError as e:
logger.error(f"Configuration error: {e}")
exit(1)
uvicorn.run(app, host=Config.HOST, port=Config.PORT)