FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: avoid repeated numpy checks for embeddings by jbeckwith-oai · Pull Request #3757 · openai/openai-python · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 5 additions & 1 deletion src/openai/lib/_parsing/_embeddings.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ def parse_embedding_response(
if not obj.data:
raise ValueError("No embedding data received")

if not any(isinstance(embedding.embedding, str) for embedding in obj.data):
return obj

use_numpy = has_numpy()
for embedding in obj.data:
data = cast(object, embedding.embedding)
if not isinstance(data, str):
continue
if not has_numpy():
if not use_numpy:
# use array for base64 optimisation
embedding.embedding = array.array("f", base64.b64decode(data)).tolist()
else:
Expand Down
26 changes: 26 additions & 0 deletions tests/lib/test_embeddings.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,32 @@ def test_decode_preserves_response_and_non_string_vectors(encoding_format: Omit
assert parsed.model == "text-embedding-3-small"


def test_decode_checks_numpy_once_per_response(monkeypatch: pytest.MonkeyPatch) -> None:
checks = 0

def has_numpy() -> bool:
nonlocal checks
checks += 1
return False

monkeypatch.setattr(embeddings_parser, "has_numpy", has_numpy)
response = make_response(ENCODED, ENCODED, ENCODED)

embeddings_parser.parse_embedding_response(response, encoding_format=omit)

assert checks == 1


def test_decode_does_not_check_numpy_without_encoded_vectors(monkeypatch: pytest.MonkeyPatch) -> None:
def unexpected_decoder() -> bool:
raise AssertionError("a response without encoded vectors must not inspect the decoder")

monkeypatch.setattr(embeddings_parser, "has_numpy", unexpected_decoder)
response = make_response([1.0, 2.0], [3.0, 4.0])

assert embeddings_parser.parse_embedding_response(response, encoding_format=omit) is response


@pytest.mark.parametrize("encoding_format", ["float", "base64", None])
@pytest.mark.parametrize("vectors", [(ENCODED,), ("abc",), ()], ids=["encoded", "invalid", "empty"])
def test_explicit_format_is_untouched(
Expand Down
Loading

Back | FazBrowse Home | New Git URL