feat: 新增多用户支持、关系历史查询与恋爱决策建议功能

- 新增用户服务,支持多用户数据隔离与认证
- 新增关系历史查询接口,支持按冲突、积极、时间线等类型过滤
- 新增恋爱决策建议接口,基于图谱分析生成关系健康报告
- 优化前端图谱可视化,增加节点详情面板、图例和边标签显示
- 改进文本分析逻辑,支持实体去重和情感标注
- 新增完整流程测试脚本,验证分析、入库、查询全链路
This commit is contained in:
KOSHM-Pig
2026-03-23 22:09:40 +08:00
parent ec21df7aa6
commit adabd63769
9 changed files with 1881 additions and 570 deletions

View File

@@ -21,12 +21,20 @@ export const createGraphRagController = (service) => ({
health: async (_request, reply) => reply.send({ ok: true }),
ready: async (_request, reply) => sendServiceResult(reply, () => service.ready()),
bootstrap: async (_request, reply) => sendServiceResult(reply, () => service.bootstrap()),
getGraphStats: async (_request, reply) => sendServiceResult(reply, () => service.getGraphStats()),
getGraphStats: async (request, reply) => sendServiceResult(reply, () => service.getGraphStats(request.query.userId || 'default')),
ingest: async (request, reply) => sendServiceResult(reply, () => service.ingest(request.body)),
queryTimeline: async (request, reply) =>
sendServiceResult(reply, () => service.queryTimeline(request.body)),
queryGraphRag: async (request, reply) =>
sendServiceResult(reply, () => service.queryGraphRag(request.body)),
analyzeAndIngest: async (request, reply) =>
sendServiceResult(reply, () => service.analyzeAndIngest(request.body.text))
sendServiceResult(reply, () => service.incrementalUpdate(request.body.text, request.body.userId || 'default')),
queryHistory: async (request, reply) =>
sendServiceResult(reply, () => service.queryRelationshipHistory(
request.body.userId || 'default',
request.body.queryType || 'all',
request.body.limit || 20
)),
getAdvice: async (request, reply) =>
sendServiceResult(reply, () => service.getRelationshipAdvice(request.body.userId || 'default'))
});

View File

@@ -245,4 +245,53 @@ export const registerGraphRagRoutes = async (app, controller) => {
*/
app.post("/query/graphrag", controller.queryGraphRag);
app.post("/analyze", controller.analyzeAndIngest);
/**
* @openapi
* /query/history:
* post:
* tags:
* - GraphRAG
* summary: 查询恋爱关系历史(支持按类型过滤)
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* userId:
* type: string
* queryType:
* type: string
* enum: [all, conflicts, positive, third_party, timeline]
* limit:
* type: integer
* responses:
* 200:
* description: 查询成功
*/
app.post("/query/history", controller.queryHistory);
/**
* @openapi
* /query/advice:
* post:
* tags:
* - GraphRAG
* summary: 获取恋爱决策建议(基于图谱分析)
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* userId:
* type: string
* responses:
* 200:
* description: 建议生成成功
*/
app.post("/query/advice", controller.getAdvice);
};

File diff suppressed because it is too large Load Diff

View File

@@ -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: '每段关系都有起伏,重要的是共同努力'
};
}
}
}

View File

@@ -0,0 +1,171 @@
import { randomUUID } from 'crypto';
/**
* 用户服务:管理用户 UUID 和认证
*/
export class UserService {
constructor(driver) {
this.driver = driver;
}
/**
* 创建或获取用户
*/
async getOrCreateUser(token) {
const session = this.driver.session();
try {
// 尝试通过 token 查找用户
const result = await session.run(
`MATCH (u:User {token: $token}) RETURN u`,
{ token }
);
if (result.records.length > 0) {
const user = result.records[0].get('u');
return {
id: user.properties.id,
token: user.properties.token,
createdAt: user.properties.created_at
};
}
// 创建新用户
const userId = randomUUID();
const createdAt = new Date().toISOString();
await session.run(
`CREATE (u:User {id: $id, token: $token, created_at: $created_at})`,
{ id: userId, token, created_at: createdAt }
);
return { id: userId, token, createdAt };
} finally {
await session.close();
}
}
/**
* 验证用户 token
*/
async validateUser(token) {
if (!token) return null;
const session = this.driver.session();
try {
const result = await session.run(
`MATCH (u:User {token: $token}) RETURN u`,
{ token }
);
if (result.records.length > 0) {
const user = result.records[0].get('u');
return {
id: user.properties.id,
token: user.properties.token
};
}
return null;
} finally {
await session.close();
}
}
/**
* 获取用户图谱统计
*/
async getUserGraphStats(userId) {
const session = this.driver.session();
try {
const result = await session.run(
`
MATCH (p:Person {user_id: $userId})
RETURN p.id AS id, p.name AS name, 'person' AS type, null AS occurred_at
LIMIT 200
`,
{ userId }
);
const persons = result.records.map(r => ({
id: r.get('id'),
name: r.get('name'),
type: r.get('type'),
occurred_at: r.get('occurred_at')
}));
const events = await session.run(
`
MATCH (e:Event {user_id: $userId})
RETURN e.id AS id, e.summary AS name, 'event' AS type, e.occurred_at AS occurred_at
LIMIT 200
`,
{ userId }
);
const topics = await session.run(
`
MATCH (t:Topic {user_id: $userId})
RETURN t.name AS id, t.name AS name, 'topic' AS type, null AS occurred_at
LIMIT 100
`,
{ userId }
);
const nodes = [
...persons,
...events.records.map(r => ({
id: r.get('id'),
name: r.get('name'),
type: r.get('type'),
occurred_at: r.get('occurred_at')
})),
...topics.records.map(r => ({
id: r.get('id'),
name: r.get('name'),
type: r.get('type'),
occurred_at: r.get('occurred_at')
}))
];
// 查询关系
const personEventRels = await session.run(
`
MATCH (p:Person {user_id: $userId})-[:PARTICIPATES_IN]->(e:Event {user_id: $userId})
RETURN p.id AS source, e.id AS target, 'PARTICIPATES_IN' AS type
LIMIT 500
`,
{ userId }
);
const eventTopicRels = await session.run(
`
MATCH (e:Event {user_id: $userId})-[:ABOUT]->(t:Topic {user_id: $userId})
RETURN e.id AS source, t.name AS target, 'ABOUT' AS type
LIMIT 300
`,
{ userId }
);
const links = [
...personEventRels.records.map(r => ({
source: r.get('source'),
target: r.get('target'),
type: r.get('type')
})),
...eventTopicRels.records.map(r => ({
source: r.get('source'),
target: r.get('target'),
type: r.get('type')
}))
];
return {
ok: true,
nodes,
links,
total: nodes.length
};
} finally {
await session.close();
}
}
}