From 3d62660256f4257523eab0c75874a7eec32c0f6b Mon Sep 17 00:00:00 2001 From: Kye Date: Sun, 23 Jul 2023 16:14:34 -0400 Subject: [PATCH] clean up[ Former-commit-id: 4a5d3546e9c3a1664cf4a51e9427fb28fa8b819b --- api/app.py | 18 +++++++++++++++--- playground/DIY.py | 0 2 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 playground/DIY.py diff --git a/api/app.py b/api/app.py index cac23332..335d03a0 100644 --- a/api/app.py +++ b/api/app.py @@ -1,12 +1,20 @@ +import logging +import os from fastapi import FastAPI, HTTPException, Depends from fastapi_cache.decorator import cache from fastapi_cache.coder import JsonCoder + from fastapi_cache import FastAPICache +from fastapi_cache.backends.redis import RedisBackend from aioredis import Redis + from pydantic import BaseModel from swarms.swarms import swarm from fastapi_limiter import FastAPILimiter from fastapi_limiter.depends import RateLimiter +from dotenv import load_dotenv + +load_dotenv() class SwarmInput(BaseModel): api_key: str @@ -16,9 +24,11 @@ app = FastAPI() @app.on_event("startup") async def startup(): - redis = Redis(host="localhost", port=6379) + redis_host = os.getenv("REDIS_HOST", "localhost") + redis_port = int(os.getenv("REDIS_PORT", 6379)) + redis = await Redis.create(redis_host, redis_port) FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache", coder=JsonCoder()) - await FastAPILimiter.init("redis://localhost:6379") + await FastAPILimiter.init(f"redis://{redis_host}:{redis_port}") @app.post("/chat", dependencies=[Depends(RateLimiter(times=2, minutes=1))]) @cache(expire=60) # Cache results for 1 minute @@ -29,6 +39,8 @@ async def run(swarm_input: SwarmInput): raise HTTPException(status_code=500, detail="Failed to run swarms") return {"results": results} except ValueError as ve: + logging.error("A ValueError occurred", exc_info=True) raise HTTPException(status_code=400, detail=str(ve)) except Exception as e: - raise HTTPException(status_code=500, detail=str(e)) \ No newline at end of file + logging.error("An error occurred", exc_info=True) + raise HTTPException(status_code=500, detail="An unexpected error occurred") diff --git a/playground/DIY.py b/playground/DIY.py new file mode 100644 index 00000000..e69de29b