ChatPerplexity Class — langchain Architecture
Architecture documentation for the ChatPerplexity class in chat_models.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 8fde69d6_df91_8b4d_d44f_559b030e31db["ChatPerplexity"] d009a608_c505_bd50_7200_0de8a69ba4b7["BaseChatModel"] 8fde69d6_df91_8b4d_d44f_559b030e31db -->|extends| d009a608_c505_bd50_7200_0de8a69ba4b7 169a8cd0_0dac_1362_0fd1_9b784bf38c56["ChatMessage"] 8fde69d6_df91_8b4d_d44f_559b030e31db -->|extends| 169a8cd0_0dac_1362_0fd1_9b784bf38c56 5c3ab140_4d2a_43d4_63cb_89f4c4b1a8d6["SystemMessage"] 8fde69d6_df91_8b4d_d44f_559b030e31db -->|extends| 5c3ab140_4d2a_43d4_63cb_89f4c4b1a8d6 e0e879bf_e732_8d0f_6ce2_3d40e66f4eb3["HumanMessage"] 8fde69d6_df91_8b4d_d44f_559b030e31db -->|extends| e0e879bf_e732_8d0f_6ce2_3d40e66f4eb3 fcfa55b0_4a86_fa31_a156_3c38c76a0a9b["AIMessage"] 8fde69d6_df91_8b4d_d44f_559b030e31db -->|extends| fcfa55b0_4a86_fa31_a156_3c38c76a0a9b 17a9b92d_bb83_78d8_7df7_7200745cc17b["AIMessageChunk"] 8fde69d6_df91_8b4d_d44f_559b030e31db -->|extends| 17a9b92d_bb83_78d8_7df7_7200745cc17b e9b34d99_3e5a_1a75_cd36_472062f121d0["chat_models.py"] 8fde69d6_df91_8b4d_d44f_559b030e31db -->|defined in| e9b34d99_3e5a_1a75_cd36_472062f121d0 33680140_9b32_2f74_016c_5c2f8ac0fd44["lc_secrets()"] 8fde69d6_df91_8b4d_d44f_559b030e31db -->|method| 33680140_9b32_2f74_016c_5c2f8ac0fd44 82be317c_0ed8_d4d2_da45_43a52f33869d["build_extra()"] 8fde69d6_df91_8b4d_d44f_559b030e31db -->|method| 82be317c_0ed8_d4d2_da45_43a52f33869d 1d4e6771_f1a2_5d36_f09e_0b43283756ed["validate_environment()"] 8fde69d6_df91_8b4d_d44f_559b030e31db -->|method| 1d4e6771_f1a2_5d36_f09e_0b43283756ed 0be3497f_e8bc_21d8_b4e4_d7e51b63b5fc["_set_model_profile()"] 8fde69d6_df91_8b4d_d44f_559b030e31db -->|method| 0be3497f_e8bc_21d8_b4e4_d7e51b63b5fc 8e1ddee3_de00_0396_3c74_3009764e9fcd["_default_params()"] 8fde69d6_df91_8b4d_d44f_559b030e31db -->|method| 8e1ddee3_de00_0396_3c74_3009764e9fcd 5d4afb1b_2b6a_0c6b_61e9_856416c1c724["_convert_message_to_dict()"] 8fde69d6_df91_8b4d_d44f_559b030e31db -->|method| 5d4afb1b_2b6a_0c6b_61e9_856416c1c724 c4bef11e_3c90_10a8_1ed1_8796c1874f5a["_create_message_dicts()"] 8fde69d6_df91_8b4d_d44f_559b030e31db -->|method| c4bef11e_3c90_10a8_1ed1_8796c1874f5a
Relationship Graph
Source Code
libs/partners/perplexity/langchain_perplexity/chat_models.py lines 105–816
class ChatPerplexity(BaseChatModel):
"""`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.
```bash
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:
```python
from langchain_perplexity import ChatPerplexity
model = ChatPerplexity(model="sonar", temperature=0.7)
```
Invoke:
```python
messages = [("system", "You are a chatbot."), ("user", "Hello!")]
model.invoke(messages)
```
Invoke with structured output:
```python
from pydantic import BaseModel
class StructuredOutput(BaseModel):
role: str
content: str
model.with_structured_output(StructuredOutput)
model.invoke(messages)
```
Stream:
```python
for chunk in model.stream(messages):
print(chunk.content)
```
Token usage:
```python
response = model.invoke(messages)
response.usage_metadata
```
Response metadata:
```python
response = model.invoke(messages)
response.response_metadata
```
""" # noqa: E501
Source
Frequently Asked Questions
What is the ChatPerplexity class?
ChatPerplexity is a class in the langchain codebase, defined in libs/partners/perplexity/langchain_perplexity/chat_models.py.
Where is ChatPerplexity defined?
ChatPerplexity is defined in libs/partners/perplexity/langchain_perplexity/chat_models.py at line 105.
What does ChatPerplexity extend?
ChatPerplexity extends BaseChatModel, ChatMessage, SystemMessage, HumanMessage, AIMessage, AIMessageChunk.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free