-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from zhengxs2018/one-api
feat(api): add completion api
- Loading branch information
Showing
10 changed files
with
249 additions
and
71 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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import OpenAI from 'openai'; | ||
import { MinimaxAI } from '@studio-b3/llmapi'; | ||
|
||
// See https://api.minimax.chat/user-center/basic-information/interface-key | ||
const api = new MinimaxAI({ | ||
orgId: process.env.MINIMAX_API_ORG, | ||
apiKey: process.env.MINIMAX_API_KEY, | ||
}); | ||
|
||
export type AskParams = { | ||
model?: MinimaxAI.ChatModel; | ||
prompt: string; | ||
system?: string; | ||
temperature?: number; | ||
presence_penalty?: number; | ||
max_tokens?: number; | ||
}; | ||
|
||
export function askAI({ | ||
model = 'abab5-chat', | ||
prompt, | ||
system, | ||
temperature = 0.6, | ||
...rest | ||
}: AskParams) { | ||
const messages: OpenAI.ChatCompletionMessageParam[] = [ | ||
{ | ||
role: 'user', | ||
content: prompt, | ||
}, | ||
]; | ||
|
||
if (system) { | ||
messages.unshift({ | ||
role: 'system', | ||
content: system, | ||
}); | ||
} | ||
|
||
return api.chat.completions.create({ | ||
...rest, | ||
stream: true, | ||
model, | ||
temperature, | ||
messages, | ||
}); | ||
} |
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,45 @@ | ||
import OpenAI from 'openai'; | ||
|
||
// Create an OpenAI API client (that's edge friendly!) | ||
const api = new OpenAI({ | ||
apiKey: process.env.OPENAI_API_KEY || '', | ||
}); | ||
|
||
export type AskParams = { | ||
model?: OpenAI.ChatCompletionCreateParams['model']; | ||
prompt: string; | ||
system?: string; | ||
temperature?: number; | ||
presence_penalty?: number; | ||
max_tokens?: number; | ||
}; | ||
|
||
export function askAI({ | ||
model = 'gpt-3.5-turbo', | ||
prompt, | ||
system, | ||
temperature = 0.6, | ||
...rest | ||
}: AskParams) { | ||
const messages: OpenAI.ChatCompletionMessageParam[] = [ | ||
{ | ||
role: 'user', | ||
content: prompt, | ||
}, | ||
]; | ||
|
||
if (system) { | ||
messages.unshift({ | ||
role: 'system', | ||
content: system, | ||
}); | ||
} | ||
|
||
return api.chat.completions.create({ | ||
...rest, | ||
stream: true, | ||
model, | ||
temperature, | ||
messages, | ||
}); | ||
} |
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,48 @@ | ||
import OpenAI from 'openai'; | ||
import { QWenAI } from '@studio-b3/llmapi'; | ||
|
||
const api = new QWenAI({ | ||
// https://help.aliyun.com/zh/dashscope/developer-reference/activate-dashscope-and-create-an-api-key | ||
apiKey: process.env.QWEN_API_KEY || '', | ||
}); | ||
|
||
export type AskParams = { | ||
model?: QWenAI.ChatModel; | ||
prompt: string; | ||
system?: string; | ||
temperature?: number; | ||
presence_penalty?: number; | ||
max_tokens?: number; | ||
}; | ||
|
||
export function askAI({ | ||
model = 'qwen-max', | ||
prompt, | ||
system, | ||
max_tokens, | ||
temperature = 0.9, | ||
...rest | ||
}: AskParams) { | ||
const messages: OpenAI.ChatCompletionMessageParam[] = [ | ||
{ | ||
role: 'user', | ||
content: prompt, | ||
}, | ||
]; | ||
|
||
if (system) { | ||
messages.unshift({ | ||
role: 'system', | ||
content: system, | ||
}); | ||
} | ||
|
||
return api.chat.completions.create({ | ||
...rest, | ||
stream: true, | ||
model, | ||
temperature, | ||
max_tokens, | ||
messages, | ||
}); | ||
} |
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,50 @@ | ||
import { OpenAIStream, StreamingTextResponse } from 'ai'; | ||
|
||
import * as minimax from './minimax/ai'; | ||
import * as openai from './openai/ai'; | ||
import * as qwen from './qwen/ai'; | ||
import * as yiyan from './yiyan/ai'; | ||
|
||
export type AskParams = | ||
| minimax.AskParams | ||
| openai.AskParams | ||
| qwen.AskParams | ||
| yiyan.AskParams; | ||
|
||
const handlers = [ | ||
{ | ||
match: /abab/, | ||
handle: minimax.askAI, | ||
}, | ||
{ | ||
match: /qwen/, | ||
handle: qwen.askAI, | ||
}, | ||
{ | ||
match: /ernie/, | ||
handle: yiyan.askAI, | ||
}, | ||
]; | ||
|
||
const fallback = openai.askAI; | ||
|
||
function askAI(params: AskParams) { | ||
const model = params.model; | ||
|
||
if (!model) return fallback(params); | ||
|
||
const matches = handlers.find((h) => h.match.test(model)); | ||
const handle = matches?.handle || fallback; | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
return handle(params); | ||
} | ||
|
||
export async function POST(req: Request) { | ||
const response = await askAI(await req.json()); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import OpenAI from 'openai'; | ||
import { ErnieAI } from '@studio-b3/llmapi'; | ||
|
||
const api = new ErnieAI({ | ||
// 访问令牌通过编程对 AI Studio ⽤户进⾏身份验证 | ||
// https://aistudio.baidu.com/index/accessToken | ||
token: process.env.AISTUDIO_ACCESS_TOKEN || '', | ||
}); | ||
|
||
export type AskParams = { | ||
model?: ErnieAI.ChatModel; | ||
prompt: string; | ||
system?: string; | ||
temperature?: number; | ||
presence_penalty?: number; | ||
max_tokens?: number; | ||
}; | ||
|
||
export function askAI({ | ||
model = 'ernie-bot', | ||
prompt, | ||
system, | ||
temperature = 0.6, | ||
...rest | ||
}: AskParams) { | ||
const messages: OpenAI.ChatCompletionMessageParam[] = [ | ||
{ | ||
role: 'user', | ||
content: prompt, | ||
}, | ||
]; | ||
|
||
if (system) { | ||
messages.unshift({ | ||
role: 'system', | ||
content: system, | ||
}); | ||
} | ||
|
||
return api.chat.completions.create({ | ||
...rest, | ||
stream: true, | ||
model, | ||
temperature, | ||
messages, | ||
}); | ||
} |
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