概述
该项目演示了如何使用Genkit、TypeScript、OpenWeatherAPI和GitHub模型构建一个AI增强的天气服务。该应用展示了现代Node.js模式和AI集成技术。
先决条件
开始之前,请确保您具备以下条件:
- 在您的计算机上安装Node.js
- GitHub帐户和GitHub API的访问令牌
- 用于获取天气数据的OpenWeatherAPI密钥
- 在您的计算机上安装Genkit CLI
技术深入挖掘
AI配置
核心AI设置使用Genkit和GitHub插件集成进行初始化。在本例中,我们将使用OpenAI GPT-3模型:
TypeScript
const ai = genkit({
plugins: [
github({ githubToken: process.env.GITHUB_TOKEN }),
],
model: openAIO3Mini,
});
天气工具实现
该应用程序使用Zod模式验证定义了一个自定义天气工具:
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流程定义
该服务公开了一个处理天气请求的AI流程:
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服务器配置
该应用程序使用Genkit Express插件创建API服务器:
TypeScript
const app = express({
flows: [helloFlow],
});
完整代码
天气服务的完整代码如下:
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]
});
设置和开发
1. 安装依赖项:
Shell
npm install
2. 配置环境变量:
Shell
GITHUB_TOKEN=your_token
OPENWEATHER_API_KEY=your_key
3. 启动开发服务器:
Shell
npm run genkit:start
4. 要在调试模式下运行项目并设置断点,您可以运行:
Shell
npm run genkit:start:debug
然后,在您的集成开发环境中启动调试器。查看.vscode/launch.json
文件以获取配置信息。
5. 如果您想要构建项目,可以运行:
Shell
npm run build
6. 以生产模式运行项目:
Shell
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
资源
结语
这个项目展示了如何在Node.js中使用Genkit构建一个天气服务,并集成了人工智能。该应用展示了现代Node.js模式和人工智能集成技术。
您可以在GitHub存储库中找到这个示例的完整代码。
祝编码愉快!
Source:
https://dzone.com/articles/building-weather-service-genkit-ai