File size: 12,180 Bytes
e4f9cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
"""Tests for dataset.select_rows(searches=[...])."""

from typing import Iterable, cast

import numpy as np
import pytest
from pytest import approx
from pytest_mock import MockerFixture
from sklearn.preprocessing import normalize
from typing_extensions import override

from ..concepts.concept import ExampleIn, LogisticEmbeddingModel
from ..concepts.db_concept import ConceptUpdate, DiskConceptDB
from ..db_manager import set_default_dataset_cls
from ..schema import UUID_COLUMN, Item, RichData, SignalInputType
from ..signals.concept_scorer import ConceptScoreSignal
from ..signals.semantic_similarity import SemanticSimilaritySignal
from ..signals.signal import TextEmbeddingSignal, clear_signal_registry, register_signal
from ..signals.substring_search import SubstringSignal
from .dataset import ConceptQuery, KeywordQuery, ListOp, Search, SemanticQuery, SortOrder
from .dataset_duckdb import DatasetDuckDB
from .dataset_test_utils import TestDataMaker, enriched_embedding_span, enriched_item
from .dataset_utils import lilac_embedding, lilac_span

TEST_DATA: list[Item] = [{
  UUID_COLUMN: '1',
  'text': 'hello world',
  'text2': 'again hello world',
}, {
  UUID_COLUMN: '2',
  'text': 'looking for world in text',
  'text2': 'again looking for world in text',
}, {
  UUID_COLUMN: '3',
  'text': 'unrelated text',
  'text2': 'again unrelated text'
}]

EMBEDDINGS: list[tuple[str, list[float]]] = [
  ('hello.', [1.0, 0.0, 0.0]),
  ('hello2.', [1.0, 1.0, 0.0]),
  ('hello world.', [1.0, 1.0, 1.0]),
  ('hello world2.', [2.0, 1.0, 1.0]),
  ('random negative 1', [0, 0, 0.3]),
  ('random negative 2', [0, 0, 0.4]),
  ('random negative 3', [0, 0.1, 0.5]),
  ('random negative 4', [0.1, 0, 0.4]),
]

STR_EMBEDDINGS: dict[str, list[float]] = {text: embedding for text, embedding in EMBEDDINGS}


@pytest.fixture(scope='module', autouse=True)
def setup_teardown() -> Iterable[None]:
  # Setup.
  set_default_dataset_cls(DatasetDuckDB)
  register_signal(TestEmbedding)

  # Unit test runs.
  yield

  # Teardown.
  clear_signal_registry()


def test_search_keyword(make_test_data: TestDataMaker) -> None:
  dataset = make_test_data(TEST_DATA)

  query = 'world'
  result = dataset.select_rows(
    searches=[Search(path='text', query=KeywordQuery(type='keyword', search=query))],
    combine_columns=True)

  expected_signal_udf = SubstringSignal(query=query)
  assert list(result) == [{
    UUID_COLUMN: '1',
    'text': enriched_item('hello world', {expected_signal_udf.key(): [lilac_span(6, 11)]}),
    'text2': 'again hello world'
  }, {
    UUID_COLUMN: '2',
    'text': enriched_item('looking for world in text',
                          {expected_signal_udf.key(): [lilac_span(12, 17)]}),
    'text2': 'again looking for world in text',
  }]


def test_search_keyword_special_chars(make_test_data: TestDataMaker) -> None:
  dataset = make_test_data([{
    UUID_COLUMN: '1',
    'text': 'This is 100%',
  }, {
    UUID_COLUMN: '2',
    'text': 'This has _underscore_',
  }])

  query = '100%'
  result = dataset.select_rows(
    searches=[Search(path='text', query=KeywordQuery(type='keyword', search=query))],
    combine_columns=True)

  expected_signal_udf = SubstringSignal(query=query)
  assert list(result) == [{
    UUID_COLUMN: '1',
    'text': enriched_item('This is 100%', {expected_signal_udf.key(): [lilac_span(8, 12)]}),
  }]

  query = '_underscore_'
  result = dataset.select_rows(
    searches=[Search(path='text', query=KeywordQuery(type='keyword', search=query))],
    combine_columns=True)

  expected_signal_udf = SubstringSignal(query=query)
  assert list(result) == [{
    UUID_COLUMN: '2',
    'text': enriched_item('This has _underscore_',
                          {expected_signal_udf.key(): [lilac_span(9, 21)]}),
  }]


