refactor: remove DeepSeek fallback — Hermes only; fail fast on error
All checks were successful
Deploy API Server / build-and-deploy (push) Successful in 39s
All checks were successful
Deploy API Server / build-and-deploy (push) Successful in 39s
This commit is contained in:
parent
6413936472
commit
c2e8f92abe
@ -1,12 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AdminAiChatController } from './admin-ai-chat.controller';
|
||||
import { AdminAiChatService } from './admin-ai-chat.service';
|
||||
import { DeepSeekProvider } from '../ai/providers/deepseek.provider';
|
||||
import { AdminAuthGuard } from '../../common/guards/admin-auth.guard';
|
||||
import { AdminRolesGuard } from '../../common/guards/admin-roles.guard';
|
||||
|
||||
@Module({
|
||||
controllers: [AdminAiChatController],
|
||||
providers: [AdminAiChatService, DeepSeekProvider, AdminAuthGuard, AdminRolesGuard],
|
||||
providers: [AdminAiChatService, AdminAuthGuard, AdminRolesGuard],
|
||||
})
|
||||
export class AdminAiChatModule {}
|
||||
@ -1,5 +1,4 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { DeepSeekProvider } from '../ai/providers/deepseek.provider';
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import type { AiChatDto } from './dto/ai-chat.dto';
|
||||
|
||||
const HERMES_API_URL = 'http://10.2.0.7:8642/v1/chat/completions';
|
||||
@ -10,7 +9,7 @@ const SYSTEM_PROMPT = '你是知习管理后台的 AI 任务助理,帮助管
|
||||
export class AdminAiChatService {
|
||||
private readonly logger = new Logger(AdminAiChatService.name);
|
||||
|
||||
constructor(private readonly deepseek: DeepSeekProvider) {}
|
||||
constructor() {}
|
||||
|
||||
async chat(dto: AiChatDto) {
|
||||
const systemMessages = dto.messages.filter(m => m.role === 'system');
|
||||
@ -20,13 +19,7 @@ export class AdminAiChatService {
|
||||
? dto.messages
|
||||
: [{ role: 'system' as const, content: SYSTEM_PROMPT }, ...dto.messages];
|
||||
|
||||
// Try Hermes Agent first, fall back to DeepSeek
|
||||
try {
|
||||
return await this.callHermes(messages);
|
||||
} catch (err: any) {
|
||||
this.logger.warn('Hermes unavailable, falling back to DeepSeek: ' + err.message);
|
||||
return await this.callDeepSeek(messages);
|
||||
}
|
||||
return await this.callHermes(messages);
|
||||
}
|
||||
|
||||
private async callHermes(messages: Array<{ role: string; content: string }>) {
|
||||
@ -47,7 +40,8 @@ export class AdminAiChatService {
|
||||
});
|
||||
|
||||
if (!resp.ok) {
|
||||
throw new Error('Hermes returned ' + resp.status);
|
||||
const text = await resp.text().catch(() => '');
|
||||
throw new Error(`Hermes API error ${resp.status}: ${text}`);
|
||||
}
|
||||
|
||||
const data = await resp.json();
|
||||
@ -59,18 +53,6 @@ export class AdminAiChatService {
|
||||
return { content, usage: { model: 'hermes-agent', inputTokens: usage.prompt_tokens, outputTokens: usage.completion_tokens } };
|
||||
}
|
||||
|
||||
private async callDeepSeek(messages: Array<{ role: string; content: string }>) {
|
||||
const start = Date.now();
|
||||
const result = await this.deepseek.generate({
|
||||
model: 'deepseek-chat',
|
||||
messages: messages as Array<{ role: 'system' | 'user' | 'assistant'; content: string }>,
|
||||
temperature: 0.7,
|
||||
maxTokens: 4096,
|
||||
});
|
||||
this.logger.log('DeepSeek chat: ' + (Date.now() - start) + 'ms');
|
||||
return { content: result.rawText, usage: result.usage };
|
||||
}
|
||||
|
||||
getDashboardConfig() {
|
||||
return {
|
||||
url: 'http://10.2.0.7:9119',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user