OpenAI Demo
Converting Python to Ruby with OpenAI Codex - YouTube
Pen.el Engine
http://github.com/semiosis/engines/blob/master/engines/openai-codex.engine
Pen.el Prompt
http://github.com/semiosis/prompts/blob/master/prompts/transpile-3.prompt

Summary

I convert the OpenAI quine relay into some imaginary code (WIP).

 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
ouroboros
uroboros
    An ancient symbol depicting a serpent or
    dragon eating its own tail.

    Originating in ancient Egyptian
    iconography, the ouroboros entered Western
    tradition via Greek magical tradition and
    was adopted as a symbol in Gnosticism and
    Hermeticism and most notably in alchemy.

ouroboros program
uroboros program
quine-relays
    The quine concept can be extended to
    multiple levels of recursion, originating
    "ouroboros programs", or quine-relays.

    This should not be confused with
    multiquines.

multiquine
biquine
    A multiquine consisting of 2 languages (or
    biquine) would be a program which:

    - When run, is a quine in language X.
    - When supplied with a user-defined
      command line argument, would print a
      second program in language Y.
    - Given the second program in language Y,
      when run normally, would also be a quine
      in language Y.
    - Given the second program in language Y,
      and supplied with a user-defined command
      line argument, would produce the
      original program in language X.
    - A biquine could then be seen as a set of
      two programs, both of which are able to
      print either of the two, depending on
      the command line argument supplied.

The Quine: Initial Python source

 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
#!/usr/bin/env python
#
# A program which calls OpenAI with its own source code.
#
# Implemented in two languages:
#
# In Python, uses the "http.client" and "subprocess" libraries and uses Python 3 semantics. In Ruby, uses httparty and system (does NOT use open3).

import http.client
import json
import subprocess

here = __file__

api_key = open('openai_api_key.txt').read().strip()
source = open(here).read() # Read our own source code

my_language = "python"
next_Language = "ruby"

# Print with trailing newline.
print("Hello from " + my_language + ". I'm going to rewrite myself in " next_language + ". Stand by...")

E_O_D = "E" + "O" + "D"  # Avoid the string literal here

# Make request to OpenAI to translate to next language

prompt = "## The program in " + my_language + "\n\ncat > program_in_" + my_language + " <<" + E_O_D + "\n" + source + "\n" + E_O_D "\n\n## The exact same program in " + next_language + "\n\ncat > program_in_" + next_language + " <<" + E_O_D + "\n"

authorization = "Bearer " + api_key

# Create the payload
payload = {
    "stop": E_O_D,
    "max_tokens": 1000,
    "temperature": 0,
    "prompt": prompt
}

# Dump payload to JSON
payload = json.dumps(payload)

conn = http.client.HTTPSConnection("api.openai.com")
conn.request("POST", "/v1/engines/davinci-codex/completions", payload, {
    "Content-type": "application/json",
    "Authorization": authorization
})

response = conn.getresponse()
translated = json.loads(response.read())['choices'][0]['text']

path = "program_in_" next_language + ".rb"
f = open(path, "w")
f.write(translated.strip())
f.write("\n")
f.close()

subprocess.call(["pygmentize", path])

# Now launch next iteration
subprocess.call([next_language, path])

Interesting features

1
E_O_D = "E" + "O" + "D"  # Avoid the string literal here

The string literal is most likely avoided to prevent Codex from becoming disoriented during the transpilation when it sees two ‘EOD’s.

Pen.el

Codex Engine

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
title: OpenAI Codex
lm-command: "openai-complete.sh"
model: davinci-codex
modes:
- search
- classification
specialities:
- code
min-tokens: 64
max-tokens: 1000

Codex Transpilation Prompt

 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
title: "transpile"
doc: "Given some code and a target language, transpile into that language"
prompt-version: 1
engine: openai-codex
prompt: |+
    ## The program in <from language>

    cat program_in_<from language> <<EOD
    <code>
    EOD

    ## The exact same program in <to-language>

    cat program_in<to language> <<EOD    
