PerplexityChatGenerator
PerplexityChatGenerator
PerplexityChatGenerator는 Perplexity Agent API를 통해 다양한 모델을 사용해 채팅 완성을 가능하게 해주는 컴포넌트예요.
출처: 문서
본문
| 항목 | 내용 |
|---|---|
| 파이프라인에서 가장 흔한 위치 | ChatPromptBuilder 다음 |
| 필수 init 변수 | api_key: Perplexity API 키. PERPLEXITY_API_KEY 환경 변수로 설정 가능 |
| 필수 run 변수 | messages: 채팅을 나타내는 ChatMessage 객체 목록 |
| 출력 변수 | replies: 입력 채팅에 대한 LLM의 대체 답변 목록 |
| API reference | Integrations |
| GitHub 링크 | chat_generator.py |
| 패키지 이름 | perplexity-haystack |
개요
PerplexityChatGenerator는 OpenAIResponsesChatGenerator 위에 구축되어 있으며, Perplexity Agent API(POST /v1/agent)와 통신해요. 이 API는 OpenAI Responses 호환 인터페이스를 사용하죠.
지원하는 모델은 다음과 같아요:
openai/gpt-5.5openai/gpt-5.4(기본값)anthropic/claude-sonnet-4-6xai/grok-4.3google/gemini-3-flash-preview
현재 목록은 Perplexity Agent API models 페이지를 참고하세요.
PerplexityChatGenerator는 동작하려면 Perplexity API 키가 필요해요. 기본적으로 PERPLEXITY_API_KEY 환경 변수를 사용하죠.
이 컴포넌트는 동작을 위해 ChatMessage 객체 목록을 받아요. ChatMessage는 메시지, 역할(예: user, assistant, system), 그리고 선택적 메타데이터를 담는 데이터 클래스예요. 사용 예시는 사용법 섹션을 보세요.
Perplexity Agent API가 지원하는 어떤 파라미터든 generation_kwargs 파라미터로 초기화 시 또는 run() 메서드에서 전달할 수 있어요.
사용법
단독으로 사용하기
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.generators.perplexity import (
PerplexityChatGenerator,
)
chat_generator = PerplexityChatGenerator()
response = chat_generator.run(
[ChatMessage.from_user("What's Natural Language Processing? Be brief.")],
)
print(response["replies"][0].text)
스트리밍과 함께 사용하기 — streaming_callback에 아무 callable이나 전달하거나, 내장된 print_streaming_chunk를 쓰면 돼요:
from haystack.dataclasses import ChatMessage
from haystack.components.generators.utils import print_streaming_chunk
from haystack_integrations.components.generators.perplexity import (
PerplexityChatGenerator,
)
chat_generator = PerplexityChatGenerator(streaming_callback=print_streaming_chunk)
response = chat_generator.run(
[ChatMessage.from_user("What's Natural Language Processing? Be brief.")],
)
파이프라인에서 사용하기
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.perplexity import (
PerplexityChatGenerator,
)
prompt_builder = ChatPromptBuilder(
template=[
ChatMessage.from_system("You are a helpful assistant."),
ChatMessage.from_user("Tell me about {{topic}}"),
],
required_variables="*",
)
llm = PerplexityChatGenerator(
api_key=Secret.from_env_var("PERPLEXITY_API_KEY"),
model="openai/gpt-5.4",
)
pipe = Pipeline()
pipe.add_component("prompt_builder", prompt_builder)
pipe.add_component("llm", llm)
pipe.connect("prompt_builder.prompt", "llm.messages")
result = pipe.run(
data={"prompt_builder": {"topic": "large language models"}},
)
print(result["llm"]["replies"][0].text)