RysoAI's picture
Clean package: terminal code only, neutral names, no comments
da32fba verified
Raw History Blame Contribute Delete
1.46 kB
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)