-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dmytro Parfeniuk
committed
Jul 3, 2024
1 parent
222cc44
commit 56d53df
Showing
8 changed files
with
134 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
|
||
|
This file contains 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 |
---|---|---|
@@ -1,37 +1,59 @@ | ||
import random | ||
from typing import List | ||
|
||
import pytest | ||
from openai.pagination import SyncPage | ||
from openai.types import Model | ||
from openai.types import Completion, Model | ||
|
||
from guidellm.backend import Backend, BackendEngine, OpenAIBackend | ||
|
||
from . import factories | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def openai_models_list_patch(mocker): | ||
def openai_models_list_patch(mocker) -> List[Model]: | ||
""" | ||
Mock available models function to avoid OpenAI API call. | ||
""" | ||
|
||
return mocker.patch( | ||
items = factories.OpenAIModel.batch(3) | ||
mocker.patch( | ||
"openai.resources.models.Models.list", | ||
return_value=SyncPage( | ||
object="list", | ||
data=[ | ||
Model( | ||
id="model-id-1", | ||
object="model", | ||
created=1686935002, | ||
owned_by="openai", | ||
), | ||
Model( | ||
id="model-id-2", | ||
object="model", | ||
created=1686935003, | ||
owned_by="openai", | ||
), | ||
Model( | ||
id="model-id-3", | ||
object="model", | ||
created=1686935004, | ||
owned_by="openai", | ||
), | ||
], | ||
), | ||
return_value=SyncPage(object="list", data=items), | ||
) | ||
|
||
return items | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def openai_completion_create_patch(mocker) -> List[Completion]: | ||
""" | ||
Mock available models function to avoid OpenAI API call. | ||
""" | ||
|
||
items = factories.OpenAICompletion.batch(random.randint(2, 5)) | ||
mocker.patch("openai.resources.completions.Completions.create", return_value=items) | ||
|
||
return items | ||
|
||
|
||
@pytest.fixture | ||
def openai_backend_factory(): | ||
""" | ||
Create a test openai backend service. | ||
Call without provided arguments returns default Backend service. | ||
""" | ||
|
||
def inner_wrapper(*_, **kwargs) -> OpenAIBackend: | ||
static = {"backend_type": BackendEngine.OPENAI_SERVER} | ||
defaults = { | ||
"openai_api_key": "dummy api key", | ||
"internal_callback_url": "http://localhost:8000", | ||
} | ||
|
||
defaults.update(kwargs) | ||
defaults.update(static) | ||
|
||
return Backend.create(**defaults) | ||
|
||
return inner_wrapper |
This file contains 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import functools | ||
import random | ||
|
||
from openai.types import Completion, CompletionChoice, Model | ||
from polyfactory.factories.pydantic_factory import ModelFactory | ||
|
||
__all__ = ["OpenAIModel", "OpenAICompletionChoice", "OpenAICompletion"] | ||
|
||
|
||
class OpenAIModel(ModelFactory[Model]): | ||
""" | ||
A model factory for Open AI Model representation. | ||
""" | ||
|
||
pass | ||
|
||
|
||
class OpenAICompletionChoice(ModelFactory[CompletionChoice]): | ||
""" | ||
A model factory for Open AI Completion Choice representation. | ||
""" | ||
|
||
finish_reason = "stop" | ||
|
||
|
||
class OpenAICompletion(ModelFactory[Completion]): | ||
""" | ||
A model factory for Open AI Completion representation. | ||
""" | ||
|
||
choices = functools.partial(OpenAICompletionChoice.batch, random.randint(3, 5)) |
This file contains 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
This file contains 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