In this tutorial, we'll explore how to integrate text generation into our p5.js projects through the openai api.
Important Note: to use the openai api, you will likely have to put in a credit card, and monitor your usage. You also need to be careful with your personal API key - if someone gets ahold of your api key, they can run queries that will get charged to your credit card. For this reason, we won't be using the p5.js web editor for this tutorial. Instead, you will have to use p5.js via VSCode on your local computer.
You should now have the basic p5.js code in a VSCode window and your canvas showing up in your browser.
const API_KEY = "PASTE YOUR KEY HERE";Let's walk through the example code:
API_KEY: This is where we store our specific API key so the OpenAI can identify who we areurl: This is the url to communicate with specifically with the ChatGPT model. If we wanted to use different parts of the OpenAI API, we might use a different urloptions: This is an object that we would use when communicating with the ChatGPT API.myButton: This is where we will store a reference to submit buttonmyInput: This is where we will store a reference to our text inputmyOutput: This is the html element where we put the results from ournoCanvas(). After that, we setup all the GUI elements and place them on the page. We place the button, input field, and the output paragraph using the normal p5.js functions.function setup() {
noCanvas();
background(200);
myButton = createButton("Submit");
myButton.position(540, 20);
myButton.mousePressed(getText);
myInput = createInput("Enter prompt");
myInput.position(20, 20);
myInput.size(500);
myOutput = createElement("p", "Results:");
myOutput.position(20, 80);
}mousePressed, we specify that the getText function should be called. We'll look at that function next.myButton.mousePressed(getText);getText() function is where we talk to the OpenAI API. First, we get the text within the text input. The if statement checks to see if the text input is blank. If it is blank, then we skip everything else.function getText(){
var inputValue = myInput.value();
console.log("myinput", inputValue);
if (!inputValue) {
return
}options.body = JSON.stringify({
model: "gpt-3.5-turbo", // Specify the model to use
messages: [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": inputValue
}
]
});fetch portion of the code sends our message to the OpenAI API, and then handles the results. We start by calling fetch, passing in the url variable and the options variable that stores all of the information about our prompt.fetch(url, options)then to specify what should happen once the API responds. The first step will be to reformat the response to json..then(function(response){
return response.json();
})then to decide what to do with the response. First, we look through the responses, make sure there are options and then grab the latest one:.then(function(response){
if(response.choices && response.choices[0]) {
var assistantMessage = response.choices[0].message.content;
}myOutput html element so the user can see the response from the API.console.log("response:", assistantMessage);
myOutput.html(assistantMessage);