Update story summary recall and prompt injection
This commit is contained in:
@@ -8,6 +8,7 @@ import { generateSummary, parseSummaryJson } from "./llm.js";
|
|||||||
|
|
||||||
const MODULE_ID = 'summaryGenerator';
|
const MODULE_ID = 'summaryGenerator';
|
||||||
const SUMMARY_SESSION_ID = 'xb9';
|
const SUMMARY_SESSION_ID = 'xb9';
|
||||||
|
const MAX_CAUSED_BY = 2;
|
||||||
|
|
||||||
// ═══════════════════════════════════════════════════════════════════════════
|
// ═══════════════════════════════════════════════════════════════════════════
|
||||||
// worldUpdate 清洗
|
// worldUpdate 清洗
|
||||||
@@ -45,6 +46,57 @@ function sanitizeWorldUpdate(parsed) {
|
|||||||
parsed.worldUpdate = ok;
|
parsed.worldUpdate = ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ═══════════════════════════════════════════════════════════════════════════
|
||||||
|
// causedBy 清洗(事件因果边)
|
||||||
|
// - 允许引用:已存在事件 + 本次新输出事件
|
||||||
|
// - 限制长度:0-2
|
||||||
|
// - 去重、剔除非法ID、剔除自引用
|
||||||
|
// ═══════════════════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
function sanitizeEventsCausality(parsed, existingEventIds) {
|
||||||
|
if (!parsed) return;
|
||||||
|
|
||||||
|
const events = Array.isArray(parsed.events) ? parsed.events : [];
|
||||||
|
if (!events.length) return;
|
||||||
|
|
||||||
|
const idRe = /^evt-\d+$/;
|
||||||
|
|
||||||
|
// 本次新输出事件ID集合(允许引用)
|
||||||
|
const newIds = new Set(
|
||||||
|
events
|
||||||
|
.map(e => String(e?.id || '').trim())
|
||||||
|
.filter(id => idRe.test(id))
|
||||||
|
);
|
||||||
|
|
||||||
|
const allowed = new Set([...(existingEventIds || []), ...newIds]);
|
||||||
|
|
||||||
|
for (const e of events) {
|
||||||
|
const selfId = String(e?.id || '').trim();
|
||||||
|
if (!idRe.test(selfId)) {
|
||||||
|
// id 不合格的话,causedBy 直接清空,避免污染
|
||||||
|
e.causedBy = [];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const raw = Array.isArray(e.causedBy) ? e.causedBy : [];
|
||||||
|
const out = [];
|
||||||
|
const seen = new Set();
|
||||||
|
|
||||||
|
for (const x of raw) {
|
||||||
|
const cid = String(x || '').trim();
|
||||||
|
if (!idRe.test(cid)) continue;
|
||||||
|
if (cid === selfId) continue;
|
||||||
|
if (!allowed.has(cid)) continue;
|
||||||
|
if (seen.has(cid)) continue;
|
||||||
|
seen.add(cid);
|
||||||
|
out.push(cid);
|
||||||
|
if (out.length >= MAX_CAUSED_BY) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
e.causedBy = out;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ═══════════════════════════════════════════════════════════════════════════
|
// ═══════════════════════════════════════════════════════════════════════════
|
||||||
// 辅助函数
|
// 辅助函数
|
||||||
// ═══════════════════════════════════════════════════════════════════════════
|
// ═══════════════════════════════════════════════════════════════════════════
|
||||||
@@ -180,6 +232,8 @@ export async function runSummaryGeneration(mesId, config, callbacks = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sanitizeWorldUpdate(parsed);
|
sanitizeWorldUpdate(parsed);
|
||||||
|
const existingEventIds = new Set((store?.json?.events || []).map(e => e?.id).filter(Boolean));
|
||||||
|
sanitizeEventsCausality(parsed, existingEventIds);
|
||||||
|
|
||||||
const merged = mergeNewData(store?.json || {}, parsed, slice.endMesId);
|
const merged = mergeNewData(store?.json || {}, parsed, slice.endMesId);
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
// LLM Service
|
// LLM Service
|
||||||
|
|
||||||
const PROVIDER_MAP = {
|
const PROVIDER_MAP = {
|
||||||
|
// ...
|
||||||
openai: "openai",
|
openai: "openai",
|
||||||
google: "gemini",
|
google: "gemini",
|
||||||
gemini: "gemini",
|
gemini: "gemini",
|
||||||
@@ -35,6 +36,7 @@ Incremental_Summary_Requirements:
|
|||||||
- 转折: 改变某条线走向
|
- 转折: 改变某条线走向
|
||||||
- 点睛: 有细节不影响主线
|
- 点睛: 有细节不影响主线
|
||||||
- 氛围: 纯粹氛围片段
|
- 氛围: 纯粹氛围片段
|
||||||
|
- Causal_Chain: 为每个新事件标注直接前因事件ID(causedBy),0-2个。只填 evt-数字 形式,必须指向“已存在事件”或“本次新输出事件”。不要写解释文字。
|
||||||
- Character_Dynamics: 识别新角色,追踪关系趋势(破裂/厌恶/反感/陌生/投缘/亲密/交融)
|
- Character_Dynamics: 识别新角色,追踪关系趋势(破裂/厌恶/反感/陌生/投缘/亲密/交融)
|
||||||
- Arc_Tracking: 更新角色弧光轨迹与成长进度(0.0-1.0)
|
- Arc_Tracking: 更新角色弧光轨迹与成长进度(0.0-1.0)
|
||||||
- World_State_Tracking: 维护当前世界的硬性约束。解决"什么不能违反"。采用 KV 覆盖模型,追踪生死、物品归属、秘密知情、关系状态、环境规则等不可违背的事实。(覆盖式更新)
|
- World_State_Tracking: 维护当前世界的硬性约束。解决"什么不能违反"。采用 KV 覆盖模型,追踪生死、物品归属、秘密知情、关系状态、环境规则等不可违背的事实。(覆盖式更新)
|
||||||
@@ -171,7 +173,8 @@ Before generating, observe the USER and analyze carefully:
|
|||||||
"summary": "1-2句话描述,涵盖丰富信息素,末尾标注楼层(#X-Y)",
|
"summary": "1-2句话描述,涵盖丰富信息素,末尾标注楼层(#X-Y)",
|
||||||
"participants": ["参与角色名"],
|
"participants": ["参与角色名"],
|
||||||
"type": "相遇|冲突|揭示|抉择|羁绊|转变|收束|日常",
|
"type": "相遇|冲突|揭示|抉择|羁绊|转变|收束|日常",
|
||||||
"weight": "核心|主线|转折|点睛|氛围"
|
"weight": "核心|主线|转折|点睛|氛围",
|
||||||
|
"causedBy": ["evt-12", "evt-14"]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"newCharacters": ["仅本次首次出现的角色名"],
|
"newCharacters": ["仅本次首次出现的角色名"],
|
||||||
@@ -211,6 +214,10 @@ Before generating, observe the USER and analyze carefully:
|
|||||||
- events.id 从 evt-{nextEventId} 开始编号
|
- events.id 从 evt-{nextEventId} 开始编号
|
||||||
- 仅输出【增量】内容,已有事件绝不重复
|
- 仅输出【增量】内容,已有事件绝不重复
|
||||||
- keywords 是全局关键词,综合已有+新增
|
- keywords 是全局关键词,综合已有+新增
|
||||||
|
- causedBy 规则:
|
||||||
|
- 数组,最多2个;无前因则 []
|
||||||
|
- 只能填 evt-数字(例如 evt-12)
|
||||||
|
- 必须引用“已存在事件”或“本次新输出事件”(允许引用本次 JSON 内较早出现的事件)
|
||||||
- worldUpdate 可为空数组
|
- worldUpdate 可为空数组
|
||||||
- 合法JSON,字符串值内部避免英文双引号
|
- 合法JSON,字符串值内部避免英文双引号
|
||||||
- 用小说家的细腻笔触记录,带烟火气
|
- 用小说家的细腻笔触记录,带烟火气
|
||||||
@@ -441,4 +448,4 @@ export async function generateSummary(options) {
|
|||||||
console.groupEnd();
|
console.groupEnd();
|
||||||
|
|
||||||
return rawOutput;
|
return rawOutput;
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -1261,9 +1261,21 @@ h1 span {
|
|||||||
|
|
||||||
#recall-log-modal .modal-box {
|
#recall-log-modal .modal-box {
|
||||||
max-width: 900px;
|
max-width: 900px;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
#recall-log-modal .modal-body {
|
||||||
|
flex: 1;
|
||||||
|
min-height: 0;
|
||||||
|
padding: 0;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
}
|
}
|
||||||
|
|
||||||
#recall-log-content {
|
#recall-log-content {
|
||||||
|
flex: 1;
|
||||||
|
min-height: 0;
|
||||||
white-space: pre-wrap;
|
white-space: pre-wrap;
|
||||||
font-family: 'SF Mono', Monaco, Consolas, 'Courier New', monospace;
|
font-family: 'SF Mono', Monaco, Consolas, 'Courier New', monospace;
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
@@ -1271,8 +1283,6 @@ h1 span {
|
|||||||
background: var(--bg3);
|
background: var(--bg3);
|
||||||
padding: 16px;
|
padding: 16px;
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
min-height: 200px;
|
|
||||||
max-height: 60vh;
|
|
||||||
overflow-y: auto;
|
overflow-y: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1283,6 +1293,21 @@ h1 span {
|
|||||||
font-style: italic;
|
font-style: italic;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 移动端适配 */
|
||||||
|
@media (max-width: 768px) {
|
||||||
|
#recall-log-modal .modal-box {
|
||||||
|
max-width: 100%;
|
||||||
|
max-height: 100%;
|
||||||
|
height: 100%;
|
||||||
|
border-radius: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#recall-log-content {
|
||||||
|
font-size: 11px;
|
||||||
|
padding: 12px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* ═══════════════════════════════════════════════════════════════════════════
|
/* ═══════════════════════════════════════════════════════════════════════════
|
||||||
HF Guide
|
HF Guide
|
||||||
═══════════════════════════════════════════════════════════════════════════ */
|
═══════════════════════════════════════════════════════════════════════════ */
|
||||||
|
|||||||
@@ -20,13 +20,18 @@ const CONFIG = {
|
|||||||
QUERY_MAX_CHARS: 600,
|
QUERY_MAX_CHARS: 600,
|
||||||
QUERY_CONTEXT_CHARS: 240,
|
QUERY_CONTEXT_CHARS: 240,
|
||||||
|
|
||||||
CANDIDATE_CHUNKS: 120,
|
// 因果链
|
||||||
CANDIDATE_EVENTS: 100,
|
CAUSAL_CHAIN_MAX_DEPTH: 10, // 放宽跳数,让图自然终止
|
||||||
|
CAUSAL_INJECT_MAX: 30, // 放宽上限,由 prompt token 预算最终控制
|
||||||
|
|
||||||
TOP_K_CHUNKS: 40,
|
CANDIDATE_CHUNKS: 200,
|
||||||
TOP_K_EVENTS: 35,
|
CANDIDATE_EVENTS: 150,
|
||||||
|
|
||||||
MIN_SIMILARITY: 0.35,
|
MAX_CHUNKS: 40,
|
||||||
|
MAX_EVENTS: 120,
|
||||||
|
|
||||||
|
MIN_SIMILARITY_CHUNK: 0.55,
|
||||||
|
MIN_SIMILARITY_EVENT: 0.6,
|
||||||
MMR_LAMBDA: 0.72,
|
MMR_LAMBDA: 0.72,
|
||||||
|
|
||||||
BONUS_PARTICIPANT_HIT: 0.08,
|
BONUS_PARTICIPANT_HIT: 0.08,
|
||||||
@@ -58,6 +63,78 @@ function normalizeVec(v) {
|
|||||||
return v.map(x => x / s);
|
return v.map(x => x / s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ═══════════════════════════════════════════════════════════════════════════
|
||||||
|
// 因果链追溯(Graph-augmented retrieval)
|
||||||
|
// - 从已召回事件出发,沿 causedBy 向上追溯祖先事件
|
||||||
|
// - 记录边:chainFrom = 哪个召回事件需要它
|
||||||
|
// - 不在这里决定“是否额外注入”,只负责遍历与结构化结果
|
||||||
|
// ═══════════════════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
function buildEventIndex(allEvents) {
|
||||||
|
const map = new Map();
|
||||||
|
for (const e of allEvents || []) {
|
||||||
|
if (e?.id) map.set(e.id, e);
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {Map<string, {event, depth, chainFrom}>}
|
||||||
|
*/
|
||||||
|
function traceCausalAncestors(recalledEvents, eventIndex, maxDepth = CONFIG.CAUSAL_CHAIN_MAX_DEPTH) {
|
||||||
|
const out = new Map();
|
||||||
|
const idRe = /^evt-\d+$/;
|
||||||
|
|
||||||
|
function visit(parentId, depth, chainFrom) {
|
||||||
|
if (depth > maxDepth) return;
|
||||||
|
if (!idRe.test(parentId)) return;
|
||||||
|
|
||||||
|
const ev = eventIndex.get(parentId);
|
||||||
|
if (!ev) return;
|
||||||
|
|
||||||
|
// 如果同一个祖先被多个召回事件引用:保留更“近”的深度或追加来源
|
||||||
|
const existed = out.get(parentId);
|
||||||
|
if (!existed) {
|
||||||
|
out.set(parentId, { event: ev, depth, chainFrom: [chainFrom] });
|
||||||
|
} else {
|
||||||
|
if (depth < existed.depth) existed.depth = depth;
|
||||||
|
if (!existed.chainFrom.includes(chainFrom)) existed.chainFrom.push(chainFrom);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const next of (ev.causedBy || [])) {
|
||||||
|
visit(String(next || '').trim(), depth + 1, chainFrom);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const r of recalledEvents || []) {
|
||||||
|
const rid = r?.event?.id;
|
||||||
|
if (!rid) continue;
|
||||||
|
for (const cid of (r.event?.causedBy || [])) {
|
||||||
|
visit(String(cid || '').trim(), 1, rid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 因果事件排序:引用数 > 深度 > 编号
|
||||||
|
*/
|
||||||
|
function sortCausalEvents(causalArray) {
|
||||||
|
return causalArray.sort((a, b) => {
|
||||||
|
// 1. 被多条召回链引用的优先
|
||||||
|
const refDiff = b.chainFrom.length - a.chainFrom.length;
|
||||||
|
if (refDiff !== 0) return refDiff;
|
||||||
|
|
||||||
|
// 2. 深度浅的优先
|
||||||
|
const depthDiff = a.depth - b.depth;
|
||||||
|
if (depthDiff !== 0) return depthDiff;
|
||||||
|
|
||||||
|
// 3. 事件编号排序
|
||||||
|
return String(a.event.id).localeCompare(String(b.event.id));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function normalize(s) {
|
function normalize(s) {
|
||||||
return String(s || '').normalize('NFKC').replace(/[\u200B-\u200D\uFEFF]/g, '').trim();
|
return String(s || '').normalize('NFKC').replace(/[\u200B-\u200D\uFEFF]/g, '').trim();
|
||||||
}
|
}
|
||||||
@@ -243,14 +320,31 @@ async function searchChunks(queryVector, vectorConfig) {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Pre-filter stats for logging
|
||||||
|
const preFilterStats = {
|
||||||
|
total: scored.length,
|
||||||
|
passThreshold: scored.filter(s => s.similarity >= CONFIG.MIN_SIMILARITY_CHUNK).length,
|
||||||
|
threshold: CONFIG.MIN_SIMILARITY_CHUNK,
|
||||||
|
distribution: {
|
||||||
|
'0.8+': scored.filter(s => s.similarity >= 0.8).length,
|
||||||
|
'0.7-0.8': scored.filter(s => s.similarity >= 0.7 && s.similarity < 0.8).length,
|
||||||
|
'0.6-0.7': scored.filter(s => s.similarity >= 0.6 && s.similarity < 0.7).length,
|
||||||
|
'0.55-0.6': scored.filter(s => s.similarity >= 0.55 && s.similarity < 0.6).length,
|
||||||
|
'<0.55': scored.filter(s => s.similarity < 0.55).length,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
const candidates = scored
|
const candidates = scored
|
||||||
.filter(s => s.similarity >= CONFIG.MIN_SIMILARITY)
|
.filter(s => s.similarity >= CONFIG.MIN_SIMILARITY_CHUNK)
|
||||||
.sort((a, b) => b.similarity - a.similarity)
|
.sort((a, b) => b.similarity - a.similarity)
|
||||||
.slice(0, CONFIG.CANDIDATE_CHUNKS);
|
.slice(0, CONFIG.CANDIDATE_CHUNKS);
|
||||||
|
|
||||||
|
// 动态 K:质量不够就少拿
|
||||||
|
const dynamicK = Math.min(CONFIG.MAX_CHUNKS, candidates.length);
|
||||||
|
|
||||||
const selected = mmrSelect(
|
const selected = mmrSelect(
|
||||||
candidates,
|
candidates,
|
||||||
CONFIG.TOP_K_CHUNKS,
|
dynamicK,
|
||||||
CONFIG.MMR_LAMBDA,
|
CONFIG.MMR_LAMBDA,
|
||||||
c => c.vector,
|
c => c.vector,
|
||||||
c => c.similarity
|
c => c.similarity
|
||||||
@@ -270,7 +364,7 @@ async function searchChunks(queryVector, vectorConfig) {
|
|||||||
const chunks = await getChunksByFloors(chatId, floors);
|
const chunks = await getChunksByFloors(chatId, floors);
|
||||||
const chunkMap = new Map(chunks.map(c => [c.chunkId, c]));
|
const chunkMap = new Map(chunks.map(c => [c.chunkId, c]));
|
||||||
|
|
||||||
return sparse.map(item => {
|
const results = sparse.map(item => {
|
||||||
const chunk = chunkMap.get(item.chunkId);
|
const chunk = chunkMap.get(item.chunkId);
|
||||||
if (!chunk) return null;
|
if (!chunk) return null;
|
||||||
return {
|
return {
|
||||||
@@ -283,6 +377,13 @@ async function searchChunks(queryVector, vectorConfig) {
|
|||||||
similarity: item.similarity,
|
similarity: item.similarity,
|
||||||
};
|
};
|
||||||
}).filter(Boolean);
|
}).filter(Boolean);
|
||||||
|
|
||||||
|
// Attach stats for logging
|
||||||
|
if (results.length > 0) {
|
||||||
|
results._preFilterStats = preFilterStats;
|
||||||
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ═══════════════════════════════════════════════════════════════════════════
|
// ═══════════════════════════════════════════════════════════════════════════
|
||||||
@@ -291,14 +392,27 @@ async function searchChunks(queryVector, vectorConfig) {
|
|||||||
|
|
||||||
async function searchEvents(queryVector, allEvents, vectorConfig, store, queryEntities) {
|
async function searchEvents(queryVector, allEvents, vectorConfig, store, queryEntities) {
|
||||||
const { chatId, name1 } = getContext();
|
const { chatId, name1 } = getContext();
|
||||||
if (!chatId || !queryVector?.length) return [];
|
if (!chatId || !queryVector?.length) {
|
||||||
|
console.warn('[searchEvents] 早期返回: chatId或queryVector为空');
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
const meta = await getMeta(chatId);
|
const meta = await getMeta(chatId);
|
||||||
const fp = getEngineFingerprint(vectorConfig);
|
const fp = getEngineFingerprint(vectorConfig);
|
||||||
|
console.log('[searchEvents] fingerprint检查:', {
|
||||||
|
metaFp: meta.fingerprint,
|
||||||
|
currentFp: fp,
|
||||||
|
match: meta.fingerprint === fp || !meta.fingerprint,
|
||||||
|
});
|
||||||
if (meta.fingerprint && meta.fingerprint !== fp) return [];
|
if (meta.fingerprint && meta.fingerprint !== fp) return [];
|
||||||
|
|
||||||
const eventVectors = await getAllEventVectors(chatId);
|
const eventVectors = await getAllEventVectors(chatId);
|
||||||
const vectorMap = new Map(eventVectors.map(v => [v.eventId, v.vector]));
|
const vectorMap = new Map(eventVectors.map(v => [v.eventId, v.vector]));
|
||||||
|
console.log('[searchEvents] 向量数据:', {
|
||||||
|
eventVectorsCount: eventVectors.length,
|
||||||
|
vectorMapSize: vectorMap.size,
|
||||||
|
allEventsCount: allEvents?.length,
|
||||||
|
});
|
||||||
if (!vectorMap.size) return [];
|
if (!vectorMap.size) return [];
|
||||||
|
|
||||||
const userName = normalize(name1);
|
const userName = normalize(name1);
|
||||||
@@ -350,14 +464,40 @@ async function searchEvents(queryVector, allEvents, vectorConfig, store, queryEn
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 相似度分布日志
|
||||||
|
const simValues = scored.map(s => s.similarity).sort((a, b) => b - a);
|
||||||
|
console.log('[searchEvents] 相似度分布(前20):', simValues.slice(0, 20));
|
||||||
|
console.log('[searchEvents] 相似度分布(后20):', simValues.slice(-20));
|
||||||
|
console.log('[searchEvents] 有向量的事件数:', scored.filter(s => s.similarity > 0).length);
|
||||||
|
console.log('[searchEvents] sim >= 0.6:', scored.filter(s => s.similarity >= 0.6).length);
|
||||||
|
console.log('[searchEvents] sim >= 0.5:', scored.filter(s => s.similarity >= 0.5).length);
|
||||||
|
console.log('[searchEvents] sim >= 0.3:', scored.filter(s => s.similarity >= 0.3).length);
|
||||||
|
|
||||||
|
// ★ 记录过滤前的分布(用 finalScore,与显示一致)
|
||||||
|
const preFilterDistribution = {
|
||||||
|
total: scored.length,
|
||||||
|
'0.85+': scored.filter(s => s.finalScore >= 0.85).length,
|
||||||
|
'0.7-0.85': scored.filter(s => s.finalScore >= 0.7 && s.finalScore < 0.85).length,
|
||||||
|
'0.6-0.7': scored.filter(s => s.finalScore >= 0.6 && s.finalScore < 0.7).length,
|
||||||
|
'0.5-0.6': scored.filter(s => s.finalScore >= 0.5 && s.finalScore < 0.6).length,
|
||||||
|
'<0.5': scored.filter(s => s.finalScore < 0.5).length,
|
||||||
|
passThreshold: scored.filter(s => s.finalScore >= CONFIG.MIN_SIMILARITY_EVENT).length,
|
||||||
|
threshold: CONFIG.MIN_SIMILARITY_EVENT,
|
||||||
|
};
|
||||||
|
|
||||||
|
// ★ 过滤改成用 finalScore(包含 bonus)
|
||||||
const candidates = scored
|
const candidates = scored
|
||||||
.filter(s => s.similarity >= CONFIG.MIN_SIMILARITY)
|
.filter(s => s.finalScore >= CONFIG.MIN_SIMILARITY_EVENT)
|
||||||
.sort((a, b) => b.finalScore - a.finalScore)
|
.sort((a, b) => b.finalScore - a.finalScore)
|
||||||
.slice(0, CONFIG.CANDIDATE_EVENTS);
|
.slice(0, CONFIG.CANDIDATE_EVENTS);
|
||||||
|
console.log('[searchEvents] 过滤后candidates:', candidates.length);
|
||||||
|
|
||||||
|
// 动态 K:质量不够就少拿
|
||||||
|
const dynamicK = Math.min(CONFIG.MAX_EVENTS, candidates.length);
|
||||||
|
|
||||||
const selected = mmrSelect(
|
const selected = mmrSelect(
|
||||||
candidates,
|
candidates,
|
||||||
CONFIG.TOP_K_EVENTS,
|
dynamicK,
|
||||||
CONFIG.MMR_LAMBDA,
|
CONFIG.MMR_LAMBDA,
|
||||||
c => c.vector,
|
c => c.vector,
|
||||||
c => c.finalScore
|
c => c.finalScore
|
||||||
@@ -370,14 +510,59 @@ async function searchEvents(queryVector, allEvents, vectorConfig, store, queryEn
|
|||||||
similarity: s.finalScore,
|
similarity: s.finalScore,
|
||||||
_recallType: s.isDirect ? 'DIRECT' : 'SIMILAR',
|
_recallType: s.isDirect ? 'DIRECT' : 'SIMILAR',
|
||||||
_recallReason: s.reasons.length ? s.reasons.join('+') : '相似',
|
_recallReason: s.reasons.length ? s.reasons.join('+') : '相似',
|
||||||
|
_preFilterDistribution: preFilterDistribution,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// ═══════════════════════════════════════════════════════════════════════════
|
// ═══════════════════════════════════════════════════════════════════════════
|
||||||
// 日志
|
// 日志:因果树格式化
|
||||||
// ═══════════════════════════════════════════════════════════════════════════
|
// ═══════════════════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
function formatRecallLog({ elapsed, segments, weights, chunkResults, eventResults, allEvents, queryEntities }) {
|
function formatCausalTree(causalEvents, recalledEvents) {
|
||||||
|
if (!causalEvents?.length) return '';
|
||||||
|
|
||||||
|
const lines = [
|
||||||
|
'',
|
||||||
|
'┌─────────────────────────────────────────────────────────────┐',
|
||||||
|
'│ 【因果链追溯】 │',
|
||||||
|
'└─────────────────────────────────────────────────────────────┘',
|
||||||
|
];
|
||||||
|
|
||||||
|
// 按 chainFrom 分组展示
|
||||||
|
const bySource = new Map();
|
||||||
|
for (const c of causalEvents) {
|
||||||
|
for (const src of c.chainFrom || []) {
|
||||||
|
if (!bySource.has(src)) bySource.set(src, []);
|
||||||
|
bySource.get(src).push(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const [sourceId, ancestors] of bySource) {
|
||||||
|
const sourceEvent = recalledEvents.find(e => e.event?.id === sourceId);
|
||||||
|
const sourceTitle = sourceEvent?.event?.title || sourceId;
|
||||||
|
lines.push(` ${sourceId} "${sourceTitle}" 的前因链:`);
|
||||||
|
|
||||||
|
// 按深度排序
|
||||||
|
ancestors.sort((a, b) => a.depth - b.depth);
|
||||||
|
|
||||||
|
for (const c of ancestors) {
|
||||||
|
const indent = ' ' + ' '.repeat(c.depth - 1);
|
||||||
|
const ev = c.event;
|
||||||
|
const title = ev.title || '(无标题)';
|
||||||
|
const refs = c.chainFrom.length > 1 ? ` [被${c.chainFrom.length}条链引用]` : '';
|
||||||
|
lines.push(`${indent}└─ [depth=${c.depth}] ${ev.id} "${title}"${refs}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lines.push('');
|
||||||
|
return lines.join('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
// ═══════════════════════════════════════════════════════════════════════════
|
||||||
|
// 日志:主报告
|
||||||
|
// ═══════════════════════════════════════════════════════════════════════════
|
||||||
|
|
||||||
|
function formatRecallLog({ elapsed, segments, weights, chunkResults, eventResults, allEvents, queryEntities, causalEvents = [], chunkPreFilterStats = null }) {
|
||||||
const lines = [
|
const lines = [
|
||||||
'╔══════════════════════════════════════════════════════════════╗',
|
'╔══════════════════════════════════════════════════════════════╗',
|
||||||
'║ 记忆召回报告 ║',
|
'║ 记忆召回报告 ║',
|
||||||
@@ -413,9 +598,21 @@ function formatRecallLog({ elapsed, segments, weights, chunkResults, eventResult
|
|||||||
|
|
||||||
lines.push('');
|
lines.push('');
|
||||||
lines.push('┌─────────────────────────────────────────────────────────────┐');
|
lines.push('┌─────────────────────────────────────────────────────────────┐');
|
||||||
lines.push(`│ 【L1 原文片段】召回 ${chunkResults.length} 条`);
|
lines.push('│ 【L1 原文片段】 │');
|
||||||
lines.push('└─────────────────────────────────────────────────────────────┘');
|
lines.push('└─────────────────────────────────────────────────────────────┘');
|
||||||
|
|
||||||
|
if (chunkPreFilterStats) {
|
||||||
|
const dist = chunkPreFilterStats.distribution || {};
|
||||||
|
lines.push(` 过滤前: ${chunkPreFilterStats.total} 条`);
|
||||||
|
lines.push(' 相似度分布:');
|
||||||
|
lines.push(` 0.8+: ${dist['0.8+'] || 0} | 0.7-0.8: ${dist['0.7-0.8'] || 0} | 0.6-0.7: ${dist['0.6-0.7'] || 0}`);
|
||||||
|
lines.push(` 0.55-0.6: ${dist['0.55-0.6'] || 0} | <0.55: ${dist['<0.55'] || 0}`);
|
||||||
|
lines.push(` 通过阈值(>=${chunkPreFilterStats.threshold}): ${chunkPreFilterStats.passThreshold} 条`);
|
||||||
|
lines.push(` MMR+Floor去重后: ${chunkResults.length} 条`);
|
||||||
|
} else {
|
||||||
|
lines.push(` 召回: ${chunkResults.length} 条`);
|
||||||
|
}
|
||||||
|
|
||||||
chunkResults.slice(0, 15).forEach((c, i) => {
|
chunkResults.slice(0, 15).forEach((c, i) => {
|
||||||
const preview = c.text.length > 50 ? c.text.slice(0, 50) + '...' : c.text;
|
const preview = c.text.length > 50 ? c.text.slice(0, 50) + '...' : c.text;
|
||||||
lines.push(` ${String(i + 1).padStart(2)}. #${String(c.floor).padStart(3)} [${c.speaker}] ${preview}`);
|
lines.push(` ${String(i + 1).padStart(2)}. #${String(c.floor).padStart(3)} [${c.speaker}] ${preview}`);
|
||||||
@@ -428,7 +625,7 @@ function formatRecallLog({ elapsed, segments, weights, chunkResults, eventResult
|
|||||||
|
|
||||||
lines.push('');
|
lines.push('');
|
||||||
lines.push('┌─────────────────────────────────────────────────────────────┐');
|
lines.push('┌─────────────────────────────────────────────────────────────┐');
|
||||||
lines.push(`│ 【L2 事件记忆】召回 ${eventResults.length} / ${allEvents.length} 条`);
|
lines.push('│ 【L2 事件记忆】 │');
|
||||||
lines.push('│ DIRECT=亲身经历 SIMILAR=相关背景 │');
|
lines.push('│ DIRECT=亲身经历 SIMILAR=相关背景 │');
|
||||||
lines.push('└─────────────────────────────────────────────────────────────┘');
|
lines.push('└─────────────────────────────────────────────────────────────┘');
|
||||||
|
|
||||||
@@ -442,16 +639,27 @@ function formatRecallLog({ elapsed, segments, weights, chunkResults, eventResult
|
|||||||
// 统计
|
// 统计
|
||||||
const directCount = eventResults.filter(e => e._recallType === 'DIRECT').length;
|
const directCount = eventResults.filter(e => e._recallType === 'DIRECT').length;
|
||||||
const similarCount = eventResults.filter(e => e._recallType === 'SIMILAR').length;
|
const similarCount = eventResults.filter(e => e._recallType === 'SIMILAR').length;
|
||||||
|
const preFilterDist = eventResults[0]?._preFilterDistribution || {};
|
||||||
|
|
||||||
lines.push('');
|
lines.push('');
|
||||||
lines.push('┌─────────────────────────────────────────────────────────────┐');
|
lines.push('┌─────────────────────────────────────────────────────────────┐');
|
||||||
lines.push('│ 【统计】 │');
|
lines.push('│ 【统计】 │');
|
||||||
lines.push('└─────────────────────────────────────────────────────────────┘');
|
lines.push('└─────────────────────────────────────────────────────────────┘');
|
||||||
lines.push(` L1 片段: ${chunkResults.length} 条`);
|
lines.push(` L1 片段: ${chunkResults.length} 条`);
|
||||||
lines.push(` L2 事件: ${eventResults.length} 条 (DIRECT: ${directCount}, SIMILAR: ${similarCount})`);
|
lines.push(` L2 事件: ${eventResults.length} / ${allEvents.length} 条 (DIRECT: ${directCount}, SIMILAR: ${similarCount})`);
|
||||||
|
if (preFilterDist.total) {
|
||||||
|
lines.push(` L2 过滤前分布(${preFilterDist.total} 条,含bonus):`);
|
||||||
|
lines.push(` 0.85+: ${preFilterDist['0.85+'] || 0} | 0.7-0.85: ${preFilterDist['0.7-0.85'] || 0} | 0.6-0.7: ${preFilterDist['0.6-0.7'] || 0}`);
|
||||||
|
lines.push(` 0.5-0.6: ${preFilterDist['0.5-0.6'] || 0} | <0.5: ${preFilterDist['<0.5'] || 0}`);
|
||||||
|
lines.push(` 通过阈值(>=${preFilterDist.threshold || 0.6}): ${preFilterDist.passThreshold || 0} 条`);
|
||||||
|
}
|
||||||
lines.push(` 实体命中: ${queryEntities?.length || 0} 个`);
|
lines.push(` 实体命中: ${queryEntities?.length || 0} 个`);
|
||||||
|
if (causalEvents.length) lines.push(` 因果链追溯: ${causalEvents.length} 条`);
|
||||||
lines.push('');
|
lines.push('');
|
||||||
|
|
||||||
|
// 追加因果树详情
|
||||||
|
lines.push(formatCausalTree(causalEvents, eventResults));
|
||||||
|
|
||||||
return lines.join('\n');
|
return lines.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -492,15 +700,53 @@ export async function recallMemory(queryText, allEvents, vectorConfig, options =
|
|||||||
searchEvents(queryVector, allEvents, vectorConfig, store, queryEntities),
|
searchEvents(queryVector, allEvents, vectorConfig, store, queryEntities),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
const chunkPreFilterStats = chunkResults._preFilterStats || null;
|
||||||
|
|
||||||
|
// ─────────────────────────────────────────────────────────────────────
|
||||||
|
// 因果链追溯:从 eventResults 出发找祖先事件
|
||||||
|
// 注意:是否“额外注入”要去重(如果祖先事件本来已召回,就不额外注入)
|
||||||
|
// ─────────────────────────────────────────────────────────────────────
|
||||||
|
const eventIndex = buildEventIndex(allEvents);
|
||||||
|
const causalMap = traceCausalAncestors(eventResults, eventIndex);
|
||||||
|
|
||||||
|
const recalledIdSet = new Set(eventResults.map(x => x?.event?.id).filter(Boolean));
|
||||||
|
const causalEvents = Array.from(causalMap.values())
|
||||||
|
.filter(x => x?.event?.id && !recalledIdSet.has(x.event.id))
|
||||||
|
.map(x => ({
|
||||||
|
event: x.event,
|
||||||
|
similarity: 0,
|
||||||
|
_recallType: 'CAUSAL',
|
||||||
|
_recallReason: `因果链(${x.chainFrom.join(',')})`,
|
||||||
|
_causalDepth: x.depth,
|
||||||
|
_chainFrom: x.chainFrom,
|
||||||
|
chainFrom: x.chainFrom,
|
||||||
|
depth: x.depth,
|
||||||
|
}));
|
||||||
|
|
||||||
|
// 排序:引用数 > 深度 > 编号,然后截断
|
||||||
|
sortCausalEvents(causalEvents);
|
||||||
|
const causalEventsTruncated = causalEvents.slice(0, CONFIG.CAUSAL_INJECT_MAX);
|
||||||
|
|
||||||
const elapsed = Math.round(performance.now() - T0);
|
const elapsed = Math.round(performance.now() - T0);
|
||||||
const logText = formatRecallLog({ elapsed, queryText, segments, weights, chunkResults, eventResults, allEvents, queryEntities });
|
const logText = formatRecallLog({
|
||||||
|
elapsed,
|
||||||
|
queryText,
|
||||||
|
segments,
|
||||||
|
weights,
|
||||||
|
chunkResults,
|
||||||
|
eventResults,
|
||||||
|
allEvents,
|
||||||
|
queryEntities,
|
||||||
|
causalEvents: causalEventsTruncated,
|
||||||
|
chunkPreFilterStats,
|
||||||
|
});
|
||||||
|
|
||||||
console.group('%c[Recall]', 'color: #7c3aed; font-weight: bold');
|
console.group('%c[Recall]', 'color: #7c3aed; font-weight: bold');
|
||||||
console.log(`Elapsed: ${elapsed}ms | Entities: ${queryEntities.join(', ') || '(none)'}`);
|
console.log(`Elapsed: ${elapsed}ms | Entities: ${queryEntities.join(', ') || '(none)'}`);
|
||||||
console.log(`L1: ${chunkResults.length} | L2: ${eventResults.length}/${allEvents.length}`);
|
console.log(`L1: ${chunkResults.length} | L2: ${eventResults.length}/${allEvents.length} | Causal: ${causalEventsTruncated.length}`);
|
||||||
console.groupEnd();
|
console.groupEnd();
|
||||||
|
|
||||||
return { events: eventResults, chunks: chunkResults, elapsed, logText };
|
return { events: eventResults, causalEvents: causalEventsTruncated, chunks: chunkResults, elapsed, logText, queryEntities };
|
||||||
}
|
}
|
||||||
|
|
||||||
export function buildQueryText(chat, count = 2, excludeLastAi = false) {
|
export function buildQueryText(chat, count = 2, excludeLastAi = false) {
|
||||||
|
|||||||
Reference in New Issue
Block a user