Skip to content

Commit

Permalink
dataclass: support for single-shot results with quotes and NLs
Browse files Browse the repository at this point in the history
  • Loading branch information
lbeurerkellner committed Oct 16, 2023
1 parent b200b6d commit 62f907f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lmql/lib/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ async def is_type(ty, description=False):
if key_type is str:
if type(existing_value) is str:
existing_value = existing_value.replace("\\n", "\\\n").replace("\"", "\\\"")
"\"{existing_value}\"{line_end}"
else:
'"[STRING_VALUE]"{line_end}' where STOPS_BEFORE(STRING_VALUE, '"') and \
Expand Down Expand Up @@ -190,6 +191,6 @@ async def is_type(ty, description=False):
try:
json_payload = json.loads(payload)
except Exception as e:
print("Failed to parse JSON from", payload)
raise ValueError("Failed to parse JSON from" + str([payload]))
return type_dict_to_type_instance(json_payload, ty)
'''
45 changes: 45 additions & 0 deletions src/lmql/tests/pending/test_dataclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import lmql
from dataclasses import dataclass
from lmql.tests.expr_test_utils import run_all_tests

card_text = """\
Yktlash, the Unseen Empress
{3}{B}{B}{G}{G}
Legendary Creature — Elf Shaman (5/5)
Trample
When Yktlash, the Unseen Empress using the " character enters the battlefield, create three 1/1 black and green Elf Warrior creature tokens.
{B}{G}, Sacrifice an Elf: Target creature gets -3/-3 until end of turn.
> 'In the depths of the forest, her reign remains elusive. Only the echoes of her whispers reveal the true power she wields.'
"""

@dataclass
class CardData:
name: str
mana_cost: str
supertypes: str
types: str
subtypes: str
rules: str
flavor: str
# Creature
attack: str
defense: str
# Planeswalker
loyalty: str

@lmql.query(model="openai/gpt-3.5-turbo-instruct")
async def test_quote_and_nl():
'''lmql
"{card_text}\n"
"Structured: [CARD_DATA]\n" where type(CARD_DATA) is CardData
assert "\"" in CARD_DATA.rules, "Expected quotes to be preserved in 'rules', but got: " + str([CARD_DATA.rules])
assert "\n" in CARD_DATA.rules, "Expected newline to be preserved in 'rules', but got: " + str([CARD_DATA.rules])
'''

if __name__ == "__main__":
run_all_tests(globals())

0 comments on commit 62f907f

Please sign in to comment.