1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { chat } from "https://esm.town/v/cosmo/chat_openai";
import { getCurrentWeather } from "https://esm.town/v/cosmo/get_current_weather";
export async function getWeatherMessage(apiKey, latitude, longitude) {
const currentWeather = await getCurrentWeather(latitude, longitude),
{ icon } = currentWeather,
weather = Object.fromEntries(
[
"isDaytime",
"temperature",
"temperatureUnit",
"shortForecast",
"windSpeed",
"windDirection",
"probabilityOfPrecipitation",
"relativeHumidity",
]
.filter(key => key in currentWeather)
.map(key => [key, currentWeather[key]]),
),
messages = [
{
role: "system",
content:
`Concisely relay the provided weather data in natural language. Don't prefix or embellish it (such as "the current weather is as follows:" or "the weather is"), just relay the weather. Mention the weather properties in the same order as the JSON
},
{ role: "user", content: JSON.stringify(weather) },
],
chatResponse = await chat(apiKey, messages),
message = chatResponse.choices[0].message.content;
return { icon, message };
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
February 1, 2024