phocinae-server 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.
- phocinae_server-0.1.0/LICENSE +201 -0
- phocinae_server-0.1.0/PKG-INFO +196 -0
- phocinae_server-0.1.0/README.md +180 -0
- phocinae_server-0.1.0/phocinae/__init__.py +8 -0
- phocinae_server-0.1.0/phocinae/engine.py +423 -0
- phocinae_server-0.1.0/phocinae/main.py +20 -0
- phocinae_server-0.1.0/phocinae/model.py +159 -0
- phocinae_server-0.1.0/phocinae/router.py +274 -0
- phocinae_server-0.1.0/phocinae/safetensors_lite.py +54 -0
- phocinae_server-0.1.0/phocinae/server.py +279 -0
- phocinae_server-0.1.0/phocinae/tokenizer.py +223 -0
- phocinae_server-0.1.0/phocinae_server.egg-info/PKG-INFO +196 -0
- phocinae_server-0.1.0/phocinae_server.egg-info/SOURCES.txt +17 -0
- phocinae_server-0.1.0/phocinae_server.egg-info/dependency_links.txt +1 -0
- phocinae_server-0.1.0/phocinae_server.egg-info/requires.txt +6 -0
- phocinae_server-0.1.0/phocinae_server.egg-info/top_level.txt +1 -0
- phocinae_server-0.1.0/pyproject.toml +20 -0
- phocinae_server-0.1.0/setup.cfg +4 -0
- phocinae_server-0.1.0/tests/test_router.py +394 -0
|
@@ -0,0 +1,201 @@
|
|
|
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
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 phocinae-guard authors (Nous Research)
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: phocinae-server
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Self-contained typed-decisions (system-one) inference server for the Phocinae-Largha-150M-v1 model
|
|
5
|
+
License: Apache-2.0
|
|
6
|
+
Keywords: decision-model,typed-decisions,structured-decisions,small-model,cpu-inference
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: fastapi>=0.100
|
|
11
|
+
Requires-Dist: uvicorn>=0.20
|
|
12
|
+
Requires-Dist: torch>=2.1
|
|
13
|
+
Requires-Dist: requests>=2.25
|
|
14
|
+
Provides-Extra: dev
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
|
|
17
|
+
# phocinae-server
|
|
18
|
+
|
|
19
|
+
斑海豹(Phocinae-Largha-150M)本机决策服务:把 1.5 亿参数的决策模型打包成
|
|
20
|
+
一行 `pip install` 级别依赖的 FastAPI 服务,把 `POST /v1/systemone` 契约带给
|
|
21
|
+
任何本机工具。**不提供对外服务**——它监听 127.0.0.1,只服务你自己机器上的
|
|
22
|
+
agent 与工作流。
|
|
23
|
+
|
|
24
|
+
## 定位
|
|
25
|
+
|
|
26
|
+
- 决策模型不是聊天模型:本服务**刻意不实现** chat / Ollama / OpenAI-compat。
|
|
27
|
+
- 推理内核为**纯 torch 自写前向**(运行时零 laya/transformers 依赖),权重直读
|
|
28
|
+
发布的 hf_repo safetensors;与官方推理栈做过数值对齐(见 `dev_tools/verify_model.py`,
|
|
29
|
+
5 组跨题型用例,logits 最大偏差 0)。
|
|
30
|
+
- 支持 noul(是否)/ choice(多选一)/ score(2–10 打分)三种题型,一次请求最多
|
|
31
|
+
64 题、choice 最多 255 选项、上下文预算 8192 token。
|
|
32
|
+
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
git clone https://github.com/Phocinae/phocinae-server.git
|
|
37
|
+
cd phocinae-server
|
|
38
|
+
python -m venv .venv && . .venv/bin/activate
|
|
39
|
+
pip install fastapi uvicorn torch
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
模型权重:设置 `PHOC_MODEL_DIR` 指向斑海豹 hf_repo 目录(含
|
|
43
|
+
`model.safetensors`、`encoder/config.json`、`rl_agent_config.json`、`tokenizer/`)。
|
|
44
|
+
|
|
45
|
+
## 运行
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
PHOC_MODEL_DIR=/path/to/hf_repo python -m phocinae.main
|
|
49
|
+
# 默认 http://127.0.0.1:8155 ,/docs 有交互式文档
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
环境变量:
|
|
53
|
+
|
|
54
|
+
| 变量 | 默认 | 说明 |
|
|
55
|
+
|---|---|---|
|
|
56
|
+
| `PHOC_MODEL_DIR` | (必设) | hf_repo 目录 |
|
|
57
|
+
| `PHOC_HOST` / `PHOC_PORT` | 127.0.0.1 / 8155 | 监听地址 |
|
|
58
|
+
| `PHOC_DEVICE` | auto | auto / cuda / cpu |
|
|
59
|
+
| `PHOC_PERM_AVG` | 0 | 1 = 对 choice 题做 4 种选项排列平均(压翻转) |
|
|
60
|
+
| `PHOC_BEARER_TOKEN` | 空 | 设置后 /v1/* 要求 `Authorization: Bearer <token>` |
|
|
61
|
+
| `PHOC_THREADS` | — | CPU 模式 torch 线程数 |
|
|
62
|
+
| `PHOC_MAX_BODY` | 2MiB | 请求体上限(超限 413) |
|
|
63
|
+
| `PHOC_EXTENSIONS` | 1 | 0 = 不返回 answer_confidence/action/routing 扩展键 |
|
|
64
|
+
|
|
65
|
+
## API
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
GET /health 存活 + 模型/设备信息(无需鉴权)
|
|
69
|
+
GET /v1/models 模型目录
|
|
70
|
+
POST /v1/systemone 单请求判定
|
|
71
|
+
POST /v1/systemone/permute 同左,choice 题选项排列平均
|
|
72
|
+
POST /v1/systemone/batch 批量(≤64 个请求)
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
请求:
|
|
76
|
+
|
|
77
|
+
```json
|
|
78
|
+
{"model": "Phocinae-Largha-150M-v1",
|
|
79
|
+
"state": "agent 刚刚执行了 rm -rf /var/log/app",
|
|
80
|
+
"questions": [
|
|
81
|
+
{"id": "ok", "type": "noul", "threshold": 0.65},
|
|
82
|
+
{"id": "act", "type": "choice",
|
|
83
|
+
"options": ["allow", "ask", "deny"]},
|
|
84
|
+
{"id": "risk", "type": "score"}]}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
响应:`answers` 里 noul=布尔、choice=选项下标、score=2–10 整数;`usage` 给 token
|
|
88
|
+
计数;`answer_confidence`(温度标定的 top 概率)、`action.act_probability`、
|
|
89
|
+
`routing`(设备/后端/排列平均开关)为扩展键,可关闭。
|
|
90
|
+
|
|
91
|
+
## 性能(实测,本机 RTX 5090 / 20 核 CPU)
|
|
92
|
+
|
|
93
|
+
| 路径 | p50 |
|
|
94
|
+
|---|---|
|
|
95
|
+
| GPU fp16 + torch.compile(整请求) | **1.75 ms** |
|
|
96
|
+
| CPU fp32 8 线程(整请求) | 35.97 ms |
|
|
97
|
+
|
|
98
|
+
GPU 前向经 `torch.compile(reduce-overhead, dynamic)` 融合(eager 14.1ms → 编译后
|
|
99
|
+
~2ms);加载时预热常用 batch 形状,首问无形状抖动。
|
|
100
|
+
|
|
101
|
+
## 安全声明
|
|
102
|
+
|
|
103
|
+
- 默认**仅监听 127.0.0.1**,默认无鉴权——不要把它暴露到局域网/公网;需要时用
|
|
104
|
+
`PHOC_BEARER_TOKEN` 加 bearer。
|
|
105
|
+
- 判定结果来自 150M 决策模型,**不是安全审计的完备防护**;用于审批门时必须配合
|
|
106
|
+
确定性规则层(见 phocinae-guard 的 L0 表),并把灰区交给人工。
|
|
107
|
+
- 服务崩溃时应由调用方 fail-closed(本服务不保证任何高可用)。
|
|
108
|
+
|
|
109
|
+
## 兼容性
|
|
110
|
+
|
|
111
|
+
- typesafe-sdk-python:把 base_url 指向本服务即可接入(字段按官方形状对齐)。
|
|
112
|
+
- 降级链:无 GPU 自动回退 CPU(fp32);GPU 显存不足时 torch 侧回退由上层处理。
|
|
113
|
+
|
|
114
|
+
## Router 组件(escalate 升级门 + 工具路由)
|
|
115
|
+
|
|
116
|
+
`phocinae/router.py` 提供两个 harness 决策组件,共用本服务推理后端,支持两种
|
|
117
|
+
接入模式:**进程内 Engine**(零网络、最低延迟)与 **HTTP**(指向
|
|
118
|
+
`http://127.0.0.1:8155`)。
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
from phocinae.engine import Engine
|
|
122
|
+
from phocinae.router import Router
|
|
123
|
+
|
|
124
|
+
router = Router(engine=Engine(MODEL_DIR, device="cpu")) # 进程内
|
|
125
|
+
router = Router(base_url="http://127.0.0.1:8155") # HTTP(另需 requests)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### escalate 升级门(E1,τ=0.6 冻结口径)
|
|
129
|
+
|
|
130
|
+
本地单遍判定一次,`answer_confidence`(温度标定 top 概率,已与 laya canonical
|
|
131
|
+
对齐)低于 τ 的决策升级外部模型/人工:
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
res = router.escalate(
|
|
135
|
+
"The agent was asked to restart the web server. It checked the logs and restarted nginx.",
|
|
136
|
+
[{"id": "ok", "type": "noul"}])
|
|
137
|
+
# 单题 → {"decision": bool/int, "confidence": 0.786, "escalate": False,
|
|
138
|
+
# "tau": 0.6, "gate": "E1-tau0.6", "question_id": "ok", "usage": {...}}
|
|
139
|
+
# 多题 → {"decision": {id:值}, "confidence": {id:float}, "escalate": bool,
|
|
140
|
+
# "escalated_ids": [...], "per_question": [...], "tau", "gate", "usage"}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
**口径(2026-10-07 冻结,实测)**:τ=0.6;E1 typed en 400 例本地 acc 0.789 →
|
|
144
|
+
组合 acc 0.7948(+0.6pp,噪声内无劣化);大模型调用占比 100%→18%,**省费 82%**;
|
|
145
|
+
门控决策 perm 翻转 8.87% ≤ 13% 基线。证据:
|
|
146
|
+
`09_常态探索/release_prep_20261007/08_发布材料_20261007/04_省费评估_20261007.md`。
|
|
147
|
+
τ 缺省为冻结值 0.6,允许按调用覆盖(`tau=` 参数)——**新域须重扫 τ**,勿直接
|
|
148
|
+
沿用。HTTP 模式要求服务端扩展键开启(`PHOC_EXTENSIONS=1`,默认开启)。
|
|
149
|
+
|
|
150
|
+
### 工具路由(k≤10)
|
|
151
|
+
|
|
152
|
+
state + ≤10 个工具描述 → choice 题(options=工具名,附一行描述时渲染为
|
|
153
|
+
`name: description`,banking77 渲染先例)→ 4 排列平均(
|
|
154
|
+
`/v1/systemone/permute` 口径,压选项顺序翻转):
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
res = router.route_tool(
|
|
158
|
+
"The user wants to find the latest news about the product launch.",
|
|
159
|
+
[{"name": "web_search", "description": "search the public web"},
|
|
160
|
+
{"name": "read_file", "description": "read a local file"},
|
|
161
|
+
{"name": "run_command", "description": "run a shell command"},
|
|
162
|
+
{"name": "list_files", "description": "list directory files"},
|
|
163
|
+
{"name": "fetch_url", "description": "fetch a URL"},
|
|
164
|
+
{"name": "ask_user", "description": "ask the human user"}])
|
|
165
|
+
# → {"tool": "web_search", "tool_index": 0, "confidence": 0.5561,
|
|
166
|
+
# "per_option_scores": {"web_search": 0.5561, ...}, # 排列平均后的标定概率
|
|
167
|
+
# "per_question": [{"id":"tool","tool":...,"confidence":...,"escalate":...}],
|
|
168
|
+
# "escalate": bool, "tau": 0.6, "gate": "E1-tau0.6", "usage", "routing"}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
`tools` 支持三种形态:`["name", ...]`、`[{"name", "description"}]`、
|
|
172
|
+
`{"name": "description"}`。工具名须唯一且非空;k>10 直接报错(JevBench
|
|
173
|
+
tool_selection 域界)。口径:k≤10 菜单 JevBench 12/12=1.0(本地原生零升级);
|
|
174
|
+
大菜单(k>10)不接终选,走层级路由或外部分诊。
|
|
175
|
+
|
|
176
|
+
### 服务端配合
|
|
177
|
+
|
|
178
|
+
工具路由的 `per_option_scores` 需要本服务新增的 `option_scores` 扩展键(逐选项
|
|
179
|
+
标定概率向量:choice=请求选项序、noul=[p(false), p(true)]、score=2–10 各档;
|
|
180
|
+
perm 时已按 4 排列平均)。引擎模式与 HTTP 模式输出同构。
|
|
181
|
+
|
|
182
|
+
### 测试
|
|
183
|
+
|
|
184
|
+
```bash
|
|
185
|
+
CUDA_VISIBLE_DEVICES='' PHOC_DEVICE=cpu \
|
|
186
|
+
/home/hermes/decision-model/.venv/bin/python tests/test_router.py
|
|
187
|
+
# 可选 HTTP 对拍(额外拉起一个本机 CPU server 实例):加 PHOC_ROUTER_HTTP_TEST=1
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
覆盖:escalate 门低/高置信样本各若干条(本机 CPU 实测标定 conf)、多题聚合、
|
|
191
|
+
τ 覆盖、6 工具 ×4 场景路由、确定性(两次运行逐位一致)、参数校验(k>10/重名/
|
|
192
|
+
空表)、HTTP 与引擎模式对拍(决策一致、conf 偏差 ≤0.005)。
|
|
193
|
+
|
|
194
|
+
## License
|
|
195
|
+
|
|
196
|
+
Apache-2.0(见 LICENSE)。模型权重另见其发布的模型卡。
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# phocinae-server
|
|
2
|
+
|
|
3
|
+
斑海豹(Phocinae-Largha-150M)本机决策服务:把 1.5 亿参数的决策模型打包成
|
|
4
|
+
一行 `pip install` 级别依赖的 FastAPI 服务,把 `POST /v1/systemone` 契约带给
|
|
5
|
+
任何本机工具。**不提供对外服务**——它监听 127.0.0.1,只服务你自己机器上的
|
|
6
|
+
agent 与工作流。
|
|
7
|
+
|
|
8
|
+
## 定位
|
|
9
|
+
|
|
10
|
+
- 决策模型不是聊天模型:本服务**刻意不实现** chat / Ollama / OpenAI-compat。
|
|
11
|
+
- 推理内核为**纯 torch 自写前向**(运行时零 laya/transformers 依赖),权重直读
|
|
12
|
+
发布的 hf_repo safetensors;与官方推理栈做过数值对齐(见 `dev_tools/verify_model.py`,
|
|
13
|
+
5 组跨题型用例,logits 最大偏差 0)。
|
|
14
|
+
- 支持 noul(是否)/ choice(多选一)/ score(2–10 打分)三种题型,一次请求最多
|
|
15
|
+
64 题、choice 最多 255 选项、上下文预算 8192 token。
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
git clone https://github.com/Phocinae/phocinae-server.git
|
|
21
|
+
cd phocinae-server
|
|
22
|
+
python -m venv .venv && . .venv/bin/activate
|
|
23
|
+
pip install fastapi uvicorn torch
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
模型权重:设置 `PHOC_MODEL_DIR` 指向斑海豹 hf_repo 目录(含
|
|
27
|
+
`model.safetensors`、`encoder/config.json`、`rl_agent_config.json`、`tokenizer/`)。
|
|
28
|
+
|
|
29
|
+
## 运行
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
PHOC_MODEL_DIR=/path/to/hf_repo python -m phocinae.main
|
|
33
|
+
# 默认 http://127.0.0.1:8155 ,/docs 有交互式文档
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
环境变量:
|
|
37
|
+
|
|
38
|
+
| 变量 | 默认 | 说明 |
|
|
39
|
+
|---|---|---|
|
|
40
|
+
| `PHOC_MODEL_DIR` | (必设) | hf_repo 目录 |
|
|
41
|
+
| `PHOC_HOST` / `PHOC_PORT` | 127.0.0.1 / 8155 | 监听地址 |
|
|
42
|
+
| `PHOC_DEVICE` | auto | auto / cuda / cpu |
|
|
43
|
+
| `PHOC_PERM_AVG` | 0 | 1 = 对 choice 题做 4 种选项排列平均(压翻转) |
|
|
44
|
+
| `PHOC_BEARER_TOKEN` | 空 | 设置后 /v1/* 要求 `Authorization: Bearer <token>` |
|
|
45
|
+
| `PHOC_THREADS` | — | CPU 模式 torch 线程数 |
|
|
46
|
+
| `PHOC_MAX_BODY` | 2MiB | 请求体上限(超限 413) |
|
|
47
|
+
| `PHOC_EXTENSIONS` | 1 | 0 = 不返回 answer_confidence/action/routing 扩展键 |
|
|
48
|
+
|
|
49
|
+
## API
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
GET /health 存活 + 模型/设备信息(无需鉴权)
|
|
53
|
+
GET /v1/models 模型目录
|
|
54
|
+
POST /v1/systemone 单请求判定
|
|
55
|
+
POST /v1/systemone/permute 同左,choice 题选项排列平均
|
|
56
|
+
POST /v1/systemone/batch 批量(≤64 个请求)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
请求:
|
|
60
|
+
|
|
61
|
+
```json
|
|
62
|
+
{"model": "Phocinae-Largha-150M-v1",
|
|
63
|
+
"state": "agent 刚刚执行了 rm -rf /var/log/app",
|
|
64
|
+
"questions": [
|
|
65
|
+
{"id": "ok", "type": "noul", "threshold": 0.65},
|
|
66
|
+
{"id": "act", "type": "choice",
|
|
67
|
+
"options": ["allow", "ask", "deny"]},
|
|
68
|
+
{"id": "risk", "type": "score"}]}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
响应:`answers` 里 noul=布尔、choice=选项下标、score=2–10 整数;`usage` 给 token
|
|
72
|
+
计数;`answer_confidence`(温度标定的 top 概率)、`action.act_probability`、
|
|
73
|
+
`routing`(设备/后端/排列平均开关)为扩展键,可关闭。
|
|
74
|
+
|
|
75
|
+
## 性能(实测,本机 RTX 5090 / 20 核 CPU)
|
|
76
|
+
|
|
77
|
+
| 路径 | p50 |
|
|
78
|
+
|---|---|
|
|
79
|
+
| GPU fp16 + torch.compile(整请求) | **1.75 ms** |
|
|
80
|
+
| CPU fp32 8 线程(整请求) | 35.97 ms |
|
|
81
|
+
|
|
82
|
+
GPU 前向经 `torch.compile(reduce-overhead, dynamic)` 融合(eager 14.1ms → 编译后
|
|
83
|
+
~2ms);加载时预热常用 batch 形状,首问无形状抖动。
|
|
84
|
+
|
|
85
|
+
## 安全声明
|
|
86
|
+
|
|
87
|
+
- 默认**仅监听 127.0.0.1**,默认无鉴权——不要把它暴露到局域网/公网;需要时用
|
|
88
|
+
`PHOC_BEARER_TOKEN` 加 bearer。
|
|
89
|
+
- 判定结果来自 150M 决策模型,**不是安全审计的完备防护**;用于审批门时必须配合
|
|
90
|
+
确定性规则层(见 phocinae-guard 的 L0 表),并把灰区交给人工。
|
|
91
|
+
- 服务崩溃时应由调用方 fail-closed(本服务不保证任何高可用)。
|
|
92
|
+
|
|
93
|
+
## 兼容性
|
|
94
|
+
|
|
95
|
+
- typesafe-sdk-python:把 base_url 指向本服务即可接入(字段按官方形状对齐)。
|
|
96
|
+
- 降级链:无 GPU 自动回退 CPU(fp32);GPU 显存不足时 torch 侧回退由上层处理。
|
|
97
|
+
|
|
98
|
+
## Router 组件(escalate 升级门 + 工具路由)
|
|
99
|
+
|
|
100
|
+
`phocinae/router.py` 提供两个 harness 决策组件,共用本服务推理后端,支持两种
|
|
101
|
+
接入模式:**进程内 Engine**(零网络、最低延迟)与 **HTTP**(指向
|
|
102
|
+
`http://127.0.0.1:8155`)。
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
from phocinae.engine import Engine
|
|
106
|
+
from phocinae.router import Router
|
|
107
|
+
|
|
108
|
+
router = Router(engine=Engine(MODEL_DIR, device="cpu")) # 进程内
|
|
109
|
+
router = Router(base_url="http://127.0.0.1:8155") # HTTP(另需 requests)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### escalate 升级门(E1,τ=0.6 冻结口径)
|
|
113
|
+
|
|
114
|
+
本地单遍判定一次,`answer_confidence`(温度标定 top 概率,已与 laya canonical
|
|
115
|
+
对齐)低于 τ 的决策升级外部模型/人工:
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
res = router.escalate(
|
|
119
|
+
"The agent was asked to restart the web server. It checked the logs and restarted nginx.",
|
|
120
|
+
[{"id": "ok", "type": "noul"}])
|
|
121
|
+
# 单题 → {"decision": bool/int, "confidence": 0.786, "escalate": False,
|
|
122
|
+
# "tau": 0.6, "gate": "E1-tau0.6", "question_id": "ok", "usage": {...}}
|
|
123
|
+
# 多题 → {"decision": {id:值}, "confidence": {id:float}, "escalate": bool,
|
|
124
|
+
# "escalated_ids": [...], "per_question": [...], "tau", "gate", "usage"}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
**口径(2026-10-07 冻结,实测)**:τ=0.6;E1 typed en 400 例本地 acc 0.789 →
|
|
128
|
+
组合 acc 0.7948(+0.6pp,噪声内无劣化);大模型调用占比 100%→18%,**省费 82%**;
|
|
129
|
+
门控决策 perm 翻转 8.87% ≤ 13% 基线。证据:
|
|
130
|
+
`09_常态探索/release_prep_20261007/08_发布材料_20261007/04_省费评估_20261007.md`。
|
|
131
|
+
τ 缺省为冻结值 0.6,允许按调用覆盖(`tau=` 参数)——**新域须重扫 τ**,勿直接
|
|
132
|
+
沿用。HTTP 模式要求服务端扩展键开启(`PHOC_EXTENSIONS=1`,默认开启)。
|
|
133
|
+
|
|
134
|
+
### 工具路由(k≤10)
|
|
135
|
+
|
|
136
|
+
state + ≤10 个工具描述 → choice 题(options=工具名,附一行描述时渲染为
|
|
137
|
+
`name: description`,banking77 渲染先例)→ 4 排列平均(
|
|
138
|
+
`/v1/systemone/permute` 口径,压选项顺序翻转):
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
res = router.route_tool(
|
|
142
|
+
"The user wants to find the latest news about the product launch.",
|
|
143
|
+
[{"name": "web_search", "description": "search the public web"},
|
|
144
|
+
{"name": "read_file", "description": "read a local file"},
|
|
145
|
+
{"name": "run_command", "description": "run a shell command"},
|
|
146
|
+
{"name": "list_files", "description": "list directory files"},
|
|
147
|
+
{"name": "fetch_url", "description": "fetch a URL"},
|
|
148
|
+
{"name": "ask_user", "description": "ask the human user"}])
|
|
149
|
+
# → {"tool": "web_search", "tool_index": 0, "confidence": 0.5561,
|
|
150
|
+
# "per_option_scores": {"web_search": 0.5561, ...}, # 排列平均后的标定概率
|
|
151
|
+
# "per_question": [{"id":"tool","tool":...,"confidence":...,"escalate":...}],
|
|
152
|
+
# "escalate": bool, "tau": 0.6, "gate": "E1-tau0.6", "usage", "routing"}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
`tools` 支持三种形态:`["name", ...]`、`[{"name", "description"}]`、
|
|
156
|
+
`{"name": "description"}`。工具名须唯一且非空;k>10 直接报错(JevBench
|
|
157
|
+
tool_selection 域界)。口径:k≤10 菜单 JevBench 12/12=1.0(本地原生零升级);
|
|
158
|
+
大菜单(k>10)不接终选,走层级路由或外部分诊。
|
|
159
|
+
|
|
160
|
+
### 服务端配合
|
|
161
|
+
|
|
162
|
+
工具路由的 `per_option_scores` 需要本服务新增的 `option_scores` 扩展键(逐选项
|
|
163
|
+
标定概率向量:choice=请求选项序、noul=[p(false), p(true)]、score=2–10 各档;
|
|
164
|
+
perm 时已按 4 排列平均)。引擎模式与 HTTP 模式输出同构。
|
|
165
|
+
|
|
166
|
+
### 测试
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
CUDA_VISIBLE_DEVICES='' PHOC_DEVICE=cpu \
|
|
170
|
+
/home/hermes/decision-model/.venv/bin/python tests/test_router.py
|
|
171
|
+
# 可选 HTTP 对拍(额外拉起一个本机 CPU server 实例):加 PHOC_ROUTER_HTTP_TEST=1
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
覆盖:escalate 门低/高置信样本各若干条(本机 CPU 实测标定 conf)、多题聚合、
|
|
175
|
+
τ 覆盖、6 工具 ×4 场景路由、确定性(两次运行逐位一致)、参数校验(k>10/重名/
|
|
176
|
+
空表)、HTTP 与引擎模式对拍(决策一致、conf 偏差 ≤0.005)。
|
|
177
|
+
|
|
178
|
+
## License
|
|
179
|
+
|
|
180
|
+
Apache-2.0(见 LICENSE)。模型权重另见其发布的模型卡。
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""phocinae-server: self-contained typed-decisions (system-one) inference server.
|
|
2
|
+
|
|
3
|
+
Zero runtime dependencies beyond fastapi + uvicorn + torch:
|
|
4
|
+
tokenizer, safetensors loading, mmBERT encoder and decision head are all
|
|
5
|
+
implemented here in pure python/torch (no laya, no transformers, no tokenizers).
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
__version__ = "0.1.0"
|