File size: 2,468 Bytes
0a50834
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import json
import os

from ._graph import Graph
from ._state import locked_state
from .config import TARGETS
from .download import fetch
from .embedding import embed_graph
from .indexes import create_indexes, inspect_graph
from .parquet import load_vectors


def positive(value):
    value = int(value)
    if value < 1:
        raise argparse.ArgumentTypeError('must be positive')
    return value


def main():
    parser = argparse.ArgumentParser(description='Restore vectors and manage the ICJ graph. API calls are explicit.')
    commands = parser.add_subparsers(dest='command', required=True)
    download = commands.add_parser('fetch')
    download.add_argument('--data-dir', default='/dataset')
    download.add_argument('--revision', default=os.getenv('ICJ_REVISION', 'main'))
    download.add_argument('--vectors', action='store_true')
    commands.add_parser('inspect')
    for name in ['vectors', 'embed', 'indexes']:
        child = commands.add_parser(name)
        child.add_argument('--targets', nargs='+', choices=list(TARGETS), default=list(TARGETS))
        if name == 'indexes':
            child.add_argument('--property')
            child.add_argument('--timeout', type=positive, default=3600)
        else:
            child.add_argument('--batch-size', type=positive, default=64)
            child.add_argument('--limit', type=positive, help='Maximum newly added vectors per target in this invocation; omit to process all.')
            if name == 'vectors':
                child.add_argument('--data-dir', default='/dataset')
            else:
                child.add_argument('--property', default='api_embedding')
    args = parser.parse_args()
    if args.command == 'fetch':
        fetch(args.data_dir, args.revision, args.vectors)
        return
    graph = Graph()
    try:
        if args.command == 'inspect':
            print(json.dumps(inspect_graph(graph), indent=2))
            return
        with locked_state(os.getenv('STATE_DIR', '/state')) as cache:
            if args.command == 'vectors':
                load_vectors(graph, args.targets, args.data_dir, args.batch_size, args.limit)
            elif args.command == 'embed':
                embed_graph(graph, args.targets, cache, args.property, args.batch_size, args.limit)
            else:
                create_indexes(graph, args.targets, args.property, args.timeout)
    finally:
        graph.close()


if __name__ == '__main__':
    main()