diff --git a/src/modules/ai/prompts/schemas/rag-chat.schema.ts b/src/modules/ai/prompts/schemas/rag-chat.schema.ts index 3756b6e..1009cbf 100644 --- a/src/modules/ai/prompts/schemas/rag-chat.schema.ts +++ b/src/modules/ai/prompts/schemas/rag-chat.schema.ts @@ -1,6 +1,9 @@ +import { z } from 'zod'; + +export const RagChatOutputSchema = z.object({ + answer: z.string().min(1).max(5000), +}); + export const RAG_CHAT_OUTPUT_SCHEMA_DESC = `{ - "answer": "你的回答内容", - "citations": [ - { "title": "引用的知识点标题", "snippet": "引用的原文片段" } - ] + "answer": "你的回答内容" }`; diff --git a/src/modules/rag-chat/rag-chat.service.ts b/src/modules/rag-chat/rag-chat.service.ts index 7adf096..9b5e0fc 100644 --- a/src/modules/rag-chat/rag-chat.service.ts +++ b/src/modules/rag-chat/rag-chat.service.ts @@ -2,6 +2,7 @@ import { Injectable, NotFoundException, Logger, Optional } from '@nestjs/common' import { PrismaService } from '../../infrastructure/database/prisma.service'; import { ContentSafetyService } from '../content-safety/content-safety.service'; import { AiGatewayService } from '../ai/gateway/ai-gateway.service'; +import { RagChatOutputSchema } from '../ai/prompts/schemas/rag-chat.schema'; const MAX_CONTEXT_CHARS = 4000; @@ -75,6 +76,7 @@ export class RagChatService { promptVersion: 'v1', messages, maxTokens: 2048, + outputSchema: RagChatOutputSchema, }); this.logger.log(`AI Gateway response: parsed=${!!resp.parsed}, keys=${resp.parsed ? Object.keys(resp.parsed).join(',') : 'null'}, raw=${JSON.stringify(resp.parsed).substring(0, 300)}`); reply = resp.parsed?.answer ?? String(resp.parsed?.content ?? '抱歉,AI 暂时无法生成回答。');