Open Weather API (and Chat GPT)

Has anyone tried pulling the new human-readable summary from the api? I get get invalid expression with the following
$context.response.daily.summary
$context.response.daily.0.summary
$context.response.data.summary
$context.response.data.0.summary
$context.response.data.daily.summary
$context.response.data.daily.0.summary

I’m trying to pull it for today and tomorrow.

2 Likes

Can you clarify which API endpoint you’re using? Open Weather has several API endpoints.

For their OneCall 3.0 API, make sure your Open Weather account is subscribed to that API. From what I recall, you can subscribe, but set a limit to 1000 API calls per day to stay within the free tier.

  • Can you clarify what you mean by you are seeing an ‘invalid expression’?

  • Can you share a screenshot of your rule (making sure to redact your appid/token)?

PS. I originally missed the comment about the ‘invalid expression’ and drafted a long response about troubleshooting HTTP Action responses which I’ve now posted as a help article. :slight_smile:

I’ve tried it with OneCall 3.0, both of these work

{{$context.response.data.daily.0.summary}}
{{$context.response.data.daily.1.summary}}

To be honest, the summary reply isn’t great
Below is what it returns for today and tomorrow for me

(Today) You can expect partly cloudy in the morning, with rain in the afternoon

(Tomorrow) Expect a day of partly cloudy with rain

1 Like

@josh Sorry, I’m using OneCall 3.0.

It would be really cool to feed the daily[0] response to OpenAI’s GPT API (Chat GPT API) and have it generate a summary:

Can you put together a single sentence forecast with the most relevant information from the following Open Weather API JSON response for tomorrow's weather? The units are imperial. Please keep the response concise and only include information if it is relevant. 

Before generating a response, take a moment to internally (silently) think about the steps you would take to determine what information is relevant to include in the forecast. Ignore the provided "summary" if one is included as I want you to generate your own. 
~~~
{
    "dt": 1690308000,
    "sunrise": 1690284742,
    "sunset": 1690334902,
    "moonrise": 1690310280,
    "moonset": 1690262340,
    "moon_phase": 0.25,
    "summary": "Expect a day of partly cloudy with clear spells",
    "temp": {
        "day": 93.81,
        "min": 73.45,
        "max": 102.87,
        "night": 84.83,
        "eve": 100.17,
        "morn": 77.09
    },
    "feels_like": {
        "day": 101.23,
        "night": 84.51,
        "eve": 102.6,
        "morn": 77.97
    },
    "pressure": 1019,
    "humidity": 48,
    "dew_point": 71.15,
    "wind_speed": 10.49,
    "wind_deg": 206,
    "wind_gust": 26.42,
    "weather": [
        {
            "id": 800,
            "main": "Clear",
            "description": "clear sky",
            "icon": "01d"
        }
    ],
    "clouds": 0,
    "pop": 0,
    "uvi": 10.78
}
~~~

And the output:

I also found that using GPT-4 for this kind of logical thinking and composition results in a ‘better’ result, but that’s subjective:

I’m sure with a bit of prompt engineering, you could probably get it to summarize things just the way you want.

1 Like

Here’s an example where I asked it to “try again in the style of Carrot Weather which is known for its amusing, often sassy, and sometimes profanity laden forecast summaries?”

4 Likes

This is a great idea

How did you do this?

I just did the example directly in the ChatGPT interface.

What I was suggesting, is you could use the OpenAI API to accomplish the same thing in a rule using HTTP Actions.

Here’s an approach that worked for me for getting the Open Weather response and then feeding that into OpenAI to leverage their GPT models:

:stop_sign: Note that you have to be very specific about the syntax or it won’t work. The little details really matter here.

Here’s a summary of the flow:

  1. Make the HTTP request to Open Weather’s OneCall API
  2. Use an expression to build the specially formatted payload for OpenAI’s GPT API
    1. Stub the base message template for OpenAI
    2. Double stringify the weather response data
    3. Trim off the wrapping " characters from double stringifying
    4. Build the final message using the template
  3. Make the HTTP request to OpenAI
  4. Store the response message content from Open AI in a variable

Snippets

And the relevant snippets for your reference.

First Expression
Build the specially formatted payload for OpenAI’s API.

