File size: 4,847 Bytes
c02657c eabbe1a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | import os
import random
import sys
import unittest
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "train"))
from opusdb import synth # noqa: E402
from opusdb.decontam import Decontaminator # noqa: E402
from opusdb.dedup import ExactDeduper, MinHashLSH # noqa: E402
from opusdb.text import gopher_quality, guess_language, scrub_pii # noqa: E402
from eval_synth import score # noqa: E402
class SynthTests(unittest.TestCase):
def setUp(self):
self.examples = list(synth.generate(1500, seed=11))
def test_unique_and_both_languages(self):
self.assertEqual(len({e["prompt"] for e in self.examples}), len(self.examples))
self.assertEqual({e["language"] for e in self.examples}, {"en", "tr"})
def test_arithmetic_lines_are_correct(self):
for e in self.examples:
self.assertEqual(synth.check_arithmetic(e["response"]), 0, e["response"])
def test_reference_responses_pass_scorer(self):
for e in self.examples:
item = {"family": e["family"], "answer": e.get("tests") or e["answer"]}
self.assertTrue(score(item, e["response"]), (e["family"], e["response"][-200:]))
def test_corrupted_responses_fail_scorer(self):
r = random.Random(0)
wrong = total = 0
for e in self.examples:
if e["answer"] in ("UNANSWERABLE", "CODE_VERIFIED_BY_TESTS"):
continue
bad = synth.corrupt(e, r)
if bad:
total += 1
wrong += score({"family": e["family"], "answer": e["answer"]}, bad)
self.assertGreater(total, 300)
self.assertLess(wrong / total, 0.01)
def test_turkish_suffixes(self):
self.assertEqual(synth.tr_ek("Can", "abl"), "Can'dan")
self.assertEqual(synth.tr_ek("Mehmet", "abl"), "Mehmet'ten")
self.assertEqual(synth.tr_ek("Ayşe", "gen"), "Ayşe'nin")
self.assertEqual(synth.tr_ek("Ankara", "loc"), "Ankara'da")
class FilterTests(unittest.TestCase):
def test_decontamination(self):
d = Decontaminator()
d.add("Janet's ducks lay 16 eggs per day. She eats three for breakfast every morning and bakes muffins.")
d.finalize()
self.assertTrue(d.contaminated("Q: janet's ducks lay 16 eggs per day she eats three for breakfast every morning!"))
self.assertFalse(d.contaminated("A completely different question about the price of apples in the market."))
def test_dedup(self):
e = ExactDeduper()
self.assertFalse(e.is_dup("Hello, World"))
self.assertTrue(e.is_dup("hello world"))
m = MinHashLSH()
base = "the quick brown fox jumps over the lazy dog " * 5 + "near duplicate detection test sentence here"
self.assertFalse(m.is_dup(base))
self.assertTrue(m.is_dup(base + " extra"))
def test_text_utils(self):
self.assertEqual(scrub_pii("mail me at a.b@example.com"), "mail me at <EMAIL>")
self.assertEqual(guess_language("Bu bir deneme cümlesidir ve çok güzel bir gün."), "tr")
self.assertEqual(guess_language("This is the test of the system and it is fine."), "en")
self.assertIn("too_short", gopher_quality("short text"))
if __name__ == "__main__":
unittest.main()
class ToolTests(unittest.TestCase):
def test_runtime(self):
from opusdb.agent_tools import calculator, date_calc, unit_convert, parse_tool_calls
self.assertEqual(calculator("2**10 + 3")["result"], "1027")
self.assertEqual(calculator("1/0"), {"error": "division by zero"})
self.assertEqual(date_calc("diff", "2026-01-01", other_date="2026-01-31")["result"], 30)
self.assertAlmostEqual(unit_convert(212, "F", "C")["result"], 100)
self.assertEqual(parse_tool_calls('<tool_call>{"name": "calculator", "arguments": {"expression": "1+1"}}</tool_call>'),
[{"name": "calculator", "arguments": {"expression": "1+1"}}])
def test_synth_tools_valid(self):
from opusdb import synth_tools
from opusdb.tool_formats import valid, encode_messages, to_chat_template
import json
for ex in synth_tools.generate(200, seed=1):
self.assertTrue(valid(ex["messages"], ex["tools"]), ex)
row = {"messages": encode_messages(ex["messages"]), "tools": json.dumps(ex["tools"])}
msgs, tools = to_chat_template(row)
self.assertEqual(len(msgs), len(ex["messages"]))
def test_toolace_parser(self):
from opusdb.tool_formats import _split_calls
self.assertEqual(_split_calls('[Countries All (min)(limit=5), getRoute(a="x, y")]', {"Countries All (min)", "getRoute"}),
[("Countries All (min)", {"limit": 5}), ("getRoute", {"a": "x, y"})])
|