개요
이 프로젝트는 Genkit, TypeScript, OpenWeatherAPI 및 GitHub 모델을 사용하여 AI 강화 날씨 서비스를 구축하는 방법을 보여줍니다. 이 애플리케이션은 현대적인 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 서버 구성
애플리케이션은 Genkit Express 플러그인을 사용하여 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
그런 다음 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 지원
라이선스
아파치 2.0
리소스
결론
이 프로젝트는 Genkit을 사용하여 Node.js에서 AI 통합을 활용한 날씨 서비스를 구축하는 방법을 보여줍니다. 이 애플리케이션은 현대적인 Node.js 패턴과 AI 통합 기술을 보여줍니다.
이 예제의 전체 코드는 GitHub 저장소에서 확인할 수 있습니다.
코딩을 즐기세요!
Source:
https://dzone.com/articles/building-weather-service-genkit-ai