def test_search_keyword_multiple(make_test_data: TestDataMaker) -> None:
  dataset = make_test_data(TEST_DATA)

  query_world = 'world'
  query_looking_world = 'looking for world'
  expected_world_udf = SubstringSignal(query=query_world)
  expected_again_looking_udf = SubstringSignal(query=query_looking_world)

  result = dataset.select_rows(
    searches=[
      Search(path='text', query=KeywordQuery(type='keyword', search=query_world)),
      Search(path='text2', query=KeywordQuery(type='keyword', search=query_looking_world)),
    ],
    combine_columns=True)

  assert list(result) == [{
    UUID_COLUMN: '2',
    'text': enriched_item('looking for world in text', {
      expected_world_udf.key(): [lilac_span(12, 17)],
    }),
    'text2': enriched_item('again looking for world in text',
                           {expected_again_looking_udf.key(): [lilac_span(6, 23)]})
  }]


def test_search_keyword_with_filters(make_test_data: TestDataMaker) -> None:
  dataset = make_test_data(TEST_DATA)

  query = 'world'
  result = dataset.select_rows(
    filters=[(UUID_COLUMN, ListOp.IN, ['1', '3'])],
    searches=[Search(path='text', query=KeywordQuery(type='keyword', search=query))],
    combine_columns=True)

  expected_signal_udf = SubstringSignal(query=query)
  assert list(result) == [
    {
      UUID_COLUMN: '1',
      'text': enriched_item('hello world', {expected_signal_udf.key(): [lilac_span(6, 11)]}),
      'text2': 'again hello world'
    },
    # The second row doesn't match the UUID filter.
  ]


class TestEmbedding(TextEmbeddingSignal):
  """A test embed function."""
  name = 'test_embedding'

  @override
  def compute(self, data: Iterable[RichData]) -> Iterable[Item]:
    """Call the embedding function."""
    for example in data:
      embedding = np.array(STR_EMBEDDINGS[cast(str, example)])
      embedding = normalize([embedding])[0]
      yield [lilac_embedding(0, len(example), embedding)]


def test_semantic_search(make_test_data: TestDataMaker) -> None:
  dataset = make_test_data([{
    UUID_COLUMN: '1',
    'text': 'hello world.',
  }, {
    UUID_COLUMN: '2',
    'text': 'hello world2.',
  }])

  test_embedding = TestEmbedding()
  dataset.compute_signal(test_embedding, ('text'))

  query = 'hello2.'
  result = dataset.select_rows(
    searches=[
      Search(
        path='text', query=SemanticQuery(type='semantic', search=query, embedding='test_embedding'))
    ],
    combine_columns=True)
  expected_signal_udf = SemanticSimilaritySignal(query=query, embedding='test_embedding')
  assert list(result) == [
    # Results are sorted by score desc.
    {
      UUID_COLUMN: '2',
      'text': enriched_item(
        'hello world2.', {
          test_embedding.key():
            [enriched_embedding_span(0, 13, {expected_signal_udf.key(): approx(0.916, 1e-3)})]
        })
    },
    {
      UUID_COLUMN: '1',
      'text': enriched_item(
        'hello world.', {
          test_embedding.key():
            [enriched_embedding_span(0, 12, {expected_signal_udf.key(): approx(0.885, 1e-3)})]
        })
    },
  ]


