Skip to content

Commit

Permalink
Add perplexity chat API route
Browse files Browse the repository at this point in the history
Implement a new route that integrates with the Perplexity API to handle chat completions. The route processes incoming messages, requests a response from the Perplexity API, and returns the response as a text stream.
  • Loading branch information
mikepsinn committed Jul 28, 2024
1 parent 83a3473 commit 742c4c1
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions app/api/chat/perplexity/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { OpenAIStream, StreamingTextResponse } from 'ai';
import OpenAI from 'openai';

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();

// Request the OpenAI-compatible API for the response based on the prompt
const response = await perplexity.chat.completions.create({
model: 'llama-3-sonar-large-32k-online',
stream: true,
messages: messages,
});

// Convert the response into a friendly text-stream
const stream = OpenAIStream(response);

// Respond with the stream
return new StreamingTextResponse(stream);
}

0 comments on commit 742c4c1

Please sign in to comment.