File size: 5,142 Bytes
4a6405d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""
Unit tests for the data pipeline.
"""

import os
import sys
import pytest

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))

from data.pipeline import MinHashDeduplicator, QualityFilter
from data.chat_templates import (
    format_chat,
    parse_alpaca,
    parse_sharegpt,
    parse_hh_rlhf,
)


class TestQualityFilter:
    """Test document quality filtering."""

    def setup_method(self):
        self.filter = QualityFilter()

    def test_passes_good_document(self):
        text = "This is a well-written document about machine learning. " * 10
        assert self.filter.filter(text) is True

    def test_rejects_too_short(self):
        assert self.filter.filter("Short.") is False

    def test_rejects_too_long(self):
        text = "a " * 100001
        assert self.filter.filter(text) is False

    def test_rejects_too_few_words(self):
        text = "word " * 5  # Only 5 words
        assert self.filter.filter(text) is False

    def test_rejects_repeated_lines(self):
        text = ("This line is repeated.\n" * 50)
        assert self.filter.filter(text) is False

    def test_rejects_special_chars(self):
        text = "@#$%^&*!" * 100
        assert self.filter.filter(text) is False


class TestMinHashDeduplicator:
    """Test MinHash LSH deduplication."""

    def setup_method(self):
        self.dedup = MinHashDeduplicator(threshold=0.85)

    def test_exact_duplicate(self):
        text = "The quick brown fox jumps over the lazy dog. " * 20
        assert self.dedup.is_duplicate(text, "doc1") is False  # First time
        assert self.dedup.is_duplicate(text, "doc2") is True   # Duplicate

    def test_unique_documents(self):
        text1 = "Machine learning is a subset of artificial intelligence. " * 20
        text2 = "The weather forecast predicts rain for tomorrow. " * 20
        assert self.dedup.is_duplicate(text1, "doc1") is False
        assert self.dedup.is_duplicate(text2, "doc2") is False

    def test_near_duplicate(self):
        text1 = "The quick brown fox jumps over the lazy dog every single day. " * 20
        text2 = "The quick brown fox jumps over the lazy dog every single night. " * 20
        self.dedup.is_duplicate(text1, "doc1")
        # Near-duplicate should be caught
        result = self.dedup.is_duplicate(text2, "doc2")
        # This may or may not be caught depending on threshold
        assert isinstance(result, bool)

    def test_short_text(self):
        assert self.dedup.is_duplicate("hi", "doc1") is False


class TestChatTemplates:
    """Test chat template formatting."""

    def test_format_chat_basic(self):
        messages = [
            {"role": "user", "content": "Hello!"},
            {"role": "assistant", "content": "Hi there!"},
        ]
        result = format_chat(messages)
        assert "<|system|>" in result
        assert "<|user|>" in result
        assert "Hello!" in result
        assert "<|assistant|>" in result
        assert "Hi there!" in result

    def test_format_chat_with_system(self):
        messages = [
            {"role": "system", "content": "You are a helpful bot."},
            {"role": "user", "content": "What is AI?"},
        ]
        result = format_chat(messages)
        assert "You are a helpful bot." in result

    def test_format_chat_generation_prompt(self):
        messages = [{"role": "user", "content": "Hello"}]
        result = format_chat(messages, add_generation_prompt=True)
        assert result.endswith("<|assistant|>\n")

    def test_parse_alpaca(self):
        example = {
            "instruction": "Write a haiku",
            "input": "",
            "output": "Cherry blossoms fall\nSoftly on the temple steps\nSpring has come again",
        }
        result = parse_alpaca(example)
        assert "text" in result
        assert "messages" in result
        assert len(result["messages"]) == 2
        assert result["messages"][0]["role"] == "user"
        assert result["messages"][1]["role"] == "assistant"

    def test_parse_alpaca_with_input(self):
        example = {
            "instruction": "Translate to French",
            "input": "Hello, how are you?",
            "output": "Bonjour, comment allez-vous?",
        }
        result = parse_alpaca(example)
        assert "Input:" in result["text"]

    def test_parse_sharegpt(self):
        example = {
            "conversations": [
                {"from": "human", "value": "What is Python?"},
                {"from": "gpt", "value": "Python is a programming language."},
            ]
        }
        result = parse_sharegpt(example)
        assert "text" in result
        assert "Python" in result["text"]

    def test_parse_hh_rlhf(self):
        example = {
            "chosen": "Human: What is 2+2?\n\nAssistant: 2+2 equals 4.",
            "rejected": "Human: What is 2+2?\n\nAssistant: I don't know.",
        }
        result = parse_hh_rlhf(example)
        assert "prompt" in result
        assert "chosen" in result
        assert "rejected" in result
        assert "4" in result["chosen"]


if __name__ == "__main__":
    pytest.main([__file__, "-v"])