Summary

Following on from YASnippet combined with Pen.el - Controllable prompt generation // Bodacious Blog, I make another language-agnostic code generator.

Behind the scenes, this prompt generates the comments describing a code snippet. The comments are in the desired language and the comments are removed from the results.

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
title: code generator from description
doc: Language agnostic
prompt-version: 1
lm-command: "openai-complete.sh"
model: davinci
# lm-command: "aix-complete.sh"
# model: GPT-J-6B
temperature: 0.3
max-tokens: 200
stop-sequences:
- "###"
top-p: 1
# Unfortunately, it's not yet possible to have a prompt which ends in whitespace.
# It would really help with suggesting the comments have finished.
prompt: |+
  Language: `(pen-detect-language)``(comment-line 3)`
  Description: The following code is an implementation of <code description>:
  Code:
  >  
vars:
- code description
postprocessor: sed 1,3d | sed "/^[^>]/q" | sed -e "\$d" -e "s/^> *//"
end-yas: on
# The start will not be trimmed
insertion: on
# I guess that this would usually be done manually
continuation-prompt: Generic completion 50 tokens

Demo

Results

GPT-3

haskell

1
2
quicksort [] = []
quicksort (x:xs) = (quicksort lesser) ++ [x] ++ (quicksort greater)

perl

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
sub quicksort {
    my ($left, $right) = @_;
    my $pivot = $_[0];
    my $pivotIndex = 0;
    for ($left; $left <= $right; $left++) {
        if ($left == $pivot) {
            $pivotIndex = $left;
        } else {
            $pivotIndex = ($left + $right) / 2;
        }
        $left = $pivotIndex;
    }
    $right = $pivotIndex;
    return ($left, $right);
}

print "Enter the array to be sorted: ";
my $array = <STDIN>;
print "Enter the number of elements in the array: ";

GPT-J

Unfortunately, I ran out of credits, but I don’t think it worked too well anyway.