forked from mlflow/mlflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only include necessary information in prompt for GenAI metrics (mlflo…
…w#10698) Signed-off-by: Roshni Malani <[email protected]> Co-authored-by: Roshni Malani <[email protected]>
- Loading branch information
Showing
7 changed files
with
120 additions
and
79 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
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
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
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
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
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
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,36 +1,57 @@ | ||
import re | ||
|
||
import pytest | ||
|
||
from mlflow.exceptions import MlflowException | ||
from mlflow.metrics.genai.prompt_template import PromptTemplate | ||
|
||
|
||
def test_prompt_template_formatting(): | ||
def test_prompt_template_flat_str_no_variables(): | ||
prompt = PromptTemplate(template_str="Say {foo}") | ||
assert prompt.format(foo="bar") == "Say bar" | ||
|
||
prompt = PromptTemplate(template_str="Say {foo} {baz}") | ||
with pytest.raises(KeyError, match="baz"): | ||
prompt.format(foo="bar") | ||
assert prompt.format(foo="bar") == "" | ||
|
||
prompt = PromptTemplate(template_str="Say foo") | ||
assert prompt.format() == "Say foo" | ||
|
||
with pytest.raises( | ||
MlflowException, | ||
match=re.escape( | ||
"The provided variables ['foo'] are not a subset of " | ||
"the extracted variables ['foo', 'baz'] from the template string" | ||
), | ||
): | ||
prompt = PromptTemplate(template_str="Say {foo} {baz}", variables=["foo"]) | ||
|
||
prompt = PromptTemplate(template_str="Say {foo} {foo}") | ||
assert prompt.format(foo="bar") == "Say bar bar" | ||
|
||
prompt = PromptTemplate(template_str="Say {foo}") | ||
assert prompt.format(foo=None) == "" | ||
|
||
|
||
def test_prompt_template_arr_str_no_variables(): | ||
prompt = PromptTemplate(template_str=["Say {foo}"]) | ||
assert prompt.format(foo="bar") == "Say bar" | ||
|
||
prompt = PromptTemplate(template_str=["Say {foo} {baz}"]) | ||
assert prompt.format(foo="bar") == "" | ||
|
||
prompt = PromptTemplate(template_str=["Say {foo}", " {baz}"]) | ||
assert prompt.format(foo="bar") == "Say bar" | ||
|
||
prompt = PromptTemplate(template_str=["Say {foo}", " {baz}"]) | ||
assert prompt.format(baz="qux") == " qux" | ||
|
||
prompt = PromptTemplate(template_str=["Say foo", ", and say bar"]) | ||
assert prompt.format() == "Say foo, and say bar" | ||
|
||
prompt = PromptTemplate(template_str=["Say {foo} {foo}", ", and {foo}", ", and {foo} again"]) | ||
assert prompt.format(foo="bar") == "Say bar bar, and bar, and bar again" | ||
|
||
prompt = PromptTemplate(template_str=["Say {foo}", " {baz}"]) | ||
assert prompt.format(foo="bar", baz="qux") == "Say bar qux" | ||
|
||
prompt = PromptTemplate(template_str=["Say {foo}", " {baz}"]) | ||
assert prompt.format(foo="bar", baz=None) == "Say bar" | ||
|
||
|
||
def test_prompt_template_partial_formatting(): | ||
prompt = PromptTemplate(template_str="Say {foo} {baz}") | ||
partial_prompt = prompt.partial_fill(foo="bar") | ||
assert partial_prompt.format(baz="qux") == "Say bar qux" | ||
assert partial_prompt.format(baz=None) == "" | ||
|
||
prompt = PromptTemplate(template_str=["Say {foo}", " {baz}"]) | ||
partial_prompt = prompt.partial_fill(foo="bar") | ||
assert partial_prompt.format(baz="qux") == "Say bar qux" | ||
assert partial_prompt.format(baz=None) == "Say bar" | ||
assert partial_prompt.format() == "Say bar" |