dtflow 0.5.6__py3-none-any.whl → 0.5.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dtflow/mcp/docs.py DELETED
@@ -1,416 +0,0 @@
1
- """Datatron MCP 文档内容
2
-
3
- 提供 Datatron 的用法文档,供 MCP 服务使用。
4
- """
5
-
6
- # 可用的文档主题
7
- TOPICS = [
8
- "overview",
9
- "basic_usage",
10
- "presets",
11
- "cli",
12
- "storage",
13
- "chain_api",
14
- ]
15
-
16
- # 文档内容
17
- DOCS = {
18
- "overview": """# Datatron 概述
19
-
20
- Datatron 是一个专业的数据格式转换工具,专注于机器学习训练数据的格式转换。
21
-
22
- ## 核心特点
23
-
24
- 1. **简洁的 API**: 链式调用,代码简洁
25
- 2. **多格式支持**: JSONL、JSON、CSV、Parquet
26
- 3. **预设模板**: OpenAI Chat、Alpaca、ShareGPT、DPO 等
27
- 4. **属性访问**: lambda 中使用 `x.field` 代替 `x["field"]`
28
-
29
- ## 安装
30
-
31
- ```bash
32
- pip install dtflow
33
-
34
- # 完整安装(含存储、显示等)
35
- pip install dtflow[full]
36
- ```
37
-
38
- ## 快速开始
39
-
40
- ```python
41
- from dtflow import DataTransformer
42
-
43
- # 加载数据
44
- dt = DataTransformer.load("data.jsonl")
45
-
46
- # 查看数据
47
- print(dt.fields()) # 查看字段
48
- print(dt.stats()) # 统计信息
49
- print(len(dt)) # 数据量
50
-
51
- # 转换格式
52
- result = dt.to(lambda x: {
53
- "instruction": x.q,
54
- "output": x.a
55
- })
56
-
57
- # 链式调用
58
- dt.filter(lambda x: len(x.q) > 10) \\
59
- .sample(100) \\
60
- .transform(lambda x: {"q": x.q, "a": x.a}) \\
61
- .save("output.jsonl")
62
- ```
63
-
64
- ## CLI 使用
65
-
66
- ```bash
67
- # 使用预设转换
68
- dt transform data.jsonl --preset=openai_chat
69
-
70
- # 配置文件模式(首次生成配置,编辑后再次运行)
71
- dt transform data.jsonl
72
-
73
- # 采样数据
74
- dt sample data.jsonl --num=10
75
- ```
76
- """,
77
- "basic_usage": """# Datatron 基本用法
78
-
79
- ## 加载与保存
80
-
81
- ```python
82
- from dtflow import DataTransformer
83
-
84
- # 加载(支持 jsonl, json, csv, parquet)
85
- dt = DataTransformer.load("data.jsonl")
86
-
87
- # 保存(根据扩展名自动选择格式)
88
- dt.save("output.jsonl")
89
- ```
90
-
91
- ## 数据转换
92
-
93
- ### 使用 to() - 返回列表
94
- ```python
95
- # lambda 参数支持属性访问
96
- result = dt.to(lambda x: {
97
- "instruction": x.q,
98
- "output": x.a
99
- })
100
- ```
101
-
102
- ### 使用 transform() - 返回 DataTransformer(支持链式调用)
103
- ```python
104
- new_dt = dt.transform(lambda x: {
105
- "instruction": x.q,
106
- "output": x.a
107
- })
108
- new_dt.save("output.jsonl")
109
- ```
110
-
111
- ## 数据筛选
112
-
113
- ```python
114
- # 筛选
115
- dt.filter(lambda x: len(x.text) > 10)
116
-
117
- # 采样
118
- dt.sample(100) # 随机采样 100 条
119
- dt.sample(100, seed=42) # 指定随机种子
120
-
121
- # 取前/后 N 条
122
- dt.head(10)
123
- dt.tail(10)
124
- ```
125
-
126
- ## 数据信息
127
-
128
- ```python
129
- # 查看字段
130
- print(dt.fields())
131
- # ['a', 'meta.source', 'q']
132
-
133
- # 统计信息
134
- print(dt.stats())
135
- # {'total': 1000, 'fields': ['q', 'a'], 'field_stats': {...}}
136
- ```
137
-
138
- ## 工具方法
139
-
140
- ```python
141
- # 深拷贝
142
- new_dt = dt.copy()
143
-
144
- # 打乱顺序
145
- dt.shuffle()
146
- dt.shuffle(seed=42)
147
-
148
- # 分割数据集
149
- train, test = dt.split(ratio=0.8)
150
- ```
151
- """,
152
- "presets": """# 预设转换模板
153
-
154
- Datatron 提供常用的格式转换预设,可直接用于 CLI 或 Python API。
155
-
156
- ## 可用预设
157
-
158
- | 预设名称 | 输出格式 | 用途 |
159
- |---------|---------|------|
160
- | openai_chat | OpenAI Chat 格式 | 对话微调 |
161
- | alpaca | Alpaca 指令格式 | 指令微调 |
162
- | sharegpt | ShareGPT 多轮对话 | 对话训练 |
163
- | dpo_pair | DPO 偏好对格式 | RLHF 训练 |
164
- | simple_qa | 简单问答格式 | 问答任务 |
165
-
166
- ## CLI 使用
167
-
168
- ```bash
169
- # 使用预设转换
170
- dt transform data.jsonl --preset=openai_chat
171
- dt transform data.jsonl --preset=alpaca --output=alpaca.jsonl
172
- ```
173
-
174
- ## Python API 使用
175
-
176
- ```python
177
- from dtflow.presets import get_preset, list_presets
178
-
179
- # 列出所有预设
180
- print(list_presets())
181
- # ['openai_chat', 'alpaca', 'sharegpt', 'dpo_pair', 'simple_qa']
182
-
183
- # 获取预设转换函数
184
- transform_fn = get_preset("openai_chat",
185
- user_field="q",
186
- assistant_field="a")
187
-
188
- # 使用预设
189
- dt = DataTransformer.load("data.jsonl")
190
- result = dt.to(transform_fn)
191
- ```
192
-
193
- ## 预设输出格式详情
194
-
195
- ### openai_chat
196
- ```python
197
- {
198
- "messages": [
199
- {"role": "system", "content": "..."}, # 可选
200
- {"role": "user", "content": "..."},
201
- {"role": "assistant", "content": "..."}
202
- ]
203
- }
204
- ```
205
-
206
- ### alpaca
207
- ```python
208
- {
209
- "instruction": "...",
210
- "input": "...",
211
- "output": "..."
212
- }
213
- ```
214
-
215
- ### sharegpt
216
- ```python
217
- {
218
- "conversations": [
219
- {"from": "human", "value": "..."},
220
- {"from": "gpt", "value": "..."}
221
- ]
222
- }
223
- ```
224
-
225
- ### dpo_pair
226
- ```python
227
- {
228
- "prompt": "...",
229
- "chosen": "...",
230
- "rejected": "..."
231
- }
232
- ```
233
- """,
234
- "cli": """# CLI 命令行工具
235
-
236
- Datatron 提供 `dt` 命令行工具。
237
-
238
- ## transform 命令 - 数据转换
239
-
240
- ### 预设模式
241
- ```bash
242
- # 使用预设直接转换
243
- dt transform data.jsonl --preset=openai_chat
244
- dt transform data.jsonl --preset=alpaca --output=alpaca.jsonl
245
-
246
- # 只转换前 N 条
247
- dt transform data.jsonl --preset=openai_chat --num=100
248
- ```
249
-
250
- ### 配置文件模式
251
- ```bash
252
- # 首次运行:生成配置文件(.dt/data.py)
253
- dt transform data.jsonl
254
-
255
- # 编辑配置文件后再次运行:执行转换
256
- dt transform data.jsonl
257
- ```
258
-
259
- 配置文件示例(.dt/data.py):
260
- ```python
261
- def transform(item):
262
- return {
263
- "instruction": item.get("q", ""),
264
- "output": item.get("a", ""),
265
- }
266
- ```
267
-
268
- ## sample 命令 - 数据采样
269
-
270
- ```bash
271
- # 随机采样 10 条(默认)
272
- dt sample data.jsonl
273
-
274
- # 指定采样数量
275
- dt sample data.jsonl --num=50
276
-
277
- # 采样方式:random(默认), head, tail
278
- dt sample data.jsonl --sample_type=head --num=20
279
-
280
- # 输出到文件
281
- dt sample data.jsonl --num=100 --output=sampled.jsonl
282
-
283
- # 指定随机种子
284
- dt sample data.jsonl --num=50 --seed=42
285
- ```
286
-
287
- ## 支持的文件格式
288
-
289
- - JSONL (.jsonl)
290
- - JSON (.json)
291
- - CSV (.csv)
292
- - Excel (.xlsx, .xls) - 需要安装 pandas
293
- - Parquet (.parquet) - 需要安装 pyarrow
294
- """,
295
- "storage": """# 存储格式支持
296
-
297
- Datatron 支持多种常用数据格式。
298
-
299
- ## 支持的格式
300
-
301
- | 格式 | 扩展名 | 依赖 |
302
- |-----|-------|------|
303
- | JSONL | .jsonl | 无(内置) |
304
- | JSON | .json | 无(内置) |
305
- | CSV | .csv | 无(内置) |
306
- | Excel | .xlsx, .xls | pandas, openpyxl |
307
- | Parquet | .parquet | pyarrow |
308
-
309
- ## 使用示例
310
-
311
- ```python
312
- from dtflow import DataTransformer
313
-
314
- # 加载不同格式(自动检测)
315
- dt1 = DataTransformer.load("data.jsonl")
316
- dt2 = DataTransformer.load("data.csv")
317
- dt3 = DataTransformer.load("data.parquet")
318
-
319
- # 保存不同格式(根据扩展名)
320
- dt.save("output.jsonl")
321
- dt.save("output.csv")
322
- dt.save("output.parquet")
323
- ```
324
-
325
- ## 直接使用 IO 函数
326
-
327
- ```python
328
- from dtflow.storage import load_data, save_data
329
-
330
- # 加载
331
- data = load_data("input.jsonl")
332
-
333
- # 保存
334
- save_data(data, "output.jsonl")
335
- ```
336
-
337
- ## 安装额外依赖
338
-
339
- ```bash
340
- # 完整安装(含所有格式支持)
341
- pip install dtflow[full]
342
-
343
- # 仅存储格式支持
344
- pip install dtflow[storage]
345
- ```
346
- """,
347
- "chain_api": """# 链式 API 设计
348
-
349
- Datatron 支持流畅的链式调用。
350
-
351
- ## 链式调用示例
352
-
353
- ```python
354
- from dtflow import DataTransformer
355
-
356
- # 加载 -> 筛选 -> 采样 -> 转换 -> 保存
357
- DataTransformer.load("input.jsonl") \\
358
- .filter(lambda x: len(x.q) > 10) \\
359
- .sample(1000) \\
360
- .transform(lambda x: {
361
- "instruction": x.q,
362
- "output": x.a
363
- }) \\
364
- .save("output.jsonl")
365
- ```
366
-
367
- ## 可链式调用的方法
368
-
369
- | 方法 | 返回类型 | 说明 |
370
- |-----|---------|------|
371
- | `filter(func)` | DataTransformer | 筛选数据 |
372
- | `sample(n)` | DataTransformer | 随机采样 |
373
- | `head(n)` | DataTransformer | 取前 N 条 |
374
- | `tail(n)` | DataTransformer | 取后 N 条 |
375
- | `transform(func)` | DataTransformer | 格式转换 |
376
- | `shuffle()` | DataTransformer | 打乱顺序 |
377
- | `copy()` | DataTransformer | 深拷贝 |
378
-
379
- ## 终结方法
380
-
381
- | 方法 | 返回类型 | 说明 |
382
- |-----|---------|------|
383
- | `to(func)` | List | 转换并返回列表 |
384
- | `save(path)` | None | 保存到文件 |
385
- | `fields()` | List[str] | 获取字段列表 |
386
- | `stats()` | Dict | 获取统计信息 |
387
- | `split(ratio)` | Tuple | 分割数据集 |
388
-
389
- ## 属性访问
390
-
391
- lambda 参数支持属性访问,更简洁:
392
-
393
- ```python
394
- # 使用属性访问(推荐)
395
- dt.filter(lambda x: x.score > 0.8)
396
- dt.transform(lambda x: {"q": x.question, "a": x.answer})
397
-
398
- # 支持嵌套属性
399
- dt.filter(lambda x: x.meta.source == "wiki")
400
-
401
- # 也支持字典访问
402
- dt.filter(lambda x: x["score"] > 0.8)
403
-
404
- # 安全获取(带默认值)
405
- dt.transform(lambda x: {"q": x.get("question", "")})
406
- ```
407
- """,
408
- }
409
-
410
-
411
- def get_doc(topic: str) -> str:
412
- """获取指定主题的文档"""
413
- if topic not in DOCS:
414
- available = ", ".join(TOPICS)
415
- return f"未知主题: {topic}\n\n可用主题: {available}"
416
- return DOCS[topic]
dtflow/mcp/server.py DELETED
@@ -1,153 +0,0 @@
1
- """Datatron MCP 服务
2
-
3
- 提供 Datatron 用法查询的 MCP (Model Context Protocol) 服务。
4
- """
5
-
6
- from mcp.server import Server
7
- from mcp.server.stdio import stdio_server
8
- from mcp.types import TextContent, Tool
9
-
10
- from .docs import DOCS, TOPICS, get_doc
11
-
12
- # 创建 MCP 服务实例
13
- mcp = Server("dtflow")
14
-
15
-
16
- @mcp.list_tools()
17
- async def list_tools() -> list[Tool]:
18
- """列出可用的工具"""
19
- return [
20
- Tool(
21
- name="dt_usage",
22
- description="查询 Datatron 的用法文档。Datatron 是一个专业的数据格式转换工具,用于机器学习训练数据的格式转换(SFT、RLHF等)。可查询的主题包括: overview(概述), basic_usage(基本用法), presets(预设模板), cli(命令行), storage(存储格式), chain_api(链式API)",
23
- inputSchema={
24
- "type": "object",
25
- "properties": {
26
- "topic": {
27
- "type": "string",
28
- "description": f"要查询的主题。可选值: {', '.join(TOPICS)}",
29
- "enum": TOPICS,
30
- }
31
- },
32
- "required": ["topic"],
33
- },
34
- ),
35
- Tool(
36
- name="dt_list_topics",
37
- description="列出所有可用的 Datatron 文档主题",
38
- inputSchema={
39
- "type": "object",
40
- "properties": {},
41
- },
42
- ),
43
- Tool(
44
- name="dt_quick_start",
45
- description="获取 Datatron 快速入门指南,包含最常用的用法示例",
46
- inputSchema={
47
- "type": "object",
48
- "properties": {},
49
- },
50
- ),
51
- ]
52
-
53
-
54
- @mcp.call_tool()
55
- async def call_tool(name: str, arguments: dict) -> list[TextContent]:
56
- """处理工具调用"""
57
- if name == "dt_usage":
58
- topic = arguments.get("topic", "overview")
59
- content = get_doc(topic)
60
- return [TextContent(type="text", text=content)]
61
-
62
- elif name == "dt_list_topics":
63
- topics_info = """# Datatron 文档主题
64
-
65
- 可用主题列表:
66
-
67
- | 主题 | 描述 |
68
- |------|------|
69
- | overview | 项目概述和快速开始 |
70
- | basic_usage | 基本操作(加载、转换、筛选) |
71
- | presets | 预设模板(OpenAI Chat、Alpaca、ShareGPT 等) |
72
- | cli | 命令行工具使用 |
73
- | storage | 存储格式支持(JSONL、CSV、Parquet 等) |
74
- | chain_api | 链式 API 设计 |
75
-
76
- 使用 `dt_usage` 工具并传入主题名称即可查看详细文档。
77
- """
78
- return [TextContent(type="text", text=topics_info)]
79
-
80
- elif name == "dt_quick_start":
81
- quick_start = """# Datatron 快速入门
82
-
83
- ## 安装
84
- ```bash
85
- pip install dtflow
86
- ```
87
-
88
- ## 最常用的用法
89
-
90
- ### 1. Python API
91
- ```python
92
- from dtflow import DataTransformer
93
-
94
- # 加载数据
95
- dt = DataTransformer.load("data.jsonl")
96
-
97
- # 查看字段和统计
98
- print(dt.fields()) # ['a', 'q', ...]
99
- print(dt.stats()) # {'total': 1000, ...}
100
-
101
- # 转换格式(lambda 支持属性访问)
102
- result = dt.to(lambda x: {
103
- "instruction": x.q,
104
- "output": x.a
105
- })
106
-
107
- # 链式调用
108
- dt.filter(lambda x: len(x.q) > 10) \\
109
- .sample(100) \\
110
- .transform(lambda x: {"q": x.q, "a": x.a}) \\
111
- .save("output.jsonl")
112
- ```
113
-
114
- ### 2. CLI 命令
115
- ```bash
116
- # 使用预设转换
117
- dt transform data.jsonl --preset=openai_chat
118
- dt transform data.jsonl --preset=alpaca
119
-
120
- # 数据采样
121
- dt sample data.jsonl --num=10
122
- ```
123
-
124
- ### 3. 可用预设
125
- - `openai_chat`: OpenAI Chat 格式
126
- - `alpaca`: Alpaca 指令格式
127
- - `sharegpt`: ShareGPT 多轮对话
128
- - `dpo_pair`: DPO 偏好对格式
129
- - `simple_qa`: 简单问答格式
130
-
131
- 更多详情请使用 `dt_usage` 查询具体主题。
132
- """
133
- return [TextContent(type="text", text=quick_start)]
134
-
135
- else:
136
- return [TextContent(type="text", text=f"未知工具: {name}")]
137
-
138
-
139
- async def run_server():
140
- """运行 MCP 服务"""
141
- async with stdio_server() as (read_stream, write_stream):
142
- await mcp.run(read_stream, write_stream, mcp.create_initialization_options())
143
-
144
-
145
- def main():
146
- """入口函数"""
147
- import asyncio
148
-
149
- asyncio.run(run_server())
150
-
151
-
152
- if __name__ == "__main__":
153
- main()
File without changes