Member-only story
ChatGPT as a Co-Author: Transforming 25 Python Examples into an Article
Empowering the content creation.
I created a list of Python examples on how to handle missing values in a Pandas DataFrame.
I was going to write an article using these examples. The process is actually straightforward: List the examples and explain what the code does.
Recently, I have been experimenting with ChatGPT and other LLMs so I wanted to give it a shot at this task as well.
I’ll use the OpenAI API through the openai
library for this task. Let’s first import the libraries and set up the API key.
import openai
import os
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
openai.api_key = os.getenv('OPENAI_API_KEY')
OPENAI_API_KEY
is the environment variable that holds the API key, which can be obtained from the API Keys menu on OpenAI website.
The following is a helper function that can be used for querying the API:
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0,
)
return response.choices[0].message["content"]