Perplexity LLM
Installation
Section titled “Installation”npm i @llamaindex/perplexity
import { Settings } from "llamaindex";import { perplexity } from "@llamaindex/perplexity";Settings.llm = perplexity({apiKey: "<YOUR_API_KEY>",model: "sonar", // or available models});
Example
Section titled “Example”import { perplexity } from "@llamaindex/perplexity";
const perplexityLlm = perplexity({ apiKey: "<YOUR_API_KEY>", model: "sonar", // or avaiable models});
async function main() { const response = await perplexityLlm.chat({ messages: [ { role: "system", content: "You are an AI assistant", }, { role: "user", content: "Tell me about San Francisco", }, ], stream: false, }); console.log(response);
const stream = await perplexityLlm.chat({ messages: [ { role: "system", content: "You are a creative AI assistant that tells engaging stories", }, { role: "user", content: "Tell me a short story", }, ], stream: true, });
console.log("\nStreaming response:"); for await (const chunk of stream) { process.stdout.write(chunk.delta); }}
Full Example
Section titled “Full Example”import { perplexity } from "@llamaindex/perplexity";import { Document, Settings, VectorStoreIndex } from "llamaindex";
// Use the perplexity LLMSettings.llm = perplexity({ model: "sonar", apiKey: "<YOUR_API_KEY>" });
async function main() { const document = new Document({ text: essay, id_: "essay" });
// Load and index documents const index = await VectorStoreIndex.fromDocuments([document]);
// get retriever const retriever = index.asRetriever();
// Create a query engine const queryEngine = index.asQueryEngine({ retriever, });
const query = "What is the meaning of life?";
// Query const response = await queryEngine.query({ query, });
// Log the response console.log(response.response);}
Available Models
Section titled “Available Models”The following models are available:
sonar
: 128k context windowsonar-pro
: 200k context windowsonar-deep-research
: 128k context windowsonar-reasoning
: 128k context windowsonar-reasoning-pro
: 128k context windowr1-1776
: 128k context window
Limitations
Section titled “Limitations”Currently does not support function calling.