FeaturesTemplatesShowcaseTownie
AI
BlogDocsPricing
Log inSign up
project logo

ljus

askSMHI

Public
Like
askSMHI
Home
Code
5
gpt
1
smhi
3
README.md
H
api
service
Branches
1
Pull requests
Remixes
History
Environment variables
1
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Sign up now
Code
/
README.md
Code
/
README.md
Search
3/22/2025
README.md

askSMHI

Using OpenAI chat completion 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

  • SMHI, forecast documentation
  • OPEN AI, GPT function calling documentation

How to use this endpoint

GET:

/ask?q=What is the weather like in Stockholm today

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.

How it works

  1. Handle the API request and count the tokens in the question.
  2. Send the question to Open AI API moderation
  3. Create tool calling by converting schema to JSON schema
  4. Send the question to Open AI Chat Completion and expose tool calling
  5. Make the API call to the SMHI API with parameters from OPEN AI
  6. Add additional information to the SMHI response (converting numerical categories to text)
  7. Remove data from the response that was not requested by the Open AI parameters.
  8. Send the modified response to Open AI API together with the SMHI response JSON Schema.
  9. Send a summary of the forcast (by Open AI API) together with the forecast data as a response to the initial request.

Enviroment variables

  • OPENAI_CHAT: Needs to be authorized to write chat completions and to the moderation API.

Packages used

  • openai: For typesafe API request and responses
  • valibot: for describing the SMHI API response and function API input
  • valibot/to-json-schema: Transform the schema to json schema (readable by the GPT API)
  • gpt-tokenizer: count the number of tokens
  • formkit/temporal: To handle dates in a specific timezone (Europe/Stockholm)
FeaturesVersion controlCode intelligenceCLI
Use cases
TeamsAI agentsSlackGTM
ExploreDocsShowcaseTemplatesNewestTrendingAPI examplesNPM packages
PricingNewsletterBlogAboutCareersBrandhi@val.townStatus
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Terms of usePrivacy policyAbuse contact
© 2025 Val Town, Inc.