| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 | import osfrom collections.abc import Callablefrom typing import Literalimport pytest# import monkeypatchfrom _pytest.monkeypatch import MonkeyPatchfrom openai.resources.audio.transcriptions import Transcriptionsfrom openai.resources.chat import Completions as ChatCompletionsfrom openai.resources.completions import Completionsfrom openai.resources.embeddings import Embeddingsfrom openai.resources.models import Modelsfrom openai.resources.moderations import Moderationsfrom tests.integration_tests.model_runtime.__mock.openai_chat import MockChatClassfrom tests.integration_tests.model_runtime.__mock.openai_completion import MockCompletionsClassfrom tests.integration_tests.model_runtime.__mock.openai_embeddings import MockEmbeddingsClassfrom tests.integration_tests.model_runtime.__mock.openai_moderation import MockModerationClassfrom tests.integration_tests.model_runtime.__mock.openai_remote import MockModelClassfrom tests.integration_tests.model_runtime.__mock.openai_speech2text import MockSpeech2TextClassdef mock_openai(    monkeypatch: MonkeyPatch,    methods: list[Literal["completion", "chat", "remote", "moderation", "speech2text", "text_embedding"]],) -> Callable[[], None]:    """    mock openai module    :param monkeypatch: pytest monkeypatch fixture    :return: unpatch function    """    def unpatch() -> None:        monkeypatch.undo()    if "completion" in methods:        monkeypatch.setattr(Completions, "create", MockCompletionsClass.completion_create)    if "chat" in methods:        monkeypatch.setattr(ChatCompletions, "create", MockChatClass.chat_create)    if "remote" in methods:        monkeypatch.setattr(Models, "list", MockModelClass.list)    if "moderation" in methods:        monkeypatch.setattr(Moderations, "create", MockModerationClass.moderation_create)    if "speech2text" in methods:        monkeypatch.setattr(Transcriptions, "create", MockSpeech2TextClass.speech2text_create)    if "text_embedding" in methods:        monkeypatch.setattr(Embeddings, "create", MockEmbeddingsClass.create_embeddings)    return unpatchMOCK = os.getenv("MOCK_SWITCH", "false").lower() == "true"@pytest.fixturedef setup_openai_mock(request, monkeypatch):    methods = request.param if hasattr(request, "param") else []    if MOCK:        unpatch = mock_openai(monkeypatch, methods=methods)    yield    if MOCK:        unpatch()
 |