generated from arvinxx/npm-template
-
Notifications
You must be signed in to change notification settings - Fork 93
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
1 parent
315d0af
commit bd64479
Showing
4 changed files
with
42 additions
and
47 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 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,27 +1,25 @@ | ||
import { OpenAIStream, StreamingTextResponse } from 'ai'; | ||
import OpenAI from 'openai'; | ||
import { createOpenAI } from '@ai-sdk/openai'; | ||
import { StreamingTextResponse, streamText } from 'ai'; | ||
|
||
export async function POST(request: Request) { | ||
const { messages = [] }: Partial<{ messages: Array<any> }> = await request.json(); | ||
|
||
const openai = new OpenAI({ | ||
apiKey: 'OpenAI Key', // your openai key | ||
baseURL: 'base url', // if u dont need change baseUrl,you can delete this line | ||
}); | ||
|
||
const PickMessages = messages.map((message) => { | ||
return { | ||
role: message.role, | ||
content: message.content, | ||
}; | ||
}); | ||
|
||
const response = await openai.chat.completions.create({ | ||
model: 'gpt-3.5-turbo', | ||
const openai = createOpenAI({ | ||
// custom settings, e.g. | ||
apiKey: 'OpenAI Key', // your openai key | ||
baseURL: 'base url', // if u dont need change baseUrl,you can delete this line | ||
compatibility: 'compatible', | ||
}); | ||
const stream = await streamText({ | ||
model: openai('gpt-3.5-turbo'), | ||
messages: [...PickMessages], | ||
stream: true, | ||
}); | ||
|
||
const stream = OpenAIStream(response); | ||
return new StreamingTextResponse(stream); | ||
return new StreamingTextResponse(stream.textStream); | ||
} |
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