Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/filters/rate_limit_filter_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from typing import List, Optional
from fastapi import HTTPException
from pydantic import BaseModel
from schemas import OpenAIChatMessage
import time
Expand Down Expand Up @@ -121,7 +122,10 @@ async def inlet(self, body: dict, user: Optional[dict] = None) -> dict:
if user.get("role", "admin") == "user":
user_id = user["id"] if user and "id" in user else "default_user"
if self.rate_limited(user_id):
raise Exception("Rate limit exceeded. Please try again later.")
raise HTTPException(
status_code=429,
detail="Rate limit exceeded. Please try again later.",
)

self.log_request(user_id)
return body
4 changes: 4 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,8 @@ async def filter_inlet(pipeline_id: str, form_data: FilterForm):
return body
else:
return form_data.body
except HTTPException:
raise
except Exception as e:
print(e)
raise HTTPException(
Expand Down Expand Up @@ -648,6 +650,8 @@ async def filter_outlet(pipeline_id: str, form_data: FilterForm):
return body
else:
return form_data.body
except HTTPException:
raise
except Exception as e:
print(e)
raise HTTPException(
Expand Down