Google AI Studio ์ ์ฉํ๊ธฐ ๐
ํ๋ก๊ทธ๋๋ฐ ๊ด๋ จ Q&A ์ฌ์ดํธ๋ฅผ ์ด์ฉํ๋ฉฐ ์๊ฒจ๋ ์ถ๊ฐ์ ์ธ ์ง๋ฌธ์, ์๋น์ค ๋ด์์ AI์๊ฒ ์ง๋ฌธํ ์ ์๋๋ก, ๊ด๋ จ ํ์ด์ง๋ฅผ ๊ฐ๋ฐํ๊ณ ์ถ์๋ค.
Google AI Studio๋ฅผ ํ์ฉํ๋ค.
Get API Key ๋ฒํผ์ ํด๋ฆญํ ๋ค, API ํค ๋ง๋ค๊ธฐ ๋ฒํผ์ ํด๋ฆญํ์ฌ API Key๋ฅผ ๋ฐ๊ธ๋ฐ๋๋ค.


๊ฒฐ์ ์ค์ ์ ๋ฐ๋ก ํ์ง ์์๊ธฐ์, ๊ธฐ๋ณธ์๊ธ์ ๋ '๋ฌด๋ฃ'๋ก ์ ์ฉ๋๋ค.

SDK ํจํค์ง๋ฅผ ์ค์นํ๋ค. ํฅํ ํด๋น SDK ํจํค์ง๋ฅผ ํตํด genAI ๋ชจ๋ธ์ ์์ฑํ ์ ์๋ค.


๊ธฐ๋ณธ์ ์ผ๋ก AskAIPage์์ ์ง๋ฌธํ๊ธฐ ๋ฒํผ์ ํด๋ฆญํ๋ฉด, ์๋ฒ ์ฃผ์๋ก ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ prompt์ ๋ํ POST ์์ฒญ์ด ์งํ๋๋ค.
์์ฒญ์ ๋ฐ๋ ์๋ฒ์ ๊ฒฝ๋ก๋ controller ์์ ๋ช ์๋์ด ์๊ณ , controller์์ ์ํํ๋ ์ธ๋ถ ๋ก์ง์ธ generateContent ํจ์ ์ฝ๋๋ service์ ์์ฑ๋์ด ์๋ค.
์ต์ข ์ ์ผ๋ก controller์์ ์ฒ๋ฆฌ๋ response ๊ฐ์ state์ ์ ์ฅํ์ฌ front-end์์ ๋ ๋๋ง ํ๋ค.
import { Module } from '@nestjs/common';
import { GoogleGenerativeAIService } from './gemini.service';
import { GoogleGenerativeAIController } from './gemini.controller';
@Module({
controllers: [GoogleGenerativeAIController],
providers: [GoogleGenerativeAIService],
})
export class GeminiModule {}
์ปจํธ๋กค๋ฌ์ ์๋น์ค๋ฅผ ๋ชจ๋ ๋จ์๋ก ๊ด๋ฆฌํ๊ธฐ ์ํด gemini.module.ts ํ์ผ์ ์์ฑํ๋ค. ์ด ํ์ผ์ ์ต์๋จ app.module.ts์ ๋ฑ๋ก๋์ด์ผ ํ๋ค.
import { Controller, Post, Body } from '@nestjs/common';
import { GoogleGenerativeAIService } from './gemini.service';
@Controller('api/ask-ai')
export class GoogleGenerativeAIController {
constructor(
private readonly googleGenerativeAIService: GoogleGenerativeAIService,
) {}
@Post()
async askAI(@Body('prompt') prompt: string): Promise<{ result: string }> {
try {
const result =
await this.googleGenerativeAIService.generateContent(prompt);
return { result };
} catch (error) {
console.error('Error in GoogleGenerativeAIController:', error);
return { result: 'Failed to generate content from Google Generative AI' };
}
}
}
์์ฒญ์ body์์ prompt๋ฅผ ๋ฐ์์์, ์๋น์ค ํ์ผ์ ์์ฑ๋์ด ์๋ generateContent ํจ์๋ก ์ ๋ฌํ๋ค. ํด๋น ํจ์์ return ๊ฐ์ result์ ์ ์ฅํ ๋ค ์ต์ข ์ ์ผ๋ก ๋ฐํํ๊ฒ ๋๋ค.
import { Injectable } from '@nestjs/common';
import { GoogleGenerativeAI } from '@google/generative-ai';
@Injectable()
export class GoogleGenerativeAIService {
private genAI: GoogleGenerativeAI;
constructor() {
const apiKey = process.env.GEMINI_API_KEY;
if (!apiKey) {
throw new Error('API Key is required');
}
this.genAI = new GoogleGenerativeAI(apiKey);
}
async generateContent(prompt: string): Promise<string> {
try {
const model = this.genAI.getGenerativeModel({ model: 'gemini-pro' });
const result = await model.generateContent(prompt);
const response = await result.response;
const text = await response.text();
return text;
} catch (error) {
console.error('Google Generative AI Error:', error);
throw new Error('Failed to generate content from Google Generative AI');
}
}
}
env ํ์ผ์ ๋ฏธ๋ฆฌ ์์ฑํด๋์ apiKey๋ฅผ GoogleGenerativeAI ํจ์์ ์ ๋ฌํด์ genAI๋ผ๋ ํด๋์ค๋ฅผ ์์ฑํ๋ค.
์ดํ ํด๋น ํด๋์ค์ getGenerativeModel์ ํตํด gemini-pro ๋ชจ๋ธ์ ์ฌ์ฉํ ๊ฒ์ ๋ช ์ํ๋ค.
model.generateContent(prompt)๋ฅผ ํตํด prompt์ ๋ํ ์ฝํ ์ธ ๋ฅผ ์์ฑํ๋ค. prompt๋ ํด๋ผ์ด์ธํธ์์ ์ ๋ฌ๋ ํ ์คํธ๋ก, Google Generative AI ๋ชจ๋ธ์ด ํด๋น ํ ์คํธ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ฝํ ์ธ ๋ฅผ ์์ฑํ๊ฒ ๋๋ค.
์ต์ข ์ ์ผ๋ก response.text()๋ฅผ ํตํด ์๋ต์ผ๋ก๋ถํฐ ํ ์คํธ๋ฅผ ์ถ์ถํ๋ค.

