forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
close lobehub#20
- Loading branch information
Showing
8 changed files
with
73 additions
and
38 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 |
---|---|---|
@@ -1 +1,3 @@ | ||
export const OPENAI_SERVICE_ERROR_CODE = 555; | ||
|
||
export const OPENAI_API_KEY_HEADER_KEY = 'X-OPENAI-API-KEY'; |
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,36 +1,47 @@ | ||
import { StreamingTextResponse } from 'ai'; | ||
import { ChatCompletionRequestMessage } from 'openai-edge'; | ||
|
||
import { OPENAI_API_KEY_HEADER_KEY } from '@/const/fetch'; | ||
import { OpenAIStreamPayload } from '@/types/openai'; | ||
|
||
import pluginList from '../../plugins'; | ||
import { createChatCompletion, openai } from './openai'; | ||
import { createChatCompletion, createOpenAI } from './openai'; | ||
|
||
export const runtime = 'edge'; | ||
|
||
export default async function handler(req: Request) { | ||
const payload = (await req.json()) as OpenAIStreamPayload; | ||
const apiKey = req.headers.get(OPENAI_API_KEY_HEADER_KEY); | ||
|
||
const stream = await createChatCompletion(payload, (payload) => ({ | ||
experimental_onFunctionCall: async ({ name, arguments: args }, createFunctionCallMessages) => { | ||
console.log(`检测到 functionCall: ${name}`); | ||
const openai = createOpenAI(apiKey); | ||
|
||
const func = pluginList.find((f) => f.name === name); | ||
const stream = await createChatCompletion({ | ||
OPENAI_API_KEY: apiKey, | ||
callbacks: (payload) => ({ | ||
experimental_onFunctionCall: async ( | ||
{ name, arguments: args }, | ||
createFunctionCallMessages, | ||
) => { | ||
console.log(`检测到 functionCall: ${name}`); | ||
|
||
if (func) { | ||
const result = await func.runner(args as any); | ||
const func = pluginList.find((f) => f.name === name); | ||
|
||
console.log(`[${name}]`, args, `result:`, JSON.stringify(result, null, 2)); | ||
if (func) { | ||
const result = await func.runner(args as any); | ||
|
||
const newMessages = createFunctionCallMessages(result) as ChatCompletionRequestMessage[]; | ||
console.log(`[${name}]`, args, `result:`, JSON.stringify(result, null, 2)); | ||
|
||
return openai.createChatCompletion({ | ||
...payload, | ||
messages: [...payload.messages, ...newMessages], | ||
}); | ||
} | ||
}, | ||
})); | ||
const newMessages = createFunctionCallMessages(result) as ChatCompletionRequestMessage[]; | ||
|
||
return openai.createChatCompletion({ | ||
...payload, | ||
messages: [...payload.messages, ...newMessages], | ||
}); | ||
} | ||
}, | ||
}), | ||
payload, | ||
}); | ||
|
||
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