feat: openai base url

This commit is contained in:
archer
2023-04-25 20:02:35 +08:00
parent ce68791c3c
commit fb08f61eb5
21 changed files with 47 additions and 385 deletions

View File

@@ -1,6 +1,6 @@
import { DataItem } from '@/service/mongo';
import { getOpenAIApi } from '@/service/utils/auth';
import { httpsAgent } from '@/service/utils/tools';
import { axiosConfig } from '@/service/utils/tools';
import { getOpenApiKey } from '../utils/openai';
import type { ChatCompletionRequestMessage } from 'openai';
import { DataItemSchema } from '@/types/mongoSchema';
@@ -80,8 +80,8 @@ export async function generateAbstract(next = false): Promise<any> {
]
},
{
timeout: 120000,
httpsAgent: httpsAgent(!userApiKey)
timeout: 180000,
...axiosConfig
}
);

View File

@@ -1,6 +1,6 @@
import { SplitData } from '@/service/mongo';
import { getOpenAIApi } from '@/service/utils/auth';
import { httpsAgent } from '@/service/utils/tools';
import { axiosConfig } from '@/service/utils/tools';
import { getOpenApiKey } from '../utils/openai';
import type { ChatCompletionRequestMessage } from 'openai';
import { ChatModelEnum } from '@/constants/model';
@@ -97,7 +97,7 @@ A2:
},
{
timeout: 180000,
httpsAgent: httpsAgent(!userApiKey)
...axiosConfig
}
)
.then((res) => {

View File

@@ -31,21 +31,11 @@ export async function connectToDatabase(): Promise<void> {
generateVector(true);
// 创建代理对象
if (
process.env.AXIOS_PROXY_HOST &&
process.env.AXIOS_PROXY_PORT_FAST &&
process.env.AXIOS_PROXY_PORT_NORMAL
) {
global.httpsAgentFast = tunnel.httpsOverHttp({
if (process.env.AXIOS_PROXY_HOST && process.env.AXIOS_PROXY_PORT) {
global.httpsAgent = tunnel.httpsOverHttp({
proxy: {
host: process.env.AXIOS_PROXY_HOST,
port: +process.env.AXIOS_PROXY_PORT_FAST
}
});
global.httpsAgentNormal = tunnel.httpsOverHttp({
proxy: {
host: process.env.AXIOS_PROXY_HOST,
port: +process.env.AXIOS_PROXY_PORT_NORMAL
port: +process.env.AXIOS_PROXY_PORT
}
});
}

View File

@@ -8,7 +8,8 @@ import mongoose from 'mongoose';
export const getOpenAIApi = (apiKey: string) => {
const configuration = new Configuration({
apiKey
apiKey,
basePath: process.env.OPENAI_BASE_URL || 'https://api.openai.com/v1'
});
return new OpenAIApi(configuration, undefined);

View File

@@ -2,7 +2,7 @@ import type { NextApiResponse } from 'next';
import type { PassThrough } from 'stream';
import { createParser, ParsedEvent, ReconnectInterval } from 'eventsource-parser';
import { getOpenAIApi } from '@/service/utils/auth';
import { httpsAgent } from './tools';
import { axiosConfig } from './tools';
import { User } from '../models/user';
import { formatPrice } from '@/utils/user';
import { embeddingModel } from '@/constants/model';
@@ -85,7 +85,7 @@ export const openaiCreateEmbedding = async ({
},
{
timeout: 60000,
httpsAgent: httpsAgent(isPay)
...axiosConfig
}
)
.then((res) => ({

View File

@@ -84,9 +84,10 @@ export const authOpenApiKey = async (req: NextApiRequest) => {
}
};
/* 代理 */
export const httpsAgent = (fast: boolean) =>
fast ? global.httpsAgentFast : global.httpsAgentNormal;
/* openai axios config */
export const axiosConfig = {
httpsAgent: global.httpsAgent
};
/* delete invalid symbol */
const simplifyStr = (str: string) =>