TogetherAIChatGenerator
TogetherAIChatGenerator
TogetherAIChatGenerator 는 Together AI에 호스팅된 모델로 **채팅 완성(chat completion)**을 생성하는 컴포넌트예요. meta-llama/Llama-3.3-70B-Instruct-Turbo 같은 Together AI 모델을 파이프라인에 연결할 수 있어요.
출처: 문서
본문
개요 (Overview)
TogetherAIChatGenerator 는 Together AI에 호스팅된 모델을 지원해요. 예: meta-llama/Llama-3.3-70B-Instruct-Turbo. 지원되는 전체 모델 목록은 Together AI 문서를 참고하세요.
이 컴포넌트는 동작에 ChatMessage 객체 목록이 필요해요. ChatMessage 는 메시지, 역할(누가 메시지를 생성했는지 — user, assistant, system, tool 등), 그리고 선택적 메타데이터를 담는 데이터 클래스예요.
Together AI 채팅 완성 API에 유효한 텍스트 생성 파라미터는 __init__ 의 generation_kwargs 파라미터나 run 메서드의 generation_kwargs 파라미터로 직접 전달할 수 있어요. Together AI API가 지원하는 파라미터에 대한 자세한 내용은 Together AI API 문서를 참고하세요.
이 통합을 쓰려면 충분한 크레딧이 있는 활성 TogetherAI 구독과 API 키가 필요해요. 키는 다음으로 제공할 수 있어요:
TOGETHER_API_KEY환경 변수(권장)api_keyinit 파라미터와 Haystack Secret API:Secret.from_token("your-api-key-here")
기본적으로 컴포넌트는 Together AI의 OpenAI 호환 기본 URL(https://api.together.xyz/v1)을 사용하며, 필요하면 api_base_url 로 오버라이드할 수 있어요.
도구 지원 (Tool Support)
TogetherAIChatGenerator 는 tools 파라미터를 통한 함수 호출(function calling)을 지원하며, 유연한 도구 구성을 받아요:
- Tool 객체 목록: 개별 도구를 리스트로 전달
- 단일 Toolset: Toolset 전체를 직접 전달
- Tool과 Toolset 혼합: 여러 Toolset을 독립 도구와 함께 단일 리스트로 결합
이렇게 하면 관련 도구를 논리적 그룹으로 정리하면서 필요한 독립 도구도 함께 넣을 수 있어요.
from haystack.tools import Tool, Toolset
from haystack_integrations.components.generators.togetherai import (
TogetherAIChatGenerator,
)
# 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 = TogetherAIChatGenerator(
tools=[math_toolset, weather_tool, news_tool] # Mix of Toolset and Tool objects
)
도구 작업에 대한 자세한 내용은 Tool 및 Toolset 문서를 참고하세요.
스트리밍 (Streaming)
TogetherAIChatGenerator 는 LLM의 스트리밍 응답을 지원해서, 토큰이 생성되는 대로 내보낼 수 있어요. 스트리밍을 켜려면 초기화 시 streaming_callback 파라미터에 콜러블을 전달하세요.
사용법 (Usage)
TogetherAIChatGenerator 를 쓰려면 togetherai-haystack 패키지를 설치하세요:
pip install togetherai-haystack
단독으로 쓰기
기본 사용법:
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.generators.togetherai import (
TogetherAIChatGenerator,
)
client = TogetherAIChatGenerator()
response = client.run([ChatMessage.from_user("What are Agentic Pipelines? Be brief.")])
print(response["replies"][0].text)
스트리밍 사용:
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.generators.togetherai import (
TogetherAIChatGenerator,
)
client = TogetherAIChatGenerator(
model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
streaming_callback=lambda chunk: print(chunk.content, end="", flush=True),
)
response = client.run([ChatMessage.from_user("What are Agentic Pipelines? Be brief.")])
# check the model used for the response
print("\n\nModel used:", response["replies"][0].meta.get("model"))
파이프라인에서 쓰기
from haystack import Pipeline
from haystack.components.builders import ChatPromptBuilder
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.generators.togetherai import (
TogetherAIChatGenerator,
)
prompt_builder = ChatPromptBuilder()
llm = TogetherAIChatGenerator(model="meta-llama/Llama-3.3-70B-Instruct-Turbo")
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)
더 알아보기 (Learn more)
- ChatPromptBuilder — 채팅 프롬프트를 만드는 빌더
- ChatMessage — 채팅 메시지 데이터 구조
- Tool, Toolset — 도구·도구셋 다루기
- Secret Management — API 키 관리
- TogetherAI API 참조
- GitHub 저장소