View Source LangChain.ChatModels.ChatPerplexity (LangChain v0.3.3)

Represents the Perplexity Chat model.

This module implements a client for the Perplexity Chat API, providing functions to validate input parameters, format API requests, and parse API responses into LangChain's structured data types.

Perplexity does not natively support tool calling in the same manner as some other chat models. To overcome this limitation, this module employs a workaround using structured outputs via a JSON schema. When tools are provided, the API request is augmented with a JSON schema that defines the expected format for tool calls. The response processing logic then detects and decodes these tool call details, converting them into corresponding ToolCall structs. This approach allows LangChain to seamlessly emulate tool calling functionality and integrate it with its standard workflow, similar to how ChatOpenAI handles function calls.

In addition, this module supports various configuration options such as temperature, top_p, top_k, and streaming, as well as callbacks for token usage and new message events.

Overall, this implementation provides a unified interface for interacting with the Perplexity Chat API while working around its limitations regarding tool calling.

Tool Calls

In order to use tool calls, you need specifically prompt Perplexity as outlined in their Prompt Guide as well as the Structured Outputs Guide.

Provide it additional prompting like:

Rules:
1. Provide only the final answer. It is important that you do not include any explanation on the steps below.
2. Do not show the intermediate steps information.

Output a JSON object with the following fields:
- title: The article title
- keywords: An array of SEO keywords
- meta_description: The SEO meta description

Summary

Functions

Decode a streamed response from the Perplexity API.

Convert a LangChain Message-based structure to the expected map of data for the Perplexity API.

Return the params formatted for an API request.

Setup a ChatPerplexity client configuration.

Setup a ChatPerplexity client configuration and return it or raise an error if invalid.

Restores the model from the config.

Generate a config map that can later restore the model's configuration.

Types

@type t() :: %LangChain.ChatModels.ChatPerplexity{
  api_key: term(),
  callbacks: term(),
  endpoint: term(),
  frequency_penalty: term(),
  max_tokens: term(),
  model: term(),
  presence_penalty: term(),
  receive_timeout: term(),
  response_format: term(),
  return_images: term(),
  return_related_questions: term(),
  search_domain_filter: term(),
  search_recency_filter: term(),
  stream: term(),
  temperature: term(),
  top_k: term(),
  top_p: term()
}

Functions

Link to this function

decode_stream(arg, done \\ [])

View Source

Decode a streamed response from the Perplexity API.

Link to this function

do_process_response(model, data)

View Source
Link to this function

for_api(chat_perplexity, msg)

View Source
@spec for_api(t(), LangChain.Message.t()) :: %{required(String.t()) => any()}

Convert a LangChain Message-based structure to the expected map of data for the Perplexity API.

Link to this function

for_api(perplexity, messages, tools)

View Source
@spec for_api(t(), [LangChain.Message.t()], LangChain.ChatModels.ChatModel.tools()) ::
  %{
    required(atom()) => any()
  }

Return the params formatted for an API request.

@spec new(attrs :: map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}

Setup a ChatPerplexity client configuration.

@spec new!(attrs :: map()) :: t() | no_return()

Setup a ChatPerplexity client configuration and return it or raise an error if invalid.

Restores the model from the config.

@spec serialize_config(t()) :: %{required(String.t()) => any()}

Generate a config map that can later restore the model's configuration.