okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-split-int' into f-split-vision7c5e84612

main
2.7 KiB60 linesraw
1"""Partial-tag parity, including overlapping prefixes and arbitrary chunk boundaries."""
2import itertools
3import random
4import unittest
5
6from serve.frontend import OutputParser
7
8
9def reference_hold(text, tags):
10    return max((n for tag in tags for n in range(1, len(tag)) if text.endswith(tag[:n])), default=0)
11
12
13class ReferenceParser(OutputParser):
14    def _hold(self, text, tags):
15        return reference_hold(text, tags)
16
17
18class HoldTests(unittest.TestCase):
19    def test_overlapping_and_empty_tags(self):
20        parser = OutputParser()
21        tags = ("", "a", "aaa", "abab", "baab", "<tool_call>", "</think>")
22        for size in range(8):
23            for chars in itertools.product("ab<", repeat=size):
24                text = "".join(chars)
25                self.assertEqual(parser._hold(text, tags), reference_hold(text, tags), text)
26
27    def test_every_real_tag_split(self):
28        tags = ("<tool_call>", "</think>", "</parameter>")
29        parser = OutputParser()
30        for tag in tags:
31            for n in range(len(tag) + 1):
32                for prefix in ("", "prose\n", "<", "<<<", "🙂日本語"):
33                    text = prefix + tag[:n]
34                    self.assertEqual(parser._hold(text, tags), reference_hold(text, tags), text)
35
36    def test_streamed_events_match_reference(self):
37        texts = ["Reasoning.\n</think>\nAnswer café 🙂\n",
38                 "Quoted `</think>` text and <tool_call> prose.",
39                 "\n```xml\n<tool_call>\n```\nAnswer\n<tool_",
40                 "<tool_call><function=lookup><parameter=q>abc</parameter></function></tool_call>\nDone",
41                 "<tool_call><function=lookup><parameter=q>abc</parameter></function></tool_call></think>Done"]
42        rng = random.Random(1028)
43        for text, thinking, recover, stream_tools in itertools.product(texts, (False, True), (False, True), (False, True)):
44            for _ in range(5):
45                args = dict(thinking=thinking, tools=[{"name": "lookup"}], recover=recover, stream_tools=stream_tools)
46                actual, expected = OutputParser(**args), ReferenceParser(**args)
47                def normalized(events):
48                    # Tool call IDs are deliberately random; compare their semantic contents.
49                    return [(e.kind, e.text, (e.call.name, e.call.arguments) if e.call else None) for e in events]
50                pos = 0
51                while pos < len(text):
52                    end = pos + rng.randint(1, 9)
53                    delta = text[pos:end]
54                    self.assertEqual(normalized(actual.feed(delta)), normalized(expected.feed(delta)))
55                    pos = end
56                self.assertEqual(normalized(actual.finish()), normalized(expected.finish()))
57
58
59if __name__ == "__main__":
60    unittest.main()