-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Opik-413] fix missing prompt data (#814)
* Refactor metadata builder * Add test for experiment.build_metadata_from_prompt_version
- Loading branch information
1 parent
8e3a40c
commit 3925650
Showing
5 changed files
with
106 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .experiment import Experiment | ||
from .helpers import build_metadata_and_prompt_version | ||
|
||
__all__ = ["Experiment"] | ||
__all__ = ["Experiment", "build_metadata_and_prompt_version"] |
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,40 @@ | ||
from typing import Optional, Dict, Mapping, Tuple, Any | ||
from .. import prompt | ||
import logging | ||
from opik import jsonable_encoder | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def build_metadata_and_prompt_version( | ||
experiment_config: Optional[Dict[str, Any]], prompt: Optional[prompt.Prompt] | ||
) -> Tuple[Optional[Dict[str, Any]], Optional[Dict[str, str]]]: | ||
metadata = None | ||
prompt_version: Optional[Dict[str, str]] = None | ||
|
||
if experiment_config is None: | ||
experiment_config = {} | ||
|
||
if not isinstance(experiment_config, Mapping): | ||
LOGGER.error( | ||
"Experiment config must be dictionary, but %s was provided. Provided config will be ignored.", | ||
experiment_config, | ||
) | ||
experiment_config = {} | ||
|
||
if prompt is not None and "prompt" in experiment_config: | ||
LOGGER.warning( | ||
"The prompt parameter will not be added to experiment since there is already `prompt` specified in experiment_config" | ||
) | ||
return (experiment_config, None) | ||
|
||
if prompt is not None: | ||
prompt_version = {"id": prompt.__internal_api__version_id__} | ||
experiment_config["prompt"] = prompt.prompt | ||
|
||
if experiment_config == {}: | ||
return None, None | ||
|
||
metadata = jsonable_encoder.jsonable_encoder(experiment_config) | ||
|
||
return metadata, prompt_version |
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
Empty file.
60 changes: 60 additions & 0 deletions
60
sdks/python/tests/unit/api_objects/experiment/test_helpers.py
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,60 @@ | ||
from opik.api_objects import experiment | ||
import pytest | ||
import types | ||
|
||
|
||
def fake_prompt(): | ||
return types.SimpleNamespace( | ||
__internal_api__version_id__="some-prompt-version-id", | ||
prompt="some-prompt-value", | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
argnames="input_kwargs,expected", | ||
argvalues=[ | ||
( | ||
{"experiment_config": None, "prompt": None}, | ||
{"metadata": None, "prompt_version": None}, | ||
), | ||
( | ||
{"experiment_config": {}, "prompt": None}, | ||
{"metadata": None, "prompt_version": None}, | ||
), | ||
( | ||
{"experiment_config": None, "prompt": fake_prompt()}, | ||
{ | ||
"metadata": {"prompt": "some-prompt-value"}, | ||
"prompt_version": {"id": "some-prompt-version-id"}, | ||
}, | ||
), | ||
( | ||
{"experiment_config": {}, "prompt": fake_prompt()}, | ||
{ | ||
"metadata": {"prompt": "some-prompt-value"}, | ||
"prompt_version": {"id": "some-prompt-version-id"}, | ||
}, | ||
), | ||
( | ||
{"experiment_config": {"some-key": "some-value"}, "prompt": None}, | ||
{"metadata": {"some-key": "some-value"}, "prompt_version": None}, | ||
), | ||
( | ||
{ | ||
"experiment_config": "NOT-DICT-VALUE-THAT-WILL-BE-IGNORED-AND-REPLACED-WITH-DICT-WITH-PROMPT", | ||
"prompt": fake_prompt(), | ||
}, | ||
{ | ||
"metadata": {"prompt": "some-prompt-value"}, | ||
"prompt_version": {"id": "some-prompt-version-id"}, | ||
}, | ||
), | ||
], | ||
) | ||
def test_experiment_build_metadata_from_prompt_version(input_kwargs, expected): | ||
metadata, prompt_version = experiment.build_metadata_and_prompt_version( | ||
**input_kwargs | ||
) | ||
|
||
assert metadata == expected["metadata"] | ||
assert prompt_version == expected["prompt_version"] |