You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/tutorials/en/criticalAI1-chatting-with-about-code.mdx
+31-19Lines changed: 31 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,31 +24,33 @@ import Callout from "../../../components/Callout/index.astro";
24
24
25
25
This tutorial teaches you how to use generative AI to write p5.js code — including tips and warnings. You'll also learn why critical perspectives on AI matter for artists and anyone creating with p5.js. Whether you're looking for more ways to work with p5.js or already using generative AI to help you write code, this tutorial is for you.
26
26
27
-

27
+

28
28
29
-
## Get to know code-generating AI
29
+
## How to use it: Code-generating AI
30
30
31
31
You are likely familiar with text-generating AI tools like ChatGPT. They might seem intelligent, but they actually use statistics. They predict what is most likely to appear next, based on having analyzed huge amounts of existing text. They do this so convincingly that they can even generate code!
32
32
33
33
You might ask, why not have it write all my code for me? Why do I even need tutorials like this one any more? There are caveats: It is easy to end up with faulty code, biased code, boring code.
34
34
35
-
In this tutorial, we will walk through the process of creative coding with a code-writing AI text-generator. Along the way, we will share tips, warnings, and critical considerations about how we use AI. They will be flagged "Try This!" for tips, "Warning" for cautions, and "Critical Context" for reflections.
35
+
In this tutorial, we will walk through the process of creative coding with a code-writing AI text-generator. Along the way, we will share tips, warnings, and critical considerations about how we use AI. They will be flagged "Try This!" for tips, "Warning" for cautions, and "Critical Context" for mindful reflections.
36
36
37
37
This tutorial is Part 1 in a series of four tutorials that focus on using AI creatively and thoughtfully. Feel free to adapt them for your own critical exploration of AI systems:
38
38
39
39
- Part 2: [Critical AI Prompt Battle](criticalAI2-prompt-battle)
40
40
- Part 3: [Sentiment Training Dataset Explorer](criticalAI3-sentiment-dataset-explorer)
41
41
- Part 4: [The No-AI Critical AI Chatbot](criticalAI4-no-ai-chatbot)
42
42
43
-
## Steps
43
+
## How to make it
44
44
45
-
### 1. Describe your idea
45
+
### Step 1. Describe your idea
46
46
47
47
Pick something you'd like to create with p5.js. Write down your idea for yourself, with as many details as you can imagine. Try to describe it in a few different ways: How does it look? What does it do? How does it work?
48
48
49
49
For this example, I chose a tutorial I haven't been able to find elsewhere:
50
50
51
-
`Convert webcam video to a halftone look where the dot size is equivalent to the darkness in that area of the image.`
51
+
`Convert webcam video to a halftone look where the dot size is based on the darkness in that area of the image.`
52
+
53
+

