C.W.K.
Stream
Lesson 01 of 04 · published

설치와 GoogleGenAI

~10 min · typescript, node, google-genai

Level 0Spark
0 XP0/35 lessons0/10 achievements
0/140 XP to next level140 XP to go0% complete

원하는 패키지는 @google/genai

공식 TypeScript/JavaScript SDK 는 @google/genai (npm scoped 패키지). @google/generative-ai 를 대체 — 그건 2025 년 8 월 EOL. Node.js ≥ 20 필요.

생성할 클래스 하나

Entry point 가 GoogleGenAI. Python SDK 와 미러링되는 세 생성 패턴:

  • new GoogleGenAI({ apiKey: '...' }) — explicit key.
  • new GoogleGenAI({}) — env 의 GOOGLE_API_KEY 읽음 (Node.js).
  • new GoogleGenAI({ vertexai: true, project: '...', location: '...' }) — Vertex AI.

Submodule

생성된 ai 객체가 Python client 와 같은 surface 노출:

  • ai.models — generate, stream, count tokens.
  • ai.chats — multi-turn chat.
  • ai.files — File API 업로드.
  • ai.caches — context caching.
  • ai.live — live multimodal session.

자주 쓸 헬퍼 import

패키지가 schema 헬퍼와 content factory 도 export:

  • Type — schema type enum (Type.STRING, Type.OBJECT 등).
  • createUserContent, createPartFromUri — multimodal content builder.
  • FunctionCallingConfigModeAUTO, ANY, NONE.
  • ApiError — exception 클래스.

Code

설치·bash
npm install @google/genai
# or
pnpm add @google/genai
# or
bun add @google/genai
생성 패턴·typescript
import {
  GoogleGenAI,
  Type,
  ApiError,
  FunctionCallingConfigMode,
  ThinkingLevel,
  createUserContent,
  createPartFromUri,
} from '@google/genai';

// 1. Explicit key
const ai = new GoogleGenAI({ apiKey: 'AIzaSy...' });

// 2. From env (set GOOGLE_API_KEY first)
const ai = new GoogleGenAI({});

// 3. Vertex AI
const ai = new GoogleGenAI({
  vertexai: true,
  project: 'my-gcp-project',
  location: 'us-central1',
});

// 4. Pin a specific API version
const ai = new GoogleGenAI({ apiKey: 'KEY', apiVersion: 'v1' });
첫 호출·typescript
const response = await ai.models.generateContent({
  model: 'gemini-2.5-flash',
  contents: 'Hello, Gemini!',
});
console.log(response.text);  // property, not method

External links

Exercise

fresh Node 프로젝트 (npm init -y) 에 @google/genai 설치, env 에서 client 만들고 Flash 응답 출력하는 작은 hello.mjs 작성. Node ≥ 20 확인. 보너스: 같은 코드 Bun 프로젝트에서 시도 — 변경 없이 동작해야 함.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.