LlamaStackChatGenerator
LlamaStackChatGenerator
Llama Stack 서버의 추론 제공자가 제공하는 어떤 모델로든 채팅 완성을 만들어주는 컴포넌트예요. 주로 ChatPromptBuilder 뒤에 두면 돼요.
본문
개요
Llama Stack은 다양한 환경에서 AI 애플리케이션 개발을 간소화하는 빌딩 블록과 통합 API를 제공해요.
LlamaStackChatGenerator는 Llama Stack 서버에 호스팅된 추론 제공자가 노출하는 어떤 LLM이든 사용할 수 있게 해줘요. 기본 제공자의 세부 사항을 추상화해서, 추론 백엔드와 무관하게 같은 클라이언트 코드를 재사용할 수 있어요. 지원되는 제공자와 설정 옵션 목록은 Llama Stack 문서를 참고하세요.
이 컴포넌트는 구조화된 입출력을 위해 다른 Haystack Chat Generator와 같은 ChatMessage 형식을 사용해요. 자세한 내용은 ChatMessage 문서를 확인하세요.
도구 지원
LlamaStackChatGenerator는 tools 파라미터를 통해 함수 호출을 지원해요. 유연한 도구 구성을 받아들여요.
- Tool 객체 목록: 개별 도구를 리스트로 전달.
- 단일 Toolset: 전체 Toolset을 바로 전달.
- 도구·Toolset 혼합: 여러 Toolset을 독립 도구와 한 리스트에 결합.
관련 도구를 논리적 그룹으로 묶으면서 필요할 때 독립 도구도 포함할 수 있어요.
from haystack.tools import Tool, Toolset
from haystack_integrations.components.generators.llama_stack import (
LlamaStackChatGenerator,
)
# 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 = LlamaStackChatGenerator(
model="ollama/llama3.2:3b",
tools=[math_toolset, weather_tool, news_tool], # Mix of Toolset and Tool objects
)
도구 작업에 대한 자세한 내용은 Tool과 Toolset 문서를 참고하세요.
초기화
이 통합을 쓰려면 다음이 필요해요.
- 실행 중인 Llama Stack 서버 인스턴스(로컬 또는 원격)
- 선택한 추론 제공자가 지원하는 유효한 모델 이름
그다음 model 이름이나 ID를 지정해서 LlamaStackChatGenerator를 초기화해요. 값은 서버에서 실행 중인 추론 제공자에 따라 달라져요.
예시:
- Ollama:
model="ollama/llama3.2:3b" - vLLM:
model="meta-llama/Llama-3.2-3B"
참고: 추론 제공자를 바꾸려면 모델 이름만 업데이트하면 돼요.
스트리밍
이 Generator는 LLM의 토큰을 출력에 직접 스트리밍할 수 있어요. 그러려면 streaming_callback 초기화 파라미터에 함수를 전달하면 돼요.
사용법
이 통합을 쓰려면 패키지를 설치해요.
pip install llama-stack-haystack
단독 사용:
import os
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.generators.llama_stack import (
LlamaStackChatGenerator,
)
client = LlamaStackChatGenerator(model="ollama/llama3.2:3b")
response = client.run([ChatMessage.from_user("What are Agentic Pipelines? Be brief.")])
print(response["replies"])
스트리밍과 함께:
import os
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.generators.llama_stack import (
LlamaStackChatGenerator,
)
from haystack.components.generators.utils import print_streaming_chunk
client = LlamaStackChatGenerator(
model="ollama/llama3.2:3b",
streaming_callback=print_streaming_chunk,
)
response = client.run([ChatMessage.from_user("What are Agentic Pipelines? Be brief.")])
print(response["replies"])
파이프라인 안에서:
from haystack import Pipeline
from haystack.components.builders import ChatPromptBuilder
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.generators.llama_stack import (
LlamaStackChatGenerator,
)
prompt_builder = ChatPromptBuilder()
llm = LlamaStackChatGenerator(model="ollama/llama3.2:3b")
pipe = Pipeline()
pipe.add_component("builder", prompt_builder)
pipe.add_component("llm", llm)
pipe.connect("builder.prompt", "llm.messages")
messages = [
ChatMessage.from_system("Give brief answers."),
ChatMessage.from_user("Tell me about {{city}}"),
]
response = pipe.run(
data={"builder": {"template": messages, "template_variables": {"city": "Berlin"}}},
)
print(response)