Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

WeatherGPT

Using OpenAI chat completion (GPT3) with function calls to SMHI api

The API is instructed to use the current time in Europe/Stockholm timezone. If the message can not be handled with the weather API endpoint, the Open AI assistant will reply instead.

Relevant API documentation

How to use this endpoint

GET:

RUN_ENDPOINT?args=["A weather question"]

Response

{ answer?: string // If the message could not be answered with the SMHI API error?:string // data?: unknown // the actual data returned from SMHI, if the API is called summary?: string // a summary of the data, by GPT API }

Examples

  • How is the weather in the Capital of Sweden tomorrow.
  • How is the weather at Liseberg on Friday.

Packages used

  • zod: for describing the SMHI API response and function API input
  • zod-to-json-schema: Transform the zod schema to json schema (readable by the GPT API)
  • gpt3-tokenizer: count the number of tokens
  • date-fns-tz: To handle dates in a specific timezone (Europe/Stockholm)
1
2
3
4
5
import { callWeatherGPT } from "https://esm.town/v/ljus/callWeatherGPT";
export async function weatherGPT(message: string) {
return callWeatherGPT(message);
}
ljus-weathergpt.web.val.run
October 23, 2023