Skip to content

Commit

Permalink
add e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
japdubengsub committed Nov 6, 2024
1 parent 22a431f commit 5e9b2fa
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 9 deletions.
5 changes: 1 addition & 4 deletions sdks/python/src/opik/api_objects/opik_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,10 +721,7 @@ def get_prompt(
prompt_detail: PromptDetail = self._rest_client.prompts.get_prompt_by_id(
id=prompt_version.prompt_id
)

# to build correct prompt instance (if 'commit' was specified) we will substitute the latest version
prompt_detail.latest_version = prompt_version
prompt = Prompt.from_fern_prompt_detail(prompt_detail)
prompt = Prompt.from_fern_prompt_detail(prompt_detail, prompt_version)

return prompt

Expand Down
18 changes: 13 additions & 5 deletions sdks/python/src/opik/api_objects/prompt/prompt.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from typing import Any, Optional

from opik.rest_api import PromptDetail
from opik.rest_api import PromptDetail, PromptVersionDetail


class Prompt:
def __init__(
self,
name: str,
template: str,
description: Optional[str],
description: Optional[str] = None,
) -> None:
from opik.api_objects import opik_client

Expand Down Expand Up @@ -67,15 +67,23 @@ def format(self, **kwargs: Any) -> str:
return template

@classmethod
def from_fern_prompt_detail(cls, prompt_detail: PromptDetail) -> "Prompt":
def from_fern_prompt_detail(
cls,
prompt_detail: PromptDetail,
prompt_version_detail: Optional[PromptVersionDetail] = None,
) -> "Prompt":
# will not call __init__ to avoid API calls, create new instance with __new__
prompt = cls.__new__(cls)

prompt._id = prompt_detail.id
prompt._name = prompt_detail.name
prompt._description = prompt_detail.description

prompt._template = prompt_detail.latest_version.template
prompt._commit = prompt_detail.latest_version.commit
if prompt_version_detail:
prompt._template = prompt_version_detail.template
prompt._commit = prompt_version_detail.commit
else:
prompt._template = prompt_detail.latest_version.template
prompt._commit = prompt_detail.latest_version.commit

return prompt
127 changes: 127 additions & 0 deletions sdks/python/tests/e2e/test_prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import uuid

from opik import Prompt


def test_prompt__create__happyflow(opik_client):
unique_identifier = str(uuid.uuid4())[-6:]

prompt_name = f"some-prompt-name-{unique_identifier}"
prompt_template = f"some-prompt-text-{unique_identifier}"
prompt_description = f"some-prompt-description-{unique_identifier}"

prompt = opik_client.create_prompt(
name=prompt_name,
template=prompt_template,
description=prompt_description,
)

assert prompt.name == prompt_name
assert prompt.template == prompt_template
assert prompt.description == prompt_description
assert prompt.id is not None
assert prompt.commit is not None


def test_prompt__create_new_version__happyflow(opik_client):
unique_identifier = str(uuid.uuid4())[-6:]

prompt_name = f"some-prompt-name-{unique_identifier}"
prompt_template = f"some-prompt-text-{unique_identifier}"
prompt_description = f"some-prompt-description-{unique_identifier}"

prompt = opik_client.create_prompt(
name=prompt_name,
template=prompt_template,
description=prompt_description,
)

unique_identifier_new = str(uuid.uuid4())[-6:]
prompt_template_new = f"some-prompt-text-{unique_identifier_new}"

# must create new version
new_prompt = opik_client.create_prompt(
name=prompt_name,
template=prompt_template_new,
)

assert new_prompt.name == prompt.name
assert new_prompt.template == prompt_template_new
assert new_prompt.description == prompt.description
assert new_prompt.id == prompt.id
assert new_prompt.commit != prompt.commit


def test_prompt__get__happyflow(opik_client):
unique_identifier = str(uuid.uuid4())[-6:]

prompt_name = f"some-prompt-name-{unique_identifier}"
prompt_template = f"some-prompt-text-{unique_identifier}"
prompt_description = f"some-prompt-description-{unique_identifier}"

prompt = opik_client.create_prompt(
name=prompt_name,
template=prompt_template,
description=prompt_description,
)

unique_identifier_new = str(uuid.uuid4())[-6:]
prompt_template_new = f"some-prompt-text-{unique_identifier_new}"

# must create new version
new_prompt = opik_client.create_prompt(
name=prompt_name,
template=prompt_template_new,
)

# ASSERTIONS
p1 = opik_client.get_prompt(name=prompt.name)

assert p1.name == new_prompt.name
assert p1.template == new_prompt.template
assert p1.description == new_prompt.description
assert p1.id == new_prompt.id
assert p1.commit == new_prompt.commit

p2 = opik_client.get_prompt(name=prompt.name, commit=prompt.commit)

assert p2.name == prompt.name
assert p2.template == prompt.template
assert p2.description == prompt.description
assert p2.id == prompt.id
assert p2.commit == prompt.commit


def test_prompt__initialize_class_instance(opik_client):
unique_identifier = str(uuid.uuid4())[-6:]
template = "Hello, {name} from {place}! Nice to meet you, {name}."

prompt = Prompt(name=f"test-{unique_identifier}", template=template)
prompt_from_api = opik_client.get_prompt(name=prompt.name)

assert prompt.name == prompt_from_api.name
assert prompt.template == prompt_from_api.template
assert prompt.description == prompt_from_api.description
assert prompt.id == prompt_from_api.id
assert prompt.commit == prompt_from_api.commit


def test_prompt__format(opik_client):
unique_identifier = str(uuid.uuid4())[-6:]
template = "Hello, {name} from {place}! Nice to meet you, {name}."

prompt = Prompt(name=f"test-{unique_identifier}", template=template)

result = prompt.format()
assert result == "Hello, {name} from {place}! Nice to meet you, {name}."

result = prompt.format(name="John")
assert result == "Hello, John from {place}! Nice to meet you, John."

result = prompt.format(name="John", place="The Earth")
assert result == "Hello, John from The Earth! Nice to meet you, John."

result = prompt.format(name="John", place="The Earth", unexisting_key="value")
assert result == "Hello, John from The Earth! Nice to meet you, John."

assert prompt.template == template

0 comments on commit 5e9b2fa

Please sign in to comment.