Use GPT-3 in Mini Micro
Tags: miniscript, minimicro, ai, gpt3
Published: 2022-12-21 04:48:06

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](https://miniscript.org/MiniMicro/), if you don't have it already.  Mini Micro is a free development environment based on the [MiniScript](https://miniscript.org) 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."
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ervmcmxcpsuoev0j2k8x.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4fqymob2suj3jhplyfkn.png)

Or have it write some code for you!

![GPT3 writing a Python program to find fibonacci numbers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkv5rb5k95q89oxzbrfi.png)

(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")
```

...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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/judi0wwq001bqv6l9zxm.png)

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."
```

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

![GPT3 acting as dungeon master in a role-playing game](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1k95p9w2kg21lob53qhv.png)

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](https://miniscript.org/MiniMicro/), get yourself an [OpenAI API key](https://openai.com/api/), and try it yourself today!