-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive.py
113 lines (106 loc) · 3.27 KB
/
interactive.py
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
import sys, os
import exrex
# import matplotlib
# matplotlib.use('tkAgg')
from Levenshtein import jaro_winkler as levenshtein
# from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import random
import re
from termcolor import colored
import traceback
from process import (
GPT3, MockGPT3,
read_cache,
set_seed
)
keys = [
'max_tokens',
'temperature',
'top_p',
'stop',
'n',
'stream',
'logprobs',
'echo',
'presence_penalty',
'frequency_penalty',
'best_of',
'engine',
]
def match_key(_key):
return sorted(keys, key=lambda x: -levenshtein(x, _key))[0]
def run(gpt):
kwargs = {
'engine': 'davinci',
'max_tokens': 10,
'stop': ["\n", "\r", "\n\n"],
'logprobs': 100,
'temperature': 0.,
}
while True:
k = None
while k not in list('qrst'):
k = input('Command ([q]uit/[r]un/update [s]ettings/enter [t]ext): ')
if k == 's':
try:
kv = input('Settings to update: ').split(' ')
_key = kv[0]
value = ' '.join(kv[1:])
except Exception as e:
print('ERROR: %s' % e)
continue
if _key == 'del':
value = match_key(value)
if value not in kwargs:
print(f'{value} not set')
else:
del kwargs[value]
print(f'Unset {value}')
else:
# vals = [levenshtein(x, _key) for x in keys]
# print(list(sorted(zip(keys, vals), key=lambda x: -x[1])))
key = match_key(_key)
if key == 'stop':
try:
value = eval(value)
except Exception as e:
print('ERROR: %s' % e)
continue
try:
value = float(value)
if (value).is_integer():
value = int(value)
except (TypeError, ValueError):
pass
try:
kwargs[key] = value
print(f'Set {key} to {value}')
except Exception as e:
print('ERROR: %s' % e)
print('== Settings ==')
for key, value in kwargs.items():
print(f'{key}: {value}')
elif k == 't':
# s = input('Enter text:\n')
print('Enter text:')
s = sys.stdin.read()
# s = """“Thank you for doing business at our house, and I hope to see you again!” I left not a little unnerved, and still woozy from the **leeches**. “Come on,” said Nepthys, “you could use a drink.”\nQ: What are appropriate substitutes for **leeches** in the above text?\nA: bloodsucker, parasite, bloodletting, bleeding, worm, blood sucker, blood let, insect\n\nElectronic theft by foreign and industrial spies and disgruntled employees is costing U.S. companies billions and eroding their international competitive advantage. That was the message delivered by government and private security experts at an all-day conference on corporate **electronic** espionage. "Hostile and even friendly nations routinely steal information from U.S. companies and share it with their own companies," said Noel D. Matchett, a former staffer at the federal National Security Agency and now president of Information Security Inc., Silver Spring, Md.\nQ: What are appropriate substitutes for **electronic** in the above text?\nA:"""
gpt.complete(
prompt=s,
**kwargs
)
elif k == 'r':
gpt.run_staged_queries()
elif k == 'q':
break
def main(argv):
GPT = GPT3 if 'submit' in argv else MockGPT3
print('Using ' + GPT.__name__)
cache_fname = f'cache_{GPT.__name__}.jsonl'
cache = read_cache(cache_fname)
gpt = GPT(cache)
run(gpt)
if __name__ == '__main__':
main(sys.argv)