Prerequisites
Before you begin, ensure you have:- Python 3.7+ installed
- An OpenAI API key (keep this secret!)
Never hardcode your API key in source files. Use environment variables or a secrets manager.
Install the OpenAI Python SDK
Configure Your API Key
Export your key as an environment variable:Collecting Ingredients
Prompt users to input ingredients one at a time. Typedone to finish:
Defining the Recipe Generator Function
Createrecipe_gen() to format messages for GPT-4 and request a chat completion.
Chat Completion Parameters
Running the Generator
Once ingredients are collected, invokerecipe_gen() and display the result: