A colorful image of a bird beside a pool in an alien landscape

How I generate mock API data using AI

One thing I’ve always found frustrating about building app prototypes was getting mock API data to use in the app. You can use free resources like JSONPlaceholder and PokéAPI, but sometimes you need content tailored to your own use case. Creating API data manually is tedious, but thankfully, we live in the age of AI when this type of task can easily be delegated to AI tools.

Generating images with Midjourney

I thought it would be fun to build a “Birds of Mars” API that returned data about fictional birds that live on Mars. I wanted colorful images of the birds, and for that I turned to Midjourney. I gave the AI prompts for several types of birds. My favorite, which I call the Swamp Crane, was generated with the prompt “exotic scarlet bird by green pool in alien landscape.” This was the result:

I generated several different images of birds, but I also wanted color variations of each. I wasn’t sure what I was looking for at first, but after a little Googling and chatting with GPT, I learned that I wanted to perform “hue rotation” on my images to generate different variations.

GPT helped me come up with this Python script that generates 17 variations of each image in a directory by rotating the hue of each pixel in the image by increments of 20 degrees.

Here are a few of my favorite variations of “Swamp Crane:”

Generating structured text data with GPT function calling

What if you’re building a Twitter clone and need to generate a ton of fake tweets to populate your app with? OpenAI’s new function calling feature makes it a breeze to generate that kind of structured data. Here’s an example of a Node script that generates Tweets by various founding fathers.

Here’s an example of the data that GPT might respond with:

The idea behind the GPT functions parameter is that you can describe a function you want the model to generate arguments for. Here we describe a function that takes an array of tweet objects, each with a date stamp, the content, and the sender’s name. This approach ensures we get uniform, structured data for each completion.


Posted

in

by

Tags: