File size: 4,117 Bytes
286c429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# AgentGraph Sample Data System

这是重构后的 sample data 系统,使用 JSON 文件而不是硬编码的 Python 数据,更容易维护和扩展。

## 📁 文件结构

```
samples/
├── README.md                          # 本文档
├── samples_config.json                # 样本配置文件
├── traces/                           # Trace数据目录
│   └── python_documentation_inquiry.json
└── knowledge_graphs/                  # Knowledge Graph数据目录
    └── kg_python_documentation_enhanced.json
```

## 🔧 配置系统

### `samples_config.json`

主配置文件,定义所有可用的样本:

```json
{
  "samples": [
    {
      "id": "python_documentation_demo",
      "name": "Python Documentation Assistant Demo",
      "description": "...",
      "trace_file": "traces/python_documentation_inquiry.json",
      "knowledge_graph_file": "knowledge_graphs/kg_python_documentation_enhanced.json",
      "tags": ["programming", "rag_assistant", "documentation"],
      "complexity": "enhanced",
      "trace_type": "documentation_search",
      "trace_source": "sample_data",
      "features": [
        "rag_search",
        "failure_detection",
        "optimization_recommendations"
      ]
    }
  ],
  "metadata": {
    "version": "1.0.0",
    "created": "2025-01-27",
    "description": "..."
  }
}
```

## 📄 数据文件格式

### Trace 文件

- 位置:`traces/`目录
- 格式:标准 JSON,包含 filename, title, description, content 等字段
- Content 字段包含完整的 trace 数据(observations, metadata 等)

### Knowledge Graph 文件

- 位置:`knowledge_graphs/`目录
- 格式:标准 JSON,包含 filename, trace_index, graph_data 等字段
- Graph_data 包含 entities, relations, failures, optimizations, metadata

## 🔄 向后兼容性

新的`sample_data.py`保持了与旧 API 的完全兼容性:

```python
# 这些调用仍然正常工作
from backend.database.sample_data import SAMPLE_TRACES, SAMPLE_KNOWLEDGE_GRAPHS
from backend.database.sample_data import insert_sample_data, get_sample_data_info
```

## ✨ 新增功能

### 动态加载

- 支持运行时添加新样本(修改 JSON 文件即可)
- 自动验证 JSON 格式
- 更好的错误处理和日志

### 配置管理

```python
from backend.database.sample_data import list_available_samples, get_sample_data_info

# 列出所有可用样本
samples = list_available_samples()

# 获取详细信息
info = get_sample_data_info()
```

## 🚀 添加新样本

### 1. 准备数据文件

创建 trace 和 knowledge graph 的 JSON 文件,放在相应目录下。

### 2. 更新配置`samples_config.json`中添加新条目:

```json
{
  "id": "new_sample_id",
  "name": "New Sample Name",
  "description": "Description of the sample",
  "trace_file": "traces/new_trace.json",
  "knowledge_graph_file": "knowledge_graphs/new_kg.json",
  "tags": ["tag1", "tag2"],
  "complexity": "standard",
  "trace_type": "custom",
  "trace_source": "sample_data",
  "features": ["feature1", "feature2"]
}
```

### 3. 自动加载

系统会自动检测并加载新样本,无需重启。

## 🎯 优势

1. **易于维护**:数据与代码分离,修改样本不需要改 Python 代码
2. **版本控制友好**:JSON diff 更清晰,方便 code review
3. **扩展性强**:添加新样本只需添加 JSON 文件
4. **类型安全**:JSON schema 验证(可扩展)
5. **向后兼容**:现有代码无需修改

## 🛠️ 开发工具

### 测试新系统

```bash
cd backend/database
python sample_data_new.py
```

### 验证 JSON 格式

```bash
python -m json.tool samples/traces/new_trace.json
python -m json.tool samples/knowledge_graphs/new_kg.json
```

## 📊 从 algorithm-generated.jsonl 迁移

当我们准备好从 algorithm-generated.jsonl 中选择样本时:

1. 运行`multi_agent_knowledge_extractor.py`生成 KG
2. 将 trace 和 KG 分别保存为 JSON 文件
3.`samples_config.json`中添加配置条目
4. 自动集成到系统中

这个结构使得批量添加真实样本变得非常简单!