67 lines
1.8 KiB
Python
67 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
import httpx
|
|
|
|
from graphrag_platform.config import Settings
|
|
from graphrag_platform.llm import OpenAICompatibleClient
|
|
|
|
|
|
def test_llm_retries_rate_limit_and_honors_retry_after() -> None:
|
|
requests = 0
|
|
delays: list[float] = []
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
nonlocal requests
|
|
requests += 1
|
|
if requests == 1:
|
|
return httpx.Response(
|
|
429,
|
|
headers={"Retry-After": "0.25"},
|
|
json={"error": "rate limited"},
|
|
request=request,
|
|
)
|
|
return httpx.Response(
|
|
200,
|
|
json={"choices": [{"message": {"content": "answer"}}]},
|
|
request=request,
|
|
)
|
|
|
|
settings = Settings(
|
|
_env_file=None,
|
|
llm_api_key="test-key",
|
|
llm_max_retries=2,
|
|
)
|
|
client = OpenAICompatibleClient(
|
|
settings,
|
|
transport=httpx.MockTransport(handler),
|
|
sleeper=delays.append,
|
|
jitter=lambda: 0,
|
|
)
|
|
|
|
assert client.complete_text("system", "user") == "answer"
|
|
assert requests == 2
|
|
assert delays == [0.25]
|
|
|
|
|
|
def test_llm_does_not_retry_authentication_failure() -> None:
|
|
requests = 0
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
nonlocal requests
|
|
requests += 1
|
|
return httpx.Response(401, json={"error": "unauthorized"}, request=request)
|
|
|
|
client = OpenAICompatibleClient(
|
|
Settings(_env_file=None, llm_api_key="bad-key", llm_max_retries=3),
|
|
transport=httpx.MockTransport(handler),
|
|
sleeper=lambda _: None,
|
|
)
|
|
|
|
try:
|
|
client.complete_text("system", "user")
|
|
except httpx.HTTPStatusError:
|
|
pass
|
|
else:
|
|
raise AssertionError("Expected an HTTPStatusError")
|
|
assert requests == 1
|