Files
ai_academy/internal/searchutil/image_evidence.go
T
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

157 lines
5.3 KiB
Go

package searchutil
import (
"context"
"encoding/json"
"maps"
"slices"
"strings"
"academy/internal/logger"
"academy/internal/models/imageprep"
"academy/internal/types"
)
// MaxContextImages caps the retrieved images shown to a vision chat model in
// one answer: each costs as much context as a long passage.
const MaxContextImages = 3
// VisionImageLimits are what every vision chat API this build talks to takes:
// PNG, JPEG, WebP and GIF, and Anthropic's 5 MB per image, the smallest of
// the documented limits (OpenAI and Gemini allow 20 MB).
var VisionImageLimits = imageprep.Limits{
MaxBytes: 5_000_000,
MIMETypes: []string{"image/png", "image/jpeg", "image/webp", "image/gif"},
}
// IsImageEvidence reports whether a result rests on an image matched by its
// own vector: an image_vector hit, or the copy that stands in for one.
func IsImageEvidence(r *types.SearchResult) bool {
if r == nil {
return false
}
return len(r.MatchedImages) > 0 || r.ChunkType == string(types.ChunkTypeImageVector) ||
r.Metadata[types.MetadataImageVectorMatch] == "true" ||
r.Metadata[types.MetadataKeptBy] == types.KeptByImageVector
}
// InheritImageEvidence marks kept as resting on an image vector match when
// dropped, a copy de-duplication removed in its favour, did. An image hit and
// its caption hit carry the same text once merged, so either can be the one
// kept, and the image must not go with the dropped one.
func InheritImageEvidence(kept, dropped *types.SearchResult) {
if kept == nil || !IsImageEvidence(dropped) {
return
}
CaptureImageEvidence(kept)
CaptureImageEvidence(dropped)
kept.MatchedImages = slices.Clone(kept.MatchedImages)
for _, image := range dropped.MatchedImages {
if !slices.Contains(kept.MatchedImages, image) {
kept.MatchedImages = append(kept.MatchedImages, image)
}
}
kept.Metadata = maps.Clone(kept.Metadata)
if kept.Metadata == nil {
kept.Metadata = make(map[string]string, 1)
}
kept.Metadata[types.MetadataImageVectorMatch] = "true"
}
// IsKeptOutsideTopK reports a result kept for a reason other than its score,
// which a top-k cut must not drop. See types.MetadataKeptBy.
func IsKeptOutsideTopK(r *types.SearchResult) bool {
return r != nil && r.Metadata[types.MetadataKeptBy] != ""
}
// TopKKeepingKept returns the first k of the ranked results, followed by the
// results kept outside top-k, each group in its input order.
func TopKKeepingKept(results []*types.SearchResult, k int) []*types.SearchResult {
var ranked, kept []*types.SearchResult
for _, r := range results {
if IsKeptOutsideTopK(r) {
kept = append(kept, r)
} else {
ranked = append(ranked, r)
}
}
if k >= 0 && len(ranked) > k {
ranked = ranked[:k]
}
return append(ranked, kept...)
}
// CaptureImageEvidence freezes the original image address while ImageInfo still
// belongs to the image hit. Call it before any merge rewrites ImageInfo.
func CaptureImageEvidence(r *types.SearchResult) {
if r == nil || len(r.MatchedImages) > 0 || r.ChunkType != string(types.ChunkTypeImageVector) {
return
}
// Older stored references may already contain expanded parent images.
// Without a captured identity, guessing their first image is unsafe.
if r.MatchType == types.MatchTypeHistory {
return
}
var infos []types.ImageInfo
if json.Unmarshal([]byte(r.ImageInfo), &infos) != nil || len(infos) == 0 {
return
}
url := strings.TrimSpace(infos[0].URL)
if url == "" {
url = strings.TrimSpace(infos[0].OriginalURL)
}
if url != "" {
r.MatchedImages = []types.MatchedImage{{ChunkID: r.ID, KnowledgeBaseID: r.KnowledgeBaseID, URL: url}}
}
}
// ContextImages reads the images of the image-evidence results for a vision
// chat model: at most maxImages distinct images, in result order, prepared to
// VisionImageLimits, as data URIs. positions are the indices into results the
// images belong to. An image that cannot be read or prepared is skipped; the
// model still has its caption.
func ContextImages(
ctx context.Context, results []*types.SearchResult,
read func(context.Context, *types.SearchResult) ([]byte, error), maxImages int,
) (images []string, positions []int) {
if read == nil || maxImages <= 0 {
return nil, nil
}
seen := make(map[struct{ kb, url string }]bool)
for i, r := range results {
if !IsImageEvidence(r) {
continue
}
CaptureImageEvidence(r)
for _, ref := range r.MatchedImages {
if len(images) >= maxImages {
return images, positions
}
key := struct{ kb, url string }{ref.KnowledgeBaseID, ref.URL}
if ref.URL == "" || seen[key] {
continue
}
seen[key] = true
// Use the matched image's own storage and chunk identity even if a
// duplicate from another document/KB now carries its evidence.
source := *r
source.ID, source.KnowledgeBaseID = ref.ChunkID, ref.KnowledgeBaseID
info, _ := json.Marshal([]types.ImageInfo{{URL: ref.URL}})
source.ImageInfo = string(info)
data, err := read(ctx, &source)
if err != nil {
logger.Warnf(ctx, "[ContextImages] Image of %s unreadable: %v", ref.ChunkID, err)
continue
}
img, err := imageprep.Prepare(data, VisionImageLimits)
if err != nil {
logger.Warnf(ctx, "[ContextImages] Image of %s does not fit a vision model: %v", ref.ChunkID, err)
continue
}
images = append(images, img.DataURI())
positions = append(positions, i)
}
}
return images, positions
}