-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
text.generate.format
constrained generation
- Loading branch information
Showing
7 changed files
with
149 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
from .continuation import continuation | ||
from .regex import choice, float, integer, json, regex | ||
from .regex import choice, format, json, regex |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import datetime | ||
from typing import Any | ||
|
||
INTEGER = r"[+-]?(0|[1-9][0-9]*)" | ||
BOOLEAN = "(True|False)" | ||
FLOAT = rf"{INTEGER}(\.[0-9]+)?([eE][+-][0-9]+)?" | ||
DATE = r"(\d{4})-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])" | ||
TIME = r"([0-1][1-9]|2[0-4]):([0-5][0-9]):([0-5][0-9])" | ||
DATETIME = rf"({DATE})(\s)({TIME})" | ||
|
||
|
||
def python_types_to_regex(python_type: Any) -> str: | ||
if python_type == float: | ||
return FLOAT | ||
elif python_type == int: | ||
return INTEGER | ||
elif python_type == bool: | ||
return BOOLEAN | ||
elif python_type == datetime.date: | ||
return DATE | ||
elif python_type == datetime.time: | ||
return TIME | ||
elif python_type == datetime.datetime: | ||
return DATETIME | ||
else: | ||
raise NotImplementedError( | ||
f"The Python type {python_type} is not supported. Please open an issue." | ||
) |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import datetime | ||
|
||
import pytest | ||
|
||
from outlines.text.types import ( | ||
BOOLEAN, | ||
DATE, | ||
DATETIME, | ||
FLOAT, | ||
INTEGER, | ||
TIME, | ||
python_types_to_regex, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"python_type,regex", | ||
[ | ||
(int, INTEGER), | ||
(float, FLOAT), | ||
(bool, BOOLEAN), | ||
(datetime.date, DATE), | ||
(datetime.time, TIME), | ||
(datetime.datetime, DATETIME), | ||
], | ||
) | ||
def test_python_types(python_type, regex): | ||
test_regex = python_types_to_regex(python_type) | ||
assert regex == test_regex |