-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
278 lines (249 loc) · 10.9 KB
/
main.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import os
import re
from dotenv import load_dotenv
load_dotenv()
from interact import Interaction
from llmapi import Claude, GPT
fname = "gpt-base-drop-last.txt"
# You are also given the 'appendo' relation which takes 3 parameters: l, s, and ls. The relation is satisfied when l and s are appended, equate to ls.
# This is the definition of appendo in miniKanren:
# (define-relation (appendo ab c abc)
# (conde
# ((== '() ab) (== c abc))
# ((fresh (a b bc)
# (== `(,a . ,b) ab)
# (== `(,a . ,bc) abc)
# (appendo b c bc)))))
init_prompt = """
You are a helpful assistant that is helping to guide the search process of a constraint logic programming language called miniKanren, embedded in the Racket programming language.
Information about the current step and current relational constraints are provided at each step.
One of the relations you are given is `==` which equates two logic variables.
You are also given the relation `evalo` which takes: `expr` and `value`. Evalo will take a program expression and evaluate it to a value.
Here is the definition of `evalo`, `lookupo`, and `evalo-list` in miniKanren:
```racket
(define-relation (eval-expo expr env value)
(conde
((fresh (body)
(== `(lambda ,body) expr) ;; expr is a procedure definition
(== `(closure ,body ,env) value)))
((== `(quote ,value) expr)) ;; expr is a literal constant
((fresh (a*)
(== `(list . ,a*) expr) ;; expr is a list operation
(eval-listo a* env value)))
((fresh (a d va vd)
(== `(cons ,a ,d) expr) ;; expr is a cons operation
(== `(,va . ,vd) value)
(eval-expo a env va)
(eval-expo d env vd)))
((fresh (index)
(== `(var ,index) expr) ;; expr is a variable
(lookupo index env value)))
((fresh (c va vd)
(== `(car ,c) expr) ;; expr is a car operation
(== va value)
(eval-expo c env `(,va . ,vd))))
((fresh (c va vd)
(== `(cdr ,c) expr) ;; expr is a cdr operation
(== vd value)
(eval-expo c env `(,va . ,vd))))
((fresh (rator rand arg env^ body)
(== `(app ,rator ,rand) expr) ;; expr is a procedure application
(eval-expo rator env `(closure ,body ,env^))
(eval-expo rand env arg)
(eval-expo body `(,arg . ,env^) value)))))
;; Lookup the value a variable is bound to.
;; Variables are represented namelessly using relative De Bruijn indices.
;; These indices are encoded as peano numerals: (), (s), (s s), etc.
(define-relation (lookupo index env value)
(fresh (arg e*)
(== `(,arg . ,e*) env)
(conde
((== '() index) (== arg value))
((fresh (i* a d)
(== `(s . ,i*) index)
(== `(,a . ,d) e*)
(lookupo i* e* value))))))
;; This helper evaluates arguments to a list construction.
(define-relation (eval-listo e* env value)
(conde
((== '() e*) (== '() value))
((fresh (ea ed va vd)
(== `(,ea . ,ed) e*)
(== `(,va . ,vd) value)
(eval-expo ea env va)
(eval-listo ed env vd)))))
;; Evaluation of a program expression with an empty environment.
(define (evalo expr value) (eval-expo expr '() value))
```
When prompted with the text "[u]ndo or choice number>", you will either respond with a `u` or a positive integer (for example: 1, 2, 3, 4, etc.).
"""
model_prompt = """
You are a helpful assistant that is helping to guide the search process of a constraint logic programming language called miniKanren, embedded in the Racket programming language.
Information about the current step and current relational constraints are provided at each step.
One of the relations you are given is `==` which equates two logic variables.
You are also given the relation `evalo` which takes: `expr` and `value`. Evalo will take a program expression and evaluate it to a value.
Here is the definition of `evalo-expro`, `lookupo`, `evalo-list`, and `evalo` in miniKanren:
```racket
;; This is an interpreter for a simple Lisp. Variables in this language are
;; represented namelessly, using De Bruijn indices.
;; Because it is implemented as a relation, we can run this interpreter with
;; unknowns in any argument position. If we place unknowns in the `expr`
;; position, we can synthesize programs.
(define-relation (eval-expo expr env value)
(conde
((fresh (body)
(== `(lambda ,body) expr) ;; expr is a procedure definition
(== `(closure ,body ,env) value)))
((== `(quote ,value) expr)) ;; expr is a literal constant
((fresh (a*)
(== `(list . ,a*) expr) ;; expr is a list operation
(eval-listo a* env value)))
((fresh (a d va vd)
(== `(cons ,a ,d) expr) ;; expr is a cons operation
(== `(,va . ,vd) value)
(eval-expo a env va)
(eval-expo d env vd)))
((fresh (index)
(== `(var ,index) expr) ;; expr is a variable
(lookupo index env value)))
((fresh (c va vd)
(== `(car ,c) expr) ;; expr is a car operation
(== va value)
(eval-expo c env `(,va . ,vd))))
((fresh (c va vd)
(== `(cdr ,c) expr) ;; expr is a cdr operation
(== vd value)
(eval-expo c env `(,va . ,vd))))
((fresh (rator rand arg env^ body)
(== `(app ,rator ,rand) expr) ;; expr is a procedure application
(eval-expo rator env `(closure ,body ,env^))
(eval-expo rand env arg)
(eval-expo body `(,arg . ,env^) value)))))
;; Lookup the value a variable is bound to.
;; Variables are represented namelessly using relative De Bruijn indices.
;; These indices are encoded as peano numerals: (), (s), (s s), etc.
(define-relation (lookupo index env value)
(fresh (arg e*)
(== `(,arg . ,e*) env)
(conde
((== '() index) (== arg value))
((fresh (i* a d)
(== `(s . ,i*) index)
(== `(,a . ,d) e*)
(lookupo i* e* value))))))
;; This helper evaluates arguments to a list construction.
(define-relation (eval-listo e* env value)
(conde
((== '() e*) (== '() value))
((fresh (ea ed va vd)
(== `(,ea . ,ed) e*)
(== `(,va . ,vd) value)
(eval-expo ea env va)
(eval-listo ed env vd)))))
;; Evaluation of a program expression with an empty environment.
(define (evalo expr value) (eval-expo expr '() value))
```
When prompted with the text "[u]ndo, or choices (period-separated)>", you will make a decision, responding with a `u` to undo or period-separated positive integers (for example: 1, 2, 2.1.2, etc.).
Note that the choices are 1-indexed.
Given multiple `evalo` relations means they should be solved in conjunction by the same `q`.
Use the `var` expression to access function arguments.
First, explain the problem the query is trying to solve.
Then, think step by step about how to solve it.
You will respond with the decision(s) in a new line.
"""
# Then, predict the possible values for the logic variables.
# You should first explain the problem you are trying to solve, then explain the decision you want to make and the reasoning behind it.
# Your goal is to construct a quine: a program that evaluates to itself.
# ```racket
# '(((app (lambda (list (quote app) (var ())
# (list (quote quote) (var ()))))
# (quote (lambda (list (quote app) (var ())
# (list (quote quote) (var ())))))))))
# ```
steps_matcher = re.compile(r"Steps: (\d+)")
valid_input = re.compile(r"(u(ndo)?|(\d.)*\d)")
# response = client.chat.completions.create(
# model="gpt-3.5-turbo",
# # consider outputing to JSON object?
# messages=[
# {"role": "system", "content": prompt.strip() },
# {"role": "user", "content": "Evaluate this prompt..." }
# ]
# )
#
# print(response.choices[0].message.content)
def load_few_shots():
messages = []
with open("few_shots.txt", "r") as f:
all = f.read().split("==")
for i in range(0, len(all), 2):
messages.append({"role": "user", "content": all[i].strip()})
messages.append({"role": "assistant", "content": all[i + 1].strip()})
return messages
few_shot_messages = load_few_shots()
few_shots_as_single_message = "\n".join(map(lambda x: x["content"], few_shot_messages))
def base_message():
# return few_shot_messages
return []
model_prompt = init_prompt
model = "gpt"
llm = GPT(model_prompt.strip()) if model == "gpt" else Claude(model_prompt.strip())
base_messages = base_message()
if __name__ == "__main__":
print("Starting interaction\n============================\n\n")
running = True
messages = base_messages
steps_taken = []
with open(os.path.join("logs", fname), "w") as f:
with Interaction() as env:
i = 0
while i < 100:
messages = base_messages
prompt = env.read_prompt()
print(prompt)
f.write(prompt)
if "Goodbye" in prompt:
break
elif "Number of results" in prompt or "Enter" in prompt:
match = steps_matcher.search(prompt)
if match:
steps = int(match.group(1))
steps_taken.append(steps)
print(
f"\n############################\nSteps taken so far: {steps_taken}"
)
print(f"Took {steps} steps\n############################\n\n")
messages.clear()
messages.extend(base_messages)
env.send("1\n")
continue
messages.append({"role": "user", "content": prompt})
# messages.append(
# {
# "role": "user",
# "content": few_shots_as_single_message + "\n" + prompt,
# }
# )
model_message = llm.get_response(messages)
print(f"Model responds:\n> {model_message.content}\n=====\n")
f.write("\n================================\n")
f.write(model_message.content)
f.write("\n================================\n")
messages.append(model_message.get_message())
to_send = (model_message.content or "u").split("\n")
while len(to_send) > 0 and valid_input.match(to_send[-1]) is None:
to_send.pop(-1)
if len(to_send) == 0:
to_send = ["u"]
to_send = valid_input.match(to_send[-1]).group(1)
env.send(to_send)
print(f"Sent: {to_send}")
# messages quite long, let's trim some (keep the initial prompt)
# if len(messages) >= 2:
# messages.pop(-1)
# messages.pop(-1)
i += 1
print("\n\nEnd of interaction. Goodbye.")
print("steps taken:")
print(steps_taken)
f.write(f"steps taken: {steps_taken}")