main
This commit is contained in:
1 parent
b54f013fd9
commit
23eac68661
12 files changed
+8499
No files matched your search
@@ -0,0 +1,50 @@
|
||||
// ./api/chat.ts
|
||||
import { Configuration, OpenAIApi } from 'openai-edge'
|
||||
import { OpenAIStream } from 'ai'
|
||||
import { systemPrompt } from '../../prompts/mdt'
|
||||
|
||||
// Create an OpenAI API client (that's edge friendly!)
|
||||
const config = new Configuration({
|
||||
// eslint-disable-next-line react-hooks/rules-of-hooks
|
||||
apiKey: useRuntimeConfig().openaiApiKey
|
||||
})
|
||||
const openai = new OpenAIApi(config)
|
||||
|
||||
export default defineEventHandler(async (event: any) => {
|
||||
// Extract the `prompt` from the body of the request
|
||||
const { messages } = await readBody(event)
|
||||
const mainPrompt = systemPrompt
|
||||
|
||||
const mergeMessages = [
|
||||
...messages,
|
||||
{ role: "system", content: mainPrompt },
|
||||
];
|
||||
|
||||
// Ask OpenAI for a streaming chat completion given the prompt
|
||||
const response = await openai.createChatCompletion({
|
||||
model: 'gpt-3.5-turbo-16k-0613',
|
||||
stream: true,
|
||||
messages: mergeMessages.map((message: any) => ({
|
||||
content: message.content,
|
||||
role: message.role
|
||||
}))
|
||||
})
|
||||
|
||||
// Convert the response into a friendly text-stream
|
||||
const stream = OpenAIStream(response)
|
||||
// Respond with the stream
|
||||
const reader = stream.getReader()
|
||||
return new Promise((resolve, reject) => {
|
||||
function read() {
|
||||
reader.read().then(({ done, value }) => {
|
||||
if (done) {
|
||||
event.node.res.end()
|
||||
return
|
||||
}
|
||||
event.node.res.write(value)
|
||||
read()
|
||||
})
|
||||
}
|
||||
read()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user