Skip to content

Commit

Permalink
Use mock OpenAI embedding method in test cases instead of calling API
Browse files Browse the repository at this point in the history
  • Loading branch information
lsz05 committed Apr 9, 2024
1 parent cc493e3 commit b71bd36
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
19 changes: 18 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pydantic = "^2.6.3"
eval-type-backport = "^0.1.3"
smart-open = "^7.0.1"
openai = "^1.16.2"
pytest-mock = "^3.14.0"

[tool.poetry.group.dev.dependencies]
black = "^23.11.0"
Expand Down
40 changes: 36 additions & 4 deletions tests/embedders/test_openai.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
from __future__ import annotations

from dataclasses import dataclass

import numpy as np
import pytest
from pytest_mock import MockerFixture

from jmteb.embedders.openai_embedder import OpenAIEmbedder
from jmteb.embedders import OpenAIEmbedder, TextEmbedder

OUTPUT_DIM = 1536


class TestSentenceBertEmbedder:
def setup_class(cls):
cls.model = OpenAIEmbedder(model="text-embedding-3-small")
@pytest.fixture(scope="function")
def mock_openai_embedder(mocker: MockerFixture):
mocker.patch("jmteb.embedders.openai_embedder.OpenAI")
return OpenAIEmbedder(model="text-embedding-3-small")


@dataclass
class MockData:
data: list


@dataclass
class MockEmbedding:
embedding: list


class MockEmbedder:
def create(input: str | list[str], model: str, dimensions: int):
if isinstance(input, str):
input = [input]
return MockData([MockEmbedding(embedding=[0.1] * dimensions)] * len(input))


@pytest.mark.usefixtures("mock_openai_embedder")
class TestOpenAIEmbedder:
@pytest.fixture(autouse=True)
def setup_class(cls, mocker: MockerFixture, mock_openai_embedder: TextEmbedder):
cls.model = mock_openai_embedder
cls.mock_create = mocker.patch.object(cls.model.client, "embeddings", new=MockEmbedder)

def test_encode(self):
embeddings = self.model.encode("任意のテキスト")
Expand Down

0 comments on commit b71bd36

Please sign in to comment.