feat: user openai account

This commit is contained in:
archer
2023-07-28 13:29:06 +08:00
parent dfda5285bd
commit fb8635a951
9 changed files with 75 additions and 39 deletions

View File

@@ -1,3 +1,4 @@
import { UserModelSchema } from '@/types/mongoSchema';
import { Configuration, OpenAIApi } from 'openai';
export const openaiBaseUrl = 'https://api.openai.com/v1';
@@ -5,22 +6,22 @@ export const baseUrl = process.env.ONEAPI_URL || process.env.OPENAI_BASE_URL ||
export const systemAIChatKey = process.env.ONEAPI_KEY || process.env.OPENAIKEY || '';
export const getAIChatApi = (props?: { base?: string; apikey?: string }) => {
export const getAIChatApi = (props?: UserModelSchema['openaiAccount']) => {
return new OpenAIApi(
new Configuration({
basePath: props?.base || baseUrl,
apiKey: props?.apikey || systemAIChatKey
basePath: props?.baseUrl || baseUrl,
apiKey: props?.key || systemAIChatKey
})
);
};
/* openai axios config */
export const axiosConfig = (props?: { base?: string; apikey?: string }) => {
export const axiosConfig = (props?: UserModelSchema['openaiAccount']) => {
return {
baseURL: props?.base || baseUrl, // 此处仅对非 npm 模块有效
baseURL: props?.baseUrl || baseUrl, // 此处仅对非 npm 模块有效
httpsAgent: global.httpsAgent,
headers: {
Authorization: `Bearer ${props?.apikey || systemAIChatKey}`,
Authorization: `Bearer ${props?.key || systemAIChatKey}`,
auth: process.env.OPENAI_BASE_URL_AUTH || ''
}
};