-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_openai_embeddings.py
More file actions
98 lines (71 loc) · 2.8 KB
/
Copy pathtest_openai_embeddings.py
File metadata and controls
98 lines (71 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from types import SimpleNamespace
import pytest
from src.core.embeddings import openai_embeddings as openai_module
class FakeRateLimitError(Exception):
def __init__(self, headers=None):
super().__init__("rate limited")
self.response = SimpleNamespace(headers=headers or {})
class FakeEmbeddingsEndpoint:
def __init__(self, responses):
self._responses = list(responses)
self.calls = 0
async def create(self, model: str, input):
del model # Unused in test doubles.
del input
self.calls += 1
result = self._responses.pop(0)
if isinstance(result, Exception):
raise result
return result
class FakeClient:
def __init__(self, responses):
self.embeddings = FakeEmbeddingsEndpoint(responses)
def embedding_response(count: int) -> SimpleNamespace:
return SimpleNamespace(
data=[SimpleNamespace(embedding=[float(i), float(i + 1)]) for i in range(count)]
)
@pytest.mark.asyncio
async def test_embed_texts_retries_on_rate_limit(monkeypatch):
sleep_calls = []
async def fake_sleep(seconds):
sleep_calls.append(seconds)
fake_client = FakeClient(
[
FakeRateLimitError(headers={"retry-after": "0.25"}),
embedding_response(1),
]
)
monkeypatch.setattr(openai_module.asyncio, "sleep", fake_sleep)
monkeypatch.setattr(openai_module, "RateLimitError", FakeRateLimitError)
monkeypatch.setattr(openai_module, "AsyncOpenAI", lambda **kwargs: fake_client)
service = openai_module.OpenAIEmbeddings(
api_key="test",
max_texts_per_request=1,
request_concurrency=1,
rate_limit_max_retries=2,
rate_limit_base_backoff_seconds=0.01,
rate_limit_max_backoff_seconds=1.0,
)
embeddings = await service.embed_texts(["hello"])
assert len(embeddings) == 1
assert fake_client.embeddings.calls == 2
assert sleep_calls == [pytest.approx(0.25)]
@pytest.mark.asyncio
async def test_embed_texts_respects_min_request_spacing(monkeypatch):
sleep_calls = []
async def fake_sleep(seconds):
sleep_calls.append(seconds)
fake_client = FakeClient([embedding_response(1), embedding_response(1)])
monkeypatch.setattr(openai_module.asyncio, "sleep", fake_sleep)
monkeypatch.setattr(openai_module, "AsyncOpenAI", lambda **kwargs: fake_client)
monkeypatch.setattr(openai_module.time, "monotonic", lambda: 100.0)
service = openai_module.OpenAIEmbeddings(
api_key="test",
max_texts_per_request=1,
request_concurrency=1,
min_seconds_between_requests=0.2,
)
embeddings = await service.embed_texts(["first", "second"])
assert len(embeddings) == 2
assert fake_client.embeddings.calls == 2
assert sleep_calls == [pytest.approx(0.2)]