从 answer 事件的 sample["graph"] 构建完整的图
(graph_data)
| 872 | |
| 873 | |
| 874 | def build_graph_from_sample(graph_data): |
| 875 | """从 answer 事件的 sample["graph"] 构建完整的图""" |
| 876 | nodes = [] |
| 877 | edges = [] |
| 878 | |
| 879 | for node in graph_data: |
| 880 | node_id = node.get('id', '') |
| 881 | parent_ids = node.get('parent_ids', []) |
| 882 | |
| 883 | if node_id == 'root': |
| 884 | nodes.append({ |
| 885 | "id": "root", |
| 886 | "label": "🌐 Query", |
| 887 | "title": node.get('content', '用户问题'), |
| 888 | "color": {"background": "#6366f1", "border": "#4f46e5"}, |
| 889 | "shape": "circle", |
| 890 | "size": 38, |
| 891 | "font": {"color": "#333", "size": 14} |
| 892 | }) |
| 893 | elif node_id == 'answer': |
| 894 | nodes.append({ |
| 895 | "id": "answer", |
| 896 | "label": "✨ Answer", |
| 897 | "title": "最终答案", |
| 898 | "color": {"background": "#f97316", "border": "#ea580c"}, |
| 899 | "shape": "circle", |
| 900 | "size": 38, |
| 901 | "font": {"color": "#333", "size": 14} |
| 902 | }) |
| 903 | for pid in parent_ids: |
| 904 | edges.append({"from": pid, "to": "answer"}) |
| 905 | else: |
| 906 | # 搜索节点 |
| 907 | label = node_id[:12] + "..." if len(node_id) > 12 else node_id |
| 908 | summary = node.get('summary', node.get('query', '')) |
| 909 | nodes.append({ |
| 910 | "id": node_id, |
| 911 | "label": f"🔍 {label}", |
| 912 | "title": summary[:200] + "..." if len(summary) > 200 else summary, |
| 913 | "color": {"background": "#06b6d4", "border": "#0891b2"}, |
| 914 | "shape": "box", |
| 915 | "borderRadius": 8, |
| 916 | "size": 25, |
| 917 | "font": {"color": "#333", "size": 12} |
| 918 | }) |
| 919 | for pid in parent_ids: |
| 920 | edges.append({"from": pid, "to": node_id}) |
| 921 | |
| 922 | return nodes, edges |
| 923 | |
| 924 | |
| 925 | # ==================== 主函数 ==================== |