Perplexity LLM
Installation
npm i @llamaindex/perplexity
pnpm add @llamaindex/perplexity
yarn add @llamaindex/perplexity
bun add @llamaindex/perplexity
Usage
import { Settings } from "llamaindex";
import { perplexity } from "@llamaindex/perplexity";
Settings.llm = perplexity({
apiKey: "<YOUR_API_KEY>",
model: "sonar", // or available models
});
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
import { perplexity } from "@llamaindex/perplexity";
import { Document, Settings, VectorStoreIndex } from "llamaindex";
// Use the perplexity LLM
Settings.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
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
Currently does not support function calling.
API Reference
Edit on GitHub
Last updated on