CohereChatGenerator — Cohere 채팅 생성
CohereChatGenerator — Cohere 채팅 생성
CohereChatGenerator는 Cohere의 대규모 언어 모델(LLM)을 이용해 채팅 완성(chat completion)을 만들 수 있게 해 주는 컴포넌트예요. 파이프라인 안에서 Cohere 모델과 대화를 주고받는 한 조각이라고 보시면 돼요.
이 통합은 Cohere의 chat 모델을 지원해요. 기본 모델 command-a-03-2025를 비롯해 command-a-plus-05-2026, command-r-plus-08-2024 같은 모델들을 쓸 수 있어요. 최신 전체 목록은 Cohere 문서에서 확인하세요.
개요 (Overview)
CohereChatGenerator는 동작하려면 Cohere API 키가 필요해요. 키는 다음 두 가지 방법으로 설정할 수 있어요.
- Secret API를 이용한
api_key초기화 파라미터 COHERE_API_KEY환경 변수 (권장)
그리고 이 컴포넌트는 동작하려면 프롬프트가 필요해요. Co.chat 메서드에서 쓸 수 있는 텍스트 생성 파라미터는 대부분 generation_kwargs 파라미터로 직접 전달할 수 있어요. 초기화할 때도, run() 메서드를 호출할 때도 둘 다 가능하죠. Cohere API가 지원하는 파라미터에 대한 자세한 내용은 Cohere 문서를 참고하세요.
마지막으로, 이 컴포넌트는 ChatMessage 객체의 리스트를 받아서 동작해요. ChatMessage는 메시지와 역할(누가 만들었는지 — user, assistant, system, tool 등), 선택적 메타데이터를 담는 데이터 클래스예요.
도구 지원 (Tool Support)
CohereChatGenerator는 tools 파라미터를 통해 함수 호출(function calling)을 지원해요. 이 파라미터는 유연한 도구 구성을 받아들여요.
- Tool 객체 리스트: 개별 도구를 리스트로 전달
- 단일 Toolset: Toolset 하나를 통째로 전달
- Tool과 Toolset 혼합: 여러 Toolset과 단독 도구를 한 리스트에 섞어서 전달
이렇게 하면 관련된 도구들을 논리적인 그룹으로 묶으면서, 필요할 때 단독 도구도 같이 포함할 수 있어요.
from haystack.tools import Tool, Toolset
from haystack_integrations.components.generators.cohere import CohereChatGenerator
# 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 = CohereChatGenerator(
tools=[math_toolset, weather_tool, news_tool] # Mix of Toolset and Tool objects
)
도구를 다루는 더 자세한 내용은 Tool과 Toolset 문서를 참고하세요.
스트리밍 (Streaming)
이 Generator는 LLM의 토큰을 출력에 바로 스트리밍하는 스트리밍을 지원해요. streaming_callback 초기화 파라미터에 함수를 넘기면 됩니다.
사용법 (Usage)
CohereChatGenerator를 쓰려면 cohere-haystack 패키지를 설치해야 해요.
pip install cohere-haystack
단독 사용 (On its own)
from haystack_integrations.components.generators.cohere import CohereChatGenerator
from haystack.dataclasses import ChatMessage
generator = CohereChatGenerator()
message = ChatMessage.from_user("What's Natural Language Processing? Be brief.")
print(generator.run([message]))
멀티모달 입력으로 사용:
from haystack.dataclasses import ChatMessage, ImageContent
from haystack_integrations.components.generators.cohere import CohereChatGenerator
# Use a multimodal model like Command A Vision
llm = CohereChatGenerator(model="command-a-vision-07-2025")
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.
파이프라인에서 사용 (In a Pipeline)
CohereChatGenerator를 사용해서 파이프라인에서도 Cohere 채팅 모델을 쓸 수 있어요.
from haystack import Pipeline
from haystack.components.builders import ChatPromptBuilder
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.generators.cohere import CohereChatGenerator
from haystack.utils import Secret
pipe = Pipeline()
pipe.add_component("prompt_builder", ChatPromptBuilder())
pipe.add_component("llm", CohereChatGenerator())
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)