GoogleGenAIChatGenerator
GoogleGenAIChatGenerator
Google Gen AI SDK를 통해 Google Gemini 모델을 사용한 채팅 완성을 가능하게 하는 구성 요소예요.
출처: 문서
본문
GoogleGenAIChatGenerator는 gemini-3.8-flash, gemini-3.7-flash, gemini-3.6-flash, gemini-3.5-flash, gemini-3.5-flash-lite, gemini-3.1-pro-preview, gemini-3.1-flash-lite, gemini-3-flash-preview, gemini-2.5-pro, gemini-2.5-flash, gemini-2.5-flash-lite 같은 Gemini 생성 모델을 지원해요. gemini-3.8-flash가 기본값이에요.
Tool Support
GoogleGenAIChatGenerator는 tools 파라미터를 통한 함수 호출을 지원하며, 유연한 tool 구성을 받아들여요:
- Tool 객체 리스트: 개별 tool을 리스트로 전달
- 단일 Toolset: Toolset 전체를 직접 전달
- Tool과 Toolset 혼합: 여러 Toolset과 독립 tool을 단일 리스트로 결합
이렇게 하면 관련 tool을 논리적 그룹으로 정리하면서 필요한 독립 tool도 함께 포함할 수 있어요.
from haystack.tools import Tool, Toolset
from haystack_integrations.components.generators.google_genai import (
GoogleGenAIChatGenerator,
)
# Create individual tools
weather_tool = Tool(
name="weather", description="Get weather info", parameters=..., function=...
)
news_tool = Tool(
name="news", description="Get latest news", parameters=..., function=...
)
# Group related tools into a toolset
math_toolset = Toolset([add_tool, subtract_tool, multiply_tool])
# Pass mixed tools and toolsets to the generator
generator = GoogleGenAIChatGenerator(
tools=[math_toolset, weather_tool, news_tool] # Mix of Toolset and Tool objects
)
tool 작업에 대한 자세한 내용은 Tool 및 Toolset 문서를 확인하세요.
Streaming
이 Generator는 LLM의 토큰을 출력으로 직접 스트리밍하는 것을 지원해요. 그러려면 streaming_callback init 파라미터에 함수를 전달하세요.
Authentication
Google Gen AI는 Gemini Developer API와 Vertex AI API 모두와 호환돼요. Gemini Developer API와 함께 이 컴포넌트를 사용하고 API 키를 얻으려면 Google AI Studio를 방문하세요. Vertex AI API와 함께 사용하려면 Google Cloud > Vertex AI를 방문하세요. 컴포넌트는 기본적으로 GOOGLE_API_KEY 또는 GEMINI_API_KEY 환경 변수를 사용해요. 그렇지 않으면 초기화 시점에 Secret과 Secret.from_token 정적 메서드로 API 키를 전달할 수 있어요:
chat_generator = GoogleGenAIChatGenerator(api_key=Secret.from_token("<your-api-key>"))
다음 예시는 Gemini Developer API와 Vertex AI API로 이 컴포넌트를 사용하는 방법을 보여줘요.
Gemini Developer API (API Key Authentication)
from haystack_integrations.components.generators.google_genai import (
GoogleGenAIChatGenerator,
)
# set the environment variable (GOOGLE_API_KEY or GEMINI_API_KEY)
chat_generator = GoogleGenAIChatGenerator()
Vertex AI (Application Default Credentials)
from haystack_integrations.components.generators.google_genai import (
GoogleGenAIChatGenerator,
)
# Using Application Default Credentials (requires gcloud auth setup)
chat_generator = GoogleGenAIChatGenerator(
api="vertex",
vertex_ai_project="my-project",
vertex_ai_location="us-central1",
)
Vertex AI (API Key Authentication)
from haystack_integrations.components.generators.google_genai import (
GoogleGenAIChatGenerator,
)
# set the environment variable (GOOGLE_API_KEY or GEMINI_API_KEY)
chat_generator = GoogleGenAIChatGenerator(api="vertex")
- 대표적인 파이프라인 위치:
ChatPromptBuilder뒤 - 필수 init 변수:
api_key— Google API 키.GOOGLE_API_KEYenv var로 설정 가능. - 필수 run 변수:
messages— 채팅을 나타내는ChatMessage객체 리스트 - 출력 변수:
replies— 입력 채팅에 대한 모델의 대안 답변 리스트 - API reference: Google GenAI
- 패키지명:
google-genai-haystack
Usage
pip install google-genai-haystack
On its own
from haystack.dataclasses.chat_message import ChatMessage
from haystack_integrations.components.generators.google_genai import (
GoogleGenAIChatGenerator,
)
# Initialize the chat generator
chat_generator = GoogleGenAIChatGenerator()
# Generate a response
messages = [ChatMessage.from_user("Tell me about movie Shawshank Redemption")]
response = chat_generator.run(messages=messages)
print(response["replies"][0].text)
멀티모달 입력:
from haystack.dataclasses import ChatMessage, ImageContent
from haystack_integrations.components.generators.google_genai import (
GoogleGenAIChatGenerator,
)
llm = GoogleGenAIChatGenerator()
image = ImageContent.from_file_path("apple.jpg")
user_message = ChatMessage.from_user(
content_parts=["What does the image show? Max 5 words.", image],
)
response = llm.run([user_message])["replies"][0].text
print(response)
# Red apple on straw.
함수 호출도 쉽게 사용할 수 있어요. 먼저 함수를 로컬로 정의하고 Tool로 변환하세요:
from typing import Annotated
from haystack.tools import create_tool_from_function
# example function to get the current weather
def get_current_weather(
location: Annotated[
str,
"The city for which to get the weather, e.g. 'San Francisco'",
] = "Munich",
unit: Annotated[str, "The unit for the temperature, e.g. 'celsius'"] = "celsius",
) -> str:
return f"The weather in {location} is sunny. The temperature is 20 {unit}."
tool = create_tool_from_function(get_current_weather)
GoogleGenAIChatGenerator의 새 인스턴스를 만들어 tool을 설정하세요:
import os
from haystack_integrations.components.generators.google_genai import (
GoogleGenAIChatGenerator,
)
os.environ["GOOGLE_API_KEY"] = "<MY_API_KEY>"
genai_chat = GoogleGenAIChatGenerator(tools=[tool])
그리고 질문을 던지면 돼요. 모델이 tool 호출을 준비하고, 코드가 Tool.invoke로 그것을 실행하며, 결과가 최종 답변을 위해 모델로 돌아가요:
from haystack.dataclasses import ChatMessage
messages = [ChatMessage.from_user("What is the temperature in celsius in Berlin?")]
replies = genai_chat.run(messages=messages)["replies"]
print(replies[0].tool_calls)
# >> [ToolCall(tool_name='get_current_weather',
# >> arguments={'unit': 'celsius', 'location': 'Berlin'}, id=None, extra=None)]
tool_messages = []
for tool_call in replies[0].tool_calls:
result = tool.invoke(**tool_call.arguments)
tool_messages.append(ChatMessage.from_tool(tool_result=result, origin=tool_call))
messages = messages + replies + tool_messages
final_replies = genai_chat.run(messages=messages)["replies"]
print(final_replies[0].text)
# >> The temperature in Berlin is 20 degrees Celsius.
With an Agent
tool 호출 루프를 직접 돌리는 대신, Generator와 tool을 Agent에 전달하세요. Agent가 모델이 tool 호출을 준비하게 하고, 실행하며, 최종 답변이 나올 때까지 결과를 다시 공급해요:
import os
from haystack.components.agents import Agent
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.generators.google_genai import (
GoogleGenAIChatGenerator,
)
os.environ["GOOGLE_API_KEY"] = "<MY_API_KEY>"
agent = Agent(
chat_generator=GoogleGenAIChatGenerator(),
tools=[tool],
)
result = agent.run(
messages=[ChatMessage.from_user("What is the temperature in celsius in Berlin?")]
)
print(result["last_message"].text)
# >> The temperature in Berlin is 20 degrees Celsius.
With Streaming
from haystack.dataclasses.chat_message import ChatMessage
from haystack.dataclasses import StreamingChunk
from haystack_integrations.components.generators.google_genai import (
GoogleGenAIChatGenerator,
)
def streaming_callback(chunk: StreamingChunk):
print(chunk.content, end="", flush=True)
# Initialize with streaming callback
chat_generator = GoogleGenAIChatGenerator(streaming_callback=streaming_callback)
# Generate a streaming response
messages = [ChatMessage.from_user("Write a short story")]
response = chat_generator.run(messages=messages)
# Text will stream in real-time through the callback
In a pipeline
import os
from haystack.components.builders import ChatPromptBuilder
from haystack.dataclasses import ChatMessage
from haystack import Pipeline
from haystack_integrations.components.generators.google_genai import (
GoogleGenAIChatGenerator,
)
# no parameter init, we don't use any runtime template variables
prompt_builder = ChatPromptBuilder()
os.environ["GOOGLE_API_KEY"] = "<MY_API_KEY>"
genai_chat = GoogleGenAIChatGenerator()
pipe = Pipeline()
pipe.add_component("prompt_builder", prompt_builder)
pipe.add_component("genai", genai_chat)
pipe.connect("prompt_builder.prompt", "genai.messages")
location = "Rome"
messages = [ChatMessage.from_user("Tell me briefly about {{location}} history")]
res = pipe.run(
data={
"prompt_builder": {
"template_variables": {"location": location},
"template": messages,
},
},
)
print(res)