Skip to content

Commit

Permalink
Create agent_api_app.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill670 authored Oct 16, 2024
1 parent fbf6509 commit b5205bf
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions agent_api_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Optional
from reflection_tuner import ReflectionTuner
from token_cache_and_adaptive_factory import TokenCache, AdaptiveAgentFactory
from swarms import Agent
from swarm_models import OpenAIChat
from swarms_memory import ChromaDB
import os

# Initialize FastAPI application
app = FastAPI()

# Define the token cache and model
token_cache = TokenCache(cache_duration_minutes=30)
model = OpenAIChat(api_key=os.getenv("OPENAI_API_KEY"), model_name="gpt-4o-mini", temperature=0.1)
memory = ChromaDB(metric="cosine", output_dir="api_memory")

# Agent creation factory
adaptive_factory = AdaptiveAgentFactory(model, token_cache)

# Input model for API request
class AgentRequest(BaseModel):
agent_name: str
system_prompt: str
task: str
reflection_steps: Optional[int] = 2

# Endpoint for creating and running an agent with Reflection-Tuning
@app.post("/run_agent")
async def run_agent(request: AgentRequest):
# Create or retrieve the agent from cache
agent = adaptive_factory.create_agent(
agent_name=request.agent_name,
system_prompt=request.system_prompt,
task=request.task,
memory=memory
)

# Initialize Reflection-Tuning
reflection_tuner = ReflectionTuner(agent, reflection_steps=request.reflection_steps)
response = reflection_tuner.reflect_and_tune(request.task)
return {"response": response}

# Endpoint for running an existing agent without creating a new one
@app.post("/run_existing_agent")
async def run_existing_agent(agent_name: str, task: str):
# Retrieve agent from cache
agent_token = token_cache.get_token(agent_name)
if not agent_token:
raise HTTPException(status_code=404, detail="Agent not found in cache. Create a new agent instead.")

# Run the agent
response = agent_token.run(task)
return {"response": response}

# Endpoint to clear cache for a specific agent
@app.delete("/clear_cache/{agent_name}")
async def clear_cache(agent_name: str):
token_cache.token_cache.pop(agent_name, None)
return {"detail": f"Cache for agent {agent_name} cleared."}

0 comments on commit b5205bf

Please sign in to comment.