Perplexity

Vercel AI SDK provides a set of utilities to make it easy to use Perplexity's APIs and models. In this guide, we'll walk through how to use the utilities to create a chat bot.

Perplexity's REST APIs are compatible with OpenAI so we will use OpenAI's JavaScript SDK to make the requests. This makes it very easy to migrate and try out Perplexity.ai's models.

Guide: PPLX 70B Online Chatbot

Create a Next.js app

Create a Next.js application and install ai and openai, the Vercel AI SDK and OpenAI API client respectively. Perplexity's REST APIs are compatible with OpenAI's so we will use OpenAI's JavaScript SDK to make the requests.

pnpm dlx create-next-app my-ai-app
cd my-ai-app
pnpm install ai openai

Add your Perplexity API Key to .env

Create a .env file in your project root and add your Perplexity API Key:

PERPLEXITY_API_KEY=xxxxxxxxx

Create a Route Handler

Create a Next.js Route Handler that uses the Edge Runtime that we'll use to generate a chat completion via Perplexity that we'll then stream back to our Next.js.

For this example, we'll create a route handler at app/api/chat/route.ts that accepts a POST request with a messages array of strings:

import OpenAI from 'openai';
import { OpenAIStream, StreamingTextResponse } from 'ai';
// Create an OpenAI API client
// but configure it to point to perplexity.ai
const perplexity = new OpenAI({
apiKey: process.env.PERPLEXITY_API_KEY || '',
baseURL: 'https://api.perplexity.ai/',
});
export async function POST(req: Request) {
// Extract the `messages` from the body of the request
const { messages } = await req.json();
// Ask Perplexity for a streaming chat completion using PPLX 70B online model
// @see https://blog.perplexity.ai/blog/introducing-pplx-online-llms
const response = await perplexity.chat.completions.create({
model: 'pplx-70b-online',
stream: true,
max_tokens: 1000,
messages,
});
// Convert the response into a friendly text-stream.
const stream = OpenAIStream(response);
// Respond with the stream
return new StreamingTextResponse(stream);
}

Vercel AI SDK provides 2 utility helpers to make the above seamless: First, we pass the streaming response we receive from Perplexity to OpenAIStream. This method decodes/extracts the text tokens in the response and then re-encodes them properly for simple consumption. We can then pass that new stream directly to StreamingTextResponse. This is another utility class that extends the normal Node/Edge Runtime Response class with the default headers you probably want (hint: 'Content-Type': 'text/plain; charset=utf-8' is already set for you).

Wire up the UI

Create a Client component with a form that we'll use to gather the prompt from the user and then stream back the completion from. By default, the useChat hook will use the POST Route Handler we created above (it defaults to /api/chat). You can override this by passing a api prop to useChat({ api: '...'}).

'use client';
import { useChat } from 'ai/react';
export default function Chat() {
const { messages, input, handleInputChange, handleSubmit } = useChat();
return (
<div className="mx-auto w-full max-w-md py-24 flex flex-col stretch">
{messages.map(m => (
<div key={m.id}>
{m.role === 'user' ? 'User: ' : 'AI: '}
{m.content}
</div>
))}
<form onSubmit={handleSubmit}>
<label>
Say something...
<input
className="fixed w-full max-w-md bottom-0 border border-gray-300 rounded mb-8 shadow-xl p-2"
value={input}
onChange={handleInputChange}
/>
</label>
<button type="submit">Send</button>
</form>
</div>
);
}

Guide: Text Completion

Use the Completion API

Similar to the Chatbot example above, we'll create a Next.js Route Handler that generates a text completion via Perplexity that we'll then stream back to our Next.js. It accepts a POST request with a prompt string:

import OpenAI from 'openai';
import { OpenAIStream, StreamingTextResponse } from 'ai';
// Create an OpenAI API client
// but configure it to point to perplexity.ai
const perplexity = new OpenAI({
apiKey: process.env.PERPLEXITY_API_KEY || '',
baseURL: 'https://api.perplexity.ai/',
});
export async function POST(req: Request) {
// Extract the `prompt` from the body of the request
const { prompt } = await req.json();
// Ask Perplexity for a streaming chat completion using PPLX 70B online model
// @see https://blog.perplexity.ai/blog/introducing-pplx-online-llms
const response = await perplexity.chat.completions.create({
model: 'pplx-70b-online',
stream: true,
max_tokens: 1000,
messages,
});
// Convert the response into a friendly text-stream.
const stream = OpenAIStream(response);
// Respond with the stream
return new StreamingTextResponse(stream);
}

Wire up the UI

We can use the useCompletion hook to make it easy to wire up the UI. By default, the useCompletion hook will use the POST Route Handler we created above (it defaults to /api/completion). You can override this by passing a api prop to useCompletion({ api: '...'}).

'use client';
import { useCompletion } from 'ai/react';
export default function Completion() {
const {
completion,
input,
stop,
isLoading,
handleInputChange,
handleSubmit,
} = useCompletion({
api: '/api/completion',
});
return (
<div className="mx-auto w-full max-w-md py-24 flex flex-col stretch">
<form onSubmit={handleSubmit}>
<label>
Say something...
<input
className="fixed w-full max-w-md bottom-0 border border-gray-300 rounded mb-8 shadow-xl p-2"
value={input}
onChange={handleInputChange}
/>
</label>
<output>Completion result: {completion}</output>
<button type="button" onClick={stop}>
Stop
</button>
<button disabled={isLoading} type="submit">
Send
</button>
</form>
</div>
);
}

Guide: Save to Database After Completion

It’s common to want to save the result of a completion to a database after streaming it back to the user. The OpenAIStream adapter accepts a couple of optional callbacks that can be used to do this.

export async function POST(req: Request) {
// ...
// Convert the response into a friendly text-stream
const stream = OpenAIStream(response, {
onStart: async () => {
// This callback is called when the stream starts
// You can use this to save the prompt to your database
await savePromptToDatabase(prompt);
},
onToken: async (token: string) => {
// This callback is called for each token in the stream
// You can use this to debug the stream or save the tokens to your database
console.log(token);
},
onCompletion: async (completion: string) => {
// This callback is called when the stream completes
// You can use this to save the final completion to your database
await saveCompletionToDatabase(completion);
},
});
// Respond with the stream
return new StreamingTextResponse(stream);
}