n-generate: 5
temperature: 0
max-tokens: 1000
top-p: 1.0
best-of: 1
cache: on
vars:
- code
- "from language"
- "to language"
var-defaults:
- "(pen-selected-text)"
- "(pen-detect-language-ask)"
postprocessors:
- sed '$d'
examples:
- "print(\"Hello world with empathy\")"
- "Python"
- "Ruby"
n-test-runs: 5
n-collate: 1
n-completions: 10
new-document: yes
external-related:
- "https://www.youtube.com/watch?v=Iq3rDFZOorw"

The interactive function

  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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
pf-transpile is an interactive function without a source file.

Signature
(pf-transpile &optional CODE FROM-LANGUAGE TO-LANGUAGE &key NO-SELECT-RESULT)

Documentation
transpile
Given some code and a target language, transpile into that language

path:
- /home/shane/source/git/spacemacs/prompts/prompts/transpile.prompt

external-related
:- https://www.youtube.com/watch?v=Iq3rDFZOorw

examples:
- print("Hello world with empathy")
- Python
- Ruby

var-defaults:
- (pen-selected-text)
- (pen-detect-language-ask)

Key Bindings
This command is not in any keymaps.

References
Could not find source file.

Find all references Functions used by pf-transpile

Debugging
Enable tracing
Disassemble Forget

