概要
このプロジェクトでは、Genkit、TypeScript、OpenWeatherAPI、およびGitHubモデルを使用してAIを活用した天気サービスを構築する方法を示しています。このアプリケーションは、モダンなNode.jsパターンとAI統合技術を紹介しています。
前提条件
始める前に、以下のものを用意してください:
- マシンにNode.jsがインストールされていること
- GitHubアカウントとGitHub APIのアクセストークン
- 天気データを取得するための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サーバーの設定
アプリケーションは、APIサーバーを作成するためにGenkit Expressプラグインを使用しています:
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
その後、IDE でデバッガを起動します。構成については、.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"
) NodeNext
モジュール解決を使用した TypeScript- 出力ディレクトリ: lib
- 型定義を含む完全な TypeScript サポート
ライセンス
Apache 2.0
リソース
Conclusion
このプロジェクトは、Genkitを使用してNode.jsで天気サービスを構築する方法を示しています。アプリケーションは、最新のNode.jsパターンとAI統合技術を紹介しています。
この例の完全なコードは、GitHubリポジトリで見つけることができます。
Happy coding!
Source:
https://dzone.com/articles/building-weather-service-genkit-ai