Tuesday, September 2, 2025

Using OpenAI in code

AI is everywhere you can't escape. Google search something? AI will want to chip in what it thinks. Every Microsoft thing has Co-Pilots that can help you in various ways in its products. Now how about write your own code using it? OpenAI's ChatGPT is the thing to go to...Python is the preferred language. Just about every example out there uses it. Watch out! Some examples you see from websites, youtube, even generated by AI itself... can be outdated as OpenAI is still constantly evolving.

Using OpenAI in code is isn't free but looks affordable at first glance. Look at its pricing though. A million tokens for a few cents? Well look again, one call can be many tokens as it counts output characters, and image generation is a lot of tokens. Watch your budget.

After you pay up to get a OPEN_API_KEY, do not include it in your code as you don't want that stolen. Put it in a file named .env, and then use load_dotenv after installing with "pip install python-dotenv". Then you have to install the OpenAI library with "pip install openai". After you have the client you can do A LOT of stuff with it. See https://github.com/openai/openai-python to start your adventure.

I did, see it in https://github.com/josephmaklc/openapi/blob/main/openai_experiment.py

I got OpenAI to answer questions, also remember my previous question so I can ask followup questions. I got it to generate an audio file that I cannot tell at all it isn't recorded by human. I even got it to generate code! One cool aspect of ChatGPT is generating images! However, that requires you to "Verify you Organization" which means take a picture of your government ID and send to them... Eew I don't want to do that.