Panoramica
Questo progetto dimostra come costruire un servizio meteorologico potenziato dall’IA utilizzando Genkit, TypeScript, OpenWeatherAPI e modelli GitHub. L’applicazione mostra modelli moderni di Node.js e tecniche di integrazione dell’IA.
Prerequisiti
Prima di iniziare, assicurati di avere quanto segue:
- Node.js installato sul tuo computer
- Account GitHub e token di accesso per le API di GitHub
- Una chiave OpenWeatherAPI per recuperare i dati meteorologici
- CLI Genkit installato sul tuo computer
Approfondimento tecnico
Configurazione dell’IA
La configurazione di base dell’IA è inizializzata con l’integrazione del plugin Genkit e GitHub. In questo caso, useremo il modello OpenAI GPT-3:
const ai = genkit({
plugins: [
github({ githubToken: process.env.GITHUB_TOKEN }),
],
model: openAIO3Mini,
});
Implementazione dello Strumento Meteorologico
L’applicazione definisce uno strumento meteorologico personalizzato utilizzando la convalida dello schema Zod:
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`;
}
);
Definizione del Flusso dell’IA
Il servizio espone un flusso dell’IA che elabora le richieste meteorologiche:
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;
}
);
Configurazione del Server Express
L’applicazione utilizza il plugin Genkit Express per creare un server API:
const app = express({
flows: [helloFlow],
});
Codice Completo
Il codice completo per il servizio meteorologico è il seguente:
/* 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]
});
Configurazione e Sviluppo
1. Installa le dipendenze:
npm install
2. Configura le variabili d’ambiente:
GITHUB_TOKEN=your_token
OPENWEATHER_API_KEY=your_key
3. Avvia il server di sviluppo:
npm run genkit:start
4. Per eseguire il progetto in modalità debug e impostare i breakpoint, puoi eseguire:
npm run genkit:start:debug
Poi, avvia il debugger nel tuo IDE. Vedi il file .vscode/launch.json
per la configurazione.
5. Se vuoi costruire il progetto, puoi eseguire:
npm run build
6. Esegui il progetto in modalità produzione:
npm run start:production
Dipendenze
Dipendenze principali
genkit
: ^1.0.5@genkit-ai/express
: ^1.0.5openweather-api-node
: ^3.1.5genkitx-github
: ^1.13.1dotenv
: ^16.4.7
Dipendenze di sviluppo
tsx
: ^4.19.2typescript
: ^5.7.2
Configurazione del progetto
- Usa i moduli ES (
"type": "module"
) - TypeScript con risoluzione del modulo
NodeNext
- Directory di output: lib
- Supporto completo per TypeScript con definizioni di tipo
Licenza
Apache 2.0
Risorse
Conclusione
Questo progetto dimostra come costruire un servizio meteorologico utilizzando Genkit in Node.js con integrazione AI. L’applicazione presenta modelli moderni di Node.js e tecniche di integrazione AI.
Puoi trovare il codice completo di questo esempio nel repository GitHub.
Buon coding!
Source:
https://dzone.com/articles/building-weather-service-genkit-ai