feat(model,capacity,vision,io): per-persona 路由 + 本地三级(默认关) + L3 分类 + OCR 独立模型 + image-crop 工具(裁决 03.4.1/03.4.3/14.4.1/14.4.4/14.4.5)
- 03.4.1:per-persona 路由载体选 models.json(组内新增 personaModels/personaProviders + 校验 + PersonaRoute()), Router/RuntimeLLM 快照绑定、loop 流式路径按该 turn 的 persona 路由;主步用路由结果、降级步钉自己的模型 - 03.4.3:本地第三级降级链(LLM_LOCAL_*,Configured() 含 Enabled ⇒ **默认关**),warn 标注来源 - 14.4.4:capacity 接入 L3(分类器接口 + TTL 缓存 + 严格 JSON 解析),CAPACITY_L3_ENABLED **默认 false** - 14.4.1:语义修正 —— group.multimodal 才是「主模型能否收图」(env 路径不再臆断),LLM_VISION_MODEL 只配视觉工具; vision/OCR 可各用不同模型;runtime 不再因有图抢路由(vision.Router 是唯一分层权威) - 14.4.5:新增 image-crop 工具(工具数 5→6,策略默认 owner-only),schema 支持按区域裁切 - 组合根:wire_runtime 读 group.Multimodal;capacity 装配拆出 wire_capacity.go(守 500 行) - 复核:`go build`/`go vet`/各包 `go test -count=1` 全绿;**6 条变异反证**先红后还原(含作者自查出的 2 条假阳性变异) - 未落:按 provider 的 thinking 开关(需 openai_encode.go/openai.go 文件面)、persona awakening(需 loader 文件面)
This commit is contained in:
1 parent
63e5b70e3d
commit
fa33c79e11
24 files changed
+1941
-122
No files matched your search
@@ -35,11 +35,16 @@ type OCRAdapter interface {
|
||||
// endpoint(图像作为内容部件内嵌)同时实现 VisionAdapter 与 OCRAdapter。
|
||||
// 它是开箱可用的默认实现;真实的
|
||||
// 云端 vision/OCR 提供方暴露相同协议。
|
||||
//
|
||||
// 两个方向可以跑在同一个 endpoint 的两个模型上(裁决 14.4.1 的「独立视觉
|
||||
// 模型 + OCR 模型」):Describe 用 model(LLM_VISION_MODEL),Extract 用
|
||||
// ocrModel(LLM_OCR_MODEL),未配 OCR 模型时回落到同一个模型。
|
||||
type OpenAICompatibleAdapter struct {
|
||||
cfg config.LLMConfig
|
||||
model string
|
||||
client *http.Client
|
||||
log *slog.Logger
|
||||
cfg config.LLMConfig
|
||||
model string
|
||||
ocrModel string
|
||||
client *http.Client
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
// OpenAIOption 用于定制 OpenAICompatibleAdapter。
|
||||
@@ -72,6 +77,16 @@ func WithLogger(l *slog.Logger) OpenAIOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithOCRModel 覆盖 OCR 方向使用的模型(默认取 cfg.OCRModel,再回落
|
||||
// 到 model)。
|
||||
func WithOCRModel(m string) OpenAIOption {
|
||||
return func(a *OpenAICompatibleAdapter) {
|
||||
if m != "" {
|
||||
a.ocrModel = m
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NewOpenAICompatible 用 cfg 构造一个适配器,未覆盖时使用
|
||||
// cfg.Model。
|
||||
func NewOpenAICompatible(cfg config.LLMConfig, opts ...OpenAIOption) *OpenAICompatibleAdapter {
|
||||
@@ -84,6 +99,13 @@ func NewOpenAICompatible(cfg config.LLMConfig, opts ...OpenAIOption) *OpenAIComp
|
||||
for _, o := range opts {
|
||||
o(a)
|
||||
}
|
||||
if a.ocrModel == "" {
|
||||
// LLM_OCR_MODEL 未配置时 OCR 与视觉共用同一个模型(向后兼容)。
|
||||
a.ocrModel = cfg.OCRModel
|
||||
}
|
||||
if a.ocrModel == "" {
|
||||
a.ocrModel = a.model
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
@@ -92,20 +114,22 @@ func (a *OpenAICompatibleAdapter) Describe(ctx context.Context, img ImageRef, pr
|
||||
if prompt == "" {
|
||||
prompt = "Describe this image in detail."
|
||||
}
|
||||
return a.ask(ctx, img, prompt)
|
||||
return a.ask(ctx, a.model, img, prompt)
|
||||
}
|
||||
|
||||
// Extract 经同一 endpoint 返回图像中发现的文字。
|
||||
// Extract 经同一 endpoint 返回图像中发现的文字;当配置了独立 OCR 模型
|
||||
// (LLM_OCR_MODEL / WithOCRModel)时用它,否则与视觉模型相同。
|
||||
func (a *OpenAICompatibleAdapter) Extract(ctx context.Context, img ImageRef) (string, error) {
|
||||
return a.ask(ctx, img, "Extract all text visible in this image. Return only the text.")
|
||||
return a.ask(ctx, a.ocrModel, img, "Extract all text visible in this image. Return only the text.")
|
||||
}
|
||||
|
||||
// ask 解析图像、构造 chat 请求并返回助手文本。
|
||||
func (a *OpenAICompatibleAdapter) ask(ctx context.Context, img ImageRef, prompt string) (string, error) {
|
||||
// ask 解析图像、构造 chat 请求并返回助手文本。model 是本次请求使用的模型,
|
||||
// 让两个方向能在同一 endpoint 上跑不同模型。
|
||||
func (a *OpenAICompatibleAdapter) ask(ctx context.Context, model string, img ImageRef, prompt string) (string, error) {
|
||||
if strings.TrimSpace(a.cfg.BaseURL) == "" {
|
||||
return "", ErrNotConfigured
|
||||
}
|
||||
if strings.TrimSpace(a.model) == "" {
|
||||
if strings.TrimSpace(model) == "" {
|
||||
return "", ErrNotConfigured
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
@@ -115,7 +139,7 @@ func (a *OpenAICompatibleAdapter) ask(ctx context.Context, img ImageRef, prompt
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
payload, err := json.Marshal(a.request(imageURL, prompt))
|
||||
payload, err := json.Marshal(a.request(model, imageURL, prompt))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("vision: %w: marshal request: %v", ErrUpstream, err)
|
||||
}
|
||||
@@ -187,9 +211,9 @@ type openAIResponseMessage struct {
|
||||
}
|
||||
|
||||
// request 构造图像 + prompt 的线上请求体。
|
||||
func (a *OpenAICompatibleAdapter) request(imageURL, prompt string) openAIVisionRequest {
|
||||
func (a *OpenAICompatibleAdapter) request(model, imageURL, prompt string) openAIVisionRequest {
|
||||
return openAIVisionRequest{
|
||||
Model: a.model,
|
||||
Model: model,
|
||||
Messages: []openAIMessage{
|
||||
{Role: "user", Content: []openAIPart{
|
||||
{Type: "text", Text: prompt},
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package vision
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
|
||||
)
|
||||
|
||||
// modelRecorder is a /chat/completions endpoint that records the requested model,
|
||||
// so a test can pin which model each direction (Describe / Extract) asked for.
|
||||
type modelRecorder struct {
|
||||
mu sync.Mutex
|
||||
models []string
|
||||
}
|
||||
|
||||
func (r *modelRecorder) seen() []string {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return append([]string(nil), r.models...)
|
||||
}
|
||||
|
||||
func newModelRecorderServer(t *testing.T) (*httptest.Server, *modelRecorder) {
|
||||
t.Helper()
|
||||
rec := &modelRecorder{}
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var body struct {
|
||||
Model string `json:"model"`
|
||||
}
|
||||
_ = json.NewDecoder(r.Body).Decode(&body)
|
||||
rec.mu.Lock()
|
||||
rec.models = append(rec.models, body.Model)
|
||||
rec.mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = io.WriteString(w, `{"choices":[{"message":{"content":"ok"}}]}`)
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
return srv, rec
|
||||
}
|
||||
|
||||
// TestOpenAICompatibleAdapter_OCRUsesOwnModel pins the independent OCR model
|
||||
// (裁决 14.4.1 的「独立视觉模型 + OCR 模型」): when LLM_OCR_MODEL is set the OCR
|
||||
// direction sends it, while Describe keeps the vision model.
|
||||
func TestOpenAICompatibleAdapter_OCRUsesOwnModel(t *testing.T) {
|
||||
t.Parallel()
|
||||
srv, rec := newModelRecorderServer(t)
|
||||
ad := NewOpenAICompatible(config.LLMConfig{
|
||||
BaseURL: srv.URL, Model: "vision-main", OCRModel: "ocr-special",
|
||||
})
|
||||
|
||||
if _, err := ad.Describe(context.Background(), ImageRef{Source: SourceBase64, Data: "aGVsbG8="}, ""); err != nil {
|
||||
t.Fatalf("Describe: %v", err)
|
||||
}
|
||||
if _, err := ad.Extract(context.Background(), ImageRef{Source: SourceBase64, Data: "aGVsbG8="}); err != nil {
|
||||
t.Fatalf("Extract: %v", err)
|
||||
}
|
||||
seen := rec.seen()
|
||||
if len(seen) != 2 || seen[0] != "vision-main" || seen[1] != "ocr-special" {
|
||||
t.Fatalf("models seen = %v, want [vision-main ocr-special]", seen)
|
||||
}
|
||||
}
|
||||
|
||||
// TestOpenAICompatibleAdapter_OCRWithoutOwnModelFallsBack pins the compatibility
|
||||
// edge: without LLM_OCR_MODEL both directions use the vision model.
|
||||
func TestOpenAICompatibleAdapter_OCRWithoutOwnModelFallsBack(t *testing.T) {
|
||||
t.Parallel()
|
||||
srv, rec := newModelRecorderServer(t)
|
||||
ad := NewOpenAICompatible(config.LLMConfig{BaseURL: srv.URL, Model: "vision-main"})
|
||||
|
||||
if _, err := ad.Extract(context.Background(), ImageRef{Source: SourceBase64, Data: "aGVsbG8="}); err != nil {
|
||||
t.Fatalf("Extract: %v", err)
|
||||
}
|
||||
if seen := rec.seen(); len(seen) != 1 || seen[0] != "vision-main" {
|
||||
t.Fatalf("models seen = %v, want [vision-main]", seen)
|
||||
}
|
||||
// WithOCRModel overrides cfg.OCRModel, so an explicit option still wins.
|
||||
ad2 := NewOpenAICompatible(config.LLMConfig{BaseURL: srv.URL, Model: "vision-main", OCRModel: "cfg-ocr"}, WithOCRModel("opt-ocr"))
|
||||
if _, err := ad2.Extract(context.Background(), ImageRef{Source: SourceBase64, Data: "aGVsbG8="}); err != nil {
|
||||
t.Fatalf("Extract: %v", err)
|
||||
}
|
||||
if seen := rec.seen(); len(seen) != 2 || seen[1] != "opt-ocr" {
|
||||
t.Fatalf("models seen = %v, want the explicit option opt-ocr", seen)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,12 @@
|
||||
package vision
|
||||
|
||||
// Assessment 是模型对自己读图结果的置信度。
|
||||
//
|
||||
// Deprecated placeholder(裁决 14.4.1③/14.4.5②):第二层不再是「结构化自评」,
|
||||
// 而是主模型自己按需调用视觉/OCR 工具(design/06 §0 第 2 层)。因此
|
||||
// SelfAssessment 目前没有任何生产者:主模型要复核读图效果时,直接调用
|
||||
// vision/ocr 工具即可,本包不再要求它吐出一个自评字段。保留该类型只为兼容
|
||||
// 既有调用面;新代码不要依赖它。
|
||||
type Assessment string
|
||||
|
||||
const (
|
||||
@@ -49,6 +55,11 @@ type Decision struct {
|
||||
}
|
||||
|
||||
// RouteRequest 携带调用方在模型信息之外给出的路由提示。
|
||||
//
|
||||
// Deprecated(裁决 14.4.5②):生产没有任何调用方填充这两个提示 —— 图像是转成
|
||||
// 文字还是留给主模型,由主模型的能力声明(models.json multimodal)与它自己
|
||||
// 按需调用视觉/OCR 工具决定,不再靠调用方猜「这张图主要是文字」。字段保留
|
||||
// 只为兼容既有测试与调用面。
|
||||
type RouteRequest struct {
|
||||
// TextPrimary 表示用户指出图像以文字为主,因此
|
||||
// 优先使用 OCR 工具而非通用视觉模型。
|
||||
|
||||
Reference in new issue
Block a user