Use GPT-3 in Mini Micro

JoeStrout - Dec 21 '22 - - Dev Community

ChatGPT is all the rage this month, and for good reason. What it can do is really quite impressive. But ChatGPT is built on GPT-3.5, which can also be accessed from your own programs. In this post, I'll show you how to query GPT-3.5 (GPT3 for short) in Mini Micro, with a small program that can do amazing things!

First, you'll need to go to https://openai.com and create an account, if you don't have one already. Then, while logged in, click your user name in the top-right corner and select "View API Keys". Save your API key to a file called API-key.txt.

Now download Mini Micro, if you don't have it already. Mini Micro is a free development environment based on the MiniScript language, a new(ish) language that is especially simple and easy to learn. Launch it (right-clicking to bypass security warnings, if necessary), click the disk slot under the screen, and use the "Mount Folder..." option to mount the folder where you saved your API-key.txt as the /usr disk.

Now use the edit command, and paste in the following program:

import "json"
import "stringUtil"
import "dateTime"
context = []
context.push "Assistant is a large language model capable of "
context.push "helping the user in many ways, including writing "
context.push "code, poetry, stories, and more."
context.push "Knowledge cutoff: 2022-09"
context.push "Current date: " + dateTime.now("yyyy-MM-dd")

apiKey = function
    if outer.hasIndex("_apiKey") then return _apiKey
    data = file.readLines("/usr/API-key.txt")
    if data == null then
        print "API-key.txt file not found."
        exit
    end if
    outer._apiKey = data[0]
    return _apiKey
end function

respond = function(prompt, temperature=0.5)
    url = "https://api.openai.com/v1/completions"
    headers = {}
    headers["Content-Type"] = "application/json"
    headers["Authorization"] = "Bearer " + apiKey
    data = {}
    data.model = "text-davinci-003"
    data.prompt = prompt
    data.temperature = temperature
    data.max_tokens = 2048

    globals.rawResult = http.post(url, json.toJSON(data), headers)
    globals.result = json.parse(rawResult)
    if result == null or not result.hasIndex("choices") then
        return rawResult
    end if
    return result.choices[0].text.trim
end function

clear
print "Talk to GPT-3!"
_printMark "(Enter `quit` to exit.)"
while true
    inp = input(">")
    if inp.lower == "quit" or inp.lower == "exit" then break
    context.push "User: " + inp
    context.push "Assistant: "
    resp = respond(context.join(char(13)))
    context[-1] = "Assistant: " + resp
    oldColor = text.color; text.color = color.aqua
    for line in resp.wrap
        print line
    end for
    text.color = oldColor
end while

print "Thank you for talking to GPT-3, and have a nice day."
Enter fullscreen mode Exit fullscreen mode

That's it! You can use the toolbar buttons to save and exit, and then run your program. If all goes well, you should see a greeting and a prompt. At this point you just type something, in plain English. Questions are especially good at keeping the conversation rolling. I'm a bit of a neuroscience nerd, so my first conversation went like this:

Conversation with GPT3 about neurons in AI and various animals

But you can do all sorts of things! Ask it for recipe ideas:

Conversation in which GPT3 recommends baking sugar cookies, based on ingredients I have on hand

Or have it write some code for you!

GPT3 writing a Python program to find fibonacci numbers

(Unfortunately GPT3 is not smart enough to write MiniScript code yet — perhaps GPT4 will be better!)

This is already very cool. But wait, it gets better! Our program begins with some context telling GPT3 what is expected of it. By changing that, we can set get GPT3 to pretend to be almost anything! For example, try changing the lines at the top to:

context = []
context.push "Server is an Ubuntu Linux server.  User will type Linux commands;"
context.push "Server should respond with standard Linux output."
context.push "Current date: " + dateTime.now("yyyy-MM-dd")
Enter fullscreen mode Exit fullscreen mode

...and then down in the main program, change "Assistant:" to "Server:". Then run, and type some Linux commands:

GPT3 pretending to be a Linux server

Isn't that amazing? Or try this one:

context.push "DM is a dungeon master in a Dungeons and Dragons role-playing game."
context.push "Player is playing the role of an adventurer in the game."
context.push "Player is currently in the wilds of a fantasy world, inhabited by "
context.push "trolls, goblins, elves, dwarves, and other fantasy creatures."
context.push "The player has a lantern, a short sword, and 30 gold pieces."
Enter fullscreen mode Exit fullscreen mode

and change "Server:" in the main program to "DM:". Now run!

GPT3 acting as dungeon master in a role-playing game

Think of the implications of this! With only a few words describing what you want, GPT3 can be almost anything. I've shown only a few examples here, but there are many more. Studying Spanish? Have GPT3 pretend to be a waiter or a train conductor who speaks only Spanish. Want to make a classic "interactive fiction" game in the style of Zork? Tell it to do that. Need a book or movie recommendation? Talk it over with your AI assistant. Want it to write a book or movie plot for you? It can do that too.

It's a crazy new world we're entering. I couldn't believe how easy and fun this was! Download Mini Micro, get yourself an OpenAI API key, and try it yourself today!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .