Curated News: OpenAI + NewsAPI using python

"Isn't it fantastic to discover news that aligns with your interests? Lets create a python application using OpenAI and NewsAPI APIs to accomplish this!"

Curated News: OpenAI + NewsAPI using python

It's incredibly easy to get the news you're interested in without any unnecessary content. By leveraging the power of NewsAPI and OpenAI APIs, you can effortlessly generate an HTML-formatted news output.

Where do I get the news from?

There are various sources of news that have a REST API interface. The three popular ones are shown below. Apart from these, there are several others. RapidApi shows the other popular ones.

One can use any of the APIs. I chose NewsAPI because the free version does not have a time limit. The highlights of NewsAPI are:

  • Sources: Able to specify multiple sources such as ESPN, CNN, BBC, Bloomberg...
  • From: Ability to specify a time period for the news.
  • Language: Supports multiple languages.

In the code, I had to use GPT-3.5 turbo OpenAPI model which has a token limit of 4096 (max newsAPI output). GPT-4 and GPT-4-32k have limits of 8192 and 32768 tokens, respectively, unfortunately, I do not have access to GPT-4 yet.

import os
import openai
from newsapi import NewsApiClient

openai.api_key = os.getenv("OPENAI_API_KEY")
newsapi_key = os.getenv("NEWSAPI_KEY")

def get_completion(prompt):
    messages = [{"role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages,
        temperature=0,
    )
    return response.choices[0].message["content"]

newsapi = NewsApiClient(api_key=newsapi_key)
top_headlines = newsapi.get_top_headlines(sources='espn', language='en')

prompt = f"""
Remove the URL
Create an HTML file with heading and content using CSS table-layout
Do not paraphrase and add the entire content
Add alternating background colors to each row: light grey and dark grey
Show only the title and the description fields
Add a header at the top of the page with the source and date
Keep the content within an A4 size sheet
Review:```{top_headlines}```
"""

response = get_completion(prompt)
with open("news.html", "w") as f:
    f.write(response)

Curated news


I used a simple prompt that created an HTML output. The prompts, as shown in the code, are a combination of HTML formatting and CSS table-layout, with some color sprinkled into the output, as shown below.

HTML formatted news summary

I've got this set up as a cronjob. It runs every day, delivering me just the right dosage of sports news. Keeps me shipshape and informed!

The code is also available at my github.