def test_concept_search(make_test_data: TestDataMaker, mocker: MockerFixture) -> None:
  concept_model_mock = mocker.spy(LogisticEmbeddingModel, 'fit')

  dataset = make_test_data([{
    UUID_COLUMN: '1',
    'text': 'hello world.',
  }, {
    UUID_COLUMN: '2',
    'text': 'hello world2.',
  }, {
    UUID_COLUMN: '3',
    'text': 'random negative 1',
  }, {
    UUID_COLUMN: '4',
    'text': 'random negative 2',
  }, {
    UUID_COLUMN: '5',
    'text': 'random negative 3',
  }, {
    UUID_COLUMN: '6',
    'text': 'random negative 4',
  }])

  test_embedding = TestEmbedding()
  dataset.compute_signal(test_embedding, ('text'))

  concept_db = DiskConceptDB()
  concept_db.create(namespace='test_namespace', name='test_concept', type=SignalInputType.TEXT)
  concept_db.edit(
    'test_namespace', 'test_concept',
    ConceptUpdate(insert=[
      ExampleIn(label=False, text='hello world.'),
      ExampleIn(label=True, text='hello world2.')
    ]))

  result = dataset.select_rows(
    searches=[
      Search(
        path='text',
        query=ConceptQuery(
          type='concept',
          concept_namespace='test_namespace',
          concept_name='test_concept',
          embedding='test_embedding'))
    ],
    filters=[(UUID_COLUMN, ListOp.IN, ['1', '2'])],
    combine_columns=True)
  expected_signal_udf = ConceptScoreSignal(
    namespace='test_namespace', concept_name='test_concept', embedding='test_embedding')

  assert list(result) == [
    # Results are sorted by score desc.
    {
      UUID_COLUMN: '2',
      'text': enriched_item(
        'hello world2.', {
          test_embedding.key():
            [enriched_embedding_span(0, 13, {expected_signal_udf.key(): approx(0.75, abs=0.25)})],
          'test_namespace/test_concept/labels': [lilac_span(0, 13, {'label': True})]
        })
    },
    {
      UUID_COLUMN: '1',
      'text': enriched_item(
        'hello world.', {
          test_embedding.key():
            [enriched_embedding_span(0, 12, {expected_signal_udf.key(): approx(0.25, abs=0.25)})],
          'test_namespace/test_concept/labels': [lilac_span(0, 12, {'label': False})]
        })
    },
  ]

  # Make sure fit was called with negative examples.
  (_, embeddings, labels, _) = concept_model_mock.call_args_list[-1].args
  assert embeddings.shape == (8, 3)
  assert labels == [
    # Negative implicit labels.
    False,
    False,
    False,
    False,
    False,
    False,
    # Explicit labels.
    False,
    True
  ]


def test_sort_override_search(make_test_data: TestDataMaker) -> None:
  dataset = make_test_data([{
    UUID_COLUMN: '1',
    'text': 'hello world.',
    'value': 10
  }, {
    UUID_COLUMN: '2',
    'text': 'hello world2.',
    'value': 20
  }])

  test_embedding = TestEmbedding()
  dataset.compute_signal(test_embedding, ('text'))

  query = 'hello2.'
  search = Search(
    path='text', query=SemanticQuery(type='semantic', search=query, embedding='test_embedding'))

  expected_signal_udf = SemanticSimilaritySignal(query=query, embedding='test_embedding')
  expected_item_1 = {
    UUID_COLUMN: '1',
    'text': enriched_item(
      'hello world.', {
        test_embedding.key():
          [enriched_embedding_span(0, 12, {expected_signal_udf.key(): approx(0.885, 1e-3)})]
      }),
    'value': 10
  }
  expected_item_2 = {
    UUID_COLUMN: '2',
    'text': enriched_item(
      'hello world2.', {
        test_embedding.key():
          [enriched_embedding_span(0, 13, {expected_signal_udf.key(): approx(0.916, 1e-3)})]
      }),
    'value': 20
  }

  sort_order = SortOrder.ASC
  result = dataset.select_rows(
    searches=[search], sort_by=[('value',)], sort_order=sort_order, combine_columns=True)
  assert list(result) == [
    # Results are sorted by score ascending.
    expected_item_1,
    expected_item_2
  ]

  sort_order = SortOrder.DESC
  result = dataset.select_rows(
    searches=[search], sort_by=[('text',)], sort_order=sort_order, combine_columns=True)
  assert list(result) == [
    # Results are sorted by score descending.
    expected_item_2,
    expected_item_1
  ]


def test_search_keyword_and_semantic(make_test_data: TestDataMaker) -> None:
  dataset = make_test_data([{
    UUID_COLUMN: '1',
    'text': 'hello world.',
  }, {
    UUID_COLUMN: '2',
    'text': 'hello world2.',
  }])

  test_embedding = TestEmbedding()
  dataset.compute_signal(test_embedding, ('text'))

  query = 'hello2.'
  keyword_query = 'rld2'
  result = dataset.select_rows(
    searches=[
      Search(
        path='text', query=SemanticQuery(type='semantic', search=query,
                                         embedding='test_embedding')),
      Search(path='text', query=KeywordQuery(type='keyword', search=keyword_query))
    ],
    combine_columns=True)
  expected_semantic_signal = SemanticSimilaritySignal(query=query, embedding='test_embedding')
  expected_keyword_signal = SubstringSignal(query=keyword_query)
  assert list(result) == [
    # Results are sorted by score desc.
    {
      UUID_COLUMN: '2',
      'text': enriched_item(
        'hello world2.', {
          test_embedding.key():
            [enriched_embedding_span(0, 13, {expected_semantic_signal.key(): approx(0.916, 1e-3)})],
          expected_keyword_signal.key(): [lilac_span(8, 12)],
        })
    },
    # UUID '1' is not returned because it does not match the keyword query.
  ]