-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
And the `output_type` should now be a Pydantic model or a JSON Schema str
- Loading branch information
1 parent
ac77adb
commit 214a2f3
Showing
3 changed files
with
54 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,41 @@ | ||
from unittest.mock import MagicMock | ||
from unittest.mock import Mock, patch | ||
|
||
import pytest | ||
from pydantic import BaseModel | ||
|
||
from outlines.outline import Outline | ||
from outlines import Outline | ||
|
||
|
||
def test_outline_int_output(): | ||
model = MagicMock() | ||
model.generate.return_value = "6" | ||
class OutputModel(BaseModel): | ||
result: int | ||
|
||
def template(a: int) -> str: | ||
return f"What is 2 times {a}?" | ||
|
||
fn = Outline(model, template, int) | ||
result = fn(3) | ||
assert result == 6 | ||
def template(a: int) -> str: | ||
return f"What is 2 times {a}?" | ||
|
||
|
||
def test_outline_str_output(): | ||
model = MagicMock() | ||
model.generate.return_value = "'Hello, world!'" | ||
def test_outline(): | ||
mock_model = Mock() | ||
mock_generator = Mock() | ||
mock_generator.return_value = '{"result": 6}' | ||
|
||
def template(a: int) -> str: | ||
return f"Say 'Hello, world!' {a} times" | ||
with patch("outlines.generate.json", return_value=mock_generator): | ||
outline_instance = Outline(mock_model, template, OutputModel) | ||
result = outline_instance(3) | ||
|
||
fn = Outline(model, template, str) | ||
result = fn(1) | ||
assert result == "Hello, world!" | ||
assert result.result == 6 | ||
|
||
|
||
def test_outline_str_input(): | ||
model = MagicMock() | ||
model.generate.return_value = "'Hi, Mark!'" | ||
def test_outline_with_json_schema(): | ||
mock_model = Mock() | ||
mock_generator = Mock() | ||
mock_generator.return_value = '{"result": 6}' | ||
|
||
def template(a: str) -> str: | ||
return f"Say hi to {a}" | ||
with patch("outlines.generate.json", return_value=mock_generator): | ||
outline_instance = Outline( | ||
mock_model, | ||
template, | ||
'{"type": "object", "properties": {"result": {"type": "integer"}}}', | ||
) | ||
result = outline_instance(3) | ||
|
||
fn = Outline(model, template, str) | ||
result = fn(1) | ||
assert result == "Hi, Mark!" | ||
|
||
|
||
def test_outline_invalid_output(): | ||
model = MagicMock() | ||
model.generate.return_value = "not a number" | ||
|
||
def template(a: int) -> str: | ||
return f"What is 2 times {a}?" | ||
|
||
fn = Outline(model, template, int) | ||
with pytest.raises(ValueError): | ||
fn(3) | ||
|
||
|
||
def test_outline_mismatched_output_type(): | ||
model = MagicMock() | ||
model.generate.return_value = "'Hello, world!'" | ||
|
||
def template(a: int) -> str: | ||
return f"What is 2 times {a}?" | ||
|
||
fn = Outline(model, template, int) | ||
with pytest.raises( | ||
ValueError, | ||
match="Unable to parse response: 'Hello, world!'", | ||
): | ||
fn(3) | ||
assert result["result"] == 6 |