mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-30 10:28:42 +00:00
feat: set openai account
This commit is contained in:
@@ -1,27 +1,26 @@
|
||||
import { Configuration, OpenAIApi } from 'openai';
|
||||
|
||||
const baseUrl =
|
||||
process.env.ONEAPI_URL || process.env.OPENAI_BASE_URL || 'https://api.openai.com/v1';
|
||||
export const openaiBaseUrl = 'https://api.openai.com/v1';
|
||||
export const baseUrl = process.env.ONEAPI_URL || process.env.OPENAI_BASE_URL || openaiBaseUrl;
|
||||
|
||||
export const getSystemOpenAiKey = () => {
|
||||
return process.env.ONEAPI_KEY || process.env.OPENAIKEY || '';
|
||||
};
|
||||
export const systemAIChatKey = process.env.ONEAPI_KEY || process.env.OPENAIKEY || '';
|
||||
|
||||
export const getOpenAIApi = () => {
|
||||
export const getAIChatApi = (props?: { base?: string; apikey?: string }) => {
|
||||
return new OpenAIApi(
|
||||
new Configuration({
|
||||
basePath: baseUrl
|
||||
basePath: props?.base || baseUrl,
|
||||
apiKey: props?.apikey || systemAIChatKey
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
/* openai axios config */
|
||||
export const axiosConfig = () => {
|
||||
export const axiosConfig = (props?: { base?: string; apikey?: string }) => {
|
||||
return {
|
||||
baseURL: baseUrl, // 此处仅对非 npm 模块有效
|
||||
baseURL: props?.base || baseUrl, // 此处仅对非 npm 模块有效
|
||||
httpsAgent: global.httpsAgent,
|
||||
headers: {
|
||||
Authorization: `Bearer ${getSystemOpenAiKey()}`,
|
||||
Authorization: `Bearer ${props?.apikey || systemAIChatKey}`,
|
||||
auth: process.env.OPENAI_BASE_URL_AUTH || ''
|
||||
}
|
||||
};
|
||||
|
@@ -5,7 +5,7 @@ import { TrainingModeEnum } from '@/constants/plugin';
|
||||
import { ERROR_ENUM } from '../errorCode';
|
||||
import { sendInform } from '@/pages/api/user/inform/send';
|
||||
import { authBalanceByUid } from '../utils/auth';
|
||||
import { axiosConfig, getOpenAIApi } from '../ai/openai';
|
||||
import { axiosConfig, getAIChatApi } from '../ai/openai';
|
||||
import { ChatCompletionRequestMessage } from 'openai';
|
||||
import { modelToolMap } from '@/utils/plugin';
|
||||
import { gptMessage2ChatType } from '@/utils/adapt';
|
||||
@@ -55,7 +55,7 @@ export async function generateQA(): Promise<any> {
|
||||
|
||||
const startTime = Date.now();
|
||||
|
||||
const chatAPI = getOpenAIApi();
|
||||
const chatAPI = getAIChatApi();
|
||||
|
||||
// 请求 chatgpt 获取回答
|
||||
const response = await Promise.all(
|
||||
|
@@ -35,18 +35,17 @@ const UserSchema = new Schema({
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'user'
|
||||
},
|
||||
promotion: {
|
||||
rate: {
|
||||
// 返现比例
|
||||
type: Number,
|
||||
default: 15
|
||||
}
|
||||
},
|
||||
limit: {
|
||||
exportKbTime: {
|
||||
// Every half hour
|
||||
type: Date
|
||||
}
|
||||
},
|
||||
openaiAccount: {
|
||||
type: {
|
||||
key: String,
|
||||
baseUrl: String
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
@@ -2,7 +2,7 @@ import { adaptChatItem_openAI } from '@/utils/plugin/openai';
|
||||
import { ChatContextFilter } from '@/service/utils/chat/index';
|
||||
import type { ChatHistoryItemResType, ChatItemType } from '@/types/chat';
|
||||
import { ChatModuleEnum, ChatRoleEnum, TaskResponseKeyEnum } from '@/constants/chat';
|
||||
import { getOpenAIApi, axiosConfig } from '@/service/ai/openai';
|
||||
import { getAIChatApi, axiosConfig } from '@/service/ai/openai';
|
||||
import type { ClassifyQuestionAgentItemType } from '@/types/app';
|
||||
import { countModelPrice } from '@/service/events/pushBill';
|
||||
|
||||
@@ -63,7 +63,7 @@ export const dispatchClassifyQuestion = async (props: Record<string, any>): Prom
|
||||
required: ['type']
|
||||
}
|
||||
};
|
||||
const chatAPI = getOpenAIApi();
|
||||
const chatAPI = getAIChatApi();
|
||||
|
||||
const response = await chatAPI.createChatCompletion(
|
||||
{
|
||||
|
@@ -5,7 +5,7 @@ import { adaptChatItem_openAI } from '@/utils/plugin/openai';
|
||||
import { ChatContextFilter } from '@/service/utils/chat/index';
|
||||
import type { ChatItemType } from '@/types/chat';
|
||||
import { ChatRoleEnum } from '@/constants/chat';
|
||||
import { getOpenAIApi, axiosConfig } from '@/service/ai/openai';
|
||||
import { getAIChatApi, axiosConfig } from '@/service/ai/openai';
|
||||
import type { ClassifyQuestionAgentItemType } from '@/types/app';
|
||||
import { authUser } from '@/service/utils/auth';
|
||||
|
||||
@@ -79,7 +79,7 @@ export async function extract({ agents, history = [], userChatInput, description
|
||||
}
|
||||
};
|
||||
|
||||
const chatAPI = getOpenAIApi();
|
||||
const chatAPI = getAIChatApi();
|
||||
|
||||
const response = await chatAPI.createChatCompletion(
|
||||
{
|
||||
|
@@ -9,7 +9,7 @@ import type { ChatHistoryItemResType } from '@/types/chat';
|
||||
import { ChatModuleEnum, ChatRoleEnum, sseResponseEventEnum } from '@/constants/chat';
|
||||
import { SSEParseData, parseStreamChunk } from '@/utils/sse';
|
||||
import { textAdaptGptResponse } from '@/utils/adapt';
|
||||
import { getOpenAIApi, axiosConfig } from '@/service/ai/openai';
|
||||
import { getAIChatApi, axiosConfig } from '@/service/ai/openai';
|
||||
import { TaskResponseKeyEnum } from '@/constants/chat';
|
||||
import { getChatModel } from '@/service/utils/data';
|
||||
import { countModelPrice } from '@/service/events/pushBill';
|
||||
@@ -77,7 +77,7 @@ export const dispatchChatCompletion = async (props: Record<string, any>): Promis
|
||||
// FastGpt temperature range: 1~10
|
||||
temperature = +(modelConstantsData.maxTemperature * (temperature / 10)).toFixed(2);
|
||||
temperature = Math.max(temperature, 0.01);
|
||||
const chatAPI = getOpenAIApi();
|
||||
const chatAPI = getAIChatApi();
|
||||
|
||||
const response = await chatAPI.createChatCompletion(
|
||||
{
|
||||
|
@@ -162,11 +162,6 @@ export const authUser = async ({
|
||||
};
|
||||
};
|
||||
|
||||
/* random get openai api key */
|
||||
export const getSystemOpenAiKey = () => {
|
||||
return process.env.ONEAPI_KEY || process.env.OPENAIKEY || '';
|
||||
};
|
||||
|
||||
// 模型使用权校验
|
||||
export const authApp = async ({
|
||||
appId,
|
||||
|
Reference in New Issue
Block a user