-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
8 changed files
with
86 additions
and
140 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
This file was deleted.
Oops, something went wrong.
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,15 +1,31 @@ | ||
import { OpenAIStream, OpenAIStreamPayload } from './OpenAIStream'; | ||
import { OpenAIStream, StreamingTextResponse } from 'ai'; | ||
import { Configuration, OpenAIApi } from 'openai-edge'; | ||
|
||
if (!process.env.OPENAI_API_KEY) { | ||
throw new Error('Missing env var from OpenAI'); | ||
} | ||
import { OpenAIStreamPayload } from '@/types/openai'; | ||
|
||
const isDev = process.env.NODE_ENV === 'development'; | ||
const OPENAI_PROXY_URL = process.env.OPENAI_PROXY_URL; | ||
|
||
// Create an OpenAI API client (that's edge friendly!) | ||
const config = new Configuration({ | ||
apiKey: process.env.OPENAI_API_KEY, | ||
}); | ||
|
||
const openai = new OpenAIApi(config, isDev && OPENAI_PROXY_URL ? OPENAI_PROXY_URL : undefined); | ||
|
||
export const runtime = 'edge'; | ||
|
||
export const config = { | ||
runtime: 'edge', | ||
}; | ||
export default async function handler(req: Request) { | ||
// Extract the `messages` from the body of the request | ||
const { messages, ...params } = (await req.json()) as OpenAIStreamPayload; | ||
|
||
export default async function handler(request: Request) { | ||
const payload = (await request.json()) as OpenAIStreamPayload; | ||
console.log(params); | ||
const response = await openai.createChatCompletion({ | ||
stream: true, | ||
...params, | ||
messages: messages.map((m) => ({ content: m.content, role: m.role })), | ||
}); | ||
|
||
return new Response(OpenAIStream(payload)); | ||
const stream = OpenAIStream(response); | ||
return new StreamingTextResponse(stream); | ||
} |
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