Skip to content

Commit

Permalink
Add to_promptfoo method
Browse files Browse the repository at this point in the history
  • Loading branch information
akonarski-ds committed Oct 2, 2024
1 parent 877b35f commit eadfdcc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/ragbits-core/src/ragbits/core/prompt/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,16 @@ def parse_response(self, response: str) -> OutputT:
ResponseParsingError: If the response cannot be parsed.
"""
return self.response_parser(response)

@classmethod
def to_promptfoo(cls, config: dict[str, Any]) -> ChatFormat:
"""
Generate a prompt in the promptfoo format from a promptfoo test configuration.
Args:
config: The promptfoo test configuration.
Returns:
ChatFormat: The prompt in the format used by promptfoo.
"""
return cls(cls.input_type.model_validate(config["vars"])).chat # type: ignore
20 changes: 20 additions & 0 deletions packages/ragbits-core/tests/unit/prompts/test_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,23 @@ class TestPrompt(Prompt[_PromptInput, str]):

prompt = TestPrompt(_PromptInput(name="John", age=15, theme="pop"))
assert prompt.output_schema() is None


def test_to_promptfoo():
"""Test that a prompt can be converted to a promptfoo prompt."""
promptfoo_test_config = {
"vars": {"name": "John", "age": 25, "theme": "pop"},
}

class TestPrompt(Prompt[_PromptInput, str]): # pylint: disable=unused-variable
"""A test prompt"""

system_prompt = """
You are a song generator for a {% if age > 18 %}adult{% else %}child{% endif %} named {{ name }}.
"""
user_prompt = "Theme for the song is {{ theme }}."

assert TestPrompt.to_promptfoo(promptfoo_test_config) == [
{"role": "system", "content": "You are a song generator for a adult named John."},
{"role": "user", "content": "Theme for the song is pop."},
]

0 comments on commit eadfdcc

Please sign in to comment.