-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdo.py
245 lines (202 loc) · 9.53 KB
/
do.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
from openai import OpenAI
import safety
import random
import ast
client : str = "give me a proper value in GPT.py"
section = ["\n\n", "\n\n\n"]
def cont(token_count : int, text : str) -> str: # short for "continuation"
if token_count <= 0: raise ValueError("token_count must be positive")
if token_count != int(token_count): raise ValueError("token_count must be integer")
safety.check(text.__len__(), token_count)
return client.completions.create(
model = "gpt-3.5-turbo-instruct",
prompt = text,
temperature = 1,
max_tokens = token_count,
top_p = 1,
frequency_penalty = 0,
presence_penalty = 0
).choices[0].text
# Trailing "\n" after for example a colon generates "\n ..." (2 total),
# but trailing "\n\n" generates "\n ..." (3 total).
# beta.openai.com/playground also has this problem
# as a result some the features include extra .strip() or [1:]
def ask(max_tokens : int, question : str, question_prompt : str = "Question",
answer_prompt : str = "Answer", answer_start : str = None,
question_prompt_symbol = ":", answer_prompt_symbol : str = ":") -> str:
return cont(max_tokens, question_prompt + question_prompt_symbol + section[0] +
question + section[1] + answer_prompt + answer_prompt_symbol +
((section[0] + answer_start) if answer_start else "\n")).strip()
# Specific question, answer here "(output) --> answer"
def query_quote(token_count : int, query_text : str) -> str:
result = cont(token_count, query_text + " \"")
response = ""
for char in result:
response += char
if char == "\"": break
return response.strip()
# Question: Two pieces of text follow.
#
# 1. text block
#
# 2. text block
#
#
# From only the answers "Yes" and "No", do the two pieces of text mean the same thing?
#
# (output) --> answer
def same_meaning(text : str, text2 : str) -> bool:
result = ask(1, "Two pieces of text follow" + section[0] + "1. " + text +
section[0] + "2. " + text2, answer_prompt =
"From only the answers \"Yes\" and \"No\", do the two pieces of text mean the same thing",
answer_prompt_symbol = "?")
if result == "Yes":
return True
else:
if result == "No": return False
else: return None # the boolean form of None is False
def simplify(token_count : int, text : str) -> str:
return ask(token_count, text, question_prompt = "Text", answer_prompt =
"Repeated but mildly summarized, the resulting text should be shorter")
def simplify_iter(simplification_steps : int,
max_tokens_per_simplification : int, text : str) -> str:
for _ in range(simplification_steps):
text = simplify(max_tokens_per_simplification, text)
return text
# for debugging
def simplify_iter_verbose(simplification_steps : int,
max_tokens_per_simplification : int, text : str) -> str:
for _ in range(simplification_steps):
text += "\n\n" + simplify(max_tokens_per_simplification, text)
return text
# "stool, pint glass, door, pint glass, person, window, person" --> "pub"
def location_from_entities(max_tokens : int, entities : str) -> str:
return query_quote(max_tokens, 0, "The following items are nearby \"" + \
entities + "\". Therefore my location is a\\the")
# "tall, quiet, older gentleman who loves to play golf"
def random_personality() -> str:
traits = ("friendly", "curious", "analytical", "creative", "angry", "confrontational",
"opinionated", "tactile", "merry", "boisterous", "self-absorbed", "speedy", "introspective",
"empathetic", "understanding", "resilient ", "humble", "impulsive", "happy",
"adventurous", "risk-adverse", "reliable", "ambitious", "stoic", "shy", "nervous",
"cautious", "dangerous", "charming", "pleasant", "attractive", "egotistic",
"determined", "energetic", "enthusiastic", "generous", "meticulous", "aggravating",
"optimistic", "pessimistic", "rebellious", "authoritarian", "skeptical", "outspoken",
"doubtful", "questioning", "spontaneous", "stubborn", "thoughtful", "reflective")
chosen_traits = []
while True:
chosen_traits += random.choice(traits)
if random.randint(0, 2) == 0: break
result = "A "
for trait in chosen_traits:
result.append(trait)
if result != chosen_traits[-1]:
result.append(" ")
result.append(" " + random.choice("man", "women", "individual"))
return result
# ("learn'ed scholar", "history book") --> "a wealth of insight into the ways of ..."
# ("history book", "learn'ed scholar") --> "is always attentive towards me"
def opinion(subject: str, of: str) -> str:
return ask(200, "Imagine a fiction, in this fiction \"" +
subject + "\" has an opinion of \"" + of + "\", state the opinion").strip()
def run_code(safety : set, max_tokens : int, description : str):
print("\nPython Code:\n")
code = ask(max_tokens,
"Write Python code to perform the given task, no notes or comments, just code." +
section[0] + description, question_prompt = "Instructions", answer_prompt = "Code")
print("\"\n" + code + "\n\"")
run_code_core(safety, code)
def run_code_core(safety: set, code: str):
if not safety:
raise ValueError("\"safety\" should contain items")
for item in safety:
if item != int(item):
raise ValueError("\"safety\" should contain only integers")
if item < 0 or item > 9:
raise ValueError("items in \"safety\" should be in the range 0 to 9")
if list(safety).count(item) > 1:
raise ValueError("\"safety\" should not contain any duplicates")
permitted_features = (
# 0 - safe features
(ast.Name, ast.Await, ast.Pass, ast.Nonlocal,
# operations
ast.UnaryOp, ast.BinOp, ast.UAdd, ast.USub, ast.Add,
ast.Sub, ast.AugAssign, ast.keyword,
# expressions and variables
ast.Expr, ast.alias, ast.Assign, ast.Load, ast.Store, ast.Del,
# comparisons
ast.Compare, ast.Eq, ast.NotEq, ast.Lt, ast.LtE, ast.Gt, ast.GtE,
# bools
ast.BoolOp, ast.And, ast.Or),
# 1 - integers, strings and collections, unlimited memory
(ast.Constant, # integers and strings
# collection types
ast.Set, ast.Dict, ast.Tuple, ast.List,
# access collections
ast.Slice, ast.Subscript,
# integer operations\functions
ast.Mod, ast.Pow),
# 2 - functions and flow control, except calling functions - unlimited recursion
(ast.GeneratorExp,
# create functions and lambdas
ast.FunctionDef, ast.Lambda, ast.AsyncFunctionDef,
# return from functions, lambdas, and loops
ast.Return, ast.Yield, ast.YieldFrom, ast.Break, ast.Continue,
# loops
ast.While, ast.For, ast.AsyncFor, ast.ListComp, ast.DictComp, ast.SetComp,
# the arguments of a function
ast.arguments, ast.arg),
# 3 - modules, but also needed for classes - module use seems dangerous
(ast.Module,),
# 4 - classes
(ast.Attribute, ast.ClassDef),
# 5 - function calls - unlimited recursion and calling of built-in global functions
(ast.Call,),
# 6 - unlimited tampering with built-in globals
(ast.Global,),
# 7 - exceptions
(ast.Raise, ast.Try, ast.ExceptHandler, ast.With, ast.AsyncWith),
# 8 - importing - unlimited breaking of the computer
(ast.Import, ast.ImportFrom),
# 9 - conditions
(ast.If,)
)
class SafetyVisitor(ast.NodeVisitor):
def __init__(self, allowed_nodes):
self.allowed_nodes = allowed_nodes
self.safe = True
self.invalid_node = None
def generic_visit(self, node):
if not isinstance(node, self.allowed_nodes):
self.safe = False
self.invalid_node = node
return
super().generic_visit(node)
allowed_nodes = tuple(node for index in safety for node in permitted_features[index])
visitor = SafetyVisitor(allowed_nodes)
print("\nCode execution started")
restricted_globals = {k: v for k, v in globals().items() if
# built-in
k not in ('exec', 'eval', 'open', 'subprocess', 'compile',
'globals', 'locals', 'setattr', 'getattr', 'delattr', 'exit', 'quit',
'help', 'memoryview', 'property', 'super', 'vars', 'dir', 'input',
# libraries
'OpenAI', 'safety', 'ast', 'client', 'random',
# my objects
'section', 'cont', 'ask', 'query_quote',
'same_meaning', 'simplify', 'simplify_iter', 'simplify_iter_verbose',
'location_from_entities', 'random_personality', 'opinion',
'run_code', 'run_code_core',
# dunder
'__import__', '__name__', '__doc__', '__package__', '__loader__',
'__spec__', '__file__', '__cached__', '__builtins__')}
try:
parsed_code = ast.parse(code)
except SyntaxError as e:
print("syntax error: " + e.__str__())
visitor.visit(parsed_code)
if visitor.safe:
exec(code, restricted_globals) # TODO "input()" still works :/
else:
print("feature \"" + type(visitor.invalid_node).__name__ + "\" is not allowed")
print("Code execution finished\n")