DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Integrating Jenkins With Playwright TypeScript: A Complete Guide
  • Simplifying Multi-LLM Integration With KubeMQ
  • Comparing SDLC With and Without AI/ML Integration
  • Seamless CI/CD Integration: Playwright and GitHub Actions

Trending

  • Docker Model Runner: Streamlining AI Deployment for Developers
  • A Simple, Convenience Package for the Azure Cosmos DB Go SDK
  • AI’s Role in Everyday Development
  • AWS to Azure Migration: A Cloudy Journey of Challenges and Triumphs
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Building a Weather Service With Genkit and AI

Building a Weather Service With Genkit and AI

AI-powered weather service using Genkit, TypeScript, OpenWeatherAPI, and GitHub Models. Uses OpenAI o3-mini for AI responses.

By 
Xavier Portilla Edo user avatar
Xavier Portilla Edo
DZone Core CORE ·
Mar. 04, 25 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
4.7K Views

Join the DZone community and get the full member experience.

Join For Free

Overview

This project demonstrates how to build an AI-enhanced weather service using Genkit, TypeScript, OpenWeatherAPI, and GitHub models. The application showcases modern Node.js patterns and AI integration techniques.

Prerequisites

Before you begin, ensure you have the following:

  1. Node.js installed on your machine
  2. GitHub account and access token for GitHub APIs
  3. An OpenWeatherAPI key for fetching weather data
  4. Genkit CLI installed on your machine

Technical Deep Dive

AI Configuration

The core AI setup is initialized with Genkit and GitHub plugin integration. In this case, we are going to use the OpenAI GPT-3 model:

TypeScript
 
const ai = genkit({
  plugins: [
    github({ githubToken: process.env.GITHUB_TOKEN }),
  ],
  model: openAIO3Mini,
});


Weather Tool Implementation

The application defines a custom weather tool using Zod schema validation:

TypeScript
 
const getWeather = ai.defineTool(
  {
    name: 'getWeather',
    description: 'Gets the current weather in a given location',
    inputSchema: weatherToolInputSchema,
    outputSchema: z.string(),
  },
  async (input) => {

    const weather = new OpenWeatherAPI({
        key: process.env.OPENWEATHER_API_KEY,
        units: "metric"
    })

    const data = await weather.getCurrent({locationName: input.location});

    return `The current weather in ${input.location} is: ${data.weather.temp.cur} Degrees in Celsius`;
  }
);


AI Flow Definition

The service exposes an AI flow that processes weather requests:

TypeScript
 
const helloFlow = ai.defineFlow(
  {
    name: 'helloFlow',
    inputSchema: z.object({ location: z.string() }),
    outputSchema: z.string(),
  },
  async (input) => {
    const response = await ai.generate({
      tools: [getWeather],
      prompt: `What's the weather in ${input.location}?`
    });
    return response.text;
  }
);


Express Server Configuration

The application uses the Genkit Express plugin to create an API server:

TypeScript
 
const app = express({
  flows: [helloFlow],
});


Full Code

The full code for the weather service is as follows:

TypeScript
 
/* eslint-disable  @typescript-eslint/no-explicit-any */

import { genkit, z } from 'genkit';
import { startFlowServer } from '@genkit-ai/express';
import { openAIO3Mini, github } from 'genkitx-github';
import {OpenWeatherAPI } from 'openweather-api-node';
import dotenv from 'dotenv';

dotenv.config();

const ai = genkit({
  plugins: [
    github({ githubToken: process.env.GITHUB_TOKEN }),
  ],
  model: openAIO3Mini,
});

const weatherToolInputSchema = z.object({ 
  location: z.string().describe('The location to get the current weather for')
});

const getWeather = ai.defineTool(
  {
    name: 'getWeather',
    description: 'Gets the current weather in a given location',
    inputSchema: weatherToolInputSchema,
    outputSchema: z.string(),
  },
  async (input) => {

    const weather = new OpenWeatherAPI({
        key: process.env.OPENWEATHER_API_KEY,
        units: "metric"
    })

    const data = await weather.getCurrent({locationName: input.location});

    return `The current weather in ${input.location} is: ${data.weather.temp.cur} Degrees in Celsius`;
  }
);

const helloFlow = ai.defineFlow(
  {
    name: 'helloFlow',
    inputSchema: z.object({ location: z.string() }),
    outputSchema: z.string(),
  },
  async (input) => {

    const response  = await ai.generate({
      tools: [getWeather],
      prompt: `What's the weather in ${input.location}?`
    });

    return response.text;
  }
);

startFlowServer({
  flows: [helloFlow]
});


Setup and Development

1. Install dependencies:

Shell
 
npm install


2. Configure environment variables:

Shell
 
GITHUB_TOKEN=your_token
OPENWEATHER_API_KEY=your_key


3. Start the development server:

Shell
 
npm run genkit:start


4. To run the project in debug mode and set breakpoints, you can run:

Shell
 
npm run genkit:start:debug


Then, launch the debugger in your IDE. See the .vscode/launch.json file for the configuration.

5. If you want to build the project, you can run:

Shell
 
npm run build


6. Run the project in production mode:

Shell
 
npm run start:production


Dependencies

Core Dependencies

  • genkit: ^1.0.5
  • @genkit-ai/express: ^1.0.5
  • openweather-api-node: ^3.1.5
  • genkitx-github: ^1.13.1
  • dotenv: ^16.4.7

Development Dependencies

  • tsx: ^4.19.2
  • typescript: ^5.7.2

Project Configuration

  • Uses ES Modules ("type": "module")
  • TypeScript with NodeNext module resolution
  • Output directory: lib
  • Full TypeScript support with type definitions

License

Apache 2.0

Resources

  • Firebase Genkit
  • GitHub Models
  • Firebase Express Plugin

Conclusion

This project demonstrates how to build a weather service using Genkit in Node.js with AI integration. The application showcases modern Node.js patterns and AI integration techniques.

You can find the full code of this example in the GitHub repository.

Happy coding!

AI GitHub TypeScript Integration

Published at DZone with permission of Xavier Portilla Edo, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Integrating Jenkins With Playwright TypeScript: A Complete Guide
  • Simplifying Multi-LLM Integration With KubeMQ
  • Comparing SDLC With and Without AI/ML Integration
  • Seamless CI/CD Integration: Playwright and GitHub Actions

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: