alkaid0-prompt-splitter

module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2026 License: GPL-3.0

README

prompt-splitter

prompt/log/code 三类语义分割器 — 端侧离线、纯 Go、零依赖。
将混合了自然语言、日志、代码的文本无损分割为连续语义区间,F1 ≈ 0.85。

# 一行命令即用
echo "请分析这个错误:ERROR connection refused,代码是 retry(3)" | ./prompt-splitter

安装

go install github.com/cxykevin/prompt-splitter/cmd/prompt-splitter@latest
# 或本地构建
go build ./cmd/prompt-splitter

CLI 用法

交互模式(默认)

彩色终端输出:日志=黄色,代码=青色,prompt=默认色。

# 命令行参数
prompt-splitter "请解释:2026-07-22 ERROR timeout"

# 管道
echo "诊断信息\nWARN disk 91%\ndf -h" | prompt-splitter

# 混合输入
prompt-splitter "这段代码有bug:" 'func main() { return nil }' "请修复"

非终端环境(管道重定向、CI)自动降级为 [label]text 格式。

JSON 模式
# 单条
prompt-splitter --json "ERROR: connection refused"

# JSONL 流
cat input.jsonl | prompt-splitter --json > output.jsonl

输入格式:{"id":"可选","text":"..."}
输出格式:{"id":"...","segments":[{"start":0,"end":N,"label":"prompt"},...]}

自定义模型
prompt-splitter --model-dir ./my-models "text"

需要目录下存在 model-t15.binmodel-t22.binmodel-t30.bin


Go API 用法

import "promptsplitter/splitter"

// 加载内嵌默认模型(需用 go:embed 传入字节)
s, _ := splitter.LoadEnsembleBytes(model15bin, model22bin, model30bin)

// 从文件加载
s, _ := splitter.LoadEnsemble("model-t15.bin", "model-t22.bin", "model-t30.bin")

// 从目录加载
s, _ := splitter.DefaultEnsemble("./data")

// 单模型 + 规则
s := splitter.NewWithLineModel(model)

// 纯规则基线
s := splitter.New()
分割文本
result, err := s.Split(text, splitter.Options{})
// result.Segments → []Segment{{Start:0, End:10, Label:"prompt"}, ...}

// 带诊断
result, err := s.Split(text, splitter.Options{Explain: true})
Segment
type Segment struct {
    Start int    // UTF-8 byte offset, 半开区间 [Start, End)
    End   int
    Label Label  // "prompt" | "log" | "code"
}

保证:连续、无重叠、全覆盖、不切开 rune、合并相邻同标签。

Options
type Options struct {
    ContextBytes int  // 默认 1024,最小 1024
    Explain      bool // 返回规则/回退诊断
}

性能

单核 3 GHz x86_64,GOMAXPROCS=1

输入 耗时 (p95) 内存
64 KB < 100 ms < 10 MB
1 MB < 500 ms < 50 MB
16 MB < 5 s < 300 MB

评测

指标 Dev Test
micro-F1 0.8536 0.8241
code F1 0.8573 0.8239
log F1 0.8940 0.8703
prompt F1 0.8149 0.7833

基于冻结 canonical 数据集,rune 级字符对齐。


架构

输入 → 规则扫描 (fenced/inline/log/stack/code)
     → 阈值模型集成 (thresh 15+22+30 加权投票)
     → 贪心解码 + 置信度加权
     → 区间验证 → 输出 Segments

权重层级:fenced 100 > inline 95 > log_line 90 > stack 85 > ensemble 80 > code_line 70

模型:感知机 + 18-bit 哈希特征(1-5 char n-gram + 结构特征),模型内嵌进二进制。


数据管线

go run ./tools/ingest --input-root tagged.jsonl --output canonical.jsonl
go run ./tools/split-dataset --input canonical.jsonl --output-dir splits/
go run ./tools/train-line-model --train splits/train.jsonl --output model.bin
go run ./tools/evaluate --input splits/test.jsonl --model model.bin
go run ./tools/generate-synthetic --output synthetic.jsonl

License

GNU General Public License v3.0 — see LICENSE.

Directories

Path Synopsis
cmd
prompt-splitter command
prompt-splitter 是 prompt/log/code 三类语义分割器。
prompt-splitter 是 prompt/log/code 三类语义分割器。
internal
tools
evaluate command
generate-synthetic command
generate-synthetic 生成确定性、离线且经过标签验证的辅助训练数据。
generate-synthetic 生成确定性、离线且经过标签验证的辅助训练数据。
ingest command
split-dataset command
train-window command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL