• AI Authority
  • Posts
  • Turning ChatGPT Into a Meteorologist in Minutes 🌦️

Turning ChatGPT Into a Meteorologist in Minutes 🌦️

Discover how we're transforming ChatGPT into your personal weatherbot, forecasting weather and advising on plant care right at your fingertips.

Good morning, AI explorers!

Welcome to a freshly re-imagined edition of AI Authority.

Our mission — to empower you to navigate the AI universe in a more impactful way!

We're evolving our newsletter to bring you:

  • Practical, actionable insights to help you harness the power of AI

  • Dynamic tools to utilize in your AI journey

  • Comprehensive, step-by-step guides that demystify complex processes

🏗️ In this edition: Build your own app in minutes that leverages AI

Everything you need to know ⬇️

Learn one new skill in the AI world:

🌧 ChatGPT Meets Meteorology: Your Personal Weatherbot Guide

Did you ever wish you had your own personal meteorologist at your fingertips to forecast the weather? Maybe to provide advice on how to care for your precious plants?

Today, we're turning that wish into a reality by transforming ChatGPT, OpenAI's language model, into your personal bot-meteorologist. Let’s dive in:

⚙️ What Is an API?

How can ChatGPT possibly know the weather when its knowledge cut-off is 2021? That’s where APIs come in.

Think of APIs as the secret language of software. They're a bunch of rules and protocols, allowing online applications to talk and exchange notes.

OpenAI's API is a particularly powerful one, enabling us to leverage its machine-learning wizardry in our applications. And the free Weather API will give ChatGPT all the information it needs to become the perfect meteorologist.

🚀 Launching Our Weatherbot: Step-by-Step

Step 1: Getting Set Up ⚙️

First, you need to set up your Python environment, which is the place you’re going to write or paste code into. The simplest place to start is Online Python.

Once you’ve set up your environment, install OpenAI’s Python library by pasting the following code into the “Terminal”:

pip install openai

Step 2: Secret Handshake – API Key 🔑

Next, we need a secret password, called the API key, that OpenAI uses to know it's you. It's like a secret handshake. You can get this from OpenAI’s official website and must include it when sending a message (API request).

Hang onto this, we’ll use it later. And be careful not to share this key with anyone.

Step 3: Small Talk with OpenAI 🗣️

Now that you’ve set up your Python environment, installed OpenAI’s library, and grabbed your secret API key, you can ask OpenAI questions! For example, you can ask it to respond to a simple "Hello!" greeting.

Just paste the following code into your main.py file and replace OPENAI_API_KEY with your actual API key:

import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")

completion = openai.ChatCompletion.create(
  model="gpt-4",
  messages=[
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Hello!"}
  ]
)

print(completion.choices[0].message)

Step 4: Weatherbot in Action ☀️

The last step is to add the Weather API and send that information to ChatGPT so it can be your meteorologist.

You’ll need to install Python’s requests library, which is done the exact same way as the OpenAI library — “pip install requests”. Then, add your Weather API key, which can be found here, the same way you added your OpenAI API key.

Now that you know the basics, you can ask ChatGPT to write the code necessary to turn ChatGPT (or GPT-4) into your own personal weatherman.

Here’s that code:

import requests
import openai

# Add your OpenAI and weather API keys here
OPENAI_API_KEY = "your-openai-key"
WEATHER_API_KEY = "your-weather-key"

openai.api_key = OPENAI_API_KEY

def get_weather(location):
    """Get the weather forecast for the given location."""
    response = requests.get(f"http://api.weatherapi.com/v1/forecast.json?key={WEATHER_API_KEY}&q={location}&days=1")
    forecast = response.json()
    # Extract the temperature for the day
    temperature = forecast['forecast']['forecastday'][0]['day']['avgtemp_c']
    return temperature

def ask_gpt_weather(location):
    """Ask GPT about the weather in the given location."""
    temperature = get_weather(location)
    message = f"The temperature today in {location} is {temperature}C. How is the weather there?"

    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": "You are a knowledgeable assistant."},
            {"role": "user", "content": message},
        ]
    )

    return response['choices'][0]['message']['content'].strip()

def main():
    """Main function to run the app."""
    location = input("What's your location? ")
    gpt_response = ask_gpt_weather(location)

    print(gpt_response)

if __name__ == "__main__":
    main()

Congrats, you’ve successfully used the OpenAI and Weather APIs to create a fun app that expands ChatGPT’s knowledge base!

If you run into any problems, or have any questions or comments, about this process, reach out to us on X (Twitter).

📰 Some of the top headlines in AI that caught our eye:

— Op-Ed: AI May Take Doctor’s Roles Sooner Rather Than Later (link)
— AI Art Showdown: How Midjourney, Stable Diffusion, SDXL Stack Up (link)
— Dell is All In On Generative AI (link)
— AI Search of Neanderthal Proteins Resurrects Extinct Antibiotics (link)
— Walmart Using AI to Streamline Organization (link)

🎨 Three of our favorite tweets, threads, AI generations and more:

How to use custom parameters in Midjourney to save time and resurface your favorite settings via @nickfloats:

An entire movie trailer created using AI tools. Check out the impressive video, and the stack of platforms Marco used for each step:

Much of the mainstream sentiment around AI thinks it only copies/rearranges what already exists.

But research shows that ChatGPT is already in the top 1% of creative thinking:

That’s all for today!

If you haven’t already, subscribe to receive AI Authority in your inbox. And share it with an AI-curious friend!