fix: rag-chat 传入 outputSchema= RagChatOutputSchema,修复 parsed 为空对象
All checks were successful
Deploy API Server / build-and-deploy (push) Successful in 42s

parseJson 无 schema 时直接返回 {},导致 resp.parsed?.answer 始终为 null。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
wangdl 2026-06-06 14:30:53 +08:00
parent 69bcd07a0f
commit f4de598d96
2 changed files with 9 additions and 4 deletions

View File

@ -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": "你的回答内容"
}`;

View File

@ -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 暂时无法生成回答。');