ChatPerplexity()Model wrapper that returns outputs formatted to match the given schema for Preplexity. Currently, Perplexity only supports "json_schema" method for structured output as per their official documentation.
Perplexity AI Chat models API.
Setup:
To use, you should have the environment variable PPLX_API_KEY set to your API key.
Any parameters that are valid to be passed to the perplexity.create call
can be passed in, even if not explicitly saved on this class.
export PPLX_API_KEY=your_api_key
Key init args - completion params: model: Name of the model to use. e.g. "sonar" temperature: Sampling temperature to use. max_tokens: Maximum number of tokens to generate. streaming: Whether to stream the results or not.
Key init args - client params: pplx_api_key: API key for PerplexityChat API. request_timeout: Timeout for requests to PerplexityChat completion API. max_retries: Maximum number of retries to make when generating.
See full list of supported init args and their descriptions in the params section.
Instantiate:
from langchain_perplexity import ChatPerplexity
model = ChatPerplexity(model="sonar", temperature=0.7)
Invoke:
messages = [("system", "You are a chatbot."), ("user", "Hello!")]
model.invoke(messages)
Invoke with structured output:
from pydantic import BaseModel
class StructuredOutput(BaseModel):
role: str
content: str
model.with_structured_output(StructuredOutput)
model.invoke(messages)
Stream:
for chunk in model.stream(messages):
print(chunk.content)
Token usage:
response = model.invoke(messages)
response.usage_metadata
Response metadata:
response = model.invoke(messages)
response.response_metadata