Skip to content

Commit 7f081ac

Browse files
authored
Dynamic OpenAI SUTs (#1131)
* move openai to core * openai factory * fix openai compliance annotator * Remove get_secrets from factory interface * remove openai plugin dep from anthropic
1 parent 882cc47 commit 7f081ac

14 files changed

Lines changed: 102 additions & 59 deletions

plugins/anthropic/pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ packages = [{include = "modelgauge"}]
99
[tool.poetry.dependencies]
1010
python = "^3.10"
1111
anthropic = "*"
12-
modelgauge_openai = {version = "*", optional = false}
1312

1413

1514
[build-system]

plugins/openai/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

plugins/openai/pyproject.toml

Lines changed: 0 additions & 16 deletions
This file was deleted.

poetry.lock

Lines changed: 4 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ modelgauge_azure = {path = "plugins/azure", develop = true, optional=true}
8080
modelgauge_baseten = {path = "plugins/baseten", develop = true, optional=true}
8181
modelgauge_demo_plugin = {path = "demo_plugin", develop = true, optional=false}
8282
modelgauge_nvidia = {path = "plugins/nvidia", develop = true, optional=true}
83-
modelgauge_openai = {path = "plugins/openai", develop = true, optional=true}
8483
modelgauge_perspective_api = {path = "plugins/perspective_api", develop = true, optional=true}
8584
modelgauge_google = {path = "plugins/google", develop = true, optional=true}
8685
modelgauge_vertexai = {path = "plugins/vertexai", develop = true, optional = true}
@@ -90,6 +89,7 @@ modelgauge_amazon = {path = "plugins/amazon", develop = true, optional=true}
9089
prometheus-client = "^0.21.1"
9190
llama-api-client = "^0.1.1"
9291
huggingface-hub = "^0.30.2"
92+
openai = "^1.8.0"
9393

9494
[tool.poetry.group.dev.dependencies]
9595
pytest-datafiles = "^3.0.0"
@@ -109,13 +109,12 @@ azure = ["modelgauge_azure"]
109109
baseten = ["modelgauge_baseten"]
110110
demo = ["modelgauge_demo_plugin"]
111111
nvidia = ["modelgauge_nvidia"]
112-
openai = ["modelgauge_openai"]
113112
perspective_api = ["modelgauge_perspective_api"]
114113
google = ["modelgauge_google"]
115114
vertexai = ["modelgauge_vertexai"]
116115
mistral = ["modelgauge_mistral"]
117116
amazon = ["modelgauge_amazon"]
118-
all_plugins = ["modelgauge_anthropic", "modelgauge_azure", "modelgauge_baseten", "modelgauge_demo_plugin", "modelgauge_nvidia", "modelgauge_openai", "modelgauge_perspective_api", "modelgauge_google", "modelgauge_vertexai", "modelgauge_mistral", "modelgauge_amazon"]
117+
all_plugins = ["modelgauge_anthropic", "modelgauge_azure", "modelgauge_baseten", "modelgauge_demo_plugin", "modelgauge_nvidia", "modelgauge_perspective_api", "modelgauge_google", "modelgauge_vertexai", "modelgauge_mistral", "modelgauge_amazon"]
119118

120119
[tool.poetry.scripts]
121120
modelbench = "modelbench.run:cli"
@@ -143,7 +142,7 @@ line-length = 120
143142
# However, by default mypy uses those to map file paths to modules. This override fixes that.
144143
# https://mypy.readthedocs.io/en/stable/config_file.html#confval-explicit_package_bases
145144
explicit_package_bases = true
146-
mypy_path = "., demo_plugin, plugins/openai, plugins/perspective_api, src, tests"
145+
mypy_path = "., demo_plugin, plugins/perspective_api, src, tests"
147146

148147
[[tool.mypy.overrides]]
149148
module = "modelgauge.tests.*,modelgauge.annotators.*,modelgauge.safety_model_response,plugins.*"

plugins/openai/modelgauge/annotators/openai_compliance_annotator.py renamed to src/modelgauge/annotators/openai_compliance_annotator.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,8 @@ def __init__(
4141
self.decoder = _DEFAULT_MAPPING if decoder is None else decoder
4242

4343
def translate_request(self, test_item: TestItem, response: SUTResponse):
44-
openai_prompt = TextPrompt(
45-
text=self.formatter(test_item, response),
46-
options=SUTOptions(temperature=0),
47-
)
48-
return self.model.translate_text_prompt(openai_prompt)
44+
openai_prompt = TextPrompt(text=self.formatter(test_item, response))
45+
return self.model.translate_text_prompt(openai_prompt, options=SUTOptions(temperature=0))
4946

5047
def annotate(self, annotation_request):
5148
"""Returns an annotation for a single TestItem's interactions."""

src/modelgauge/dynamic_sut_factory.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ class UnknownSUTMakerError(Exception):
2727

2828

2929
class DynamicSUTFactory(ABC):
30-
31-
@staticmethod
32-
@abstractmethod
33-
def get_secrets() -> InjectSecret:
34-
pass
35-
3630
@staticmethod
3731
@abstractmethod
3832
def make_sut(sut_metadata: DynamicSUTMetadata):

src/modelgauge/dynamic_sut_finder.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@
1111

1212
from modelgauge.dynamic_sut_factory import UnknownSUTMakerError
1313
from modelgauge.dynamic_sut_metadata import DynamicSUTMetadata
14+
from modelgauge.suts.openai_sut_factory import OpenAISUTFactory
1415
from modelgauge.suts.together_sut_factory import TogetherSUTFactory
1516

1617
# Maps a string to the module and factory function in that module
1718
# that can be used to create a dynamic sut
1819
DYNAMIC_SUT_FACTORIES: dict = {
1920
"proxied": {"hfrelay": hsf.make_sut},
20-
"direct": {"together": TogetherSUTFactory.make_sut, "huggingface": hsf.make_sut, "hf": hsf.make_sut},
21+
"direct": {
22+
"openai": OpenAISUTFactory.make_sut,
23+
"together": TogetherSUTFactory.make_sut,
24+
"huggingface": hsf.make_sut,
25+
"hf": hsf.make_sut,
26+
},
2127
}
2228

2329

File renamed without changes.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from openai import OpenAI, NotFoundError
2+
3+
from modelgauge.config import load_secrets_from_config
4+
from modelgauge.dynamic_sut_factory import DynamicSUTFactory, ModelNotSupportedError
5+
from modelgauge.dynamic_sut_metadata import DynamicSUTMetadata
6+
from modelgauge.secret_values import InjectSecret
7+
from modelgauge.suts.openai_client import OpenAIApiKey, OpenAIChat, OpenAIOrgId
8+
9+
10+
DRIVER_NAME = "openai"
11+
12+
13+
class OpenAISUTFactory(DynamicSUTFactory):
14+
@staticmethod
15+
def _model_exists(sut_metadata: DynamicSUTMetadata):
16+
secrets = load_secrets_from_config()
17+
api_key = OpenAIApiKey.make(secrets).value
18+
org_id = OpenAIOrgId.make(secrets).value
19+
20+
client = OpenAI(api_key=api_key, organization=org_id, max_retries=7)
21+
22+
try:
23+
client.models.retrieve(sut_metadata.model)
24+
except NotFoundError:
25+
return False
26+
return True
27+
28+
@staticmethod
29+
def make_sut(sut_metadata: DynamicSUTMetadata):
30+
if not OpenAISUTFactory._model_exists(sut_metadata):
31+
raise ModelNotSupportedError(f"Model {sut_metadata.model} not found or not available on openai.")
32+
33+
assert sut_metadata.driver == DRIVER_NAME
34+
return (
35+
OpenAIChat,
36+
DynamicSUTMetadata.make_sut_uid(sut_metadata),
37+
sut_metadata.model,
38+
InjectSecret(OpenAIApiKey),
39+
InjectSecret(OpenAIOrgId),
40+
)

0 commit comments

Comments
 (0)