-
Notifications
You must be signed in to change notification settings - Fork 10
feat: implement web search rate limiting functionality #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Added a new rate limiting feature for web searches, allowing users to be restricted to a specified number of searches per hour. - Introduced `check_web_search_rate_limit` and `web_search_rate_limit` functions to handle the rate limiting logic. - Updated configuration files to include `WEB_SEARCH_RATE_LIMIT_HOUR` setting. - Integrated the web search rate limit check into the chat completion endpoint.
16eb4d4 to
4a74ebf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements web search rate limiting functionality to restrict users to a specified number of web searches per hour. The implementation adds rate limiting checks specifically for web search requests before they are processed.
- Introduces
check_web_search_rate_limitandweb_search_rate_limitfunctions for rate limiting logic - Adds
WEB_SEARCH_RATE_LIMIT_HOURconfiguration setting across different environments - Integrates web search rate limiting into the chat completion endpoint as a dependency
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
tests/unit/nilai_api/test_rate_limiting.py |
Adds unit test to verify 3 web searches per hour limit enforcement |
nilai-api/src/nilai_api/routers/private.py |
Integrates web search rate limiting into chat completion endpoint |
nilai-api/src/nilai_api/rate_limiting.py |
Implements core web search rate limiting functions |
nilai-api/src/nilai_api/config/testnet.py |
Sets web search rate limit to 3 per hour for testnet |
nilai-api/src/nilai_api/config/mainnet.py |
Disables web search rate limiting for mainnet |
nilai-api/src/nilai_api/config/__init__.py |
Adds default web search rate limit configuration |
| except Exception: | ||
| return | ||
|
|
||
| try: | ||
| from nilai_common import ChatRequest | ||
|
|
||
| chat_request = ChatRequest(**body) | ||
| except Exception: |
Copilot
AI
Jul 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a bare except Exception: is too broad and can mask unexpected errors. Consider catching specific exceptions like JSONDecodeError from the json parsing operation.
| except Exception: | |
| return | |
| try: | |
| from nilai_common import ChatRequest | |
| chat_request = ChatRequest(**body) | |
| except Exception: | |
| except JSONDecodeError: | |
| return | |
| try: | |
| from nilai_common import ChatRequest | |
| from pydantic import ValidationError | |
| chat_request = ChatRequest(**body) | |
| except ValidationError: |
| from nilai_common import ChatRequest | ||
|
|
||
| chat_request = ChatRequest(**body) | ||
| except Exception: |
Copilot
AI
Jul 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a bare except Exception: is too broad and can mask unexpected errors. Consider catching specific exceptions like ValidationError or TypeError from the ChatRequest instantiation.
| except Exception: | |
| except (ValidationError, TypeError): |
|
|
||
| @pytest.mark.asyncio | ||
| async def test_web_search_rate_limit_hour(redis_client): | ||
| """Verify that a user can only perform three web-search requests per hour.""" |
Copilot
AI
Jul 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test docstring hardcodes 'three' but the actual limit is configurable. Consider making the docstring more generic or parameterized to avoid inconsistency if the limit changes.
| """Verify that a user can only perform three web-search requests per hour.""" | |
| """Verify that a user can only perform a configurable number of web-search requests per hour.""" |
71735e0 to
03722a2
Compare
jcabrero
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
check_web_search_rate_limitandweb_search_rate_limitfunctions to handle the rate limiting logic.WEB_SEARCH_RATE_LIMIT_HOURsetting.