This commit is contained in:
Archer
2023-09-26 14:31:37 +08:00
committed by GitHub
parent 38d4db5d5f
commit f6552d0d4f
48 changed files with 536 additions and 399 deletions

View File

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

View File

@@ -0,0 +1 @@
export { ChatCompletionRequestMessageRoleEnum } from 'openai';

1
packages/core/aiApi/type.d.ts vendored Normal file
View File

@@ -0,0 +1 @@
export type { CreateChatCompletionRequest, ChatCompletionRequestMessage } from 'openai';

13
packages/core/init.ts Normal file
View File

@@ -0,0 +1,13 @@
import tunnel from 'tunnel';
export function initHttpAgent() {
// proxy obj
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
}
});
}
}

View File

@@ -1,4 +1,11 @@
{
"name": "@fastgpt/core",
"version": "1.0.0"
"version": "1.0.0",
"dependencies": {
"openai": "^3.3.0",
"tunnel": "^0.0.6"
},
"devDependencies": {
"@types/tunnel": "^0.0.4"
}
}

5
packages/core/types/index.d.ts vendored Normal file
View File

@@ -0,0 +1,5 @@
import type { Agent } from 'http';
declare global {
var httpsAgent: Agent;
}

19
packages/core/user/type.d.ts vendored Normal file
View File

@@ -0,0 +1,19 @@
export type UserModelSchema = {
_id: string;
username: string;
password: string;
avatar: string;
balance: number;
promotionRate: number;
inviterId?: string;
openaiKey: string;
createTime: number;
timezone: string;
openaiAccount?: {
key: string;
baseUrl: string;
};
limit: {
exportKbTime?: Date;
};
};