Simple news generator using ChatGPT API
1 Jan 2024
Exploring New Features and Tips for ChatGPT
Exploring ChatGPT apis using one of the examples from this book : https://www.oreilly.com/library/view/developing-apps-with/9781098152475/
Exporting Your API Key
For the created client to automatically use the API key, you can export the following in your environment, or include it in .bashrc
or .zshrc
export OPENAI_API_KEY=<your_api_key_here>
Subscription v/s credits
- ChatGPT subscription does not give you API credits, it’s for using the tools directly on the platform, with access to advanced models.
- If you need credits for the API, you can pay for them on on ongoing basis in a different section on the platform.
- This is probably a better consumption setting for cost control anyway, as long as you don’t use the “refill automatically” option.
Code
news_generator.py
# The following script generates random news article
# using OPENAI's GPT model
from openai import OpenAI
client = OpenAI();
def ask_chatgpt(messages):
response = client.chat.completions.create(
model='gpt-3.5-turbo',
messages=messages
)
# import pdb; pdb.set_trace();
return response.choices[0].message.content
prompt_role = "You are an assistant for journalists. \
Your task is to write articles, based on the FACTS that are given to you.\
You should respect the instructions: the TONE, the LENGTH and the STYLE"
from typing import List
def assist_journalist(
facts: List[str], tone: str, length_words: int, style: str
):
facts = ", ".join(facts)
prompt = f"{prompt_role} \
FACTS: {facts} \
TONE: {tone} \
LENGTH: {length_words} words\
STYLE: {style}"
return ask_chatgpt([{"role":"user", "content":prompt}])
print(
assist_journalist(
facts=[
"stock market went up by 1.5 percentage",
"Fed hinted at lower interest rates next year",
"Employement numbers came out better than expected"],
tone="professional",
length_words=100,
style="news article"
)
)
Response
python news_generator/news_generator.py
Stock Market Rises 1.5%, Fed Hints at Lower Interest Rates Next Year, and Employment Numbers Surpass Expectations
In a surprising turn of events, the stock market experienced a substantial increase of 1.5% today. Investors cheered as the positive momentum persisted throughout the trading session, pushing major indices higher. This rise can be attributed to the Federal Reserve’s recent announcement regarding potential interest rate cuts in the coming year. Investors interpreted this as a sign of reassurance, boosting confidence in the economy. Additionally, today’s employment numbers surpassed expectations, providing further support for the notion of a strong and stable job market. As the optimism prevails, market participants eagerly await further developments in the weeks ahead.
What I learnt new
- Chat Completion API changed compared to the previous version
- The migrate tool is awesome! Wish all packages came with a tool that does this best effort and refactoring for you and let you figure out the rest.
- The responses parser code has changed, so you can get the attributes using the following syntax
- Not surprisingly, if you provide some related facts, ChatGPT will find the correlation :)