top of page
Writer's pictureLuke Nyswonger

How to Brew Up Creative Beer Names with Python and OpenAI

As a beer enthusiast and technologist, I have found that combining my passions has led to exciting creative possibilities. By exploring the intersection of generative AI and beer, I have discovered new ways to generate unique beer names and descriptions and stay motivated with both my learning and my brewing. In a previous article titled 10 Ways to Utilize Generative AI in Your Homebrewing Process, I showed how you can leverage ChatGPT to do many things, including recipe development and generating beer names and descriptions.


In this article, we’ll explore how to use an API to connect to underlying models and prompt it to generate a beer name, which will be returned to the console. By the end of this article, you’ll have a better understanding of how to use Python and the OpenAI API to create unique and creative beer names, which can enhance the brewing process and inspire new ideas.


Who needs a boring, old-fashioned beer name when you can have a name like “Electric Donkey Acid”?

AI Generated Beer Label Using Microsoft Designer: Electric Donkey Acid
AI Generated Beer Label Using Microsoft Designer: Electric Donkey Acid

The OpenAI API offers an array of capabilities, including natural language processing, image recognition, and machine translation. This versatile API can be utilized in numerous scenarios, making it an invaluable resource for developers. To explore its full potential, check out the wealth of examples OpenAI provides: OpenAI API Examples.


With the OpenAI API Completions endpoint, we can generate some seriously creative beer names that will have your friends laughing (and maybe a little confused). By providing a prompt like the beer style, we can explore the intersection of generative AI and beer to create unique and quirky names that are sure to be a hit at your next beer tasting. Sure, we could use the ChatGPT client, but where’s the fun in that?


Let’s tap into the power of AI and see what kind of wacky beer names we can come up with!


 

Setup Your Environment

In this article, I assume you have some experience with scripting, but don’t worry, you don’t need to be an expert! To get this script up and running, there are a few things you’ll need to prepare. Firstly, set up a Python environment configured with your preferred IDE. In this tutorial, I am using Visual Studio Code. Secondly, make sure to install the required Python modules. Lastly, sign up for a developer account with OpenAI to obtain an API key.


Install Visual Studio Code

  1. Install Visual Studio Code: If you haven’t already, download and install Visual Studio Code from the official website: https://code.visualstudio.com.

  2. Install Python extension: Open Visual Studio Code, go to the Extensions view by clicking on the square icon in the sidebar or pressing Ctrl+Shift+X. Search for the "Python" extension by Microsoft and install it.

  3. Set up a Python environment: Ensure that you have Python installed on your system. If not, download and install it from the official website: https://www.python.org/downloads.

Install Required Python Modules

In this example, we work with an XML file and require libraries for handling it. xmltodict allows for straightforward XML-to-Python dictionary conversion, while the requestsmodule simplifies interactions with the OpenAPI API through HTTP requests.

  1. Open your command prompt or terminal on your computer.

  2. Type in the following command and press enter to install “xmltodict”: pip install xmltodict

  3. Type in the following command and press enter to install “requests”: pip install requests

If you’re using VS Code like I am, it’s recommended to install the modules within the VS Code terminal by opening the terminal within VS Code and running the above commands. Once the installation is complete, you can start using the modules in your Python code.


Obtain OpenAPI API Key

Here are the steps to get an API Key from Open API:

  1. Go to the Open API website at https://platform.openai.com/ to create an account.

  2. Once you’ve created an account, you will need to provide payment information if you want to use the API beyond the free trial period.

  3. After completing the payment information, you can go to the Open API dashboard.

  4. In the dashboard, click on “API Keys” from the left-hand menu.

  5. Click on the “Create new secret key” button and make sure to copy it and keep it safe somewhere. For security reasons, you won’t be able to view it again through your OpenAI account. If you lose this secret key, you’ll need to generate a new one.

With these in place, you’re all set to dive in!


 

Download the Python Script

The code below is an example of how to use Python and the OpenAI API to generate creative names for beers based on the beer style using a Beer XML file as a data source. The XML file we’ll be using contains a collection of recipes for homebrewed beer. The file conforms to the BeerXML standard and includes information about each recipe, such as the name, style, ingredients, and brewing instructions. This file is publicly available and can be used as a data source for various beer-related applications or experiments, such as this one!


