Skip to content

Commit ccde2b1

Browse files
committed
.
1 parent 0a22f79 commit ccde2b1

4 files changed

Lines changed: 34 additions & 37 deletions

File tree

deepeval/openai/__init__.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
1-
import os
2-
import sys
31
import importlib.util
2+
import sys
43
from importlib.machinery import SourceFileLoader
5-
64
from deepeval.openai.patch import patch_openai
75

8-
# ——— 1) Locate the real OpenAI package on disk ———
9-
spec = importlib.util.find_spec("openai")
10-
if not spec or not spec.origin:
11-
raise ImportError("OpenAI package not found")
12-
origin = spec.origin
13-
subpkg_paths = spec.submodule_search_locations
14-
15-
# ——— 2) Prepare a “fork” module loader ———
16-
loader = SourceFileLoader("deepeval_openai_fork", origin)
17-
fork_spec = importlib.util.spec_from_loader(
18-
"deepeval_openai_fork", loader, origin=origin, is_package=True
19-
)
20-
21-
# ——— 3) Instantiate the module and preserve its package path ———
22-
fork = importlib.util.module_from_spec(fork_spec)
23-
fork.__path__ = subpkg_paths # so internal imports still resolve
24-
25-
# ——— 4) Register, load, and patch the forked copy ———
26-
sys.modules[fork_spec.name] = fork
27-
loader.exec_module(fork)
28-
patch_openai(fork)
6+
def load_and_patch_openai():
7+
openai_spec = importlib.util.find_spec("openai")
8+
if not openai_spec or not openai_spec.origin:
9+
raise ImportError("Could not find the OpenAI package")
2910

30-
# ——— 5) Export only inside deepeval.openai ———
31-
openai = fork
32-
OpenAI = fork.OpenAI
33-
AsyncOpenAI = fork.AsyncOpenAI
11+
init_file = openai_spec.origin
12+
package_dirs = openai_spec.submodule_search_locations
13+
loader = SourceFileLoader("deepeval_openai", init_file)
14+
new_spec = importlib.util.spec_from_loader(
15+
"deepeval_openai",
16+
loader,
17+
origin=init_file,
18+
is_package=True,
19+
)
20+
deepeval_openai = importlib.util.module_from_spec(new_spec)
21+
deepeval_openai.__path__ = package_dirs
22+
sys.modules["deepeval_openai"] = deepeval_openai
23+
loader.exec_module(deepeval_openai)
24+
patch_openai(deepeval_openai)
25+
return deepeval_openai
3426

27+
# Load and patch OpenAI
28+
_openai = load_and_patch_openai()
29+
openai = _openai
30+
OpenAI = _openai.OpenAI
31+
AsyncOpenAI = _openai.AsyncOpenAI
3532
__all__ = ["openai", "OpenAI", "AsyncOpenAI"] + [
3633
name for name in dir(openai) if not name.startswith("_")
37-
]
34+
]

deepeval/openai/evaluate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TestCaseMetricPair:
1515
hyperparameters: Dict[str, Any]
1616

1717
@dataclass
18-
class TestCasesMetricSet:
18+
class TestRunParameters:
1919
test_cases: List[LLMTestCase]
2020
metrics: List[BaseMetric]
2121
hyperparameters: Dict[str, Any]
@@ -44,12 +44,12 @@ def evaluate_sync():
4444
sync_config = AsyncConfig(run_async=False)
4545
if not test_case_pairs:
4646
return
47-
grouped: Dict[str, TestCasesMetricSet] = {}
47+
grouped: Dict[str, TestRunParameters] = {}
4848
for pair in test_case_pairs:
4949
if pair.metrics:
5050
key = "".join([metric.__name__ for metric in pair.metrics])
5151
if key not in grouped:
52-
grouped[key] = TestCasesMetricSet(
52+
grouped[key] = TestRunParameters(
5353
test_cases=[pair.test_case],
5454
metrics=pair.metrics,
5555
hyperparameters=pair.hyperparameters

deepeval/openai/patch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def new_init(self, *args, **kwargs):
3939
original_init(self, *args, **kwargs)
4040
wrap_openai_client_methods(self, is_async)
4141
openai_class.__init__ = new_init
42-
42+
4343
openai_class = getattr(openai_module, "OpenAI", None)
4444
if openai_class:
4545
wrap_init(openai_class, is_async=False)

tests/integrations/test_openai_integration.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
}
4646
}]
4747

48-
completion_mode = "response"
48+
completion_mode = "chat"
4949
client = OpenAI()
5050
tools = CHAT_TOOLS if completion_mode == "chat" else COMPLETION_TOOLS
5151

@@ -124,6 +124,6 @@ def test_traceable_evaluate():
124124
##############################################
125125

126126
if __name__ == "__main__":
127-
# test_end_to_end_evaluation()
128-
# test_traceable_evaluate()
129-
# test_tracing()
127+
test_end_to_end_evaluation()
128+
test_traceable_evaluate()
129+
test_tracing()

0 commit comments

Comments
 (0)