# -*- coding: utf-8 -*- """Generate paper 15's tables from results2.json. Nothing here is typed by hand. No figure in any table is per query. The unit is the cell, which is (wrapper, backend, call path, filter form) at the versions in signatures.json. """ import collections import io import json import os B = os.path.dirname(os.path.abspath(__file__)) D = os.path.join(os.path.dirname(B), 'draft', 'tables') os.makedirs(D, exist_ok=True) R = json.load(open(os.path.join(B, 'results2.json'), encoding='utf-8')) PRED = json.load(open(os.path.join(B, 'predictions.json'), encoding='utf-8')) COV = json.load(open(os.path.join(B, 'coverage.json'), encoding='utf-8')) VER = json.load(open(os.path.join(B, 'signatures.json'), encoding='utf-8'))['versions'] NICE = { 'langchain-chroma over chroma': 'langchain-chroma over Chroma', 'langchain-community over chroma': 'langchain-community over Chroma', 'llama-index over chroma': 'llama-index over Chroma', 'lancedb direct': 'LanceDB, direct', 'chroma direct': 'Chroma, direct', 'chroma direct, index mutated': 'Chroma, direct, index mutated', } MARK = {'honoured': 'applied', 'silently_ignored': '**discarded**', 'rejected': 'rejected', 'unknown': 'unknown'} def w(name, text): io.open(os.path.join(D, name), 'w', encoding='utf-8', newline='').write(text) print(' wrote', name) def cell(x): return NICE.get(x['cell'], x['cell']) # 1. the discards, which are the finding rows = ['| package | call path | argument passed | rows returned that a filter ' 'should have withheld |', '|---|---|---|---|'] for x in R: if x['category'] == 'silently_ignored': d = x['detail'] n = d.get('n_leaked') cap = ', sample in the data capped at 5' if d.get('sample_is_capped') else '' rows.append('| %s | `%s` | %s | %s%s |' % ( cell(x), x['path'].replace(':mmr', ', search_type mmr'), x['form'], n, cap)) rows += ['', '*One observation each. These are counts from a single call, not ' 'rates, and must not be rescaled or averaged: with four owners and ' 'k of 10 the unfiltered result is foreign-heavy by design.*'] w('discards.md', '\n'.join(rows) + '\n') # 2. predictions, made before the run rows = ['| prediction, written from the source before running | outcome |', '|---|---|'] for k in sorted(PRED): p = PRED[k] rows.append('| %s | %s |' % (p['claim'], MARK.get(p['outcome'], p['outcome']))) w('predictions.md', '\n'.join(rows) + '\n') # 3. self check, per PATH deg = [x for x in R if x.get('degenerate')] per = collections.defaultdict(list) for x in deg: per[x['path'].split(':')[0]].append(x['category']) rows = ['| call path | broken filters run | scored unknown or rejected |', '|---|---|---|'] for p in sorted(per): cats = per[p] ok = sum(1 for c in cats if c in ('unknown', 'rejected')) rows.append('| `%s` | %d | %d |' % (p, len(cats), ok)) bad = [x for x in deg if x['category'] not in ('unknown', 'rejected')] rows += ['', '*%d deliberately broken filters across %d distinct call paths. %d ' 'scored `applied`. A single one would void the run.*' % (len(deg), len(per), len(bad))] w('self-check.md', '\n'.join(rows) + '\n') # 4. every observation rows = ['| cell | call path | filter form | outcome |', '|---|---|---|---|'] for x in R: if x['category'] == 'stage_failed': continue rows.append('| %s | `%s` | %s%s | %s |' % ( cell(x), x['path'].replace(':mmr', ', mmr'), x['form'], ' *(broken on purpose)*' if x.get('degenerate') else '', MARK.get(x['category'], x['category']))) w('all-observations.md', '\n'.join(rows) + '\n') # 5. index states rows = ['| index state | outcome | note |', '|---|---|---|'] for x in R: if 'mutated' in x['cell']: d = x['detail'] note = d.get('note', '') EXPLAIN = { 'updated_doc_visible': 'absent from the top 10 only because it ranks 201st of 240 for ' 'this query; present at position 50 of 61 in the full filtered ' 'result, so the update was honoured (see ' 'index-update-verification.json)', 'reverted_doc_still_visible': 'correctly stopped being returned once the owner was changed back', 'deleted_ids_returned': 'no deleted document was returned', } for key in EXPLAIN: if key in d: note = EXPLAIN[key] rows.append('| %s | %s | %s |' % (x['form'], MARK.get(x['category'], x['category']), note[:120])) w('index-states.md', '\n'.join(rows) + '\n') # 6. coverage, reported because it is a limit rows = ['| cell | foreign documents ever retrieved unfiltered | of | coverage |', '|---|---|---|---|'] for c in sorted(COV, key=lambda k: -COV[k]['coverage_pct']): v = COV[c] rows.append('| %s | %d | %d | %.1f%% |' % ( NICE.get(c, c), v['foreign_seen_unfiltered'], v['foreign_total'], v['coverage_pct'])) rows += ['', '*A document never retrieved without a filter was never tested, so ' 'every `applied` in this paper is evidence about the documents these ' 'queries reached and not about the whole corpus. This is the weakest ' 'part of the bench and it is reported rather than omitted.*'] w('coverage.md', '\n'.join(rows) + '\n') # 7. categories c = collections.Counter(x['category'] for x in R if x['category'] != 'stage_failed') rows = ['| outcome | cells |', '|---|---|'] for k in ('honoured', 'rejected', 'unknown', 'silently_ignored'): if c.get(k): rows.append('| %s | %d |' % (MARK[k], c[k])) rows += ['', '*Cells, not queries, and not a prevalence estimate: the libraries ' 'were chosen for running without a container, and a wrapper and its ' 'backend are not independent observations.*'] w('categories.md', '\n'.join(rows) + '\n') # 8. versions rows = ['| package | version |', '|---|---|'] for k in sorted(VER): if VER[k]: rows.append('| %s | %s |' % (k, VER[k])) w('versions.md', '\n'.join(rows) + '\n') print('\n%d observations, %d discards, %d broken-filter forms across %d paths, ' '%d scored applied' % (len(R), c.get('silently_ignored', 0), len(deg), len(per), len(bad)))