⚠️Disclaimer: This sample code is provided solely for educational purposes and must not be deployed in production environments without addressing security concerns, such as API key exposure, input validation, error handling, and other essential aspects required for a secure and functional production environment.


Execute The Script

  1. Copy the script to your local machine.

  2. Replace the openai_key variable in the script with your actual OpenAI API key. Remember not to share this key publicly, as this is a security risk.

  3. In the Visual Studio Code terminal, navigate to the directory where your Python script is located and then execute the script.

  4. After running the script, the output will be displayed in the terminal. You can analyze the results and make any necessary modifications to the script.


Remember to always follow best practices for securing sensitive information like API keys. The provided example contains an API key in the script, which should be replaced with a more secure method of managing secrets, such as environment variables or a secrets manager.

This is an example of some names you may get:

Crown Jewel IPA
Poppy Pale Perfection
Scallywag's English Ale


Unleash Your Imagination

To generate really unique beer names using this script, you can use creative prompts that provide context, style, or themes for the desired names as well as temperature adjustments.


Inspirations for Prompting

Below are 10 sample prompts to spark your creativity when considering how to prompt the model:

  1. “Generate 10 beer names inspired by classic literature and their iconic characters for a Stout beer.”

  2. “Create 10 beer names with a retro video game theme for an American IPA.”

  3. “Suggest 10 beer names that combine elements of famous historical events with a twist of humor for a Pale Ale.”

  4. “Invent 10 beer names for a Saison that evoke the spirit of the Wild West and cowboy culture.”

  5. “Come up with 10 beer names that blend flavors of exotic fruits with the feeling of adventure for a New England IPA.”

  6. “Generate 10 unique beer names inspired by famous scientific discoveries and inventions for a Belgian Tripel.”

  7. “Create 10 beer names with a fusion of street art and urban culture for a Hazy IPA.”

  8. “Suggest 10 beer names that combine elements of iconic movie quotes with a touch of mystery for a Porter.”

  9. “Invent 10 beer names for a Pilsner that evoke memories of childhood nostalgia and classic cartoons.”

  10. “Come up with 10 beer names that blend the beauty of nature’s landscapes with a sense of wanderlust for a Sour Ale.”

Take these as inspirations and get creative. Have fun and manipulate the prompts with the dictionary elements you want (Hops, Styles, etc.). This is an example prompt and output:

prompt = f"Come up with 5 beer names that blend "\
         f"the aroma of the {beer_hops} with elements "\
         f"of Stephen King's writing for a {beer_style}"
1. Gold Caps of Pet Sematary
2. Flagg's Flare Profane Pale Ale
3. Failing Fermentation of Dark Tower Paranoia
4. Witnessed Delusions of Thinner Red Ale
5. Cujo's Rabies Dolor Brew

Don’t hesitate to customize these prompts or develop your own, focusing on the style, theme, or context that suits your distinctive beer names. You can even incorporate your personal homebrewing data if desired.


Adjust the Temperature Values

The temperature setting controls the degree of randomness and creativity in the responses generated by the model. The script is configured with a temperature setting of 1.2. I’ve personally found this to be a good value to work with. Adjusting the temperature to generate unique beer names affects the creativity and diversity of the generated names.


⬇️ Lower temperature (e.g., 0.1 or 0.5): The generated beer names will be more conservative, focused, and similar to common or familiar names. The output might be more predictable and less creative, but it would be more coherent and consistent.


⬆️ Higher temperature (e.g., 1.0 or above): The generated beer names will be more diverse, creative, and potentially unusual. This could result in more interesting and unexpected names but may also generate some outputs that are less coherent or more difficult to understand.


By experimenting with different temperature settings, you can strike a balance between creativity and coherence to generate unique beer names that suit your liking.


Conclusion

With the power of Python and the OpenAI API at your fingertips, you can concoct captivating and unique beer names that are sure to be a hit at your next homebrew gathering. Your imagination is the only limit! So, dive into the boundless opportunities and let your creative juices flow.


🍻 Cheers to some zany and outlandish names like Electric Donkey Acid!

63 views0 comments

Recent Posts

See All

Comments


bottom of page