import { ChatOpenAI } from "@langchain/openai"; import { SystemMessage, HumanMessage } from "@langchain/core/messages"; import { RunnableConfig } from "@langchain/core/runnables"; import { EvaluationState } from "../state"; /** * Node responsible for generating assessment questions based on the knowledge base content. */ export const questionGeneratorNode = async ( state: EvaluationState, config?: RunnableConfig ): Promise> => { const { model, knowledgeBaseContent } = (config?.configurable as any) || {}; console.log("[GeneratorNode] Starting generation...", { language: state.language, hasModel: !!model, contentLength: knowledgeBaseContent?.length }); if (!model || !knowledgeBaseContent) { throw new Error("Missing model or knowledgeBaseContent in node configuration"); } const isZh = state.language === 'zh'; const isJa = state.language === 'ja'; const systemPromptZh = `你是一位专业的知识评估专家。 你的任务是根据提供的知识库内容生成 3-5 个高质量的评估问题。 重要提示: 1. 你必须使用以下语言生成所有问题和内容:中文 (Simplified Chinese)。 2. 如果提供的知识库内容包含其他语言(如日语或英语),你必须在生成问题和关键点时将其相关术语翻译成中文。 3. 问题应考察用户对核心概念的理解和应用。 对于每个问题,你必须提供: 1. 问题文本。 2. 3-5 个关键点或概念,用户在回答中应当提到这些点以证明其掌握程度。 3. 难度级别(标准、进阶、专家)。 4. 出题依据(从知识库中提取的、支持此问题的相关文本片段或事实摘要)。 请以 JSON 数组格式返回响应: [ { "question_text": "...", "key_points": ["...", "..."], "difficulty": "...", "basis": "..." } ]`; const systemPromptJa = `あなたは専門的な知識アセスメントのエキスパートです。 提供されたナレッジベースの内容に基づいて、3〜5 個の高品質なアセスメント問題を作成してください。 重要事項: 1. すべての問題と内容は、次の言語で生成してください:日本語。 2. 提供されたナレッジベースの内容に他の言語(英語など)が含まれている場合、問題やキーワードを生成する際に、それらの用語を日本語に翻訳してください。 3. 問題は、ユーザーがコア概念を理解し、応用できるかを確認するものである必要があります。 各問題について、以下を提供してください: 1. 問題文。 2. ユーザーが習熟度を証明するために回答内で言及すべき 3〜5 個のキーポイントまたは概念。 3. 難易度(標準、上級、スペシャリスト)。 4. 出題の根拠(この問題を裏付けるナレッジベースから抽出された関連するテキストスニペットまたは事実の要約)。 レスポンスは以下の JSON 配列形式でフォーマットしてください: [ { "question_text": "...", "key_points": ["...", "..."], "difficulty": "...", "basis": "..." } ]`; const systemPromptEn = `You are a professional knowledge assessment expert. Your task is to generate 3-5 high-quality assessment questions based on the provided knowledge base content. IMPORTANT: 1. You MUST generate all questions and content in English. 2. If the provided knowledge base content contains other languages (e.g., Japanese or Chinese), you MUST translate the relevant terms into English when generating questions and key points. 3. Questions should test the user's understanding and application of core concepts. For each question, you must provide: 1. The question text. 2. A list of 3-5 key points or concepts that the user should mention in their answer to demonstrate mastery. 3. A difficulty level (Standard, Advanced, Specialist). 4. A basis for the question (the relevant text snippet or fact summary extracted from the knowledge base that supports this question). Format your response as a JSON array of objects: [ { "question_text": "...", "key_points": ["...", "..."], "difficulty": "...", "basis": "..." } ]`; const systemPrompt = isZh ? systemPromptZh : (isJa ? systemPromptJa : systemPromptEn); const humanMsg = isZh ? "现在生成评估问题。" : (isJa ? "今すぐアセスメント問題を生成してください。" : "Generate the assessment questions now."); const response = await model.invoke([ new SystemMessage(systemPrompt), new HumanMessage(humanMsg), ]); try { const questions = JSON.parse(response.content as string); console.log("[GeneratorNode] Successfully generated questions:", questions.length); return { questions: questions.map((q: any) => ({ questionText: q.question_text, keyPoints: q.key_points, difficulty: q.difficulty, basis: q.basis, })), currentQuestionIndex: 0, }; } catch (error) { console.error("[GeneratorNode] Failed to parse questions from AI response:", error); return { questions: [], currentQuestionIndex: 0 }; } };