סקירה
פרויקט זה מדגים כיצד לבנות שירות מזג אוויר משודרג על ידי שימוש ב-Genkit, TypeScript, OpenWeatherAPI, ומודלי GitHub. האפליקציה מציגה תבניות עכשוויות של Node.js וטכניקות אינטגרציה של AI.
דרישות מוקדמות
לפני התחלת הפעולה, וודא שיש לך את הפריטים הבאים:
- Node.js מותקן על המחשב שלך
- חשבון GitHub ואסימון גישה ל- API של GitHub
- מפתח OpenWeatherAPI לקבלת נתוני מזג האוויר
- Genkit CLI מותקן על המחשב שלך
צלילה טכנית
הגדרת AI
ההגדרה העיקרית של AI מתבצעת עם תצורת Genkit ואינטגרציה של תוסף GitHub. במקרה זה, אנו משתמשים ב-OpenAI GPT-3 מודל:
const ai = genkit({
plugins: [
github({ githubToken: process.env.GITHUB_TOKEN }),
],
model: openAIO3Mini,
});
מימוש כלי מזג האוויר
האפליקציה מגדירה כלי מזג אוויר מותאם באמצעות אימות סכמה של 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`;
}
);
הגדרת זרימת AI
השירות מספק זרימת AI שמעבדת בקשות מזג אוויר:
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
האפליקציה משתמשת בתוסף Express של Genkit כדי ליצור שרת API:
const app = express({
flows: [helloFlow],
});
קוד מלא
הקוד המלא לשירות מזג האוויר הוא כדלקמן:
/* 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]
});
התקנה ופיתוח
1. התקן תלות:
npm install
2. הגדר משתני סביבה:
GITHUB_TOKEN=your_token
OPENWEATHER_API_KEY=your_key
3. הפעל את שרת הפיתוח:
npm run genkit:start
4. כדי להפעיל את הפרויקט במצב דיבאג ולהגדיר נקודות עצירה, ניתן להריץ:
npm run genkit:start:debug
לאחר מכן, להפעיל את הדיבאגר בסביבת הפיתוח שלך. ראה את הקובץ .vscode/launch.json
להגדרה.
5. אם ברצונך לבנות את הפרויקט, ניתן להריץ:
npm run build
6. להפעיל את הפרויקט במצב ייצור:
npm run start:production
תלויות
תלויות ליבה
genkit
: ^1.0.5@genkit-ai/express
: ^1.0.5openweather-api-node
: ^3.1.5genkitx-github
: ^1.13.1dotenv
: ^16.4.7
תלויות פיתוח
tsx
: ^4.19.2typescript
: ^5.7.2
הגדרות הפרויקט
- משתמש במודולי ES (
"type": "module"
) - סיומת TypeScript עם המרת תיקיות
NodeNext
- תיקיית פלט: lib
- תמיכה מלאה ב-TypeScript עם הגדרות סוג
רישיון
אפאצ'י 2.0
משאבים
סיכום
הפרויקט הזה מדגים איך לבנות שירות מזג אוויר באמצעות Genkit ב-Node.js עם שילוב AI. האפליקציה מציגה תבניות מודרניות של Node.js וטכניקות שילוב AI.
תוכל למצוא את הקוד המלא של הדוגמה ב-מאגר הקוד של GitHub.
בהצלחה בכתיבת הקודים!
Source:
https://dzone.com/articles/building-weather-service-genkit-ai