ruhui 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.
- ruhui-0.1.0/LICENSE +176 -0
- ruhui-0.1.0/NOTICE +12 -0
- ruhui-0.1.0/PKG-INFO +175 -0
- ruhui-0.1.0/README.md +147 -0
- ruhui-0.1.0/pyproject.toml +39 -0
- ruhui-0.1.0/ruhui/__init__.py +59 -0
- ruhui-0.1.0/ruhui/agent.py +385 -0
- ruhui-0.1.0/ruhui/common.py +280 -0
- ruhui-0.1.0/ruhui/email.py +104 -0
- ruhui-0.1.0/ruhui/lang.py +292 -0
- ruhui-0.1.0/ruhui/presets.py +187 -0
- ruhui-0.1.0/ruhui/router.py +329 -0
- ruhui-0.1.0/ruhui/shortlist.py +272 -0
- ruhui-0.1.0/ruhui.egg-info/PKG-INFO +175 -0
- ruhui-0.1.0/ruhui.egg-info/SOURCES.txt +18 -0
- ruhui-0.1.0/ruhui.egg-info/dependency_links.txt +1 -0
- ruhui-0.1.0/ruhui.egg-info/requires.txt +5 -0
- ruhui-0.1.0/ruhui.egg-info/top_level.txt +1 -0
- ruhui-0.1.0/setup.cfg +4 -0
- ruhui-0.1.0/setup.py +4 -0
ruhui-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
ruhui-0.1.0/NOTICE
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Ruhui
|
|
2
|
+
Copyright 2026 AnyForge
|
|
3
|
+
|
|
4
|
+
This product is a fork of Laya (https://github.com/NandhaKishorM/laya),
|
|
5
|
+
copyright the respective contributors, licensed under the Apache License 2.0.
|
|
6
|
+
|
|
7
|
+
Modifications include, but are not limited to:
|
|
8
|
+
- A multilingual (mmBERT-base) backbone fine-tuned for Chinese/English decision tasks.
|
|
9
|
+
- Package and model renamed to "ruhui".
|
|
10
|
+
- Single-checkpoint routing.
|
|
11
|
+
|
|
12
|
+
See LICENSE for the full license text.
|
ruhui-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ruhui
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Ruhui: 非自回归 System 1 决策引擎(中文/多语言),带校准概率。命名取自房谋杜断的杜如晦。
|
|
5
|
+
Author: AnyForge
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/anyforge/ruhui
|
|
8
|
+
Keywords: decision-model,rlcd,calibration,system-one,routing,guardrails,moderation,triage,chinese
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
License-File: NOTICE
|
|
22
|
+
Requires-Dist: torch>=2.0.0
|
|
23
|
+
Requires-Dist: transformers>=4.48.0
|
|
24
|
+
Requires-Dist: safetensors>=0.4.0
|
|
25
|
+
Requires-Dist: huggingface_hub>=0.20.0
|
|
26
|
+
Requires-Dist: numpy>=1.20.0
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# Ruhui · 如晦
|
|
30
|
+
|
|
31
|
+
**A non-autoregressive System 1 decision engine for Chinese & multilingual text, with calibrated probabilities.**
|
|
32
|
+
|
|
33
|
+
**非自回归 System 1 决策引擎(中文/多语言),带校准概率。**
|
|
34
|
+
|
|
35
|
+
Named after Du Ruhui (杜如晦, courtesy name Keming 克明) of the "Fang Mou Du Duan" (房谋杜断) pair — Fang Xuanling was the strategist, Du Ruhui the decisive judge. *Ruhui* inherits the "decisive" half: a fast System 1 decision maker that generates no text, has nothing to parse, and therefore cannot hallucinate.
|
|
36
|
+
|
|
37
|
+
命名取自「房谋杜断」的杜如晦(字克明),「晦」音近「hui」。房玄龄善谋、杜如晦善断——Ruhui 取「断」之意:System 1 快速决策,不生成文本、无可解析输出、因此无幻觉。
|
|
38
|
+
|
|
39
|
+
Architecture forked from [Laya](https://github.com/NandhaKishorM/laya) (Apache 2.0), with two key changes:
|
|
40
|
+
架构参照 [Laya](https://github.com/NandhaKishorM/laya)(Apache 2.0),在此基础上:
|
|
41
|
+
|
|
42
|
+
- **Chinese/multilingual backbone**: `mmBERT-base` (100+ languages) instead of English-only ModernBERT.
|
|
43
|
+
**中文/多语言底座**:`mmBERT-base`(100+ 语言),而非英语-only 的 ModernBERT。
|
|
44
|
+
- **Bilingual soft-label fine-tuning**: 30+ domain datasets (intent / sentiment / safety / agent decision / tool-calling / …).
|
|
45
|
+
**中英双语软标签微调**:覆盖 30+ 领域数据集(意图/情感/安全/智能体决策/工具调用等)。
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## Architecture · 架构
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
bidirectional encoder (mmBERT-base)
|
|
53
|
+
→ type injection (choice / score / noul)
|
|
54
|
+
→ 2-layer decision head
|
|
55
|
+
→ parallel scoring at [MASK] slots
|
|
56
|
+
→ softmax distribution (calibrated probabilities via strictly-proper-scoring-rule RLCD)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Three decision primitives · 三种决策原语:
|
|
60
|
+
|
|
61
|
+
| Primitive · 原语 | Output · 输出 |
|
|
62
|
+
|---|---|
|
|
63
|
+
| **choice** | top label + full probability distribution + confidence |
|
|
64
|
+
| **score** | expected level on an ordinal rubric |
|
|
65
|
+
| **noul** | calibrated P(true) |
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## Installation · 安装
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
pip install ruhui
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Python 3.10+. Dependencies: `torch`, `transformers`, `safetensors`, `huggingface_hub`, `numpy`.
|
|
76
|
+
依赖:`torch`、`transformers`、`safetensors`、`huggingface_hub`、`numpy`。
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## Quick Start · 快速开始
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
import ruhui
|
|
84
|
+
|
|
85
|
+
# Load the fine-tuned ruhui checkpoint (from the hub, or a local directory)
|
|
86
|
+
# 加载微调后的 ruhui checkpoint(从仓库拉取,或本地目录路径)
|
|
87
|
+
agent = ruhui.load("anyforge/ruhui")
|
|
88
|
+
|
|
89
|
+
# The Router also accepts a local path (skips hub download):
|
|
90
|
+
# Router 同样支持本地路径(不走仓库下载):
|
|
91
|
+
router = ruhui.Router(model_path="./pretrained/ruhui")
|
|
92
|
+
result = router.predict(state, questions)
|
|
93
|
+
|
|
94
|
+
# Answer multiple structured questions in a single forward pass
|
|
95
|
+
# 一次前向传播回答多个结构化问题
|
|
96
|
+
result = agent.predict(
|
|
97
|
+
{"message": "我被重复扣款了,请退款"},
|
|
98
|
+
{
|
|
99
|
+
"intent": {
|
|
100
|
+
"type": "choice",
|
|
101
|
+
"instructions": "客户想做什么?",
|
|
102
|
+
"criteria": {"refund": "退款", "technical": "技术问题", "billing": "账单咨询"},
|
|
103
|
+
},
|
|
104
|
+
"churn_risk": {
|
|
105
|
+
"type": "noul",
|
|
106
|
+
"instructions": "客户是否威胁要离开?",
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
)
|
|
110
|
+
print(result["answers"])
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
## Fine-Tuning · 微调
|
|
116
|
+
|
|
117
|
+
Fine-tune ruhui on your own domain data (RLCD + soft distillation + temperature calibration).
|
|
118
|
+
在自己的领域数据上微调(RLCD + 软蒸馏 + 温度校准)。
|
|
119
|
+
|
|
120
|
+
1. **Prepare soft-label data** · 准备软标签数据
|
|
121
|
+
`datas/soft_*.jsonl`, each line: `text` + `qtype` + `soft_label.probabilities`.
|
|
122
|
+
每行含 `text` + `qtype` + `soft_label.probabilities`。
|
|
123
|
+
|
|
124
|
+
2. **Build training items** · 转训练 items:
|
|
125
|
+
```bash
|
|
126
|
+
python3 scripts/prepare_train_data.py \
|
|
127
|
+
--model_dir <base_model_dir> \
|
|
128
|
+
--soft_dir <soft_label_dir> \
|
|
129
|
+
--out <train_items.pt>
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
3. **Train** · 训练:
|
|
133
|
+
```bash
|
|
134
|
+
python3 scripts/train.py \
|
|
135
|
+
--model_dir <base_model_dir> \
|
|
136
|
+
--train_items <train_items.pt> \
|
|
137
|
+
--output_dir <output_dir> \
|
|
138
|
+
--epochs 4
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
4. **Inference** · 推理:
|
|
142
|
+
```bash
|
|
143
|
+
python3 scripts/predict.py --model_dir <output_dir> --text "..." \
|
|
144
|
+
--question-type noul --instruction "..."
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## Repository Layout · 目录结构
|
|
150
|
+
|
|
151
|
+
```
|
|
152
|
+
ruhui/
|
|
153
|
+
ruhui/ # package · 包(agent / router / common / presets / …)
|
|
154
|
+
scripts/ # fine-tuning + inference scripts · 微调 + 推理脚本
|
|
155
|
+
prepare_train_data.py
|
|
156
|
+
train.py
|
|
157
|
+
predict.py
|
|
158
|
+
tests/
|
|
159
|
+
pyproject.toml
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## Model Repositories · 模型仓库
|
|
165
|
+
|
|
166
|
+
- Hugging Face: `anyforge/ruhui`
|
|
167
|
+
- ModelScope: `anyforge/ruhui`
|
|
168
|
+
|
|
169
|
+
> Single checkpoint: fine-tuned from `mmBERT-base`, bilingual (Chinese/English). Load it directly with `ruhui.load("anyforge/ruhui")`.
|
|
170
|
+
> 单一 checkpoint:基于 `mmBERT-base` 微调,中英双语。直接 `ruhui.load("anyforge/ruhui")` 加载。
|
|
171
|
+
|
|
172
|
+
## License
|
|
173
|
+
|
|
174
|
+
Apache 2.0 (inherited from Laya). Developed by AnyForge.
|
|
175
|
+
Apache 2.0(参照 Laya)。Developed by AnyForge。
|
ruhui-0.1.0/README.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# Ruhui · 如晦
|
|
2
|
+
|
|
3
|
+
**A non-autoregressive System 1 decision engine for Chinese & multilingual text, with calibrated probabilities.**
|
|
4
|
+
|
|
5
|
+
**非自回归 System 1 决策引擎(中文/多语言),带校准概率。**
|
|
6
|
+
|
|
7
|
+
Named after Du Ruhui (杜如晦, courtesy name Keming 克明) of the "Fang Mou Du Duan" (房谋杜断) pair — Fang Xuanling was the strategist, Du Ruhui the decisive judge. *Ruhui* inherits the "decisive" half: a fast System 1 decision maker that generates no text, has nothing to parse, and therefore cannot hallucinate.
|
|
8
|
+
|
|
9
|
+
命名取自「房谋杜断」的杜如晦(字克明),「晦」音近「hui」。房玄龄善谋、杜如晦善断——Ruhui 取「断」之意:System 1 快速决策,不生成文本、无可解析输出、因此无幻觉。
|
|
10
|
+
|
|
11
|
+
Architecture forked from [Laya](https://github.com/NandhaKishorM/laya) (Apache 2.0), with two key changes:
|
|
12
|
+
架构参照 [Laya](https://github.com/NandhaKishorM/laya)(Apache 2.0),在此基础上:
|
|
13
|
+
|
|
14
|
+
- **Chinese/multilingual backbone**: `mmBERT-base` (100+ languages) instead of English-only ModernBERT.
|
|
15
|
+
**中文/多语言底座**:`mmBERT-base`(100+ 语言),而非英语-only 的 ModernBERT。
|
|
16
|
+
- **Bilingual soft-label fine-tuning**: 30+ domain datasets (intent / sentiment / safety / agent decision / tool-calling / …).
|
|
17
|
+
**中英双语软标签微调**:覆盖 30+ 领域数据集(意图/情感/安全/智能体决策/工具调用等)。
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Architecture · 架构
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
bidirectional encoder (mmBERT-base)
|
|
25
|
+
→ type injection (choice / score / noul)
|
|
26
|
+
→ 2-layer decision head
|
|
27
|
+
→ parallel scoring at [MASK] slots
|
|
28
|
+
→ softmax distribution (calibrated probabilities via strictly-proper-scoring-rule RLCD)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Three decision primitives · 三种决策原语:
|
|
32
|
+
|
|
33
|
+
| Primitive · 原语 | Output · 输出 |
|
|
34
|
+
|---|---|
|
|
35
|
+
| **choice** | top label + full probability distribution + confidence |
|
|
36
|
+
| **score** | expected level on an ordinal rubric |
|
|
37
|
+
| **noul** | calibrated P(true) |
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## Installation · 安装
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install ruhui
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Python 3.10+. Dependencies: `torch`, `transformers`, `safetensors`, `huggingface_hub`, `numpy`.
|
|
48
|
+
依赖:`torch`、`transformers`、`safetensors`、`huggingface_hub`、`numpy`。
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Quick Start · 快速开始
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
import ruhui
|
|
56
|
+
|
|
57
|
+
# Load the fine-tuned ruhui checkpoint (from the hub, or a local directory)
|
|
58
|
+
# 加载微调后的 ruhui checkpoint(从仓库拉取,或本地目录路径)
|
|
59
|
+
agent = ruhui.load("anyforge/ruhui")
|
|
60
|
+
|
|
61
|
+
# The Router also accepts a local path (skips hub download):
|
|
62
|
+
# Router 同样支持本地路径(不走仓库下载):
|
|
63
|
+
router = ruhui.Router(model_path="./pretrained/ruhui")
|
|
64
|
+
result = router.predict(state, questions)
|
|
65
|
+
|
|
66
|
+
# Answer multiple structured questions in a single forward pass
|
|
67
|
+
# 一次前向传播回答多个结构化问题
|
|
68
|
+
result = agent.predict(
|
|
69
|
+
{"message": "我被重复扣款了,请退款"},
|
|
70
|
+
{
|
|
71
|
+
"intent": {
|
|
72
|
+
"type": "choice",
|
|
73
|
+
"instructions": "客户想做什么?",
|
|
74
|
+
"criteria": {"refund": "退款", "technical": "技术问题", "billing": "账单咨询"},
|
|
75
|
+
},
|
|
76
|
+
"churn_risk": {
|
|
77
|
+
"type": "noul",
|
|
78
|
+
"instructions": "客户是否威胁要离开?",
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
)
|
|
82
|
+
print(result["answers"])
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
## Fine-Tuning · 微调
|
|
88
|
+
|
|
89
|
+
Fine-tune ruhui on your own domain data (RLCD + soft distillation + temperature calibration).
|
|
90
|
+
在自己的领域数据上微调(RLCD + 软蒸馏 + 温度校准)。
|
|
91
|
+
|
|
92
|
+
1. **Prepare soft-label data** · 准备软标签数据
|
|
93
|
+
`datas/soft_*.jsonl`, each line: `text` + `qtype` + `soft_label.probabilities`.
|
|
94
|
+
每行含 `text` + `qtype` + `soft_label.probabilities`。
|
|
95
|
+
|
|
96
|
+
2. **Build training items** · 转训练 items:
|
|
97
|
+
```bash
|
|
98
|
+
python3 scripts/prepare_train_data.py \
|
|
99
|
+
--model_dir <base_model_dir> \
|
|
100
|
+
--soft_dir <soft_label_dir> \
|
|
101
|
+
--out <train_items.pt>
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
3. **Train** · 训练:
|
|
105
|
+
```bash
|
|
106
|
+
python3 scripts/train.py \
|
|
107
|
+
--model_dir <base_model_dir> \
|
|
108
|
+
--train_items <train_items.pt> \
|
|
109
|
+
--output_dir <output_dir> \
|
|
110
|
+
--epochs 4
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
4. **Inference** · 推理:
|
|
114
|
+
```bash
|
|
115
|
+
python3 scripts/predict.py --model_dir <output_dir> --text "..." \
|
|
116
|
+
--question-type noul --instruction "..."
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## Repository Layout · 目录结构
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
ruhui/
|
|
125
|
+
ruhui/ # package · 包(agent / router / common / presets / …)
|
|
126
|
+
scripts/ # fine-tuning + inference scripts · 微调 + 推理脚本
|
|
127
|
+
prepare_train_data.py
|
|
128
|
+
train.py
|
|
129
|
+
predict.py
|
|
130
|
+
tests/
|
|
131
|
+
pyproject.toml
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## Model Repositories · 模型仓库
|
|
137
|
+
|
|
138
|
+
- Hugging Face: `anyforge/ruhui`
|
|
139
|
+
- ModelScope: `anyforge/ruhui`
|
|
140
|
+
|
|
141
|
+
> Single checkpoint: fine-tuned from `mmBERT-base`, bilingual (Chinese/English). Load it directly with `ruhui.load("anyforge/ruhui")`.
|
|
142
|
+
> 单一 checkpoint:基于 `mmBERT-base` 微调,中英双语。直接 `ruhui.load("anyforge/ruhui")` 加载。
|
|
143
|
+
|
|
144
|
+
## License
|
|
145
|
+
|
|
146
|
+
Apache 2.0 (inherited from Laya). Developed by AnyForge.
|
|
147
|
+
Apache 2.0(参照 Laya)。Developed by AnyForge。
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "ruhui"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Ruhui: 非自回归 System 1 决策引擎(中文/多语言),带校准概率。命名取自房谋杜断的杜如晦。"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "Apache-2.0" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "AnyForge" }
|
|
14
|
+
]
|
|
15
|
+
keywords = ["decision-model", "rlcd", "calibration", "system-one", "routing", "guardrails", "moderation", "triage", "chinese"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"License :: OSI Approved :: Apache Software License",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.10",
|
|
22
|
+
"Programming Language :: Python :: 3.11",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Programming Language :: Python :: 3.13",
|
|
25
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
26
|
+
]
|
|
27
|
+
dependencies = [
|
|
28
|
+
"torch>=2.0.0",
|
|
29
|
+
"transformers>=4.48.0",
|
|
30
|
+
"safetensors>=0.4.0",
|
|
31
|
+
"huggingface_hub>=0.20.0",
|
|
32
|
+
"numpy>=1.20.0",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[tool.setuptools]
|
|
36
|
+
packages = ["ruhui"]
|
|
37
|
+
|
|
38
|
+
[project.urls]
|
|
39
|
+
Homepage = "https://github.com/anyforge/ruhui"
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""Ruhui: 非自回归 System 1 决策引擎(中文/多语言),带校准概率。
|
|
2
|
+
|
|
3
|
+
参照 Laya 架构 fork,命名取自房谋杜断的杜如晦(字克明),"晦"音近"hui",
|
|
4
|
+
寓"谋断"——System 1 快速决策。
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from .agent import Agent, RLAgent, load
|
|
8
|
+
from .common import (
|
|
9
|
+
QTYPES,
|
|
10
|
+
QTYPE_NAMES,
|
|
11
|
+
confidence_from_probs,
|
|
12
|
+
ece_score,
|
|
13
|
+
proper_reward,
|
|
14
|
+
render_options,
|
|
15
|
+
td_lambda_targets,
|
|
16
|
+
)
|
|
17
|
+
from .email import clean_email_body, email_state
|
|
18
|
+
from .lang import analyse as detect_language
|
|
19
|
+
from .lang import detect_script, is_english
|
|
20
|
+
from .presets import (
|
|
21
|
+
email_questions,
|
|
22
|
+
guard_questions,
|
|
23
|
+
moderation_questions,
|
|
24
|
+
router_questions,
|
|
25
|
+
triage_questions,
|
|
26
|
+
)
|
|
27
|
+
from .router import DEFAULT_MODELS, RouteDecision, Router
|
|
28
|
+
from .shortlist import embed_fn_from_agent, predict_shortlist, shortlist_choice
|
|
29
|
+
|
|
30
|
+
__version__ = "0.1.0"
|
|
31
|
+
__all__ = [
|
|
32
|
+
"Agent",
|
|
33
|
+
"RLAgent",
|
|
34
|
+
"load",
|
|
35
|
+
"Router",
|
|
36
|
+
"RouteDecision",
|
|
37
|
+
"DEFAULT_MODELS",
|
|
38
|
+
"shortlist_choice",
|
|
39
|
+
"predict_shortlist",
|
|
40
|
+
"embed_fn_from_agent",
|
|
41
|
+
"detect_language",
|
|
42
|
+
"detect_script",
|
|
43
|
+
"is_english",
|
|
44
|
+
"clean_email_body",
|
|
45
|
+
"email_questions",
|
|
46
|
+
"email_state",
|
|
47
|
+
"guard_questions",
|
|
48
|
+
"moderation_questions",
|
|
49
|
+
"router_questions",
|
|
50
|
+
"triage_questions",
|
|
51
|
+
"proper_reward",
|
|
52
|
+
"td_lambda_targets",
|
|
53
|
+
"ece_score",
|
|
54
|
+
"confidence_from_probs",
|
|
55
|
+
"render_options",
|
|
56
|
+
"QTYPES",
|
|
57
|
+
"QTYPE_NAMES",
|
|
58
|
+
"__version__",
|
|
59
|
+
]
|