feat: 新增多用户支持、关系历史查询与恋爱决策建议功能
- 新增用户服务,支持多用户数据隔离与认证 - 新增关系历史查询接口,支持按冲突、积极、时间线等类型过滤 - 新增恋爱决策建议接口,基于图谱分析生成关系健康报告 - 优化前端图谱可视化,增加节点详情面板、图例和边标签显示 - 改进文本分析逻辑,支持实体去重和情感标注 - 新增完整流程测试脚本,验证分析、入库、查询全链路
This commit is contained in:
@@ -4,6 +4,52 @@ const createHttpError = (statusCode, message) => {
|
||||
return error;
|
||||
};
|
||||
|
||||
/**
|
||||
* 文本预处理
|
||||
*/
|
||||
const preprocessText = (text) => {
|
||||
return text
|
||||
.replace(/\r\n/g, '\n')
|
||||
.replace(/\r/g, '\n')
|
||||
.replace(/\n{3,}/g, '\n\n')
|
||||
.split('\n')
|
||||
.map(line => line.trim())
|
||||
.filter(line => line.length > 0)
|
||||
.join('\n')
|
||||
.trim();
|
||||
};
|
||||
|
||||
/**
|
||||
* 将文本分块(适合长文本)
|
||||
*/
|
||||
const splitTextIntoChunks = (text, chunkSize = 500, overlap = 50) => {
|
||||
const chunks = [];
|
||||
let start = 0;
|
||||
|
||||
while (start < text.length) {
|
||||
const end = Math.min(start + chunkSize, text.length);
|
||||
let chunkEnd = end;
|
||||
|
||||
// 尝试在句子边界处切断
|
||||
if (end < text.length) {
|
||||
const lastPeriod = text.lastIndexOf('.', end);
|
||||
const lastQuestion = text.lastIndexOf('?', end);
|
||||
const lastExclamation = text.lastIndexOf('!', end);
|
||||
const lastNewline = text.lastIndexOf('\n', end);
|
||||
|
||||
const breakPoint = Math.max(lastPeriod, lastQuestion, lastExclamation, lastNewline);
|
||||
if (breakPoint > start + chunkSize / 2) {
|
||||
chunkEnd = breakPoint + 1;
|
||||
}
|
||||
}
|
||||
|
||||
chunks.push(text.slice(start, chunkEnd).trim());
|
||||
start = chunkEnd - overlap;
|
||||
}
|
||||
|
||||
return chunks;
|
||||
};
|
||||
|
||||
export class LLMService {
|
||||
constructor(env) {
|
||||
this.baseUrl = (env.LLM_BASE_URL ?? "").replace(/\/+$/, "");
|
||||
@@ -29,7 +75,8 @@ export class LLMService {
|
||||
body: JSON.stringify({
|
||||
model: this.model,
|
||||
messages: messages,
|
||||
temperature: temperature
|
||||
temperature: temperature,
|
||||
max_tokens: 4096
|
||||
})
|
||||
});
|
||||
|
||||
@@ -42,34 +89,100 @@ export class LLMService {
|
||||
return data;
|
||||
}
|
||||
|
||||
async analyzeText(text) {
|
||||
/**
|
||||
* 分析文本并提取详细的实体和关系(MiroFish 风格)
|
||||
* @param {string} text - 用户输入的文本
|
||||
* @param {object} existingEntities - 现有实体列表(用于识别是否已存在)
|
||||
*/
|
||||
async analyzeText(text, existingEntities = {}) {
|
||||
if (!text?.trim()) {
|
||||
throw createHttpError(400, "分析文本不能为空");
|
||||
}
|
||||
|
||||
const systemPrompt = `你是一个实体关系分析专家。请分析用户输入的文本,提取人物、事件、主题、关系。
|
||||
const existingContext = existingEntities.persons?.length > 0 || existingEntities.organizations?.length > 0
|
||||
? `
|
||||
|
||||
## 输出格式
|
||||
## 已有实体列表(极其重要!)
|
||||
**如果文本中提到的人/组织已存在于下方列表中,必须复用相同的 ID,不要创建新实体!**
|
||||
|
||||
已有的人物:
|
||||
${(existingEntities.persons || []).map(p => `- ID: "${p.id}", 名字:"${p.name}", 描述:${p.summary}`).join('\n')}
|
||||
|
||||
已有的组织:
|
||||
${(existingEntities.organizations || []).map(o => `- ID: "${o.id}", 名字:"${o.name}", 描述:${o.summary}`).join('\n')}
|
||||
|
||||
**代词解析指南**:
|
||||
- "我女朋友" = 已有实体中的"女朋友"(如果有)
|
||||
- "她" = 根据上下文推断指代哪个女性角色
|
||||
- "他" = 根据上下文推断指代哪个男性角色
|
||||
- "丽丽" = 如果已有实体中有"丽丽",复用 ID`
|
||||
: '';
|
||||
|
||||
const systemPrompt = `你是一个恋爱关系知识图谱构建专家。从用户输入的文本中提取实体和关系,用于后续的恋爱决策建议。
|
||||
|
||||
## 核心原则
|
||||
1. **重点关注**:用户本人("我")和恋爱对象(女朋友/男朋友/心仪对象)需要详细记录
|
||||
2. **其他人物**:朋友、闺蜜、同事等只需要记录基本信息(名字、与用户的关系)
|
||||
3. **事件细节**:记录争吵、约会、礼物、重要对话等影响关系的事件
|
||||
4. **情感线索**:提取情绪变化、态度、期望等软性信息
|
||||
|
||||
## 输出格式(严格 JSON)
|
||||
{
|
||||
"persons": [{"id": "p1", "name": "人物名称", "description": "人物描述"}],
|
||||
"events": [{"id": "e1", "type": "事件类型", "summary": "事件摘要", "occurred_at": "ISO 时间", "participants": ["p1"], "topics": ["t1"], "importance": 5}],
|
||||
"topics": [{"id": "t1", "name": "主题名称"}],
|
||||
"relations": [{"source": "p1", "target": "p2", "type": "关系类型", "description": "关系描述"}]
|
||||
"persons": [
|
||||
{
|
||||
"id": "p1",
|
||||
"name": "人物名称",
|
||||
"summary": "人物描述",
|
||||
"role": "用户 | 恋爱对象 | 朋友 | 家人 | 同事 | 其他"
|
||||
}
|
||||
],
|
||||
"events": [
|
||||
{
|
||||
"id": "e1",
|
||||
"type": "事件类型",
|
||||
"summary": "事件描述(包含情感细节)",
|
||||
"occurred_at": "ISO 时间",
|
||||
"participants": ["p1"],
|
||||
"emotional_tone": "positive | neutral | negative",
|
||||
"importance": 1-10
|
||||
}
|
||||
],
|
||||
"topics": [
|
||||
{
|
||||
"id": "t1",
|
||||
"name": "主题名称"
|
||||
}
|
||||
],
|
||||
"relations": [
|
||||
{
|
||||
"source": "p1",
|
||||
"target": "p2",
|
||||
"type": "关系类型",
|
||||
"summary": "关系描述"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
## 注意
|
||||
- 时间用 ISO 格式,如文本没明确时间用当前时间
|
||||
- importance 是重要性评分 1-10
|
||||
- 关系类型:PARTICIPATES_IN, ABOUT, LOVES, FIGHTS_WITH, GIVES, PROPOSES_TO 等
|
||||
- 如果文本涉及"我",推断另一个角色(如"她")
|
||||
- 即使文本很短也要提取信息,不要返回空数组
|
||||
## 关系类型参考
|
||||
- 用户与恋爱对象:LOVES, DATING, MARRIED_TO, ENGAGED_TO, BROKEN_UP_WITH, CONFLICT_WITH
|
||||
- 用户与他人:FRIENDS_WITH, COLLEAGUE_OF, CLASSMATE_OF, SIBLING_OF
|
||||
- 恋爱对象与他人:FRIENDS_WITH, COLLEAGUE_OF, FAMILY_OF, CONFLICT_WITH
|
||||
|
||||
## 重要规则
|
||||
1. **用户识别**:"我"=用户本人,固定 ID 为"user"
|
||||
2. **恋爱对象**:"女朋友/男朋友/她/他"=恋爱对象,固定 ID 为"partner"
|
||||
3. **其他人物**:不需要详细描述,只记录名字和与核心人物的关系
|
||||
4. **实体去重**:如果文本中提到的人已存在于"已有实体"列表中,**复用相同的 ID**
|
||||
5. **时间标准化**:occurred_at 使用 ISO 格式
|
||||
6. **情感标注**:emotional_tone 标注事件的情感倾向(positive/neutral/negative)
|
||||
|
||||
只返回 JSON,不要有其他文字。`;
|
||||
|
||||
const messages = [
|
||||
{ role: "system", content: systemPrompt },
|
||||
{ role: "user", content: text }
|
||||
{ role: "user", content: `${existingContext}\n\n## 待分析文本\n${text}` }
|
||||
];
|
||||
|
||||
console.log("[DEBUG] LLM request messages:", JSON.stringify(messages));
|
||||
const result = await this.chat(messages, 0.3);
|
||||
|
||||
@@ -87,33 +200,6 @@ export class LLMService {
|
||||
throw createHttpError(500, `LLM 返回格式错误:${content.substring(0, 200)}`);
|
||||
}
|
||||
|
||||
if (this.isEmptyAnalysis(parsed)) {
|
||||
const retryMessages = [
|
||||
{
|
||||
role: "system",
|
||||
content: "你是信息抽取器。必须输出非空 JSON:{persons:[{id,name,description}],events:[{id,type,summary,occurred_at,participants,topics,importance}],topics:[{id,name}],relations:[{source,target,type,description}]}"
|
||||
},
|
||||
{
|
||||
role: "user",
|
||||
content: `从下列文本提取实体关系,至少给出 2 个 persons、1 个 event、1 个 relation,且仅返回 JSON:${text}`
|
||||
}
|
||||
];
|
||||
const retryResult = await this.chat(retryMessages, 0.2);
|
||||
const retryContent = retryResult?.choices?.[0]?.message?.content;
|
||||
if (!retryContent) {
|
||||
return parsed;
|
||||
}
|
||||
try {
|
||||
const retryJsonMatch = retryContent.match(/\{[\s\S]*\}/);
|
||||
const retryParsed = retryJsonMatch ? JSON.parse(retryJsonMatch[0]) : JSON.parse(retryContent);
|
||||
if (!this.isEmptyAnalysis(retryParsed)) {
|
||||
return retryParsed;
|
||||
}
|
||||
} catch (_) {
|
||||
return parsed;
|
||||
}
|
||||
}
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
@@ -124,4 +210,67 @@ export class LLMService {
|
||||
&& (!Array.isArray(data.topics) || data.topics.length === 0)
|
||||
&& (!Array.isArray(data.relations) || data.relations.length === 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 基于图谱数据生成恋爱决策建议
|
||||
*/
|
||||
async generateRelationshipAdvice(data) {
|
||||
const systemPrompt = `你是一个恋爱关系咨询师,擅长分析情侣关系模式并给出专业建议。
|
||||
|
||||
请根据以下数据生成恋爱建议:
|
||||
1. 事件统计:各类事件的数量和情感倾向
|
||||
2. 最近事件:最近发生的 10 个事件
|
||||
3. 第三方影响:朋友/家人对关系的参与程度
|
||||
|
||||
## 输出格式(JSON)
|
||||
{
|
||||
"relationship_health": "healthy | neutral | concerning",
|
||||
"summary": "关系状态总结(100 字以内)",
|
||||
"patterns": [
|
||||
{
|
||||
"pattern": "识别出的模式(如'频繁争吵'、'缺乏沟通')",
|
||||
"evidence": "支持该模式的事件或数据",
|
||||
"suggestion": "针对性建议"
|
||||
}
|
||||
],
|
||||
"third_party_influence": "第三方影响分析",
|
||||
"action_items": [
|
||||
"具体可执行的建议 1",
|
||||
"具体可执行的建议 2",
|
||||
"具体可执行的建议 3"
|
||||
],
|
||||
"positive_notes": "关系中积极的方面(鼓励)"
|
||||
}`;
|
||||
|
||||
const userMessage = `
|
||||
## 事件统计
|
||||
${JSON.stringify(data.eventStats, null, 2)}
|
||||
|
||||
## 最近事件
|
||||
${JSON.stringify(data.recentEvents, null, 2)}
|
||||
|
||||
## 第三方参与
|
||||
${JSON.stringify(data.thirdParty, null, 2)}
|
||||
|
||||
请分析这段恋爱关系的健康状况,并给出专业建议。`;
|
||||
|
||||
const result = await this.chat([
|
||||
{ role: 'system', content: systemPrompt },
|
||||
{ role: 'user', content: userMessage }
|
||||
], 0.5);
|
||||
|
||||
const content = result?.choices?.[0]?.message?.content;
|
||||
try {
|
||||
const jsonMatch = content.match(/\{[\s\S]*\}/);
|
||||
return jsonMatch ? JSON.parse(jsonMatch[0]) : JSON.parse(content);
|
||||
} catch (e) {
|
||||
return {
|
||||
relationship_health: 'neutral',
|
||||
summary: content || '无法生成详细分析',
|
||||
patterns: [],
|
||||
action_items: ['建议与伴侣坦诚沟通', '关注彼此的情感需求'],
|
||||
positive_notes: '每段关系都有起伏,重要的是共同努力'
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user