-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
69 lines (57 loc) · 2.25 KB
/
main.py
File metadata and controls
69 lines (57 loc) · 2.25 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
import logging
from dotenv import load_dotenv
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
from typing import List
# Import core logic
from reflection_pattern_agent.reflection_agent import ReflectionAgent
# Load environment variables from a .env file
load_dotenv()
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# --- The API Contract (Pydantic Models) ---
class RunRequest(BaseModel):
prompt: str = Field(
...,
description="The initial prompt for the agent to process.",
example="Write a tweet about the future of AI agents."
)
class RunResponse(BaseModel):
initial_draft: str
reflections: List[str]
final_output: str
# --- FastAPI Application Setup ---
app = FastAPI(
title="Reflection Agent Service",
description="An API to access an AI agent that uses the reflection pattern to improve its output.",
version="1.0.0"
)
# --- API Endpoints ---
# Basic health check endpoint
@app.get("/health", tags=["Monitoring"])
def health_check():
"""Checks if the service is running."""
return {"status": "ok"}
@app.post("/run", response_model=RunResponse, tags=["Agent Logic"])
def execute_agent_run(request: RunRequest):
"""
Executes the full generate-reflect-generate cycle of the agent.
"""
try:
logger.info(f"Received request for prompt: '{request.prompt[:50]}...'") # Log de início
agent = ReflectionAgent()
initial_draft, reflections, final_output = agent.run(request.prompt)
if initial_draft is None:
logger.error("Agent failed to generate an initial draft.")
raise HTTPException(status_code=500, detail="Agent failed to generate an initial draft.")
logger.info("Successfully completed agent run.") # Log de sucesso
return RunResponse(
initial_draft=initial_draft,
reflections=reflections,
final_output=final_output
)
except Exception as e:
# ESTA É A MUDANÇA MAIS IMPORTANTE
logger.exception("An unhandled exception occurred during agent run.")
raise HTTPException(status_code=500, detail=f"An internal error occurred: {str(e)}")