STACKITChatGenerator
STACKITChatGenerator
STACKITChatGenerator 는 STACKIT API를 통해 채팅 완성(chat completion)을 생성하는 컴포넌트예요. STACKIT이 서빙하는 텍스트 생성 모델을 파이프라인에 쉽게 연결할 수 있어요.
출처: 문서
본문
개요 (Overview)
STACKITChatGenerator 는 STACKIT이 API를 통해 서빙하는 텍스트 생성 모델을 사용할 수 있게 해 줘요.
파라미터 (Parameters)
STACKITChatGenerator 를 쓰려면 STACKIT_API_KEY 를 환경 변수로 설정했는지 확인하세요. 또는 api_key 를 설정해 다른 이름의 환경 변수나 토큰으로 API 키를 제공할 수도 있어요 — 이때 Haystack의 시크릿 관리를 이용해요.
컴포넌트 초기화 시 model 파라미터로 원하는 지원 모델을 설정하세요. 지원되는 모든 모델의 전체 목록은 STACKIT 웹사이트에서 확인할 수 있어요.
선택적으로 기본 api_base_url("https://api.openai-compat.model-serving.eu01.onstackit.cloud/v1")을 바꿀 수 있어요.
STACKIT Chat Completion API에 유효한 텍스트 생성 파라미터는 generation_kwargs 파라미터로 init 또는 run 메서드에 직접 전달할 수 있어요.
컴포넌트를 실행하려면 ChatMessage 객체 목록이 필요해요. ChatMessage 는 메시지, 역할(누가 메시지를 생성했는지 — user, assistant, system, tool 등), 그리고 선택적 메타데이터를 담는 데이터 클래스예요. 자세한 내용은 ChatMessage 문서를 참고하세요.
스트리밍 (Streaming)
이 ChatGenerator는 스트리밍을 지원해서 LLM의 토큰을 출력으로 직접 흘려보낼 수 있어요. streaming_callback init 파라미터에 함수를 전달하면 돼요.
사용법 (Usage)
STACKITChatGenerator 를 쓰려면 stackit-haystack 패키지를 설치하세요:
pip install stackit-haystack
단독으로 쓰기
from haystack_integrations.components.generators.stackit import STACKITChatGenerator
from haystack.dataclasses import ChatMessage
generator = STACKITChatGenerator(model="neuralmagic/Meta-Llama-3.1-70B-Instruct-FP8")
result = generator.run([ChatMessage.from_user("Tell me a joke.")])
print(result)
멀티모달 입력을 쓰는 경우:
from haystack.dataclasses import ChatMessage, ImageContent
from haystack_integrations.components.generators.stackit import STACKITChatGenerator
llm = STACKITChatGenerator(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([user_message])["replies"][0].text
print(response)
# Red apple on straw.
파이프라인에서 쓰기
STACKITChatGenerator 를 파이프라인에서도 쓸 수 있어요:
from haystack import Pipeline
from haystack.components.builders import ChatPromptBuilder
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.generators.stackit import STACKITChatGenerator
prompt_builder = ChatPromptBuilder()
llm = STACKITChatGenerator(model="neuralmagic/Meta-Llama-3.1-70B-Instruct-FP8")
messages = [ChatMessage.from_user("Question: {{question}} \\n")]
pipeline = Pipeline()
pipeline.add_component("prompt_builder", prompt_builder)
pipeline.add_component("llm", llm)
pipeline.connect("prompt_builder.prompt", "llm.messages")
result = pipeline.run(
{
"prompt_builder": {
"template_variables": {"question": "Tell me a joke."},
"template": messages,
},
},
)
print(result)
파이프라인에서 스트리밍하는 예시는 STACKIT 통합 저장소와 전용 통합 페이지에서 확인할 수 있어요.
더 알아보기 (Learn more)
- ChatPromptBuilder — 채팅 프롬프트를 만드는 빌더
- ChatMessage — 채팅 메시지 데이터 구조
- Secret Management — API 키 관리
- STACKIT API 참조
- GitHub 저장소