Skip to content

Commit

Permalink
Merge pull request #823 from SciPhi-AI/Nolan/FastAPI
Browse files Browse the repository at this point in the history
Introduce Server Stats
  • Loading branch information
NolanTrem authored Aug 1, 2024
2 parents a2ea853 + bf43688 commit e5c2b34
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
4 changes: 4 additions & 0 deletions r2r/main/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ def _ensure_authenticated(self):
def health(self) -> dict:
return self._make_request("GET", "health")

def server_stats(self) -> dict:
self._ensure_authenticated()
return self._make_request("GET", "server_stats")

def update_prompt(
self,
name: str = "default_system",
Expand Down
27 changes: 27 additions & 0 deletions r2r/main/api/routes/management/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# TODO - Cleanup the handling for non-auth configurations
from datetime import datetime, timezone

import psutil
from fastapi import Depends
from pydantic import BaseModel

Expand All @@ -21,13 +24,37 @@
class ManagementRouter(BaseRouter):
def __init__(self, engine: R2REngine):
super().__init__(engine)
self.start_time = datetime.now(timezone.utc)
self.setup_routes()

def setup_routes(self):
@self.router.get("/health")
async def health_check():
return {"response": "ok"}

@self.router.get("/server_stats")
@self.base_endpoint
async def server_stats(
auth_user=(
Depends(self.engine.providers.auth.auth_wrapper)
if self.engine.providers.auth
else None
),
):
if not auth_user.is_superuser:
raise R2RException(
"Only an authorized user can call the `server_stats` endpoint.",
403,
)
return {
"start_time": self.start_time.isoformat(),
"uptime_seconds": (
datetime.now(timezone.utc) - self.start_time
).total_seconds(),
"cpu_usage": psutil.cpu_percent(),
"memory_usage": psutil.virtual_memory().percent,
}

@self.router.post("/update_prompt")
@self.base_endpoint
async def update_prompt_app(
Expand Down
1 change: 0 additions & 1 deletion r2r/pipes/ingestion/chunking_pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ async def _run_logic(

try:
iteration = 0
print("item.data = ", item.data)
async for chunk in self.chunking_provider.chunk(item.data):
yield Fragment(
id=generate_id_from_label(f"{item.id}-{iteration}"),
Expand Down

0 comments on commit e5c2b34

Please sign in to comment.