Home / File/ test_rate_limiting.py — langchain Source File

test_rate_limiting.py — langchain Source File

Architecture documentation for test_rate_limiting.py, a python file in the langchain codebase. 7 imports, 0 dependents.

File python CoreAbstractions MessageSchema 7 imports 10 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  df01695c_1dbe_973d_756a_a5cd86e49330["test_rate_limiting.py"]
  0c1d9a1b_c553_0388_dbc1_58af49567aa2["time"]
  df01695c_1dbe_973d_756a_a5cd86e49330 --> 0c1d9a1b_c553_0388_dbc1_58af49567aa2
  120e2591_3e15_b895_72b6_cb26195e40a6["pytest"]
  df01695c_1dbe_973d_756a_a5cd86e49330 --> 120e2591_3e15_b895_72b6_cb26195e40a6
  9eb3be64_a334_606b_b4bd_24e50a8c430d["blockbuster"]
  df01695c_1dbe_973d_756a_a5cd86e49330 --> 9eb3be64_a334_606b_b4bd_24e50a8c430d
  e51e78c8_f355_3edd_309e_1aec4323616a["langchain_core.caches"]
  df01695c_1dbe_973d_756a_a5cd86e49330 --> e51e78c8_f355_3edd_309e_1aec4323616a
  ba43b74d_3099_7e1c_aac3_cf594720469e["langchain_core.language_models"]
  df01695c_1dbe_973d_756a_a5cd86e49330 --> ba43b74d_3099_7e1c_aac3_cf594720469e
  36cce5da_d805_04c3_7e86_e1b4dd49b497["langchain_core.load"]
  df01695c_1dbe_973d_756a_a5cd86e49330 --> 36cce5da_d805_04c3_7e86_e1b4dd49b497
  cf3a1085_fa3f_4d54_9103_3f99d5f9b897["langchain_core.rate_limiters"]
  df01695c_1dbe_973d_756a_a5cd86e49330 --> cf3a1085_fa3f_4d54_9103_3f99d5f9b897
  style df01695c_1dbe_973d_756a_a5cd86e49330 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import time

import pytest
from blockbuster import BlockBuster

from langchain_core.caches import InMemoryCache
from langchain_core.language_models import GenericFakeChatModel
from langchain_core.load import dumps
from langchain_core.rate_limiters import InMemoryRateLimiter


@pytest.fixture(autouse=True)
def deactivate_blockbuster(blockbuster: BlockBuster) -> None:
    # Deactivate BlockBuster to not disturb the rate limiter timings
    blockbuster.deactivate()


def test_rate_limit_invoke() -> None:
    """Add rate limiter."""
    model = GenericFakeChatModel(
        messages=iter(["hello", "world"]),
        rate_limiter=InMemoryRateLimiter(
            requests_per_second=20,
            check_every_n_seconds=0.1,
            max_bucket_size=10,
            # At 20 requests per second we see a refresh every 0.05 seconds
        ),
    )
    tic = time.time()
    model.invoke("foo")
    toc = time.time()
    # Should be larger than check every n seconds since the token bucket starts
    # with 0 tokens.
    assert 0.10 < toc - tic < 0.15

    tic = time.time()
    model.invoke("foo")
    toc = time.time()
    # Second time we check the model, we should have 1 extra token
    # since the sleep time is 0.1 seconds
    assert 0.00 < toc - tic < 0.10


async def test_rate_limit_ainvoke() -> None:
    """Add rate limiter."""
    model = GenericFakeChatModel(
        messages=iter(["hello", "world", "!"]),
        rate_limiter=InMemoryRateLimiter(
            requests_per_second=20,
            check_every_n_seconds=0.1,
            max_bucket_size=10,
            # At 20 requests per second we see a refresh every 0.05 seconds
        ),
    )
    tic = time.time()
    await model.ainvoke("foo")
    toc = time.time()
    # Should be larger than check every n seconds since the token bucket starts
    # with 0 tokens.
    assert 0.1 < toc - tic < 0.2
// ... (211 more lines)

Subdomains

Dependencies

  • blockbuster
  • langchain_core.caches
  • langchain_core.language_models
  • langchain_core.load
  • langchain_core.rate_limiters
  • pytest
  • time

Frequently Asked Questions

What does test_rate_limiting.py do?
test_rate_limiting.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, MessageSchema subdomain.
What functions are defined in test_rate_limiting.py?
test_rate_limiting.py defines 10 function(s): deactivate_blockbuster, test_rate_limit_abatch, test_rate_limit_ainvoke, test_rate_limit_astream, test_rate_limit_batch, test_rate_limit_invoke, test_rate_limit_skips_cache, test_rate_limit_skips_cache_async, test_rate_limit_stream, test_serialization_with_rate_limiter.
What does test_rate_limiting.py depend on?
test_rate_limiting.py imports 7 module(s): blockbuster, langchain_core.caches, langchain_core.language_models, langchain_core.load, langchain_core.rate_limiters, pytest, time.
Where is test_rate_limiting.py in the architecture?
test_rate_limiting.py is located at libs/core/tests/unit_tests/language_models/chat_models/test_rate_limiting.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/core/tests/unit_tests/language_models/chat_models).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free