Aperçu
Ce projet montre comment construire un service météorologique amélioré par l’IA en utilisant Genkit, TypeScript, OpenWeatherAPI et les modèles GitHub. L’application met en avant des modèles Node.js modernes et des techniques d’intégration de l’IA.
Prérequis
Avant de commencer, assurez-vous de disposer des éléments suivants :
- Node.js installé sur votre machine
- Un compte GitHub et un jeton d’accès pour les API GitHub
- Une clé OpenWeatherAPI pour récupérer les données météorologiques
- Genkit CLI installé sur votre machine
Approfondissement technique
Configuration de l’IA
La configuration de base de l’IA est initialisée avec l’intégration de Genkit et du plugin GitHub. Dans ce cas, nous allons utiliser le modèle OpenAI GPT-3:
const ai = genkit({
plugins: [
github({ githubToken: process.env.GITHUB_TOKEN }),
],
model: openAIO3Mini,
});
Implémentation de l’outil météorologique
L’application définit un outil météorologique personnalisé en utilisant la validation du schéma 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`;
}
);
Définition du flux de l’IA
Le service expose un flux d’IA qui traite les demandes météorologiques :
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;
}
);
Configuration du serveur Express
L’application utilise le plugin Express de Genkit pour créer un serveur API :
const app = express({
flows: [helloFlow],
});
Code complet
Le code complet du service météorologique est le suivant :
/* 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]
});
Configuration et développement
1. Installer les dépendances :
npm install
2. Configurer les variables d’environnement :
GITHUB_TOKEN=your_token
OPENWEATHER_API_KEY=your_key
3. Démarrer le serveur de développement :
npm run genkit:start
4. Pour exécuter le projet en mode débogage et définir des points d’arrêt, vous pouvez exécuter :
npm run genkit:start:debug
Ensuite, lancez le débogueur dans votre IDE. Consultez le fichier .vscode/launch.json
pour la configuration.
5. Si vous souhaitez construire le projet, vous pouvez exécuter :
npm run build
6. Exécutez le projet en mode production :
npm run start:production
Dépendances
Dépendances Principales
genkit
: ^1.0.5@genkit-ai/express
: ^1.0.5openweather-api-node
: ^3.1.5genkitx-github
: ^1.13.1dotenv
: ^16.4.7
Dépendances de Développement
tsx
: ^4.19.2typescript
: ^5.7.2
Configuration du Projet
- Utilise des Modules ES (
"type": "module"
) - TypeScript avec résolution de module
NodeNext
- Répertoire de sortie : lib
- Support complet de TypeScript avec des définitions de type
Licence
Apache 2.0
Ressources
Conclusion
Ce projet démontre comment construire un service météorologique en utilisant Genkit en Node.js avec une intégration AI. L’application met en avant des modèles Node.js modernes et des techniques d’intégration AI.
Vous pouvez trouver le code complet de cet exemple dans le dépôt GitHub.
Bon codage!
Source:
https://dzone.com/articles/building-weather-service-genkit-ai