2023-05-06 16:41:57 +00:00
|
|
|
import unittest
|
2023-09-13 22:27:44 +00:00
|
|
|
|
2024-01-14 02:54:59 +00:00
|
|
|
from onnx_web.prompt.grammar import PromptPhrase, PromptToken
|
2023-05-06 16:41:57 +00:00
|
|
|
from onnx_web.prompt.parser import parse_prompt_onnx
|
|
|
|
|
2023-09-13 22:27:44 +00:00
|
|
|
|
2023-05-06 16:41:57 +00:00
|
|
|
class ParserTests(unittest.TestCase):
|
|
|
|
def test_single_word_phrase(self):
|
|
|
|
res = parse_prompt_onnx(None, "foo (bar) bin", debug=False)
|
|
|
|
self.assertListEqual(
|
|
|
|
[str(i) for i in res],
|
|
|
|
[
|
|
|
|
str(["foo"]),
|
|
|
|
str(PromptPhrase(["bar"], weight=1.5)),
|
|
|
|
str(["bin"]),
|
2023-11-20 05:18:57 +00:00
|
|
|
],
|
2023-05-06 16:41:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_multi_word_phrase(self):
|
|
|
|
res = parse_prompt_onnx(None, "foo bar (middle words) bin bun", debug=False)
|
|
|
|
self.assertListEqual(
|
|
|
|
[str(i) for i in res],
|
|
|
|
[
|
|
|
|
str(["foo", "bar"]),
|
|
|
|
str(PromptPhrase(["middle", "words"], weight=1.5)),
|
|
|
|
str(["bin", "bun"]),
|
2023-11-20 05:18:57 +00:00
|
|
|
],
|
2023-05-06 16:41:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_nested_phrase(self):
|
|
|
|
res = parse_prompt_onnx(None, "foo (((bar))) bin", debug=False)
|
|
|
|
self.assertListEqual(
|
|
|
|
[str(i) for i in res],
|
|
|
|
[
|
|
|
|
str(["foo"]),
|
2023-11-20 05:18:57 +00:00
|
|
|
str(PromptPhrase(["bar"], weight=(1.5**3))),
|
2023-05-06 16:41:57 +00:00
|
|
|
str(["bin"]),
|
2023-11-20 05:18:57 +00:00
|
|
|
],
|
2023-05-06 16:41:57 +00:00
|
|
|
)
|
2024-01-14 02:54:59 +00:00
|
|
|
|
|
|
|
def test_lora_token(self):
|
|
|
|
res = parse_prompt_onnx(None, "foo <lora:name:1.5> bin", debug=False)
|
|
|
|
self.assertListEqual(
|
|
|
|
[str(i) for i in res],
|
|
|
|
[
|
|
|
|
str(["foo"]),
|
|
|
|
str(PromptToken("lora", "name", 1.5)),
|
|
|
|
str(["bin"]),
|
|
|
|
],
|
|
|
|
)
|