-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
202 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@ai-sdk/openai': patch | ||
--- | ||
|
||
Support streams with 'chat.completion' objects. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'@ai-sdk/provider-utils': patch | ||
'@ai-sdk/anthropic': patch | ||
'@ai-sdk/mistral': patch | ||
'@ai-sdk/google': patch | ||
'@ai-sdk/openai': patch | ||
--- | ||
|
||
Rename baseUrl to baseURL. Automatically remove trailing slashes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,29 @@ | ||
import OpenAI from 'openai'; | ||
import { OpenAIStream, StreamingTextResponse } from 'ai'; | ||
import { OpenAI } from '@ai-sdk/openai'; | ||
import { StreamingTextResponse, experimental_streamText } from 'ai'; | ||
|
||
export const runtime = 'edge'; | ||
|
||
// Create an OpenAI API client (that's edge friendly!) | ||
// but configure it to point to perplexity.ai | ||
const perplexity = new OpenAI({ | ||
apiKey: process.env.PERPLEXITY_API_KEY || '', | ||
apiKey: process.env.PERPLEXITY_API_KEY ?? '', | ||
baseURL: 'https://api.perplexity.ai/', | ||
}); | ||
|
||
// IMPORTANT! Set the runtime to edge | ||
export const runtime = 'edge'; | ||
|
||
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, | ||
}); | ||
try { | ||
// Extract the `messages` from the body of the request | ||
const { messages } = await req.json(); | ||
|
||
// Convert the response into a friendly text-stream. | ||
const stream = OpenAIStream(response); | ||
// Call the language model | ||
const result = await experimental_streamText({ | ||
// see https://docs.perplexity.ai/docs/model-cards for models | ||
model: perplexity.chat('sonar-medium-chat'), | ||
messages, | ||
}); | ||
|
||
// Respond with the stream | ||
return new StreamingTextResponse(stream); | ||
// Respond with the stream | ||
return new StreamingTextResponse(result.toAIStream()); | ||
} catch (error) { | ||
console.log(error); | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.