Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions backend/app/api/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def get_project(project_id: str):
if not project:
return jsonify({
"success": False,
"error": f"项目不存在: {project_id}"
"error": f"Project not found: {project_id}"
}), 404

return jsonify({
"success": True,
"data": project.to_dict()
Expand Down Expand Up @@ -76,12 +76,12 @@ def delete_project(project_id: str):
if not success:
return jsonify({
"success": False,
"error": f"项目不存在或删除失败: {project_id}"
"error": f"Project not found or deletion failed: {project_id}"
}), 404

return jsonify({
"success": True,
"message": f"项目已删除: {project_id}"
"message": f"Project deleted: {project_id}"
})


Expand All @@ -95,9 +95,9 @@ def reset_project(project_id: str):
if not project:
return jsonify({
"success": False,
"error": f"项目不存在: {project_id}"
"error": f"Project not found: {project_id}"
}), 404

# 重置到本体已生成状态
if project.ontology:
project.status = ProjectStatus.ONTOLOGY_GENERATED
Expand All @@ -111,7 +111,7 @@ def reset_project(project_id: str):

return jsonify({
"success": True,
"message": f"项目已重置: {project_id}",
"message": f"Project reset: {project_id}",
"data": project.to_dict()
})

Expand Down Expand Up @@ -160,15 +160,15 @@ def generate_ontology():
if not simulation_requirement:
return jsonify({
"success": False,
"error": "请提供模拟需求描述 (simulation_requirement)"
"error": "Please provide simulation requirement description (simulation_requirement)"
}), 400

# 获取上传的文件
uploaded_files = request.files.getlist('files')
if not uploaded_files or all(not f.filename for f in uploaded_files):
return jsonify({
"success": False,
"error": "请至少上传一个文档文件"
"error": "Please upload at least one document file"
}), 400

# 创建项目
Expand Down Expand Up @@ -203,7 +203,7 @@ def generate_ontology():
ProjectManager.delete_project(project.project_id)
return jsonify({
"success": False,
"error": "没有成功处理任何文档,请检查文件格式"
"error": "No documents were successfully processed, please check the file format"
}), 400

# 保存提取的文本
Expand Down Expand Up @@ -285,12 +285,12 @@ def build_graph():
# 检查配置
errors = []
if not Config.ZEP_API_KEY:
errors.append("ZEP_API_KEY未配置")
errors.append("ZEP_API_KEY not configured")
if errors:
logger.error(f"配置错误: {errors}")
return jsonify({
"success": False,
"error": "配置错误: " + "; ".join(errors)
"error": "Configuration error: " + "; ".join(errors)
}), 500

# 解析请求
Expand All @@ -301,15 +301,15 @@ def build_graph():
if not project_id:
return jsonify({
"success": False,
"error": "请提供 project_id"
"error": "Please provide project_id"
}), 400

# 获取项目
project = ProjectManager.get_project(project_id)
if not project:
return jsonify({
"success": False,
"error": f"项目不存在: {project_id}"
"error": f"Project not found: {project_id}"
}), 404

# 检查项目状态
Expand All @@ -318,13 +318,13 @@ def build_graph():
if project.status == ProjectStatus.CREATED:
return jsonify({
"success": False,
"error": "项目尚未生成本体,请先调用 /ontology/generate"
"error": "Project has no ontology yet, please call /ontology/generate first"
}), 400

if project.status == ProjectStatus.GRAPH_BUILDING and not force:
return jsonify({
"success": False,
"error": "图谱正在构建中,请勿重复提交。如需强制重建,请添加 force: true",
"error": "Graph is already being built, do not resubmit. To force rebuild, add force: true",
"task_id": project.graph_build_task_id
}), 400

Expand All @@ -349,15 +349,15 @@ def build_graph():
if not text:
return jsonify({
"success": False,
"error": "未找到提取的文本内容"
"error": "No extracted text content found"
}), 400

# 获取本体
ontology = project.ontology
if not ontology:
return jsonify({
"success": False,
"error": "未找到本体定义"
"error": "No ontology definition found"
}), 400

# 创建异步任务
Expand All @@ -378,7 +378,7 @@ def build_task():
task_manager.update_task(
task_id,
status=TaskStatus.PROCESSING,
message="初始化图谱构建服务..."
message="Initializing graph build service..."
)

# 创建图谱构建服务
Expand All @@ -387,7 +387,7 @@ def build_task():
# 分块
task_manager.update_task(
task_id,
message="文本分块中...",
message="Chunking text...",
progress=5
)
chunks = TextProcessor.split_text(
Expand All @@ -400,7 +400,7 @@ def build_task():
# 创建图谱
task_manager.update_task(
task_id,
message="创建Zep图谱...",
message="Creating Zep graph...",
progress=10
)
graph_id = builder.create_graph(name=graph_name)
Expand All @@ -412,7 +412,7 @@ def build_task():
# 设置本体
task_manager.update_task(
task_id,
message="设置本体定义...",
message="Setting ontology definition...",
progress=15
)
builder.set_ontology(graph_id, ontology)
Expand All @@ -428,7 +428,7 @@ def add_progress_callback(msg, progress_ratio):

task_manager.update_task(
task_id,
message=f"开始添加 {total_chunks} 个文本块...",
message=f"Starting to add {total_chunks} text chunks...",
progress=15
)

Expand All @@ -442,7 +442,7 @@ def add_progress_callback(msg, progress_ratio):
# 等待Zep处理完成(查询每个episode的processed状态)
task_manager.update_task(
task_id,
message="等待Zep处理数据...",
message="Waiting for Zep to process data...",
progress=55
)

Expand All @@ -459,7 +459,7 @@ def wait_progress_callback(msg, progress_ratio):
# 获取图谱数据
task_manager.update_task(
task_id,
message="获取图谱数据...",
message="Retrieving graph data...",
progress=95
)
graph_data = builder.get_graph_data(graph_id)
Expand All @@ -476,7 +476,7 @@ def wait_progress_callback(msg, progress_ratio):
task_manager.update_task(
task_id,
status=TaskStatus.COMPLETED,
message="图谱构建完成",
message="Graph build complete",
progress=100,
result={
"project_id": project_id,
Expand All @@ -499,7 +499,7 @@ def wait_progress_callback(msg, progress_ratio):
task_manager.update_task(
task_id,
status=TaskStatus.FAILED,
message=f"构建失败: {str(e)}",
message=f"Build failed: {str(e)}",
error=traceback.format_exc()
)

Expand All @@ -512,7 +512,7 @@ def wait_progress_callback(msg, progress_ratio):
"data": {
"project_id": project_id,
"task_id": task_id,
"message": "图谱构建任务已启动,请通过 /task/{task_id} 查询进度"
"message": "Graph build task started, check progress via /task/{task_id}"
}
})

Expand All @@ -536,7 +536,7 @@ def get_task(task_id: str):
if not task:
return jsonify({
"success": False,
"error": f"任务不存在: {task_id}"
"error": f"Task not found: {task_id}"
}), 404

return jsonify({
Expand Down Expand Up @@ -570,7 +570,7 @@ def get_graph_data(graph_id: str):
if not Config.ZEP_API_KEY:
return jsonify({
"success": False,
"error": "ZEP_API_KEY未配置"
"error": "ZEP_API_KEY not configured"
}), 500

builder = GraphBuilderService(api_key=Config.ZEP_API_KEY)
Expand Down Expand Up @@ -598,15 +598,15 @@ def delete_graph(graph_id: str):
if not Config.ZEP_API_KEY:
return jsonify({
"success": False,
"error": "ZEP_API_KEY未配置"
"error": "ZEP_API_KEY not configured"
}), 500

builder = GraphBuilderService(api_key=Config.ZEP_API_KEY)
builder.delete_graph(graph_id)

return jsonify({
"success": True,
"message": f"图谱已删除: {graph_id}"
"message": f"Graph deleted: {graph_id}"
})

except Exception as e:
Expand Down
Loading