-
-
Notifications
You must be signed in to change notification settings - Fork 62
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
23 changed files
with
582 additions
and
92 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
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { ChatModelCard } from "../../types/model/openai/ChatGPTModel"; | ||
import OpenAIProvider from "./openai"; | ||
|
||
export const CELERIS_DEFAULT_MODEL_LIST: ChatModelCard[] = [ | ||
OpenAIProvider.chatModels, | ||
].flat(); | ||
|
||
export { default as OpenAIProvider } from "./openai"; |
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,113 @@ | ||
// refs to: https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo | ||
import type { ModelProviderCard } from "../../types/model/openai/ChatGPTModel"; | ||
|
||
const OpenAI: ModelProviderCard = { | ||
chatModels: [ | ||
{ | ||
description: "GPT 3.5 Turbo,适用于各种文本生成和理解任务", | ||
displayName: "GPT-3.5 Turbo", | ||
functionCall: true, | ||
id: "gpt-3.5-turbo", | ||
tokens: 4096, | ||
}, | ||
{ | ||
displayName: "GPT-3.5 Turbo (0125)", | ||
functionCall: true, | ||
id: "gpt-3.5-turbo-0125", | ||
tokens: 16_385, | ||
}, | ||
{ | ||
displayName: "GPT-3.5 Turbo (1106)", | ||
functionCall: true, | ||
hidden: true, | ||
id: "gpt-3.5-turbo-1106", | ||
tokens: 16_385, | ||
}, | ||
{ | ||
hidden: true, | ||
id: "gpt-3.5-turbo-instruct", | ||
tokens: 4096, | ||
}, | ||
{ | ||
displayName: "GPT-3.5 Turbo 16K", | ||
hidden: true, | ||
id: "gpt-3.5-turbo-16k", | ||
tokens: 16_385, | ||
}, | ||
{ | ||
hidden: true, | ||
id: "gpt-3.5-turbo-0613", | ||
legacy: true, | ||
tokens: 4096, | ||
}, | ||
{ | ||
hidden: true, | ||
id: "gpt-3.5-turbo-16k-0613", | ||
legacy: true, | ||
tokens: 4096, | ||
}, | ||
{ | ||
displayName: "GPT-4 Turbo Preview", | ||
functionCall: true, | ||
id: "gpt-4-turbo-preview", | ||
tokens: 128_000, | ||
}, | ||
{ | ||
displayName: "GPT-4 Turbo Preview (0125)", | ||
functionCall: true, | ||
id: "gpt-4-0125-preview", | ||
tokens: 128_000, | ||
}, | ||
{ | ||
description: "GPT-4 视觉预览版,支持视觉任务", | ||
displayName: "GPT-4 Turbo Vision (Preview)", | ||
id: "gpt-4-vision-preview", | ||
tokens: 128_000, | ||
vision: true, | ||
}, | ||
{ | ||
displayName: "GPT-4 Turbo Preview (1106)", | ||
functionCall: true, | ||
hidden: true, | ||
id: "gpt-4-1106-preview", | ||
tokens: 128_000, | ||
}, | ||
{ | ||
displayName: "GPT-4", | ||
functionCall: true, | ||
hidden: true, | ||
id: "gpt-4", | ||
tokens: 8192, | ||
}, | ||
{ | ||
functionCall: true, | ||
hidden: true, | ||
id: "gpt-4-0613", | ||
tokens: 8192, | ||
}, | ||
{ | ||
functionCall: true, | ||
hidden: true, | ||
id: "gpt-4-32k", | ||
tokens: 32_768, | ||
}, | ||
{ | ||
functionCall: true, | ||
hidden: true, | ||
id: "gpt-4-32k-0613", | ||
tokens: 32_768, | ||
}, | ||
{ | ||
files: true, | ||
functionCall: true, | ||
hidden: true, | ||
id: "gpt-4-all", | ||
tokens: 32_768, | ||
vision: true, | ||
}, | ||
], | ||
enabled: true, | ||
id: "openai", | ||
}; | ||
|
||
export default OpenAI; |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
OPENAI_API_KEY=sk-I |
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 @@ | ||
OPENAI_API_KEY=xxxxxxx |
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,104 @@ | ||
import { | ||
OpenAIStream, | ||
StreamingTextResponse, | ||
experimental_StreamData, | ||
} from "ai"; | ||
import OpenAI from "openai"; | ||
import type { ChatCompletionCreateParams } from "openai/resources/chat"; | ||
import { getServerConfig } from "../config/server"; | ||
|
||
const functions: ChatCompletionCreateParams.Function[] = [ | ||
{ | ||
name: "get_current_weather", | ||
description: "Get the current weather.", | ||
parameters: { | ||
type: "object", | ||
properties: { | ||
format: { | ||
type: "string", | ||
enum: ["celsius", "fahrenheit"], | ||
description: "The temperature unit to use.", | ||
}, | ||
}, | ||
required: ["format"], | ||
}, | ||
}, | ||
{ | ||
name: "eval_code_in_browser", | ||
description: "Execute javascript code in the browser with eval().", | ||
parameters: { | ||
type: "object", | ||
properties: { | ||
code: { | ||
type: "string", | ||
description: `Javascript code that will be directly executed via eval(). Do not use backticks in your response. | ||
DO NOT include any newlines in your response, and be sure to provide only valid JSON when providing the arguments object. | ||
The output of the eval() will be returned directly by the function.`, | ||
}, | ||
}, | ||
required: ["code"], | ||
}, | ||
}, | ||
]; | ||
|
||
export default defineLazyEventHandler(async () => { | ||
const { OPENAI_API_KEY } = getServerConfig(); | ||
if (!OPENAI_API_KEY) { | ||
throw new Error("Missing OpenAI API key"); | ||
} | ||
const openai = new OpenAI({ | ||
apiKey: OPENAI_API_KEY, | ||
}); | ||
|
||
return defineEventHandler(async (event: any) => { | ||
const { messages } = await readBody(event); | ||
|
||
const response = await openai.chat.completions.create({ | ||
model: "gpt-3.5-turbo-0613", | ||
stream: true, | ||
messages, | ||
functions, | ||
}); | ||
|
||
// eslint-disable-next-line new-cap | ||
const data = new experimental_StreamData(); | ||
const stream = OpenAIStream(response, { | ||
experimental_onFunctionCall: async ( | ||
{ name, arguments: args }, | ||
createFunctionCallMessages, | ||
) => { | ||
if (name === "get_current_weather") { | ||
// Call a weather API here | ||
const weatherData = { | ||
temperature: 20, | ||
unit: args.format === "celsius" ? "C" : "F", | ||
}; | ||
|
||
data.append({ | ||
text: "Some custom data", | ||
}); | ||
|
||
const newMessages = createFunctionCallMessages(weatherData); | ||
return openai.chat.completions.create({ | ||
messages: [...messages, ...newMessages], | ||
stream: true, | ||
model: "gpt-3.5-turbo-0613", | ||
}); | ||
} | ||
}, | ||
onCompletion(completion) { | ||
console.log("completion", completion); | ||
}, | ||
onFinal(_completion) { | ||
data.close(); | ||
}, | ||
experimental_streamData: true, | ||
}); | ||
|
||
data.append({ | ||
text: "Hello, how are you?", | ||
}); | ||
|
||
return new StreamingTextResponse(stream, {}, data); | ||
}); | ||
}); |
Oops, something went wrong.