[TIL/React] 2025/01/26
Google AI Studio ์ ์ฉํ๊ธฐ ๐ํ๋ก๊ทธ๋๋ฐ ๊ด๋ จ Q&A ์ฌ์ดํธ๋ฅผ ์ด์ฉํ๋ฉฐ ์๊ฒจ๋ ์ถ๊ฐ์ ์ธ ์ง๋ฌธ์, ์๋น์ค ๋ด์์ AI์๊ฒ ์ง๋ฌธํ ์ ์๋๋ก, ๊ด๋ จ ํ์ด์ง๋ฅผ ๊ฐ๋ฐํ๊ณ ์ถ์๋ค.Google AI Studio๋ฅผ ํ์ฉํ๋ค.Get API Key ๋ฒํผ์ ํด๋ฆญํ ๋ค, AP
Google AI Studio ์ ์ฉํ๊ธฐ ๐
ํ๋ก๊ทธ๋๋ฐ ๊ด๋ จ Q&A ์ฌ์ดํธ๋ฅผ ์ด์ฉํ๋ฉฐ ์๊ฒจ๋ ์ถ๊ฐ์ ์ธ ์ง๋ฌธ์, ์๋น์ค ๋ด์์ AI์๊ฒ ์ง๋ฌธํ ์ ์๋๋ก, ๊ด๋ จ ํ์ด์ง๋ฅผ ๊ฐ๋ฐํ๊ณ ์ถ์๋ค.
Google AI Studio๋ฅผ ํ์ฉํ๋ค.
1. ์ด๊ธฐ ์ค์ โ๏ธ
1-1. Get API Key ๐ฟ
Get API Key ๋ฒํผ์ ํด๋ฆญํ ๋ค, API ํค ๋ง๋ค๊ธฐ ๋ฒํผ์ ํด๋ฆญํ์ฌ API Key๋ฅผ ๋ฐ๊ธ๋ฐ๋๋ค.


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

1-2. Gemini SDK Initialization ๐ฟ
SDK ํจํค์ง๋ฅผ ์ค์นํ๋ค. ํฅํ ํด๋น SDK ํจํค์ง๋ฅผ ํตํด genAI ๋ชจ๋ธ์ ์์ฑํ ์ ์๋ค.

2. ๊ตฌํ ํ๋ฆ โ๏ธ
2-1. Overall Flow ๐ฟ

๊ธฐ๋ณธ์ ์ผ๋ก AskAIPage์์ ์ง๋ฌธํ๊ธฐ ๋ฒํผ์ ํด๋ฆญํ๋ฉด, ์๋ฒ ์ฃผ์๋ก ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ prompt์ ๋ํ POST ์์ฒญ์ด ์งํ๋๋ค.
์์ฒญ์ ๋ฐ๋ ์๋ฒ์ ๊ฒฝ๋ก๋ controller ์์ ๋ช ์๋์ด ์๊ณ , controller์์ ์ํํ๋ ์ธ๋ถ ๋ก์ง์ธ generateContent ํจ์ ์ฝ๋๋ service์ ์์ฑ๋์ด ์๋ค.
์ต์ข ์ ์ผ๋ก controller์์ ์ฒ๋ฆฌ๋ response ๊ฐ์ state์ ์ ์ฅํ์ฌ front-end์์ ๋ ๋๋ง ํ๋ค.
2-2. gemini.module.ts ๐ฟ
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์ ๋ฑ๋ก๋์ด์ผ ํ๋ค.
2-3. gemini.controller.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์ ์ ์ฅํ ๋ค ์ต์ข ์ ์ผ๋ก ๋ฐํํ๊ฒ ๋๋ค.
2-4. gemini.service.ts ๐ฟ
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()๋ฅผ ํตํด ์๋ต์ผ๋ก๋ถํฐ ํ ์คํธ๋ฅผ ์ถ์ถํ๋ค.
3. ๊ตฌํ ํ๋ฉด โ๏ธ
3-1. Light Mode ๐ฟ

3-2. Dark Mode ๐ฟ

4. ๋ณด์์ โ๏ธ
1. ๊ตฌํ์ ์ง์คํด์ Gemini ๋ฌธ์๋ฅผ ์์ง ์ ๋ ํ์ง๋ ์์ 2. ๋ชจ๋ธ ์ด๋ฆ์ด ํ๋์ฝ๋ฉ๋์ด ์๋๋ฐ, ์ ์ฐ์ฑ์ ์์ ์ ์๊ฒ ๋ค๋ ์๊ฐ์ด ๋ฆ 3. ๋ํ ์ผํ ํ๋ก ํธ์๋ ์ฒ๋ฆฌ๊ฐ ์์ง ๋ถ์กฑํจ
More to read
AI&ML ๊ธฐ์ด
Reference: https://bettermesol.github.io/ml/2019/09/16/ai-ml-dl/AI: ๊ธฐ๊ณ๊ฐ ์ฌ๋์ฒ๋ผ ์๊ฐํ๊ณ ํ๋จํ๊ฒ ๋ง๋๋ ๊ฐ์ฅ ๋์ ๋ฒ์ฃผ์ ๊ธฐ์ ์ ๋๋ค.ML: ๋ฐ์ดํฐ๋ฅผ ํ์ตํ์ฌ ์ค์ค๋ก ๊ท์น์ ์ฐพ์๋ด๋ AI์ ํ ๋ถ์ผ๋ก,
'AI Agent Economy'Novitas : AI Agent๊ฐ ์ง๊ฐ์ ๊ฐ์ง๋ ์ธ์
์ผ๋ง ์ , ๋ฏธ๋์์ ์ฆ๊ถ ๋ฆฌ์์น ๋ฆฌํฌํธ(์ฌํด๋ ์ด๋๋ฆฌ์์ด๋ค: ์์ด์ ํธ ์๋์ Near Automata)๋ฅผ ์ ํ๊ฒ ๋์์ต๋๋ค. AI Agent๋ฅผ ์ธ๊ฐ๊ณผ ํจ๊ปํ ๊ฒฝ์ ์ฃผ์ฒด๋ก ๋ฐ๋ผ๋ณด๋ ์๊ฐ์ ์ ์์ด ์ถฉ๊ฒฉ์ ๋ฐ์๋๋ฌ์ฃ .ํ ๊ฐ์ง ์ง๊ณ ๋์ด๊ฐ ๋ถ๋ถ์ด ์์ต๋๋ค. ์ฐ๋ฆฌ๊ฐ ํํ 'AI'
'ERC-8004'Novitas: AI ์์ด์ ํธ ๊ฒฝ์ ์ฃผ์ฒด
Web 4.0์ ํ ๋ฌธ์ฅ์ผ๋ก ์ ์ํ๋ฉด Sovereign Transact์ ๋๋ค.AI๊ฐ ์ธ๊ฐ์ ํ๋ฝ ์์ด ์ง๊ฐ์ ์์ ํ๊ณ , ๊ฒฐ์ ๋ฅผ ์ํํ๋ฉฐ, ์ธํ๋ผ๋ฅผ ํต์ ํ๋ ์ฃผ๊ถ์ ๊ฒฝ์ ์ฃผ์ฒด๊ฐ ๋๋ ์ธ๊ณ์ ๋๋ค. Web 3.0์ด ๋ธ๋ก์ฒด์ธ ๊ธฐ๋ฐ์ ํ์ค์ํ๋ฅผ ์คํํ๋ค๋ฉด, Web 4.0์ ๊ทธ