• 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!