Readme

Perplexity API Wrapper

This val exports a function pplx that provides an interface to the Perplexity AI chat completions API.

You'll need a Perplexity AI API key, see their documentation for how to get started with getting a key. By default, the function will use PERPLEXITY_API_KEY in your val town env variables unless overridden by setting apiKey in the function.

pplx(options: PplxRequest & { apiKey?: string }): Promise<PplxResponse>

Generates a model's response for the given chat conversation. Required parameters in options are the following (for other parameters, see the Types section below):

  • model (string): the name of the model that will complete your prompt. See below for possible values: pplx- 7b-chat, pplx-70b-chat, pplx-7b-online, pplx-70b-online, llama-2-70b-chat, codellama-34b -instruct, mistral-7b-instruct, and mixtral-8x7b-instruct.

  • messages(Message[]): A list of messages comprising the conversation so far. A message object must contain role (system, user, or assistant) and content (a string).

You can also specify an apiKey to override the default Deno.env.get("PERPLEXITY_API_KEY").

The function returns an object of types PplxResponse, see below.

Types

PplxRequest

Request object sent to Perplexity models.

PropertyTypeDescription
modelModelThe name of the model that will complete your prompt. Possible values: pplx- 7b-chat, pplx-70b-chat, pplx-7b-online, pplx-70b-online, llama-2-70b-chat, codellama-34b -instruct, mistral-7b-instruct, and mixtral-8x7b-instruct.
messagesMessage[]A list of messages comprising the conversation so far.
max_tokensnumber(Optional) The maximum number of completion tokens returned by the API. The total number of tokens requested in max_tokens plus the number of prompt tokens sent in messages must not exceed the context window token limit of model requested. If left unspecified, then the model will generate tokens until either it reaches its stop token or the end of its context window.
temperaturenumber(Optional) The amount of randomness in the response, valued between 0 inclusive and 2 exclusive. Higher values are more random, and lower values are more deterministic. You should either set temperature or top_p, but not both.
top_pnumber(Optional) The nucleus sampling threshold, valued between 0 and 1 inclusive. For each subsequent token, the model considers the results of the tokens with top_p probability mass. You should either alter temperature or top_p, but not both.
top_knumber(Optional) The number of tokens to keep for highest top-k filtering, specified as an integer between 0 and 2048 inclusive. If set to 0, top-k filtering is disabled.
streamboolean(Optional) Flag indicating whether to stream the response.
presence_penaltynumber(Optional) A value between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. Incompatible with frequency_penalty.
frequency_penaltynumber(Optional) A multiplicative penalty greater than 0. Values greater than 1.0 penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. A value of 1.0 means no penalty. Incompatible with presence_penalty.

PplxResponse

Response object for pplx models.

PropertyTypeDescription
idstringThe ID of the response.
modelModelThe model used for generating the response.
object"chat.completion"The type of object (always "chat.completion").
creatednumberThe timestamp indicating when the response was created.
choicesCompletionChoices[]An array of completion choices.

Please refer to the code for more details and usage examples of these types.

Message

Represents a message in a conversation.

PropertyTypeDescription
role"system" | "user" | "assistant"The role of the speaker in this turn of conversation. After the (optional) system message, user and assistant roles should alternate with user then assistant, ending in user.
contentstringThe contents of the message in this turn of conversation.

CompletionChoices

The list of completion choices the model generated for the input prompt.

PropertyTypeDescription
indexnumberThe index of the choice.
finish_reason"stop" | "length"The reason the model stopped generating tokens. Possible values are stop if the model hit a natural stopping point, or length if the maximum number of tokens specified in the request was reached.
messageMessageThe message generated by the model.
deltaMessageThe incrementally streamed next tokens. Only meaningful when stream = true.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { PplxRequest, PplxResponse } from "https://esm.town/v/nbbaier/pplxTypes";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=42";
export const pplx = async (options: PplxRequest & { apiKey?: string }): Promise<PplxResponse> => {
const token = options.apiKey ? options.apiKey : Deno.env.get("PERPLEXITY_API_KEY");
return await fetchJSON("https://api.perplexity.ai/chat/completions", {
method: "POST",
headers: {
"accept": "application/json",
"authorization": `Bearer ${token}`,
"content-type": "application/json",
},
body: JSON.stringify(options),
});
};
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.