You can find a summary of the code updates in this pull request . Read on for explanations.
1. Correct grammar
All of the work for this workshop is done in the route handler, in api/get-quote-styles/route.ts.
First I added a JSON property: corrected_quote
. I added this property before the colors because it felt more fundamental to me, but the order doesn’t really matter. Here’s how I described it:
route.ts
2. Correct gendered language
Since there’s only one corrected_quote
output, I added the instructions for gendered language to the same part of the prompt. I find it very helpful to give OpenAI examples of what I want, so here’s what I added to the prompt.
route.ts
3. Select font
First I tried asking for a Google font name for the quote:
route.ts
This didn’t really result in a font that actually fit the quotation, though. Here’s what I found that helped.
Guidance for the font
I asked Ask OpenAI to specify three adjectives for the quote to justify the color that it chose. I entered a quote into ChatGPT and asked for the color plus these three adjectives.
From the response I got in ChatGPT, I gave the OpenAI prompt an example of “bold, calm, professional”.
You don’t have to ask for the three adjectives in the returned JSON, but I did (I was curious!). I added a key to the JSON called description
.
route.ts
Unusual fonts
It helped get more appropriate fonts when I specified the that the font should match the description. Still, I found that OpenAI was giving me the same common fonts over and over. I wanted more unusual fonts, so I added that:
route.ts
I was pretty happy with these results. Think you came up with a better prompt? Let me know by tagging me on LinkedIn or Twitter !
4. Update route return
Finally, we’ll have to update the returned value for the route to include the new data. I destructured properties from the OpenAI JSON response to send back to the client:
route.ts
5. View the results
Since this workshop does not include any UI updates, we can (temporarily) print the JSON reponse to the console to see the results. I chose to print the original quote too, to compare it to the updated quote and see what kind of corrections were being made.
route.ts
Here are some example results (Note: I temporarily defined generatedQuote
at the beginning of the GET()
function as a static string instead of a random quote to look at results for quotes I was interested in).
Note: I had to start up the server and click the “use random quote” button to view the results logged in the terminal.
========> ORIGINAL QUOTE No man will make great leader who wants to do it all himself, or to get all the credit for doing it.
========> OPENAI RESPONSE {
corrected_quote: 'No person will make a great leader who wants to do it all themselves, or to get all the credit for doing it.',
description: 'insightful, wise, empowering',
hex_color: '#8E44AD',
text_color: 'white',
google_font_name: 'Cormorant Garamond'
}
========> ORIGINAL QUOTE sense of justice is noble fancy.
========> OPENAI RESPONSE {
corrected_quote: 'A sense of justice is a noble fancy.',
description: 'noble, dignified, serious',
hex_color: '#3867d6',
text_color: 'white',
google_font_name: 'Playfair Display'
}
Up next
In the next workshop, we’ll update the UI to display the quote in the suggested font.
Workshops in this series
- OpenAI Node SDK
- Prompt engineering: color
- Apply color response
- Prompt engineering: quote cleanup and font
- Apply font response
- Quote entry