Skip to content

Conversation

@blefo
Copy link
Member

@blefo blefo commented Jul 25, 2025

  • 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.

- 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.
@blefo blefo force-pushed the feat-rate-limite-web-search branch from 16eb4d4 to 4a74ebf Compare July 25, 2025 09:34
@blefo blefo requested a review from Copilot July 25, 2025 09:54
Copy link
Contributor

Copilot AI left a 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_limit and web_search_rate_limit functions for rate limiting logic
  • Adds WEB_SEARCH_RATE_LIMIT_HOUR configuration 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

Comment on lines 220 to 227
except Exception:
return

try:
from nilai_common import ChatRequest

chat_request = ChatRequest(**body)
except Exception:
Copy link

Copilot AI Jul 25, 2025

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.

Suggested change
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:

Copilot uses AI. Check for mistakes.
from nilai_common import ChatRequest

chat_request = ChatRequest(**body)
except Exception:
Copy link

Copilot AI Jul 25, 2025

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.

Suggested change
except Exception:
except (ValidationError, TypeError):

Copilot uses AI. Check for mistakes.

@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."""
Copy link

Copilot AI Jul 25, 2025

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.

Suggested change
"""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."""

Copilot uses AI. Check for mistakes.
@blefo blefo requested a review from jcabrero July 25, 2025 09:59
@blefo blefo requested review from jcabrero and removed request for jcabrero July 28, 2025 10:56
@blefo blefo force-pushed the feat-rate-limite-web-search branch from 71735e0 to 03722a2 Compare July 28, 2025 10:58
@blefo blefo requested a review from jcabrero July 28, 2025 11:25
Copy link
Member

@jcabrero jcabrero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@blefo blefo merged commit 2fb700f into main Jul 29, 2025
5 checks passed
@jcabrero jcabrero deleted the feat-rate-limite-web-search branch July 31, 2025 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants