NvidiaChatGenerator
NvidiaChatGenerator
NVIDIA에서 호스팅하는 모델로 채팅 완성(chat completion)을 할 수 있게 해 주는 Generator예요.
출처: 문서
본문
NvidiaChatGenerator는 NVIDIA API를 통해 NVIDIA 생성 모델로 채팅 완성을 지원해요. 입력과 출력 모두 ChatMessage 형식과 호환되어서 채팅 기반 파이프라인에 자연스럽게 들어가요.
NVIDIA NIM으로 자체 호스팅한 LLM을 쓰거나 NVIDIA API Catalog에 호스팅된 모델을 쓸 수 있어요. 이 컴포넌트의 기본 모델은 meta/llama-3.1-8b-instruct예요.
이 통합을 쓰려면 NVIDIA API 키가 필요해요. NVIDIA_API_KEY 환경 변수나 Secret으로 제공하면 돼요.
툴 지원
NvidiaChatGenerator는 tools 파라미터로 함수 호출(function calling)을 지원하는데, 유연한 툴 구성을 받아요.
- Tool 객체 목록: 개별 툴을 목록으로 넘겨요.
- 단일 Toolset: Toolset 전체를 그대로 넘겨요.
- Tool과 Toolset 혼합: 여러 Toolset을 독립 툴과 한 목록에 섞어요.
이렇게 하면 관련 툴을 논리적 그룹으로 묶으면서 필요할 때 독립 툴도 함께 넣을 수 있어요.
from haystack.tools import Tool, Toolset
from haystack_integrations.components.generators.nvidia import NvidiaChatGenerator
# 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 = NvidiaChatGenerator(
tools=[math_toolset, weather_tool, news_tool] # Mix of Toolset and Tool objects
)
툴 작업에 대한 자세한 내용은 Tool과 Toolset 문서를 참고하세요.
스트리밍
이 Generator는 LLM의 스트리밍 응답을 지원해요. 스트리밍을 켜려면 초기화할 때 streaming_callback 파라미터로 콜러블을 넘기면 돼요.
더 알아보기 (Learn more)
NvidiaChatGenerator를 쓰려면 nvidia-haystack 패키지를 설치해요.
pip install nvidia-haystack
NVIDIA API Catalog에 있는 모든 LLM이나 NVIDIA NIM으로 배포한 모델과 함께 쓸 수 있어요. 자세한 내용은 NVIDIA NIM for LLMs Playbook을 참고하세요.
단독으로 쓰기
NVIDIA API Catalog의 LLM을 쓰려면 필요할 때 api_base_url(기본값 https://integrate.api.nvidia.com/v1)과 API 키를 지정해요. API 키는 NVIDIA API Catalog에서 얻을 수 있어요.
from haystack.dataclasses import ChatMessage
from haystack.utils import Secret
from haystack_integrations.components.generators.nvidia import NvidiaChatGenerator
generator = NvidiaChatGenerator(
model="meta/llama-3.1-8b-instruct",
api_key=Secret.from_env_var("NVIDIA_API_KEY"),
)
messages = [ChatMessage.from_user("What's Natural Language Processing? Be brief.")]
result = generator.run(messages)
print(result["replies"])
멀티모달 입력 사용:
from haystack.dataclasses import ChatMessage, ImageContent
from haystack.utils import Secret
from haystack_integrations.components.generators.nvidia import NvidiaChatGenerator
llm = NvidiaChatGenerator(
model="meta/llama-3.2-11b-vision-instruct",
api_key=Secret.from_env_var("NVIDIA_API_KEY"),
)
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.
파이프라인에서 쓰기
from haystack import Pipeline
from haystack.components.builders import ChatPromptBuilder
from haystack.dataclasses import ChatMessage
from haystack.utils import Secret
from haystack_integrations.components.generators.nvidia import NvidiaChatGenerator
pipe = Pipeline()
pipe.add_component("prompt_builder", ChatPromptBuilder())
pipe.add_component(
"llm",
NvidiaChatGenerator(
model="meta/llama-3.1-8b-instruct",
api_key=Secret.from_env_var("NVIDIA_API_KEY"),
),
)
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)