ChatPerplexity
ChatPerplexity(
system_prompt=None,
model=None,
api_key=None,
base_url='https://api.perplexity.ai/',
seed=MISSING,
kwargs=None,
)Chat with a model hosted on perplexity.ai.
Perplexity AI is a platform for running LLMs that are capable of searching the web in real-time to help them answer questions with information that may not have been available when the model was trained.
Prerequisites
Sign up at https://www.perplexity.ai to get an API key.
Examples
import os
from chatlas import ChatPerplexity
chat = ChatPerplexity(api_key=os.getenv("PERPLEXITY_API_KEY"))
chat.chat("What is the capital of France?")Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| system_prompt | Optional[str] | A system prompt to set the behavior of the assistant. | None |
| model | Optional[str] | The model to use for the chat. The default, None, will pick a reasonable default, and warn you about it. We strongly recommend explicitly choosing a model for all but the most casual use. | None |
| api_key | Optional[str] | The API key to use for authentication. You generally should not supply this directly, but instead set the PERPLEXITY_API_KEY environment variable. |
None |
| base_url | str | The base URL to the endpoint; the default uses Perplexity’s API. | 'https://api.perplexity.ai/' |
| seed | Optional[int] | MISSING_TYPE | Optional integer seed that ChatGPT uses to try and make output more reproducible. | MISSING |
| kwargs | Optional['ChatClientArgs'] | Additional arguments to pass to the openai.OpenAI() client constructor. |
None |
Returns
| Name | Type | Description |
|---|---|---|
| Chat | A chat object that retains the state of the conversation. |
Note
This function is a lightweight wrapper around chatlas.ChatOpenAI with the defaults tweaked for perplexity.ai.
Note
Pasting an API key into a chat constructor (e.g., ChatPerplexity(api_key="...")) is the simplest way to get started, and is fine for interactive use, but is problematic for code that may be shared with others.
Instead, consider using environment variables or a configuration file to manage your credentials. One popular way to manage credentials is to use a .env file to store your credentials, and then use the python-dotenv package to load them into your environment.
pip install python-dotenv
# .env
PERPLEXITY_API_KEY=...
from chatlas import ChatPerplexity
from dotenv import load_dotenv
load_dotenv()
chat = ChatPerplexity()
chat.console()Another, more general, solution is to load your environment variables into the shell before starting Python (maybe in a .bashrc, .zshrc, etc. file):
export PERPLEXITY_API_KEY=...