Spaces:
Running
Running
File size: 3,453 Bytes
5410b93 32a7233 38d1ff9 1fb304c b3b25af 32a7233 43544d8 32a7233 df2311d 03566bf 5410b93 94b75a9 3ce4cf9 acee99e cba20b5 3ce4cf9 5410b93 9283abe 03566bf 32a7233 5410b93 3ce4cf9 5410b93 94b75a9 3ce4cf9 32a7233 1fb304c fa15b54 32a7233 5410b93 32a7233 66e8ac0 cba20b5 38d1ff9 66e8ac0 38d1ff9 94b75a9 5a8e898 4347b3c b3b25af 94b75a9 6cf3d62 94b75a9 ced802e ed1c080 acee99e 5a8e898 14faaa0 ed1c080 5a8e898 ceaf913 ed1c080 94b75a9 b3b25af | 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 | from urllib import request
from fastapi import FastAPI
from fastapi.responses import JSONResponse
import sentry_sdk
import uvicorn
import sys
import os
import json
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
from agent.agent_graph.StateTasks import ProblemState
import subprocess
from Queue_Producer import send_message
import redis
from utils import RequestModel, RequestAnswer
from Consumer import redis_send
import psutil
##################################################
# VARIABLES
##################################################
redis_host = os.environ["REDIS_HOST"]
redis_port = os.environ["REDIS_PORT"]
redis_password = os.environ["REDIS_PASSWORD"]
##################################################
# START CONSUMERS in a separate process
##################################################
for i in range(1,4): # Start 3 consumers
subprocess.Popen(['python','-u','Consumer.py', '--id', str(i)])
##################################################
# START API and METHODS
##################################################
# Create Redis connection (global to make the get very light)
redis_conn = redis.Redis(
host=redis_host,
port=redis_port,
decode_responses=True,
username="default",
password=redis_password,
)
# model and rag are not global for better security ,at least for this version
# Create app instance
app = FastAPI()
# Create Sentry Monitoring for better error tracking and performance monitoring
sentry_sdk.init(
dsn=os.environ["SENTRY_DSN"],
# see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
send_default_pii=True,
)
app = FastAPI()
#redis_send(request.user_id,request.msg_id,{"status": "pending"}) # for performance test
print("Starting API Server...")
##################################################
# ROUTES
##################################################
@app.get("/")
def read_root():
return {"message": "Hello From CodeBuddyAI!"}
@app.get("/metrics")
def metrics():
return {
"cpu": psutil.cpu_percent(),
"ram": psutil.virtual_memory().percent
}
@app.api_route("/health", methods=["GET", "HEAD", "POST", "OPTIONS"])
def get_health():
return JSONResponse({"status": "ok"})
@app.post("/Message/Send/")
def call(request: RequestModel):
redis_send(request.user_id,request.msg_id,{"status": "pending"})
return send_message(json.dumps(request.model_dump()))
@app.post("/Message/Answer/")
def call(request: RequestAnswer):
## MUST BE LIGHTWEIGHT, JUST CHECK IF ANSWER IS READY IN REDIS, IF YES RETURN IT, ELSE RETURN PENDING
try:
answer = redis_conn.get(f'ANSWER_FOR_USER_ID{request.user_id}_OF_{request.msg_id}')
if answer is None:
return {"status": "error"}
elif "status" in answer and json.loads(answer)["status"] == "pending":
return {"status": "pending"}
else:
redis_conn.delete(f'ANSWER_FOR_USER_ID{request.user_id}_OF_{request.msg_id}') # Clean up after fetching for memory and better secure as double call is wrong
return {"status": "ready", "data": json.loads(answer)}
except Exception as e:
print(f"Error fetching answer from Redis: {e}")
return {"status": "error", "message": str(e)}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860) |