writer-voice 0.1.0__tar.gz
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.
- writer_voice-0.1.0/LICENSE +21 -0
- writer_voice-0.1.0/PKG-INFO +108 -0
- writer_voice-0.1.0/README.md +88 -0
- writer_voice-0.1.0/pyproject.toml +34 -0
- writer_voice-0.1.0/setup.cfg +4 -0
- writer_voice-0.1.0/tests/test_smoke.py +111 -0
- writer_voice-0.1.0/voicekit/__init__.py +9 -0
- writer_voice-0.1.0/voicekit/__main__.py +3 -0
- writer_voice-0.1.0/voicekit/cli.py +139 -0
- writer_voice-0.1.0/voicekit/compat.py +107 -0
- writer_voice-0.1.0/voicekit/core/__init__.py +1 -0
- writer_voice-0.1.0/voicekit/core/attribution_check.py +146 -0
- writer_voice-0.1.0/voicekit/core/attribution_check_v2.py +287 -0
- writer_voice-0.1.0/voicekit/core/attribution_judge.py +297 -0
- writer_voice-0.1.0/voicekit/core/band_calibration.py +238 -0
- writer_voice-0.1.0/voicekit/core/blind_eval.py +353 -0
- writer_voice-0.1.0/voicekit/core/build_behavior_card.py +279 -0
- writer_voice-0.1.0/voicekit/core/build_book_corpus.py +165 -0
- writer_voice-0.1.0/voicekit/core/build_profile.py +559 -0
- writer_voice-0.1.0/voicekit/core/build_review_book.py +444 -0
- writer_voice-0.1.0/voicekit/core/build_strategy_card.py +242 -0
- writer_voice-0.1.0/voicekit/core/claim_eval_v2.py +336 -0
- writer_voice-0.1.0/voicekit/core/claim_extract.py +171 -0
- writer_voice-0.1.0/voicekit/core/claim_extract_v2.py +471 -0
- writer_voice-0.1.0/voicekit/core/claim_segment.py +185 -0
- writer_voice-0.1.0/voicekit/core/corpus_probe.py +278 -0
- writer_voice-0.1.0/voicekit/core/draft.py +629 -0
- writer_voice-0.1.0/voicekit/core/exemplar_eval.py +363 -0
- writer_voice-0.1.0/voicekit/core/exemplar_inputs.py +49 -0
- writer_voice-0.1.0/voicekit/core/exemplar_judge.py +987 -0
- writer_voice-0.1.0/voicekit/core/exemplar_measured.py +111 -0
- writer_voice-0.1.0/voicekit/core/gate_audit.py +77 -0
- writer_voice-0.1.0/voicekit/core/intake.py +299 -0
- writer_voice-0.1.0/voicekit/core/judge.py +921 -0
- writer_voice-0.1.0/voicekit/core/judge_lib.py +933 -0
- writer_voice-0.1.0/voicekit/core/judgments.py +272 -0
- writer_voice-0.1.0/voicekit/core/make_annotation.py +384 -0
- writer_voice-0.1.0/voicekit/core/make_author_round2_sheet.py +363 -0
- writer_voice-0.1.0/voicekit/core/make_author_type_full.py +384 -0
- writer_voice-0.1.0/voicekit/core/make_author_type_sheet.py +377 -0
- writer_voice-0.1.0/voicekit/core/make_choice_sheet.py +676 -0
- writer_voice-0.1.0/voicekit/core/make_rejection_annotation.py +576 -0
- writer_voice-0.1.0/voicekit/core/measure_extra.py +184 -0
- writer_voice-0.1.0/voicekit/core/negatives_policy.py +59 -0
- writer_voice-0.1.0/voicekit/core/pack_eval.py +311 -0
- writer_voice-0.1.0/voicekit/core/pack_lib.py +104 -0
- writer_voice-0.1.0/voicekit/core/paragraph_table.py +207 -0
- writer_voice-0.1.0/voicekit/core/pipeline.py +154 -0
- writer_voice-0.1.0/voicekit/core/preference_probe.py +314 -0
- writer_voice-0.1.0/voicekit/core/probe_arc.py +164 -0
- writer_voice-0.1.0/voicekit/core/probe_dialog.py +124 -0
- writer_voice-0.1.0/voicekit/core/probe_dispositions.py +370 -0
- writer_voice-0.1.0/voicekit/core/probe_flip.py +182 -0
- writer_voice-0.1.0/voicekit/core/probe_material.py +76 -0
- writer_voice-0.1.0/voicekit/core/probe_seven.py +166 -0
- writer_voice-0.1.0/voicekit/core/probe_shape.py +145 -0
- writer_voice-0.1.0/voicekit/core/recreate_eval.py +875 -0
- writer_voice-0.1.0/voicekit/core/recreate_prompt.py +280 -0
- writer_voice-0.1.0/voicekit/core/rewrite.py +986 -0
- writer_voice-0.1.0/voicekit/core/rewrite_prompt.py +288 -0
- writer_voice-0.1.0/voicekit/core/rubrics.yaml +234 -0
- writer_voice-0.1.0/voicekit/core/rules.py +219 -0
- writer_voice-0.1.0/voicekit/core/style_catalog.py +94 -0
- writer_voice-0.1.0/voicekit/core/style_distill.py +172 -0
- writer_voice-0.1.0/voicekit/core/style_lint.py +576 -0
- writer_voice-0.1.0/voicekit/core/thought_flow.py +192 -0
- writer_voice-0.1.0/voicekit/core/voice_lib.py +973 -0
- writer_voice-0.1.0/voicekit/core/voice_review.py +989 -0
- writer_voice-0.1.0/voicekit/core/voice_text.py +131 -0
- writer_voice-0.1.0/voicekit/paths.py +47 -0
- writer_voice-0.1.0/voicekit/report.py +209 -0
- writer_voice-0.1.0/writer_voice.egg-info/PKG-INFO +108 -0
- writer_voice-0.1.0/writer_voice.egg-info/SOURCES.txt +75 -0
- writer_voice-0.1.0/writer_voice.egg-info/dependency_links.txt +1 -0
- writer_voice-0.1.0/writer_voice.egg-info/entry_points.txt +2 -0
- writer_voice-0.1.0/writer_voice.egg-info/requires.txt +1 -0
- writer_voice-0.1.0/writer_voice.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 voicekit contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: writer-voice
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: 提炼一个作者的文风,体检一篇稿子,按他的腔起稿(命令名 voicekit)
|
|
5
|
+
Author: voicekit contributors
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/chenyinrusi/voicekit
|
|
8
|
+
Project-URL: Issues, https://github.com/chenyinrusi/voicekit/issues
|
|
9
|
+
Keywords: writing,style,chinese,prose,lint,llm
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Natural Language :: Chinese (Simplified)
|
|
14
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: PyYAML>=6.0
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# voicekit
|
|
22
|
+
|
|
23
|
+
把一个人的文风变成**能数出来的判据**,然后拿这些判据去体检稿子、按他的腔起稿。
|
|
24
|
+
|
|
25
|
+
不是"AI 替你写"。是**起稿机 + 挑刺器**:先给你一个像他的草稿,再逐条告诉你哪里不像。
|
|
26
|
+
放不放行永远是作者签字,不是分数。
|
|
27
|
+
|
|
28
|
+
## 装
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pip install writer-voice # 发行名是 writer-voice,装完命令叫 voicekit
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
装用 PyPI 包名 `writer-voice`;模块名和命令名都是 `voicekit`。以后选名字时这点说清楚省得找错。
|
|
35
|
+
|
|
36
|
+
本地开发:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
py -3.10 -m pip install -e .
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
只依赖标准库 + PyYAML。`lint` / `report` 不花钱(不调模型)。`build` 只在 `style_distill` 环节要模型;
|
|
43
|
+
`draft` 要模型。
|
|
44
|
+
|
|
45
|
+
## 三条命令
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
# 1. 体检一篇稿子(0 元)
|
|
49
|
+
voicekit lint 我的稿子.md --pack voices/panchen-voice
|
|
50
|
+
|
|
51
|
+
# 2. 一页 HTML 报告:结论 3 行 + 先改这三条 + 全部 42 条(0 元)
|
|
52
|
+
voicekit report 我的稿子.md --pack voices/panchen-voice
|
|
53
|
+
|
|
54
|
+
# 3. 从一个语料目录提炼作者 profile(一篇一个 .md)
|
|
55
|
+
voicekit build --corpus 语料/ --out voices/某人-voice --slug 某人 --author 某人
|
|
56
|
+
|
|
57
|
+
# 4. 按作者的腔起稿(要 key)
|
|
58
|
+
set DEEPSEEK_API_KEY=... # PowerShell: $env:DEEPSEEK_API_KEY="..."
|
|
59
|
+
voicekit draft --material 材料.md --dry-run # 先看提示词,0 元
|
|
60
|
+
voicekit draft --material 材料.md --out 初稿.md
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`lint` 退出码:`0` 全过,`1` 有不过的条目,`2` 读不了/太短(<200 汉字不给稳定分数)。
|
|
64
|
+
|
|
65
|
+
## 作者包长什么样
|
|
66
|
+
|
|
67
|
+
一个目录,一个作者:
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
voices/panchen-voice/
|
|
71
|
+
pack.json # schema / slug / author / 语料与 profile 的相对路径
|
|
72
|
+
profile.json # build 算出来的 42 条基线
|
|
73
|
+
corpus/*.md # 语料,一篇一个文件
|
|
74
|
+
judge_baseline.json # 可选:判官基线
|
|
75
|
+
exemplar_baseline.json # 可选:样例基线
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
查找顺序:`--pack` → 环境变量 `VOICEKIT_PACK` → `./voices/*/pack.json`。
|
|
79
|
+
|
|
80
|
+
## 它诚实的地方
|
|
81
|
+
|
|
82
|
+
- **42 条属性都从语料里数出来**,不是拍脑袋定的。但也只覆盖"可数的"那一层。
|
|
83
|
+
- **分数不是及格线**。作者签过字的老稿也常掉分;论文、说理、公号这些语料里没有的体裁,低分是预期的,不是稿子差。
|
|
84
|
+
- **不许拿分数当放行标准**。判据只作诊断,签字由人。
|
|
85
|
+
- 生成这一层我们试过微调 3B 模型、各种配方:10 篇里 0 篇过关。它现在的定位不是替身。
|
|
86
|
+
- `judge.py` 的"同人判定"是 LLM 判官,**比人不可靠**(同一条判定重复跑四次,21/21 和 19/21 各两次)。
|
|
87
|
+
证据层级:作者亲手判 > 可量数字 > LLM 判官。
|
|
88
|
+
|
|
89
|
+
## 换模型
|
|
90
|
+
|
|
91
|
+
默认 `deepseek`。也可以 `openai`、`moonshot`,或用环境变量接任何 OpenAI 兼容接口:
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
set VOICEKIT_BASE_URL=https://my-endpoint/v1
|
|
95
|
+
set VOICEKIT_API_KEY=...
|
|
96
|
+
set VOICEKIT_MODEL=my-model
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## 开发
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
py -3.10 -m pytest tests -q
|
|
103
|
+
set VOICEKIT_TEST_PACK=voices/panchen-voice # 让真包集成测试也跑
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## 协议
|
|
107
|
+
|
|
108
|
+
MIT,见 [LICENSE](LICENSE)。
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# voicekit
|
|
2
|
+
|
|
3
|
+
把一个人的文风变成**能数出来的判据**,然后拿这些判据去体检稿子、按他的腔起稿。
|
|
4
|
+
|
|
5
|
+
不是"AI 替你写"。是**起稿机 + 挑刺器**:先给你一个像他的草稿,再逐条告诉你哪里不像。
|
|
6
|
+
放不放行永远是作者签字,不是分数。
|
|
7
|
+
|
|
8
|
+
## 装
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
pip install writer-voice # 发行名是 writer-voice,装完命令叫 voicekit
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
装用 PyPI 包名 `writer-voice`;模块名和命令名都是 `voicekit`。以后选名字时这点说清楚省得找错。
|
|
15
|
+
|
|
16
|
+
本地开发:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
py -3.10 -m pip install -e .
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
只依赖标准库 + PyYAML。`lint` / `report` 不花钱(不调模型)。`build` 只在 `style_distill` 环节要模型;
|
|
23
|
+
`draft` 要模型。
|
|
24
|
+
|
|
25
|
+
## 三条命令
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
# 1. 体检一篇稿子(0 元)
|
|
29
|
+
voicekit lint 我的稿子.md --pack voices/panchen-voice
|
|
30
|
+
|
|
31
|
+
# 2. 一页 HTML 报告:结论 3 行 + 先改这三条 + 全部 42 条(0 元)
|
|
32
|
+
voicekit report 我的稿子.md --pack voices/panchen-voice
|
|
33
|
+
|
|
34
|
+
# 3. 从一个语料目录提炼作者 profile(一篇一个 .md)
|
|
35
|
+
voicekit build --corpus 语料/ --out voices/某人-voice --slug 某人 --author 某人
|
|
36
|
+
|
|
37
|
+
# 4. 按作者的腔起稿(要 key)
|
|
38
|
+
set DEEPSEEK_API_KEY=... # PowerShell: $env:DEEPSEEK_API_KEY="..."
|
|
39
|
+
voicekit draft --material 材料.md --dry-run # 先看提示词,0 元
|
|
40
|
+
voicekit draft --material 材料.md --out 初稿.md
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`lint` 退出码:`0` 全过,`1` 有不过的条目,`2` 读不了/太短(<200 汉字不给稳定分数)。
|
|
44
|
+
|
|
45
|
+
## 作者包长什么样
|
|
46
|
+
|
|
47
|
+
一个目录,一个作者:
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
voices/panchen-voice/
|
|
51
|
+
pack.json # schema / slug / author / 语料与 profile 的相对路径
|
|
52
|
+
profile.json # build 算出来的 42 条基线
|
|
53
|
+
corpus/*.md # 语料,一篇一个文件
|
|
54
|
+
judge_baseline.json # 可选:判官基线
|
|
55
|
+
exemplar_baseline.json # 可选:样例基线
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
查找顺序:`--pack` → 环境变量 `VOICEKIT_PACK` → `./voices/*/pack.json`。
|
|
59
|
+
|
|
60
|
+
## 它诚实的地方
|
|
61
|
+
|
|
62
|
+
- **42 条属性都从语料里数出来**,不是拍脑袋定的。但也只覆盖"可数的"那一层。
|
|
63
|
+
- **分数不是及格线**。作者签过字的老稿也常掉分;论文、说理、公号这些语料里没有的体裁,低分是预期的,不是稿子差。
|
|
64
|
+
- **不许拿分数当放行标准**。判据只作诊断,签字由人。
|
|
65
|
+
- 生成这一层我们试过微调 3B 模型、各种配方:10 篇里 0 篇过关。它现在的定位不是替身。
|
|
66
|
+
- `judge.py` 的"同人判定"是 LLM 判官,**比人不可靠**(同一条判定重复跑四次,21/21 和 19/21 各两次)。
|
|
67
|
+
证据层级:作者亲手判 > 可量数字 > LLM 判官。
|
|
68
|
+
|
|
69
|
+
## 换模型
|
|
70
|
+
|
|
71
|
+
默认 `deepseek`。也可以 `openai`、`moonshot`,或用环境变量接任何 OpenAI 兼容接口:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
set VOICEKIT_BASE_URL=https://my-endpoint/v1
|
|
75
|
+
set VOICEKIT_API_KEY=...
|
|
76
|
+
set VOICEKIT_MODEL=my-model
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## 开发
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
py -3.10 -m pytest tests -q
|
|
83
|
+
set VOICEKIT_TEST_PACK=voices/panchen-voice # 让真包集成测试也跑
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## 协议
|
|
87
|
+
|
|
88
|
+
MIT,见 [LICENSE](LICENSE)。
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "writer-voice"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "提炼一个作者的文风,体检一篇稿子,按他的腔起稿(命令名 voicekit)"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "voicekit contributors" }]
|
|
13
|
+
keywords = ["writing", "style", "chinese", "prose", "lint", "llm"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Programming Language :: Python :: 3.10",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Natural Language :: Chinese (Simplified)",
|
|
19
|
+
"Topic :: Text Processing :: Linguistic",
|
|
20
|
+
]
|
|
21
|
+
dependencies = ["PyYAML>=6.0"]
|
|
22
|
+
|
|
23
|
+
[project.urls]
|
|
24
|
+
Homepage = "https://github.com/chenyinrusi/voicekit"
|
|
25
|
+
Issues = "https://github.com/chenyinrusi/voicekit/issues"
|
|
26
|
+
|
|
27
|
+
[project.scripts]
|
|
28
|
+
voicekit = "voicekit.cli:main"
|
|
29
|
+
|
|
30
|
+
[tool.setuptools.packages.find]
|
|
31
|
+
include = ["voicekit*"]
|
|
32
|
+
|
|
33
|
+
[tool.setuptools.package-data]
|
|
34
|
+
"voicekit.core" = ["rubrics.yaml"]
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"""最小冒烟测试:包能导入、CLI 能跑、报告排版函数不编数字。
|
|
2
|
+
|
|
3
|
+
带真作者包的集成测试默认跳过;设 `VOICEKIT_TEST_PACK=<包目录>` 才跑。
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import json
|
|
9
|
+
import os
|
|
10
|
+
import subprocess
|
|
11
|
+
import sys
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
import pytest
|
|
15
|
+
|
|
16
|
+
ROOT = Path(__file__).resolve().parents[1]
|
|
17
|
+
sys.path.insert(0, str(ROOT))
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def run_cli(*args: str, cwd: Path | None = None) -> subprocess.CompletedProcess:
|
|
21
|
+
env = dict(os.environ, PYTHONIOENCODING="utf-8", PYTHONPATH=str(ROOT))
|
|
22
|
+
return subprocess.run(
|
|
23
|
+
[sys.executable, "-m", "voicekit", *args],
|
|
24
|
+
capture_output=True,
|
|
25
|
+
text=True,
|
|
26
|
+
encoding="utf-8",
|
|
27
|
+
cwd=str(cwd or ROOT),
|
|
28
|
+
env=env,
|
|
29
|
+
timeout=180,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def test_all_core_modules_import():
|
|
34
|
+
from voicekit.core import ( # noqa: F401
|
|
35
|
+
build_profile,
|
|
36
|
+
judge_lib,
|
|
37
|
+
pack_lib,
|
|
38
|
+
rewrite,
|
|
39
|
+
style_lint,
|
|
40
|
+
voice_lib,
|
|
41
|
+
)
|
|
42
|
+
assert callable(style_lint.main)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def test_version():
|
|
46
|
+
done = run_cli("--version")
|
|
47
|
+
assert done.returncode == 0
|
|
48
|
+
assert "voicekit" in done.stdout
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def test_lint_without_pack_says_what_to_do(tmp_path: Path):
|
|
52
|
+
draft = tmp_path / "draft.md"
|
|
53
|
+
draft.write_text("今天下午我去了一趟海边,风很大。\n", encoding="utf-8")
|
|
54
|
+
done = run_cli("lint", str(draft), cwd=tmp_path)
|
|
55
|
+
assert done.returncode != 0
|
|
56
|
+
assert "no voice pack" in (done.stdout + done.stderr)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def test_pack_loader_rejects_bad_schema(tmp_path: Path):
|
|
60
|
+
from voicekit.core.pack_lib import PackError, load_pack
|
|
61
|
+
|
|
62
|
+
(tmp_path / "pack.json").write_text(json.dumps({"schema": 2, "slug": "x"}), encoding="utf-8")
|
|
63
|
+
with pytest.raises(PackError):
|
|
64
|
+
load_pack(tmp_path)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def test_pack_loader_reads_synthetic_pack(tmp_path: Path):
|
|
68
|
+
from voicekit.core.pack_lib import load_pack
|
|
69
|
+
|
|
70
|
+
(tmp_path / "corpus").mkdir()
|
|
71
|
+
(tmp_path / "corpus" / "a.md").write_text("他写的第一篇。\n", encoding="utf-8")
|
|
72
|
+
pack = {
|
|
73
|
+
"schema": 1,
|
|
74
|
+
"slug": "demo",
|
|
75
|
+
"author": "示例作者",
|
|
76
|
+
"corpus": {"path": "corpus"},
|
|
77
|
+
"profile": {"path": "profile.json"},
|
|
78
|
+
}
|
|
79
|
+
(tmp_path / "pack.json").write_text(json.dumps(pack), encoding="utf-8")
|
|
80
|
+
data = load_pack(tmp_path)
|
|
81
|
+
assert data["slug"] == "demo"
|
|
82
|
+
assert data["corpus"] == (tmp_path / "corpus").resolve()
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def test_humanize_keeps_known_shapes():
|
|
86
|
+
from voicekit.report import humanize
|
|
87
|
+
|
|
88
|
+
assert humanize("obs=1.327 base=4.814 tol=±2.984") == "实测 1.327,他的基线 4.814,允许差 ±2.984"
|
|
89
|
+
one = humanize("1 subkey(s) out: booster_per_k: 1.33 vs 0.47 (±0.47)")
|
|
90
|
+
assert one.startswith("1 项偏离") and "1.33" in one
|
|
91
|
+
# 认不出的口径原样返回,不许编
|
|
92
|
+
assert humanize("weird shape") == "weird shape"
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def test_record_share_flags_a_record_file():
|
|
96
|
+
from voicekit.report import record_share
|
|
97
|
+
|
|
98
|
+
record = "# 签字记录\n\n| 篇 | 日期 |\n| --- | --- |\n| 047 | 2026-11-22 |\n\n- 已签\n- 存档\n"
|
|
99
|
+
prose = "风从西边来。我站在门口,看他把伞收起来,抖了两下水。" * 3
|
|
100
|
+
assert record_share(record) >= 0.25
|
|
101
|
+
assert record_share(prose) < 0.25
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@pytest.mark.skipif(not os.environ.get("VOICEKIT_TEST_PACK"), reason="no VOICEKIT_TEST_PACK")
|
|
105
|
+
def test_lint_real_pack(tmp_path: Path):
|
|
106
|
+
pack = Path(os.environ["VOICEKIT_TEST_PACK"])
|
|
107
|
+
draft = tmp_path / "d.md"
|
|
108
|
+
draft.write_text("风从西边来。我站在门口,看他把伞收起来,抖了两下水。\n" * 20, encoding="utf-8")
|
|
109
|
+
done = run_cli("lint", str(draft), "--pack", str(pack))
|
|
110
|
+
assert done.returncode in (0, 1), done.stderr
|
|
111
|
+
assert "/42" in done.stdout or "42)" in done.stdout
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"""voicekit 命令行:lint(体检)/ report(一页报告)/ build(提炼)/ draft(起稿)。
|
|
2
|
+
|
|
3
|
+
设计原则:本层只做参数与路径解析,判据与算法全在 core/ 里。
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import argparse
|
|
9
|
+
import sys
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
from . import __version__, paths, report
|
|
13
|
+
from .core import style_lint
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _lint(args: argparse.Namespace) -> int:
|
|
17
|
+
pack = paths.resolve_pack(args.pack)
|
|
18
|
+
argv = ["--file", str(args.file)]
|
|
19
|
+
argv += ["--pack", str(pack)]
|
|
20
|
+
if args.json:
|
|
21
|
+
argv += ["--json"]
|
|
22
|
+
rc = style_lint.main(argv)
|
|
23
|
+
if args.html is not None:
|
|
24
|
+
out = None if args.html is True else Path(args.html)
|
|
25
|
+
page = report.render(args.file, pack, out, args.title)
|
|
26
|
+
print(f"一页报告:{page}")
|
|
27
|
+
return rc
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _report(args: argparse.Namespace) -> int:
|
|
31
|
+
pack = paths.resolve_pack(args.pack) if not args.no_pack else None
|
|
32
|
+
page = report.render(args.file, pack, args.out, args.title)
|
|
33
|
+
print(f"一页报告:{page}")
|
|
34
|
+
return 0
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _build(args: argparse.Namespace) -> int:
|
|
38
|
+
from .core import build_profile
|
|
39
|
+
|
|
40
|
+
out = Path(args.out)
|
|
41
|
+
out.mkdir(parents=True, exist_ok=True)
|
|
42
|
+
argv = [
|
|
43
|
+
"--corpus", str(Path(args.corpus)),
|
|
44
|
+
"--out", str(out / "profile.json"),
|
|
45
|
+
"--slug", args.slug,
|
|
46
|
+
"--author", args.author,
|
|
47
|
+
]
|
|
48
|
+
argv += ["--check"] if args.check else []
|
|
49
|
+
rc = build_profile.main(argv)
|
|
50
|
+
if rc == 0 and not args.check:
|
|
51
|
+
_write_pack_json(out, args.slug, args.author, args.corpus)
|
|
52
|
+
return rc
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _write_pack_json(pack_dir: Path, slug: str, author: str, corpus: Path) -> None:
|
|
56
|
+
"""提炼完补一个 pack.json,让这个目录马上能被 --pack 用。"""
|
|
57
|
+
import json
|
|
58
|
+
|
|
59
|
+
corpus_path = Path(corpus).resolve()
|
|
60
|
+
try:
|
|
61
|
+
corpus_rel = corpus_path.relative_to(pack_dir.resolve())
|
|
62
|
+
corpus_value = corpus_rel.as_posix()
|
|
63
|
+
except ValueError:
|
|
64
|
+
corpus_value = str(corpus_path)
|
|
65
|
+
data = {
|
|
66
|
+
"schema": 1,
|
|
67
|
+
"slug": slug,
|
|
68
|
+
"author": author,
|
|
69
|
+
"corpus": {"path": corpus_value},
|
|
70
|
+
"profile": {"path": "profile.json"},
|
|
71
|
+
}
|
|
72
|
+
(pack_dir / "pack.json").write_text(
|
|
73
|
+
json.dumps(data, ensure_ascii=False, indent=2) + "\n", encoding="utf-8"
|
|
74
|
+
)
|
|
75
|
+
print(f"作者包:{pack_dir}")
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _draft(args: argparse.Namespace) -> int:
|
|
79
|
+
from .core import draft
|
|
80
|
+
|
|
81
|
+
argv = ["--file", str(args.material)]
|
|
82
|
+
argv += ["--out", str(args.out)] if args.out else []
|
|
83
|
+
argv += ["--dry-run"] if args.dry_run else []
|
|
84
|
+
argv += ["--provider", args.provider, "--model", args.model] if args.model else ["--provider", args.provider]
|
|
85
|
+
return draft.main(argv)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
89
|
+
ap = argparse.ArgumentParser(prog="voicekit", description="提炼一个人的文风,体检一篇稿子,按他的腔起稿。")
|
|
90
|
+
ap.add_argument("--version", action="version", version=f"voicekit {__version__}")
|
|
91
|
+
sub = ap.add_subparsers(dest="cmd", required=True)
|
|
92
|
+
|
|
93
|
+
lint = sub.add_parser("lint", help="体检:42 条可数属性,逐条过/不过")
|
|
94
|
+
lint.add_argument("file", type=Path, help="要体检的稿子(.md/.txt)")
|
|
95
|
+
lint.add_argument("--pack", type=Path, help="作者包目录(默认找 ./voices/)")
|
|
96
|
+
lint.add_argument("--json", action="store_true", help="输出机器可读 JSON")
|
|
97
|
+
lint.add_argument("--html", nargs="?", const=True, default=None, metavar="OUT",
|
|
98
|
+
help="同时生成一页 HTML 报告(可给输出路径)")
|
|
99
|
+
lint.add_argument("--title", help="报告标题")
|
|
100
|
+
lint.set_defaults(func=_lint)
|
|
101
|
+
|
|
102
|
+
rep = sub.add_parser("report", help="一页 HTML 报告:结论 3 行 + 先改这三条 + 全部条目")
|
|
103
|
+
rep.add_argument("file", type=Path)
|
|
104
|
+
rep.add_argument("--pack", type=Path)
|
|
105
|
+
rep.add_argument("--no-pack", action="store_true", help="不加载作者包(只跑通用检查)")
|
|
106
|
+
rep.add_argument("-o", "--out", type=Path, help="写到哪(默认 <file>-report.html)")
|
|
107
|
+
rep.add_argument("--title")
|
|
108
|
+
rep.set_defaults(func=_report)
|
|
109
|
+
|
|
110
|
+
build = sub.add_parser("build", help="提炼:从一个语料目录算出一份作者 profile")
|
|
111
|
+
build.add_argument("--corpus", required=True, type=Path, help="语料目录(一篇一个 .md/.txt)")
|
|
112
|
+
build.add_argument("--out", required=True, type=Path, help="作者包目录(写 profile.json + pack.json)")
|
|
113
|
+
build.add_argument("--slug", default="myvoice", help="作者 slug(小写连字符)")
|
|
114
|
+
build.add_argument("--author", default="我的作者", help="作者显示名")
|
|
115
|
+
build.add_argument("--check", action="store_true", help="只比对,不写文件")
|
|
116
|
+
build.set_defaults(func=_build)
|
|
117
|
+
|
|
118
|
+
dr = sub.add_parser("draft", help="起稿:按作者的腔写一篇初稿(要 API key)")
|
|
119
|
+
dr.add_argument("--material", required=True, type=Path, help="材料(事实清单)")
|
|
120
|
+
dr.add_argument("--out", type=Path, help="初稿写到哪")
|
|
121
|
+
dr.add_argument("--provider", default="deepseek")
|
|
122
|
+
dr.add_argument("--model")
|
|
123
|
+
dr.add_argument("--dry-run", action="store_true", help="0 元:只出提示词")
|
|
124
|
+
dr.set_defaults(func=_draft)
|
|
125
|
+
return ap
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def main(argv: list[str] | None = None) -> int:
|
|
129
|
+
for stream in (sys.stdout, sys.stderr):
|
|
130
|
+
try:
|
|
131
|
+
stream.reconfigure(encoding="utf-8", errors="replace")
|
|
132
|
+
except (AttributeError, ValueError):
|
|
133
|
+
pass
|
|
134
|
+
args = build_parser().parse_args(argv)
|
|
135
|
+
return int(args.func(args) or 0)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
if __name__ == "__main__":
|
|
139
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"""LLM 适配层:标准库直连 OpenAI 兼容接口(/chat/completions)。
|
|
2
|
+
|
|
3
|
+
为什么自己写:CAS 的 `src.llm.*` 依赖私有仓库;这一层只保留引擎需要的
|
|
4
|
+
`Message` / `create_llm` / `ensure_provider_sdks_loaded` / `release_llm_client` 四个符号。
|
|
5
|
+
|
|
6
|
+
环境变量(都可覆盖 provider 默认值):
|
|
7
|
+
- `VOICEKIT_BASE_URL`、`VOICEKIT_API_KEY`、`VOICEKIT_MODEL`
|
|
8
|
+
|
|
9
|
+
内置 provider:deepseek(默认)、openai、moonshot。
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import asyncio
|
|
15
|
+
import json
|
|
16
|
+
import os
|
|
17
|
+
import urllib.error
|
|
18
|
+
import urllib.request
|
|
19
|
+
from dataclasses import dataclass
|
|
20
|
+
|
|
21
|
+
#: provider -> (base_url, api key env, default model)
|
|
22
|
+
PROVIDERS: dict[str, tuple[str, str, str]] = {
|
|
23
|
+
"deepseek": ("https://api.deepseek.com/v1", "DEEPSEEK_API_KEY", "deepseek-chat"),
|
|
24
|
+
"openai": ("https://api.openai.com/v1", "OPENAI_API_KEY", "gpt-4o"),
|
|
25
|
+
"moonshot": ("https://api.moonshot.cn/v1", "MOONSHOT_API_KEY", "moonshot-v1-8k"),
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass
|
|
30
|
+
class Message:
|
|
31
|
+
"""最小的消息对象;引擎只用到 role/content 与三个类方法。"""
|
|
32
|
+
|
|
33
|
+
role: str
|
|
34
|
+
content: str
|
|
35
|
+
|
|
36
|
+
@classmethod
|
|
37
|
+
def system(cls, content: str) -> "Message":
|
|
38
|
+
return cls("system", content)
|
|
39
|
+
|
|
40
|
+
@classmethod
|
|
41
|
+
def user(cls, content: str) -> "Message":
|
|
42
|
+
return cls("user", content)
|
|
43
|
+
|
|
44
|
+
@classmethod
|
|
45
|
+
def assistant(cls, content: str) -> "Message":
|
|
46
|
+
return cls("assistant", content)
|
|
47
|
+
|
|
48
|
+
def as_dict(self) -> dict:
|
|
49
|
+
return {"role": self.role, "content": self.content}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _settings(provider: str, model: str | None) -> tuple[str, str, str]:
|
|
53
|
+
if provider not in PROVIDERS:
|
|
54
|
+
raise SystemExit(f"unknown provider {provider!r}; known: {', '.join(PROVIDERS)}")
|
|
55
|
+
base, key_env, default_model = PROVIDERS[provider]
|
|
56
|
+
base = os.environ.get("VOICEKIT_BASE_URL") or base
|
|
57
|
+
key = os.environ.get("VOICEKIT_API_KEY") or os.environ.get(key_env, "")
|
|
58
|
+
model = model or os.environ.get("VOICEKIT_MODEL") or default_model
|
|
59
|
+
if not key:
|
|
60
|
+
raise SystemExit(f"missing API key: set {key_env} (or VOICEKIT_API_KEY)")
|
|
61
|
+
return base.rstrip("/"), key, model
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class _Client:
|
|
65
|
+
def __init__(self, provider: str, model: str | None) -> None:
|
|
66
|
+
self.base, self.key, self.model = _settings(provider, model)
|
|
67
|
+
|
|
68
|
+
def _post(self, messages: list[dict], temperature: float, timeout: float) -> str:
|
|
69
|
+
body = json.dumps(
|
|
70
|
+
{"model": self.model, "messages": messages, "temperature": temperature}
|
|
71
|
+
).encode("utf-8")
|
|
72
|
+
req = urllib.request.Request(
|
|
73
|
+
f"{self.base}/chat/completions",
|
|
74
|
+
data=body,
|
|
75
|
+
headers={"Content-Type": "application/json", "Authorization": f"Bearer {self.key}"},
|
|
76
|
+
method="POST",
|
|
77
|
+
)
|
|
78
|
+
try:
|
|
79
|
+
with urllib.request.urlopen(req, timeout=timeout) as resp:
|
|
80
|
+
payload = json.loads(resp.read().decode("utf-8"))
|
|
81
|
+
except urllib.error.HTTPError as exc: # 把服务端报文带出来,别吞成一句 500
|
|
82
|
+
detail = exc.read().decode("utf-8", "replace")[:800]
|
|
83
|
+
raise SystemExit(f"LLM HTTP {exc.code}: {detail}") from exc
|
|
84
|
+
try:
|
|
85
|
+
return payload["choices"][0]["message"]["content"]
|
|
86
|
+
except (KeyError, IndexError) as exc:
|
|
87
|
+
raise SystemExit(f"unexpected LLM payload: {str(payload)[:800]}") from exc
|
|
88
|
+
|
|
89
|
+
async def chat(self, messages: list, temperature: float = 0.9, timeout: float | None = None) -> str:
|
|
90
|
+
payload = [m.as_dict() if isinstance(m, Message) else dict(m) for m in messages]
|
|
91
|
+
return await asyncio.to_thread(
|
|
92
|
+
self._post, payload, temperature, timeout if timeout is not None else 180.0
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def create_llm(provider: str, model: str | None = None) -> _Client:
|
|
97
|
+
return _Client(provider, model)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
async def ensure_provider_sdks_loaded() -> None:
|
|
101
|
+
"""兼容垫片:CAS 里用来懒加载 SDK,这里只用标准库。"""
|
|
102
|
+
return None
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
async def release_llm_client(llm: object) -> None:
|
|
106
|
+
"""兼容垫片:标准库连接随请求结束释放。"""
|
|
107
|
+
return None
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""voicekit core:从 CAS scripts/voice 机械搬来的模块(逻辑未改)。"""
|