message = "Can you put together a single sentence forecast with the most relevant information from the following Open Weather API JSON response for tomorrow's weather? The units are imperial. Please keep the response concise and only include information if it is relevant. \\n\\nBefore generating a response, take a moment to internally (silently) think about the steps you would take to determine what information is relevant to include in the forecast. Ignore the provided 'summary' if one is included as I want you to generate your own. \\n~~~\\n __WEATHER_JSON__ \\n~~~"
stringJson = stringify(stringify($context.response.data.daily[0]))
stringJson = substring(stringJson, 1, size(stringJson)-1)
replace(message, "__WEATHER_JSON__", stringJson)

Notes:

  • The base message template must properly escape any new-lines, double quotes, etc.
  • We double stringify the JSON that we get back from open weather since we need it properly escaped
    "{\"humidity\": 48, \"temp\": {\"min\": 73.45... }"
    
  • But the double stringify ends up wrapping the whole things in quotes, so we strip those off
    {\"humidity\": 48, \"temp\": {\"min\": 73.45... }
    

OpenAI HTTP Action
This is a standard HTTP action and the formatting is following the documentation from OpenAI. If you want to adjust the parameters, you might want to do so in the OpenAI Playground and use the View Code button and change the type to JSON to make sure you get the right format. It’s critical that your content within your messages gets injected properly as shown in the example.

{
  "model": "gpt-3.5-turbo",
  "messages": [{"role": "user", "content": "{{$AString}}"}],
  "temperature": 1,
  "max_tokens": 256,
  "top_p": 1,
  "frequency_penalty": 0,
  "presence_penalty": 0
}

Notes:

  • Make sure to POST the JSON data to the right endpoint
  • Ensure you’ve setup the headers properly:
    Key Value
    Authorization Bearer YOUR-KEY
  • Ensure the Payload is formatted exactly as show above and the content is pointing to the proper variable (the output from the earlier expression)

Use OpenAI Response
We’re grabbing the result from the response based on the format noted in the OpenAI documentation:

$context.response.data.choices[0].message.content
2 Likes

This is a great guide, worked first time, Thanks

Edit - It’s also really handy having Chat GPT on my dashboard and again using SharpTools Rule’s and TTS, the dashboard will kindly read the response

3 Likes

That’s really awesome, Paul! I didn’t get the notice of your post edit and just saw your screenshot! :star_struck:

Edit: You’re featured in our latest Twitter post and Facebook post! :sunglasses:

2 Likes

So for anyone who hasn’t tried @josh Chat GPT openweather summary

(Open Weather API)

Expect a day of partly cloudy with rain

(Chat GPT)

Tomorrow, 30th July 2023, the weather is expected to bring moderate rain throughout the day. The maximum temperature will reach 16°C, while the minimum temperature will be around 14°C. The sunset is set for 21:15. The atmospheric pressure will be 1006Mb. The wind will be quite strong, with a constant speed of 29Mph and gusts reaching up to 34Mph. The wind direction will be from the north. Cloud cover is estimated at 75%, meaning the sky will be mostly cloudy. The chance of rain is at a high 100%, so it is advisable to carry an umbrella or raincoat if you plan to be outside. The UV index is low at 2. The pollen count is expected to be low at 0, which may bring some relief to allergy sufferers.

1 Like

This is super cool, thanks for sharing @josh I’m having an issue where the response isn’t getting stored in my variable. I’m trying to troubleshoot it in the logs and when I click show raw response, nothing displays. Am I not doing something really obvious? I have it working in Postman, but it just won’t work in SharpTools.

Edit: I added the error status and I’m getting a 404 from OpenAI. I copied it from Postman.

I would triple check the URL. A 404 means the OpenAI servers are responding saying that the URL you’re trying to access is not found. I would check for any extra spaces before and after and little details like that too.

You can share a screenshot if you’d like the community to take a closer look. Just make sure to redact any personal information like API keys.

I had PUT instead of POST. Fixed it, thanks for taking a look.

1 Like

Are you all using the free tier for Open AI API? I decided to give this a try but I could not get the variable to populate from open ai. I tried it Postman and got an error that I exceeded my current quota. I only tried about a dozen times over the course of a few hours.

Edit - looks like my free tier credit expired :frowning:

If you’re only doing a couple per day, it’s super cheap. For the month of August having a busy test day where I hit $0.004, yes not even a penny, and then 2 hits a day after that, I am up to $0.02. You can always set an upper $$ limit, if you’re worried.

1 Like

Thanks Zach. At first I didn’t realize there was a cost to use the api until I tried it with postman and in their api playground.

Do you use the v4 or v3.5?

Since Josh was using v3.5 and this is pretty simple work for ChatGPT, I went with v3.5.

2 Likes