2026-05-21 23:57:59 +08:00
|
|
|
|
import { Injectable, Logger } from '@nestjs/common';
|
|
|
|
|
|
import { DeepSeekProvider } from '../ai/providers/deepseek.provider';
|
|
|
|
|
|
import type { AiChatDto } from './dto/ai-chat.dto';
|
|
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
|
export class AdminAiChatService {
|
|
|
|
|
|
private readonly logger = new Logger(AdminAiChatService.name);
|
|
|
|
|
|
|
|
|
|
|
|
constructor(private readonly deepseek: DeepSeekProvider) {}
|
|
|
|
|
|
|
|
|
|
|
|
async chat(dto: AiChatDto) {
|
|
|
|
|
|
const systemMessages = dto.messages.filter(m => m.role === 'system');
|
|
|
|
|
|
const hasSystemPrompt = systemMessages.length > 0;
|
|
|
|
|
|
|
|
|
|
|
|
const messages = hasSystemPrompt
|
|
|
|
|
|
? dto.messages
|
|
|
|
|
|
: [
|
|
|
|
|
|
{ role: 'system' as const, content: '你是知习管理后台的 AI 任务助理,帮助管理员处理日常任务、解答问题。请用简洁专业的中文回复。' },
|
|
|
|
|
|
...dto.messages,
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
const start = Date.now();
|
|
|
|
|
|
const result = await this.deepseek.generate({
|
|
|
|
|
|
model: 'deepseek-chat',
|
|
|
|
|
|
messages,
|
|
|
|
|
|
temperature: 0.7,
|
|
|
|
|
|
maxTokens: 4096,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
this.logger.log(`AI chat completed in ${Date.now() - start}ms, tokens: ${result.usage?.inputTokens ?? 0}/${result.usage?.outputTokens ?? 0}`);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
content: result.rawText,
|
|
|
|
|
|
usage: result.usage,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2026-05-22 00:02:14 +08:00
|
|
|
|
|
|
|
|
|
|
getDashboardConfig() {
|
|
|
|
|
|
return {
|
|
|
|
|
|
url: 'http://10.2.0.7:9119',
|
|
|
|
|
|
token: 'v8dAkvcsHaHe_yok5kjO0QErKtogQcq3IKaJT8PNczQ',
|
|
|
|
|
|
description: 'Hermes Agent Dashboard — 4核4G 上的 AI Agent,可执行服务器管理、文件操作等高级任务',
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2026-05-21 23:57:59 +08:00
|
|
|
|
}
|