Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 59 additions & 10 deletions app/controller/app-center/aiChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,76 @@
*
*/
import { Controller } from 'egg';
import { E_FOUNDATION_MODEL } from '../../lib/enum';

export default class AiChatController extends Controller {
public async aiChat() {
const { ctx } = this;
const { foundationModel, messages } = ctx.request.body;
this.ctx.logger.info('ai接口请求参参数 model选型:', foundationModel);
const options = ctx.request.body;
this.ctx.logger.info('ai接口请求参数 model选型:', options);

const messages = options.messages;
if (!messages || !Array.isArray(messages)) {
return this.ctx.helper.getResponseData('Not passing the correct message parameter');
}
const model = foundationModel?.model ?? E_FOUNDATION_MODEL.GPT_35_TURBO;
const token = foundationModel.token;
ctx.body = await ctx.service.appCenter.aiChat.getAnswerFromAi(messages, { model, token });
const apiKey = ctx.request.header?.authorization?.replace('Bearer', '');
const baseUrl = options?.baseUrl;
const model = options?.model;
const stream = options?.stream || false;
const tools = options?.tools || [];

if (stream) {
ctx.status = 200;
ctx.set({
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive'
});
try {
const result = await ctx.service.appCenter.aiChat.getAnswerFromAi(messages, {
apiKey,
baseUrl,
model,
stream,
tools
});

for await (const chunk of result) {
ctx.res.write(`data: ${JSON.stringify(chunk)}\n\n`); // SSE 格式
}

// 添加结束标记
ctx.res.write('data: [DONE]');
} catch (e: any) {
this.ctx.logger.error(`调用AI大模型接口失败: ${(e as Error).message}`);
} finally {
console.log('end');
ctx.res.end(); // 关闭连接
}

return;
}

// 非流式模式
ctx.body = await ctx.service.appCenter.aiChat.getAnswerFromAi(messages, {
apiKey,
baseUrl,
model,
stream,
tools
});
}

public async search() {
const { ctx } = this;
const { content } = ctx.request.body;

ctx.body = await ctx.service.appCenter.aiChat.search(content);
}

public async uploadFile() {
const { ctx } = this;
const fileStream = await ctx.getFileStream();
const foundationModelObject = JSON.parse(fileStream.fields.foundationModel);
const { model, token } = foundationModelObject.foundationModel;
ctx.body = await ctx.service.appCenter.aiChat.getFileContentFromAi(fileStream, { model, token });
const stream = await ctx.getFileStream();

ctx.body = await ctx.service.appCenter.aiChat.uploadFile(stream);
}
}
3 changes: 2 additions & 1 deletion app/router/appCenter/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,6 @@ export default (app: Application) => {

// AI大模型聊天接口
subRouter.post('/ai/chat', controller.appCenter.aiChat.aiChat);
subRouter.post('/ai/files', controller.appCenter.aiChat.uploadFile);
subRouter.post('/ai/search', controller.appCenter.aiChat.search);
subRouter.post('/ai/uploadFile', controller.appCenter.aiChat.uploadFile);
};
Loading
Loading