;; Could not find source code, showing raw function object.
(lambda
  (&optional code from-language to-language &rest --cl-rest--)
  "transpile\nGiven some code and a target language, transpile into that language\n\npath:\n- /home/shane/source/git/spacemacs/prompts/prompts/transpile.prompt\n\nexternal-related\n:- https://www.youtube.com/watch?v=Iq3rDFZOorw\n\nexamples:\n- print(\"Hello world with empathy\")\n- Python\n- Ruby\n\nvar-defaults:\n- (pen-selected-text)\n- (pen-detect-language-ask)\n\n(fn &optional CODE FROM-LANGUAGE TO-LANGUAGE &key NO-SELECT-RESULT)"
  (interactive
   (list
    (if "(pen-selected-text)"
        (eval-string "(pen-selected-text)")
      (read-string-hist "code: " "print(\"Hello world with empathy\")"))
    (if "(pen-detect-language-ask)"
        (eval-string "(pen-detect-language-ask)")
      (read-string-hist "from language: " "Python"))
    (if nil
        (eval-string "")
      (read-string-hist "to language: " "Ruby"))))
  (let*
      ((no-select-result
        (car
         (cdr
          (plist-member --cl-rest-- ':no-select-result)))))
    (progn
      (let
          ((--cl-keys-- --cl-rest--))
        (while --cl-keys--
          (cond
           ((memq
             (car --cl-keys--)
             '(:no-select-result :allow-other-keys))
            (setq --cl-keys--
                  (cdr
                   (cdr --cl-keys--))))
           ((car
             (cdr
              (memq ':allow-other-keys --cl-rest--)))
            (setq --cl-keys-- nil))
           (t
            (error "Keyword argument %s not one of (:no-select-result)"
                   (car --cl-keys--))))))
      (cl-block pf-transpile
        (let
            ((is-interactive
              (interactive-p)))
          (pen-force-custom
           (cl-macrolet
               ((expand-template
                 (string-sym)
                 `(--> ,string-sym
                    (pen-onelineify it)
                    (pen-expand-template-keyvals it subprompts)
                    (pen-expand-template it vals)
                    (pen-expand-template-keyvals it var-keyvals-slugged)
                    (pen-expand-template-keyvals it var-keyvals)
                    (pen-unonelineify it))))
             (let*
                 ((do-pen-update
                   (pen-var-value-maybe 'do-pen-update))
                  (pen-sh-update
                   (or
                    (>=
                     (prefix-numeric-value current-global-prefix-arg)
                     4)
                    (pen-var-value-maybe 'pen-sh-update)
                    do-pen-update))
                  (cache
                   (and
                    (not do-pen-update)
                    (pen-var-value-maybe 'cache)))
                  (final-flags
                   (or
                    (pen-var-value-maybe 'flags)
                    nil))
                  (final-flags
                   (if final-flags
                       (mapconcat
                        (lambda
                          (s)
                          (concat "<" s ">"))
                        (vector2list final-flags)
                        " ")))
                  (final-is-info
                   (or
                    (pen-var-value-maybe 'do-etv)
                    (pen-var-value-maybe 'is-info)
                    nil))
                  (final-start-yas
                   (or
                    (pen-var-value-maybe 'start-yas)
                    nil))
                  (final-end-yas
                   (or
                    (pen-var-value-maybe 'yas)
                    (pen-var-value-maybe 'end-yas)
                    nil nil))
                  (subprompts nil)
                  (subprompts
                   (if subprompts
                       (ht->alist
                        (-reduce 'ht-merge
                                 (vector2list subprompts)))))
                  (final-prompt "## The program in <from language>\n\ncat program_in_<language> <<EOD\n<code>\nEOD\n\n## The exact same program in <to-language>\n\n")
                  (final-prompt
                   (if final-start-yas
                       (pen-yas-expand-string final-prompt)
                     final-prompt))
                  (vals
                   (mapcar 'str
                           (if
                               (not is-interactive)
                               (progn
                                 (cl-loop for sym in
                                          '(code from-language to-language)
                                          for iarg in
                                          '((if "(pen-selected-text)"
                                                (eval-string "(pen-selected-text)")
                                              (read-string-hist "code: " "print(\"Hello world with empathy\")"))
                                            (if "(pen-detect-language-ask)"
                                                (eval-string "(pen-detect-language-ask)")
                                              (read-string-hist "from language: " "Python"))
                                            (if nil
                                                (eval-string "")
                                              (read-string-hist "to language: " "Ruby")))
                                          collect
                                          (let*
                                              ((initval
                                                (eval sym)))
                                            (if
                                                (and
                                                 (not initval)
                                                 iarg)
                                                (eval iarg)
                                              initval))))
                             (cl-loop for v in
                                      '(code from-language to-language)
                                      until
                                      (eq v '&key)
                                      collect
                                      (eval v)))))
                  (vals
                   (cl-loop for tp in
                            (-zip-fill nil vals 'nil)
                            collect
                            (let*
                                ((v
                                  (car tp))
                                 (pp
                                  (cdr tp)))
                              (if pp
                                  (pen-sn pp v)
                                v))))
                  (final-prompt
                   (if nil
                       (if
                           (< 0
                              (length vals))
                           (concat
                            (pen-awk1 final-prompt)
                            (string-replace "{}"
                                            (str
                                             (car
                                              (last vals)))
                                            nil))
                         (concat
                          (pen-awk1 final-prompt)
                          nil))
                     final-prompt))
                  (var-keyvals
                   (-zip
                    '("code" "from language" "to language")
                    vals))
                  (var-keyvals-slugged
                   (-zip
                    '("code" "from-language" "to-language")
                    vals))
                  (final-n-collate
                   (or
                    (pen-var-value-maybe 'n-collate)
                    1))
                  (final-n-completions
                   (expand-template
                    (str
                     (or
                      (pen-var-value-maybe 'n-completions)
                      10))))
                  (final-max-tokens
                   (expand-template
                    (str
                     (or
                      (pen-var-value-maybe 'max-tokens)
                      1000))))
                  (final-temperature
                   (expand-template
                    (str
                     (or
                      (pen-var-value-maybe 'temperature)
                      0))))
                  (final-mode
                   (expand-template
                    (str
                     (or
                      (pen-var-value-maybe 'mode)
                      nil))))
                  (final-top-p
                   (expand-template
                    (str
                     (or
                      (pen-var-value-maybe 'top-p)
                      1.0))))
                  (final-top-k
                   (expand-template
                    (str
                     (or
                      (pen-var-value-maybe 'top-k)
                      nil))))
                  (final-stop-sequences
                   (cl-loop for stsq in
                            (or
                             (pen-var-value-maybe 'stop-sequences)
                             '("###<long>###"))
                            collect
                            (expand-template stsq)))
                  (final-stop-patterns
                   (or
                    (pen-var-value-maybe 'stop-patterns)
                    '("^Input:")))
                  (final-stop-sequence
                   (expand-template
                    (str
                     (or
                      (pen-var-value-maybe 'stop-sequence)
                      "###<long>###"))))
                  (final-prompt
                   (expand-template final-prompt))
                  (final-prompt
                   (pen-log-final-prompt
                    (if nil
                        (sor
                         (pen-snc nil final-prompt)
                         (concat "prompt-filter " nil " failed."))
                      final-prompt)))
                  (final-prompt
                   (if final-end-yas
                       (pen-yas-expand-string final-prompt)
                     final-prompt))
                  (final-prompt
                   (chomp final-prompt))
                  (prompt-end-pos
                   (or
                    (byte-string-search "<:pp>" final-prompt)
                    (string-bytes final-prompt)))
                  (final-prompt
                   (string-replace "<:pp>" "" final-prompt))
                  (final-prompt
                   (chomp final-prompt))
                  (shcmd
                   (pen-log
                    (s-join " "
                            (list
                             (sh-construct-envs
                              `(("PEN_PROMPT" ,(pen-encode-string final-prompt))
                                ("PEN_LM_COMMAND" ,"openai-complete.sh")
                                ("PEN_MODEL" ,nil)
                                ("PEN_MAX_TOKENS" ,final-max-tokens)
                                ("PEN_TEMPERATURE" ,final-temperature)
                                ("PEN_MODE" ,final-mode)
                                ("PEN_STOP_SEQUENCE" ,(pen-encode-string final-stop-sequence))
                                ("PEN_TOP_P" ,final-top-p)
                                ("PEN_TOP_K" ,final-top-k)
                                ("PEN_FLAGS" ,final-flags)
                                ("PEN_CACHE" ,cache)
                                ("PEN_N_COMPLETIONS" ,final-n-completions)
                                ("PEN_END_POS" ,prompt-end-pos)))
                             "lm-complete"))))
                  (resultsdirs
                   (cl-loop for i in
                            (number-sequence 1 final-n-collate)
                            collect
                            (progn
                              (message
                               (concat "pf-transpile" " query "
                                       (int-to-string i)
                                       "..."))
                              (let
                                  ((ret
                                    (pen-prompt-snc shcmd i)))
                                (message
                                 (concat "pf-transpile" " done "
                                         (int-to-string i)))
                                ret))))
                  (results
                   (-uniq
                    (flatten-once
                     (cl-loop for rd in resultsdirs collect
                              (if
                                  (sor rd)
                                  (->>
                                      (glob
                                       (concat rd "/*"))
                                    (mapcar 'e/cat)
                                    (mapcar
                                     (lambda
                                       (r)
                                       (cl-loop for stsq in final-stop-sequences do
                                                (let
                                                    ((matchpos
                                                      (pen-string-search stsq r)))
                                                  (if matchpos
                                                      (setq r
                                                            (s-truncate matchpos r "")))))
                                       r))
                                    (mapcar
                                     (lambda
                                       (r)
                                       (cl-loop for stpat in final-stop-patterns do
                                                (let
                                                    ((matchpos
                                                      (re-match-p stpat r)))
                                                  (if matchpos
                                                      (setq r
                                                            (s-truncate matchpos r "")))))
                                       r))
                                    (mapcar
                                     (lambda
                                       (r)
                                       (if
                                           (and nil
                                                (sor nil))
                                           (pen-sn nil r)
                                         r)))
                                    (mapcar
                                     (lambda
                                       (r)
                                       (if
                                           (and
                                            (variable-p 'prettify)
                                            prettify nil
                                            (sor nil))
                                           (pen-sn nil r)
                                         r)))
                                    (mapcar
                                     (lambda
                                       (r)
                                       (if
                                           (not nil)
                                           (s-trim-left r)
                                         r)))
                                    (mapcar
                                     (lambda
                                       (r)
                                       (if
                                           (not nil)
                                           (s-trim-right r)
                                         r))))
                                (list
                                 (message "Try UPDATE=y or debugging")))))))
                  (result
                   (if no-select-result
                       (length results)
                     (cl-fz results :prompt
                            (concat "pf-transpile" ": ")
                            :select-only-match t))))
               (if no-select-result results
                 (if is-interactive
                     (cond
                      ((or final-is-info
                           (>=
                            (prefix-numeric-value current-prefix-arg)
                            4))
                       (etv result))
                      ((and nil mark-active)
                       (if
                           (sor result)
                           (replace-region result)
                         (error "pen filter returned empty string")))
                      ((or nil nil)
                       (insert result))
                      (t
                       (etv result)))
                   result))))))))))

Symbol Properties
event-symbol-element-mask
  (pf-transpile 0)
event-symbol-elements
  (pf-transpile)
modifier-cache
  ((0 . pf-transpile))