feat: integrate dynamic config into AI chat — temperature/max_tokens/URL from DB
Some checks failed
Deploy API Server / build-and-deploy (push) Has been cancelled
Some checks failed
Deploy API Server / build-and-deploy (push) Has been cancelled
This commit is contained in:
parent
585cde9431
commit
4077150295
@ -4,9 +4,10 @@ import { AdminAiChatService } from './admin-ai-chat.service';
|
||||
import { AdminAuthGuard } from '../../common/guards/admin-auth.guard';
|
||||
import { AdminRolesGuard } from '../../common/guards/admin-roles.guard';
|
||||
import { AdminConversationModule } from '../admin-conversation/admin-conversation.module';
|
||||
import { AppConfigModule } from '../config/config.module';
|
||||
|
||||
@Module({
|
||||
imports: [AdminConversationModule],
|
||||
imports: [AdminConversationModule, AppConfigModule],
|
||||
controllers: [AdminAiChatController],
|
||||
providers: [AdminAiChatService, AdminAuthGuard, AdminRolesGuard],
|
||||
})
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import type { AiChatDto } from './dto/ai-chat.dto';
|
||||
import { AdminConversationService } from '../admin-conversation/admin-conversation.service';
|
||||
import { AppConfigService } from '../config/config.service';
|
||||
import type { Response } from 'express';
|
||||
|
||||
const HERMES_API_URL = 'http://10.2.0.7:8642';
|
||||
// HERMES_API_URL now read from config, fallback to hardcoded
|
||||
const HERMES_API_KEY = 'zhixi-hermes-key-2026';
|
||||
|
||||
interface HermesRun {
|
||||
@ -25,7 +26,7 @@ export class AdminAiChatService {
|
||||
private readonly logger = new Logger(AdminAiChatService.name);
|
||||
private activeRuns = new Map<string, AbortController>();
|
||||
|
||||
constructor(private readonly conversationService: AdminConversationService) {}
|
||||
constructor(private readonly conversationService: AdminConversationService, private readonly config: AppConfigService) {}
|
||||
|
||||
async chat(dto: AiChatDto, adminUserId: string) {
|
||||
const sessionId = dto.conversationId
|
||||
@ -55,9 +56,10 @@ export class AdminAiChatService {
|
||||
};
|
||||
if (sessionId) headers['X-Hermes-Session-Id'] = sessionId;
|
||||
|
||||
const resp = await fetch(`${HERMES_API_URL}/v1/chat/completions`, {
|
||||
const hermesUrl = await this.config.get('hermes.api_url', 'http://10.2.0.7:8642');
|
||||
const resp = await fetch(`${hermesUrl}/v1/chat/completions`, {
|
||||
method: 'POST', headers,
|
||||
body: JSON.stringify({ model: 'hermes-agent', messages, temperature: 0.7, max_tokens: 4096 }),
|
||||
body: JSON.stringify({ model: 'hermes-agent', messages, temperature: parseFloat(await this.config.get('ai.temperature', '0.7')), max_tokens: parseInt(await this.config.get('ai.max_tokens', '4096')) }),
|
||||
signal: AbortSignal.timeout(120_000),
|
||||
});
|
||||
if (!resp.ok) throw new Error(`Hermes API error ${resp.status}`);
|
||||
@ -100,7 +102,8 @@ export class AdminAiChatService {
|
||||
};
|
||||
if (sessionId) headers['X-Hermes-Session-Id'] = sessionId;
|
||||
|
||||
const runResp = await fetch(`${HERMES_API_URL}/v1/runs`, {
|
||||
const hermesUrl2 = await this.config.get('hermes.api_url', 'http://10.2.0.7:8642');
|
||||
const runResp = await fetch(`${hermesUrl2}/v1/runs`, {
|
||||
method: 'POST', headers,
|
||||
body: JSON.stringify({
|
||||
input: dto.messages.map(m => ({ role: m.role, content: m.content })),
|
||||
@ -117,7 +120,7 @@ export class AdminAiChatService {
|
||||
const abortController = new AbortController();
|
||||
this.activeRuns.set(runId, abortController);
|
||||
|
||||
const eventResp = await fetch(`${HERMES_API_URL}/v1/runs/${runId}/events`, {
|
||||
const eventResp = await fetch(`${hermesUrl2}/v1/runs/${runId}/events`, {
|
||||
headers: { Authorization: 'Bearer ' + HERMES_API_KEY },
|
||||
signal: abortController.signal,
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user