Sean is Coding #7: Is English the new Python?
Deploying my first Flask app and building with Open AI's API.
“Coding” in English
This week, I had the chance to revisit a web-based version of Mad Libs we’d made in my bootcamp using Flask a few weeks ago.
The original version came with just two static, pre-written story templates. Users could select a template, fill in the required words, and read the completed story.
I revisited the project with the intent of making this experience a little more dynamic using Open AI’s ChatGPT API to replace the pre-existing stories with an AI-generated story that would be different every time. The process of doing so was extremely exciting and really demonstrates the seismic changes AI is bringing to software development right now.
Here’s the main function that retrieves a new Mad Lib story template from ChatGPT:
def fetch_gpt_story():
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system",
"content": os.getenv('MADLIBS_SYSTEM_INSTRUCTIONS')},
{"role": "user",
"content": os.getenv('MADLIBS_USER_INSTRUCTIONS')}
]
)
return response['choices'][0]['message']['content']
Most of this function is boilerplate, the real magic of "programming" this came from writing the system and user-level prompts. For instance, here is the system prompt:
You are a helpful assistant that generates funny stories with blanked-out words indicated by curly braces. The blanked words should be mad-libs style descriptors of the type of word, such as noun, adjective, adverb, person name, place, country, action verb, etc.
This prompt contains essential instructions for users to follow when filling in the blanks. For example, descriptors inside the curly braces must be entirely lowercase, and every term in curly braces must end with two underscores and a number. The descriptor should also include a numerical indicator to designate words that are intended to be re-used as part of the story.
The descriptor must be descriptive enough to signal the user to provide an input word that will make sense when placed in the sentence. For example, if the sentence is "She {verb__1} to see him but she was too late," consider saying {past_tense_verb__1} instead of {verb__1} because a past-tense verb would sound more natural in that sentence. Additionally, users must respond with the Mad Libs story only, without any chat-style conversation before or after the story content.
The exciting thing about this method of programming is that, instead of importing a dataset and processing it using an algorithm, we can simply ask the API to provide the data in a format that is immediately usable.
This approach is going to have a enormous impact on application development, making developers more productive while reducing the cost of building custom software. It’s such an exciting time to be entering this field.
I’m working on getting a deployable version of this ready to share in the next couple of days.
Deploying a Twitter Clone
Most of the class time this week was spent building and deploying an end-to-end Twitter clone called Warbler. Unlike Mad Libs, Warbler is actually live for you to check out right now.
One of the most surprisingly fascinating aspects of building Warbler was implementing user authentication, data protection, and privacy. We explored how to use cryptography and hashing to store user passwords securely, making it almost impossible for any malicious users to access user data.
Hashing, in particular, is a really fascinating area of mathematics computer science. A hashing algorithm takes an input (e.g. a user’s password) and maps it to a fixed-size output (a hashed string) that represents the original data. Importantly, a good hashing algorithm is both stable and one-way:
Stable, meaning it always provides the same hashed output string for a given input (this is ignoring any salting certain algorithms might do to vary the hashed strings superficially — the underlying hash value remains the same).
One-way, meaning that even though this input always produces the same output, there is no mathematical way to reverse the hash and turn it back into the input. 🤯
Authentication happens when a new user input (i.e. a password, correctly typed by the user) is hashed and compared to the stored hashed value in the database. The actual password itself is never stored anywhere.
Overall, I’ve gotta say, it’s been another thrilling (and admittedly exhausting) week. And I can’t get over how exciting it is to be entering software engineering at this moment with the ground beneath us shifting toward an AI-powered future.