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
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)?
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.
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?”
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:
Make the HTTP request to Open Weather’s OneCall API
Use an expression to build the specially formatted payload for OpenAI’s GPT API
Stub the base message template for OpenAI
Double stringify the weather response data
Trim off the wrapping " characters from double stringifying
Build the final message using the template
Make the HTTP request to OpenAI
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
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.
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.
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.
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.
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.