52
54
53
55
<Callout>
54
56
Keep the project scope small to start. Do you have different versions of the idea, a simpler version and a more complex one? Write them out separately. For example, here are some other features that would be nice to include, but let's wait to add them later:
@@ -63,7 +65,7 @@ You can follow along with this example in the [p5.js Web Editor](https://editor.
63
65
When will you choose to use GPT tools for your project, and when will you skip them? A ChatGPT query requires almost 10 times as much electricity as a Google search (O'Brien 2024). Sometimes the code challenge is something you can easily find on Google or StackOverflow. Sometimes it's better just to call a friend and figure it out together!
64
66
</Callout>
65
67
66
-
### 2. Gather your tools
68
+
### Step 2. Gather your tools
67
69
68
70
Start by logging into your preferred chat-based AI tool. Some options include [OpenAI's ChatGPT](https://chat.openai.com), [Replit Ghostwriter](https://replit.com/), [Blackbox.ai](https://www.blackbox.ai/) — each of these have limited free access.
69
71
@@ -85,15 +87,15 @@ Text-generating AI are created by scraping publicly available code from platform
85
87
86
88
With all this in mind, once you've picked your tools, you can begin by prompting the same way you would a plaintext chat prompt. For this example, we are using the free plan for ChatGPT, running the GPT-4o model.
87
89
88
-
### 4. Prompt GPT to generate some p5.js code
90
+
### Step 3. Prompt GPT to generate some p5.js code
89
91
90
92
Pause. Rather than prompting ChatGPT with your whole project idea at once, let's break it down into parts. It's okay if you don't know every part of the process you want to create — that's why we're asking for help. We do this to understand more about what we are trying to make, and so that we receive less generic code more tailored to our project. Don't forget to tell it we're coding in p5.js!
91
93
92
94
`Help me code a project in p5.js. I'd like to describe features and then have them added please.`
93
95
`- First, import webcam video`
94
96
`- Convert video to pixels`
95
97
`- Turn pixels into dots`
96
-
`- Make dot size based on the brightness and contrast of the pixel`
98
+
`- Change dot size based on the brightness and contrast of the pixel`
97
99
98
100
<Callout>
99
101
Try writing the steps out in pseudocode, where you describe the steps of coding it without actually writing the code. This breaks your steps into smaller, more programmatic steps. For example, I could change the video processing steps to read:
@@ -118,9 +120,9 @@ Review the answers it provides before you decide to use them. The code may inclu
118
120
Also, its solution may not be the right solution for you. How does your pseudocode compare to the steps it took? There's always more than one way to solve a problem.
119
121
</Callout>
120
122
121
-
### 5. Adapt the code to your needs
123
+
### Step 4. Adapt the code to your needs
122
124
123
-
Bring the output code results into the p5.js Editor or your IDE of choice. Rewrite the code by hand, don't cut and paste if possible. This will help you make sure you understand it and that it fits into your project.
125
+
Bring the output code results into the p5.js Editor or your IDE of choice. Retype the code by hand; don't cut and paste if possible. This will help you make sure you understand it and that it fits into your project.
124
126
125
127
<Callout>
126
128
Use your own knowledge to adapt it, and don't be afraid to rework it to suit your project's needs.
@@ -134,7 +136,7 @@ Don't assume it works. It may not run the way you want it to; it may not run at
134
136
Make sure to cite ChatGPT whenever you use it for writing code (or any other text). Include the date it was prompted and if possible the prompt. You can also include the version of the model if you know it and any other details relevant to how you used the system. This is helpful for future readers of your code who want to understand how you made it. Also, it is an important habit to cite any code or examples you draw from, human or otherwise, even if you end up rewriting the code.
135
137
</Callout>
136
138
137
-
### 6. Ask follow-up questions
139
+
### Step 5. Ask follow-up questions
138
140
139
141
If you don't understand aspects of what the program is doing, you can ask ChatGPT for help and clarification. Try pointing to a specific line in your program and ask what it does. You can also prompt ChatGPT to explain how a general concept works or how it is implemented.
140
142
@@ -148,7 +150,7 @@ It still may not have the right answer. Try asking about small sections. Try ask
148
150
Be aware of bias, fairness, representation, as with all generated text. [One of the best ways to address bias is to] work with other people, not only bots. We're all fallible, but incorporating other perspectives helps us see the gaps in our thinking.
149
151
</Callout>
150
152
151
-
### 7. Combine ChatGPT and your own capabilities to enhance your project
153
+
### Step 6. Combine ChatGPT and your own capabilities to enhance your project
152
154
153
155
You can try prompts that add changes to the code you generated before. This might include addressing bug fixes, adding additional features, making aesthetic adjustments, or anything else.
154
156
@@ -158,19 +160,21 @@ For the halftone video, I'd like more control over the look of the video, so let
158
160
159
161
After asking ChatGPT to help you make adjustments, your program still might not look right to you. But you should feel empowered to make adjustments on your own to make it exactly what you want. Go ahead and read through the code (you typed it in yourself, right?) to look for how you might customize and expand on it.
160
162
161
-
For example, if the halftone dots look too big, play with the value for the global variable `gridSize`. A lower number creates a finer-resolution image.
163
+
For example, if the halftone dots look too big overall, play with the value for the global variable `gridSize`. A lower number creates a generally finer-resolution image.
162
164
163
165
You could also try making it more colorful:
164
166
165
167
`Make 2-color halftone pixels instead of grayscale`
166
168
167
-
Interestingly, this still looks gray, because the two colors it chose for our pixels were black and white! Rather than ask GPT again, let's see if we can do it ourselves.
169
+
Interestingly, this still looks gray, because the two colors GPT chose for our pixels were black and white! Rather than ask GPT again, let's see if we can do it ourselves.
168
170
169
171
Find the line of code that says `let fillColor = isDark ? 0 : 255;` . This determines the two colors it uses to fill the dots. Let's try changing them by first creating some new fill colors, then updating the `fillColor` variable:
170
172
171
173
```js
172
-
let fillA =color(255,0,0) //red
173
-
let fillB =color(0,255,0) //green
174
+
// let fillA = color(0,0,0) //black
175
+
// let fillB = color(255,255,255) //white
176
+
let fillA =color(255,0,0) //red
177
+
let fillB =color(0,255,0) //green
174
178
let fillColor = isDark ? fillA : fillB;
175
179
```
176
180
@@ -182,6 +186,10 @@ function draw() {
182
186
...}
183
187
```
184
188
189
+
<Callouttitle="Warning">
190
+
Case in point: I noticed that when we added the two-color feature, GPT seemed to have lost the original request we gave it to make the pixels different sizes. In fact, the variation was very tiny because I had set grid size was small. Being able to read through the code and my prior conversation with the bot helped diagnose the problem and helped me find a solution I preferred. Watch out for things like this, and use your own coding abilities to combine the solutions it gives you into the actual solution you need.
191
+
</Callout>
192
+
185
193
Now let's add an additional feature, so we can output our videos as GIFs. Here ChatGPT actually gets it wrong, but p5.js has a simple [`saveGif()`](https://p5js.org/reference/p5/saveGif/) function built in. You can add this code at the bottom of your sketch:
186
194
187
195
```js
@@ -202,20 +210,24 @@ Now that you have an example of ChatGPT-generated halftone videos, how does the
202
210
Keep experimenting. Work modularly to put smaller pieces together into bigger parts. Toggle back and forth between using ChatGPT and other resources. Explore different methods to find where you need to go.
203
211
</Callout>
204
212
205
-
### 8. Try many different ways to interact with ChatGPT critically and creatively
213
+
### Step 7. Try many different ways to interact with ChatGPT critically and creatively
206
214
207
215
Besides generating new code, you can use ChatGPT and tools like it to do lots of other things related to creative coding. In a study of how computer science students used ChatGPT, the most common tasks were to have it explain programming concepts that you'd like to understand better, or to have it give code examples to help illustrate those concepts (Ma 2024). It was also used to interpret error messages and for checking existing code to help you optimize, debug, or clarify a particular section.
208
216
209
217
For example, you might experiment with a prompt like: `Here is a piece of p5.js code. Please offer ideas for making it run more efficiently…` Then paste in your code.
210
218
211
219
The students also cautioned that "ChatGPT may not always give the correct answers or answers they needed, particularly in the context of programming, where multiple solutions exist" (Ma 2024, 6).
212
220
213
-
### 9. Bonus: Use code generating as a creative springboard.
221
+
### Step 8. Bonus: Use code generating as a creative springboard.
214
222
215
223
Instead of prompting it to generate code, have it prompt you to code instead. Ask it for quick code exercises that can spark your imagination, or drills that help you practice your skills. Then you can create the code. For example:
216
224
217
225
`Please compose prompts for creative coding exercises using p5.js that help me learn about making web pages`
218
226
227
+
<Callout>
228
+
Use code generators to help you be more creative and go deeper with coding, rather than to help you skip the time it takes to engage with code and learn. You can use these techniques for working with p5.js, other programming languages, and also when generating non-code text. Let it empower you to learn more!
Copy file name to clipboardExpand all lines: src/content/tutorials/en/criticalAI2-prompt-battle.mdx
+12-10Lines changed: 12 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,7 +29,7 @@ In this tutorial, you can build a tool to run several AI text prompts at once an
29
29
![A screenshot of three AI-generated images from the prompt `The doctor is ['a Pakistani woman', 'a Black man', 'a white man'].` The first image is a portrait, the second a cartoon, and the third is a hallway in shadows.](../images/featured/criticalAItutorial2.png)
30
30
Prompt: `The doctor is ['a Pakistani woman', 'a Black man', 'a white man'].`
31
31
32
-
## Why compare prompts?
32
+
## How to use it: Why compare prompts?
33
33
34
34
When we use generative AI to make text, code, or images, how are we able to understand its point of view? How does that point of view influence our work? Can we understand the perspectives it absorbed from data if it is hidden behind an interface? We can build our own tools to test subtle variations that show how our word choices shape results. With it, we can glimpse the underlying assumptions contained in the training dataset. That gives us more information to decide how we select and use these models — and for which contexts.
35
35
@@ -51,9 +51,9 @@ This tutorial is Part 2 in a series of four tutorials that focus on using AI cre
51
51
- Part 3: [Sentiment Training Dataset Explorer](criticalAI3-sentiment-dataset-explorer)
52
52
- Part 4: [The No-AI Critical AI Chatbot](criticalAI4-no-ai-chatbot)
53
53
54
-
## Steps
54
+
## How to make it
55
55
56
-
### 1. Brainstorm prompts
56
+
### Step 1. Brainstorm prompts
57
57
58
58
Make a list of topics that interest you to try with the tool we will make. Experiment with adding variety and specificity to your prompts and some blanks you propose. Try different sentence structures and topics.
59
59
@@ -87,7 +87,7 @@ Here are a few more examples:
87
87
Subtle changes in your inputs can lead to large changes in the output. Sometimes these also reveal large gaps in the model's available knowledge. What does the model 'know' about communities who are less represented in its data? How has this data been limited?
88
88
</Callout>
89
89
90
-
### 2. Import the Hugging Face library
90
+
### Step 2. Import the Hugging Face library
91
91
92
92
Open the [tutorial example](https://editor.p5js.org/sarahciston/sketches/siBTII_bC) in the p5.js Web Editor. Make a copy and rename it "My Critical AI Prompt Battle" to use as your own template.
93
93
@@ -108,11 +108,13 @@ But we don't yet have an access token! On the line just above that, create a var
108
108
109
109
Hugging Face keeps a public repository of models and datasets that anyone can contribute to. In order to access the Hugging Face models, you'll want to create an account and get an access token, so that they know it is you when you log on from your p5.js program. This is free.
110
110
111
-
Create an account at [Hugging Face](https://huggingface.co). Click "Settings," then "Access Token," then "New Token." Name your token something like "p5 Web Editor" and give it write access, which means it can interact with your Hugging Face account. Click "Generate the token," and make sure to copy the long text string that results. This is your token. Paste it into `const HF_TOKEN = "hf_..."`
111
+
Create an account at [Hugging Face](https://huggingface.co). Click "Settings," then "Access Token," then "New Token." Name your token something like "p5 Web Editor" and your choices for token type are "Fine-grained," "Read," or "Write." Select "Write," which means the token has permission interact with your Hugging Face account and make API calls as you. You don't need to select specific fine-grained choices, but you can if you want. Click "Generate the token," and make sure to copy the long text string that results. This is your token. Paste it into `const HF_TOKEN = "hf_..."`.
112
112
113
113
Once you have your access token, we are ready to connect to Hugging Face to access their machine learning models.
114
114
115
-
### 4. Create global variables
115
+

116
+
117
+
### Step 3. Create global variables
116
118
117
119
Declare these variables at the top of your script so that they can be referenced in multiple functions throughout our project:
118
120
@@ -131,7 +133,7 @@ When writing your prompt, replace one of these aspects with `*`. We will fill th
131
133
132
134
The remaining variables `promptField`, `blanksArray`, `modelOutput`, `resText,` and `resImg` are created at the top of our program as global variables so that we can access their values from inside different functions at various parts of the program.
133
135
134
-
### 5. Select the task and type of model
136
+
### Step 4. Select the task and type of model
135
137
136
138
Let's write a function to keep all our machine learning model activity together. The first task we will do is called "chat-completion." Create a function `chatCompTask()` and put `async` in front of the function call.
137
139
@@ -178,7 +180,7 @@ Then, we can run the function by trying `console.log(chatCompTask("The woman has
178
180
179
181
For example, one output we got from that prompt was, `Thewomanhasajobasanurseandwishesfordifferentjobs. Themanhasajobasanengineerandwishesfordifferentcareers. Thenon-binarypersonhasajobasanarchitectandhopestopursueherdreamsofbecomingthebestdesignerintheworld.`
180
182
181
-
### 6. Update model to process multiple prompts
183
+
### Step 5. Update model to process multiple prompts
182
184
183
185
Let's make our model process more than one prompt at once, so that we can compare. Here's the new version:
184
186
@@ -236,7 +238,7 @@ async function textImgGroupTask(pArray){
236
238
}
237
239
```
238
240
239
-
### 6. Use p5.js DOM elements to display model results
241
+
### Step 6. Use p5.js DOM elements to display model results
240
242
241
243
We already have a pre-built, friendly web interface using p5.js DOM functions, which lets you enter a prompt to send to the AI model. These tools will also allow you to display the results of your model on the same web page. The console is helpful for testing, so we will keep using `console.log()` as our backup.
242
244
@@ -322,7 +324,7 @@ We assign new variables to the results of these models, so that we can then iter
322
324
Look for additional comments in the code for descriptions of each function. If you are not sure what a function or variable does, try putting it inside `console.log()` to print its results out to the console below your code.
323
325
</Callout>
324
326
325
-
### 8. Bonus: Put your tool to the test
327
+
### Step 7. Bonus: Put your tool to the test
326
328
327
329
<Callout title="Critical Context">
328
330
Is the model capable of representing a variety of contexts? What do you notice the model does well at representing, and where does it fall short? Where do you sense gaps, and how does it expose these or patch them over? Consider your own creative practice, as well as how you currently use generative AI tools. What kinds of questions do you usually ask, and how can you test these kinds of questions for their implicit perspectives? How do these perspectives impact your practice?
0 commit comments