WatsonxChatGenerator
WatsonxChatGenerator
IBM watsonx 모델을 채팅 생성에 사용하는 컴포넌트예요. granite-4-h-small 같은 모델을 지원해요.
파이프라인에서 가장 흔한 위치: ChatPromptBuilder 다음
필수 init 변수: api_key — IBM Cloud API 키. WATSONX_API_KEY 환경 변수로도 설정할 수 있어요. / project_id — IBM Cloud 프로젝트 ID. WATSONX_PROJECT_ID 환경 변수로도 설정할 수 있어요.
필수 run 변수: messages — ChatMessage 객체 리스트
출력 변수: replies — ChatMessage 객체 리스트
API 레퍼런스: Watsonx
GitHub 링크: https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/watsonx
패키지 이름: watsonx-haystack
출처: 문서
본문
이 통합은 ibm/granite-4-h-small, meta-llama/llama-3-3-70b-instruct, mistralai/mistral-small-3-1-24b-instruct-2503 같은 IBM watsonx.ai 파운데이션 모델을 지원해요. 이 모델들은 IBM 클라우드 플랫폼을 통해 고품질 채팅 완성 기능을 제공해요. 최신 전체 목록은 IBM watsonx.ai 문서에서 확인하세요.
개요 (Overview)
WatsonxChatGenerator는 동작하려면 IBM Cloud 자격 증명이 필요해요. 이렇게 설정할 수 있어요:
- Secret API를 사용한
api_key와project_idinit 파라미터 WATSONX_API_KEY와WATSONX_PROJECT_ID환경 변수 (권장)
그리고 컴포넌트는 동작에 프롬프트가 필요해요. IBM watsonx.ai API에 유효한 어떤 텍스트 생성 파라미터든 generation_kwargs 파라미터로 이 컴포넌트에 직접 전달할 수 있어요. 초기화 때와 run() 메서드 둘 다 가능해요. IBM watsonx.ai API가 지원하는 파라미터에 대한 자세한 내용은 IBM watsonx.ai 문서를 참고하세요.
마지막으로 컴포넌트는 동작에 ChatMessage 객체 리스트가 필요해요. ChatMessage는 메시지, 역할(누가 생성했는지 — user, assistant, system, tool 등), 그리고 선택적 메타데이터를 담는 데이터 클래스예요.
스트리밍 (Streaming)
이 Generator는 LLM에서 나오는 토큰을 출력으로 직접 스트리밍할 수 있어요. streaming_callback init 파라미터에 함수를 넘기면 돼요.
사용법 (Usage)
WatsonxChatGenerator를 쓰려면 watsonx-haystack 패키지를 설치해야 해요:
pip install watsonx-haystack
단독으로 사용하기 (On its own)
from haystack_integrations.components.generators.watsonx.chat.chat_generator import (
WatsonxChatGenerator,
)
from haystack.dataclasses import ChatMessage
from haystack.utils import Secret
generator = WatsonxChatGenerator(
api_key=Secret.from_env_var("WATSONX_API_KEY"),
project_id=Secret.from_env_var("WATSONX_PROJECT_ID"),
model="ibm/granite-4-h-small",
)
message = ChatMessage.from_user("What's Natural Language Processing? Be brief.")
print(generator.run(messages=[message]))
멀티모달 입력 사용:
from haystack.dataclasses import ChatMessage, ImageContent
from haystack_integrations.components.generators.watsonx.chat.chat_generator import (
WatsonxChatGenerator,
)
# Use a multimodal model
llm = WatsonxChatGenerator(model="meta-llama/llama-3-2-11b-vision-instruct")
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(messages=[user_message])["replies"][0].text
print(response)
# Red apple on straw.
파이프라인 안에서 사용하기 (In a Pipeline)
WatsonxChatGenerator를 사용해 파이프라인에 IBM watsonx.ai 채팅 모델을 넣을 수도 있어요.
from haystack import Pipeline
from haystack.components.builders import ChatPromptBuilder
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.generators.watsonx.chat.chat_generator import (
WatsonxChatGenerator,
)
from haystack.utils import Secret
pipe = Pipeline()
pipe.add_component("prompt_builder", ChatPromptBuilder())
pipe.add_component(
"llm",
WatsonxChatGenerator(
api_key=Secret.from_env_var("WATSONX_API_KEY"),
project_id=Secret.from_env_var("WATSONX_PROJECT_ID"),
model="ibm/granite-4-h-small",
),
)
pipe.connect("prompt_builder", "llm")
country = "Germany"
system_message = ChatMessage.from_system(
"You are an assistant giving out valuable information to language learners.",
)
messages = [
system_message,
ChatMessage.from_user("What's the official language of {{ country }}?"),
]
res = pipe.run(
data={
"prompt_builder": {
"template_variables": {"country": country},
"template": messages,
},
},
)
print(res)