langchain-google-genai 패키지는 이러한 모델에 대한 LangChain 통합을 제공합니다. 이는 개별 개발자에게 가장 좋은 시작점인 경우가 많습니다.
최신 모델, 기능, context window 등에 대한 정보는 Google AI 문서를 참조하세요. 모든 model id는 Gemini API 문서에서 확인할 수 있습니다.
Integration 세부 정보
| Class | Package | Local | Serializable | JS support | Downloads | Version |
|---|---|---|---|---|---|---|
| ChatGoogleGenerativeAI | langchain-google-genai | ❌ | beta | ✅ |
Model 기능
| Tool calling | Structured output | JSON mode | Image input | Audio input | Video input | Token-level streaming | Native async | Token usage | Logprobs |
|---|---|---|---|---|---|---|---|---|---|
| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |
Setup
Google AI 모델에 액세스하려면 Google 계정을 생성하고, Google AI API key를 받고,langchain-google-genai integration 패키지를 설치해야 합니다.
1. Installation:
Chat models
ChatGoogleGenerativeAI class를 사용하여 Google의 chat model과 상호작용하세요. 전체 세부 정보는 API reference를 참조하세요.
Instantiation
이제 model 객체를 인스턴스화하고 chat completion을 생성할 수 있습니다:Invocation
Multimodal 사용
Gemini 모델은 multimodal 입력(텍스트, 이미지, 오디오, 비디오)을 받을 수 있으며, 일부 모델의 경우 multimodal 출력을 생성할 수 있습니다.Image Input
list content 형식의HumanMessage를 사용하여 텍스트와 함께 이미지 입력을 제공하세요. gemini-2.5-flash와 같이 이미지 입력을 지원하는 모델을 사용해야 합니다.
image_url 형식:
- Google Cloud Storage URI (
gs://...). service account에 액세스 권한이 있는지 확인하세요. - PIL Image 객체 (라이브러리가 인코딩을 처리합니다).
Audio Input
텍스트와 함께 오디오 파일 입력을 제공하세요.Video Input
텍스트와 함께 비디오 파일 입력을 제공하세요.Image Generation (Multimodal Output)
특정 모델(예:gemini-2.0-flash-preview-image-generation)은 텍스트와 이미지를 인라인으로 생성할 수 있습니다. 원하는 response_modalities를 지정해야 합니다. 자세한 내용은 Gemini API 문서를 참조하세요.
Tool Calling
모델에 호출할 tool을 장착할 수 있습니다.Structured Output
Pydantic model을 사용하여 모델이 특정 구조로 응답하도록 강제합니다.Structured Output 방법
structured output에 대해 두 가지 방법이 지원됩니다:method="function_calling"(기본값): tool calling을 사용하여 구조화된 데이터를 추출합니다. 모든 Gemini 모델과 호환됩니다.method="json_schema"또는method="json_mode":responseSchema와 함께 Gemini의 네이티브 structured output을 사용합니다. 더 신뢰할 수 있지만 Gemini 1.5+ 모델이 필요합니다. (json_mode는 하위 호환성을 위해 유지됩니다)
json_schema 방법은 tool call 후처리에 의존하는 대신 모델의 생성 프로세스를 직접 제약하므로 더 나은 신뢰성을 위해 권장됩니다.
Token Usage 추적
response metadata에서 token 사용 정보에 액세스하세요.Built-in tools
Google Gemini는 다양한 built-in tool(google search, code execution)을 지원하며, 일반적인 방식으로 모델에 바인딩할 수 있습니다.Native Async
비차단 호출을 위해 비동기 메서드를 사용하세요.Safety Settings
Gemini 모델에는 재정의할 수 있는 기본 safety 설정이 있습니다. 모델에서 많은 “Safety Warning”을 받는 경우 모델의safety_settings 속성을 조정해 볼 수 있습니다. 예를 들어, 위험한 콘텐츠에 대한 safety 차단을 끄려면 다음과 같이 LLM을 구성할 수 있습니다:
API reference
모든 ChatGoogleGenerativeAI 기능 및 구성에 대한 자세한 문서는 API reference를 참조하세요.Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.