File size: 1,457 Bytes
da32fba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json, os, sys, urllib.parse, urllib.request
URL, PROV, ARGS = (sys.argv[1], os.environ.get('MOSS_PROVIDER', 'ddgs'), sys.argv[2:])
if ARGS[:1] == ['--local']:
    PROV, ARGS = ('local', ARGS[1:])
NAMES = {'ddgs': 'DuckDuckGo', 'serper': 'Google', 'local': 'the local index'}

def ask(q):
    try:
        with urllib.request.urlopen(f'{URL}/ask?q={urllib.parse.quote(q)}&provider={PROV}', timeout=180) as r:
            d = json.load(r)
    except Exception as ex:
        print(f'the model service stopped answering ({type(ex).__name__}); run ./moss.sh start')
        return
    if d.get('tier') == 1:
        if d.get('lead'):
            print(d['lead'].replace('**', ''))
        print('  “' + d['cited'] + '”' + ('  (stated by %d sources)' % d['support'] if (d.get('support') or 0) >= 2 else ''))
        print('  ' + str(d.get('url')))
    elif d.get('tier') == 3 and (not d.get('cited')):
        print('No results.' + ('  The local index is empty or missing: run ./moss.sh index <folder> first.' if PROV == 'local' else ''))
    else:
        print('No verified answer.')
if ARGS:
    ask(' '.join(ARGS))
else:
    print(f'RysoAI TinyQA-1 Moss, answering from {NAMES[PROV]}. Type a question, or press Enter on an empty line to quit.')
    while True:
        try:
            q = input('\n? ').strip()
        except (EOFError, KeyboardInterrupt):
            print()
            break
        if not q:
            break
        ask(q)