按图、按文字搜索图库,并找出近重复图片
用 dinov3 和 siglip2 为图库生成向量,存进可增量更新的本地小索引,回答三个问题:哪些图片与这张图相似,哪些图片符合这句描述,哪些图片互为近重复。smoke 数据集在免费层上运行,不需要密钥,也不需要注册。
Built with DINOv3. dinov3 的输出受 DINOv3 License 约束;基于它构建的产品应显示标签 “Built with DINOv3”。
模型: dinov3, siglip2 · 无需密钥: 冒烟运行 · 语言: Python, Go · 在 Playground 中试用 (在新标签页打开) · GitHub 上的源码 (在新标签页打开)
一个照片库有几百到几万张图片,每天增加少量新图,使用者记得的是照片的样子或内容,而不是文件名。需要三种能力:
- 找出与给定图片相似的图片(以图搜图);
- 找出与一句描述相符的图片(文字搜图);
- 找出同一张图的副本:缩放、裁剪、重新压缩、翻转或加水印后的版本(近重复)。
决定代码形状的是调用周围的约束,而不是模型本身:
- 成本。 每个模型每张图 0.05 credits,建库一次每张图 0.10 credits。每次变动都重新嵌入整个图库会成倍放大这笔费用,因此索引必须增量重建:只嵌入新增或改动的文件,按文件内容的 sha256 判断。
- 限额。 免费层按分钟和按天限制每个项目,每张图算一次调用。几百张图的图库可能撞上限额,而且 Cookbook 的共享项目还有其他读者在用,所以运行遇到
429要等待,达到每日上限时必须干净地停下、稍后接着做,而不是做到一半失败。 - 失败。 批处理可能丢失某一项、过期或断开连接。重跑不得重复嵌入,也不得重复计费。
- 规模。 这个量级下向量放得进内存,精确检索只是一次矩阵乘法。不需要向量数据库,也没有引入。
图片文件夹 | 计算每个文件的 sha256,与索引中的目录比对 v 仅新增或改动的图片 | 有密钥时:POST /v1/images(免费),再 POST /v1/batch/embeddings | 免费层:每张图一次调用,图片内联 | 每块 50 张,一次一个模型,先 dinov3 后 siglip2 v 索引目录:index.json + vectors-dinov3-N.f32 + vectors-siglip2-N.f32 | 每块之后提交(原子写入),到每日上限时干净地停止 v 查询:除查询文字或不在图库中的查询图片外,全部在本地完成 图片 -> dinov3 向量 -> 精确余弦 top-k 文字 -> POST /v1/embeddings(siglip2 文本)-> 对 siglip2 图片向量做精确余弦 top-k 近重复 -> dinov3 向量,取超过阈值的图片对,连通分量(并查集)- 使用密钥时,
POST /v1/images:每个新文件上传一次,得到句柄。免费。免费层没有上传:每张图以 base64 内联发送(解码后最大 4 MiB)。 - 使用密钥时,分别用
dinov3和siglip2调用POST /v1/batch/embeddings:每个请求最多 1,000 项,此处用 50。一个批处理占用一个每分钟限速令牌,但服务端逐项依次执行。免费层没有批处理端点,所以每张图、每个模型各是一次POST /v1/embeddings调用。 - 每一块之后写入
index.json和向量文件。向量已做 L2 归一化,点积就是余弦相似度。 - 文字查询是一次
siglip2的POST /v1/embeddings调用(每条文本 0.01 credits,每次最多 64 条),与图片的siglip2向量比较,因为图片与文字共用一个空间。图片查询在图片已在图库中时使用已存的dinov3向量,否则调用一次。 - 近重复不需要调用:直接从已存的
dinov3向量中取出超过阈值的图片对。
各语言的安装与运行
以下命令针对冒烟数据集,在仓库根目录执行。冒烟数据集不需要 API 密钥,在免费层上运行;完整数据集需要密钥,设置上限后,客户端会拒绝可能超出上限的调用。
环境要求: Python 3.12, uv 0.5
cd use-cases/05-image-library-search/pythonuv sync --frozenuv run --frozen python main.py --manifest ../assets/smoke/manifest.json --out out
# 完整数据集: 完整数据集是仓库的发布附件,需要 API 密钥;解压后传入其 manifest。export FOLDBUN_API_KEY="fbs_..."export FOLDBUN_MAX_CREDITS=51uv run --frozen python main.py --manifest ../assets/full.json --out out环境要求: Go 1.22
cd use-cases/05-image-library-search/gogo mod downloadgo run . --manifest ../assets/smoke/manifest.json --out out
# 完整数据集: 完整数据集是仓库的发布附件,需要 API 密钥;解压后传入其 manifest。export FOLDBUN_API_KEY="fbs_..."export FOLDBUN_MAX_CREDITS=51go run . --manifest ../assets/full.json --out out# 无需密钥:没有设置 FOLDBUN_API_KEY 时由免费层处理调用;设置后按标价扣费。cd use-cases/05-image-library-search/python
uv run imgsearch.py index ~/photos # 嵌入新增和改动的文件;再运行一次即可续做uv run imgsearch.py image ~/photos/2026/beach.jpg # 与这张图相似的图片uv run imgsearch.py text "a photo of a dog on a beach" # 符合一句描述的图片uv run imgsearch.py duplicates --threshold 0.90 # 近重复分组uv run imgsearch.py status # 索引中有什么
uv run main.py # smoke 数据集,写出 out/result.json(harness 运行的就是它)index 支持 --batch-size。服务端返回 429 时,它按 Retry-After 等待后继续。项目的每日上限用完时,它会打印还缺多少向量、调用数何时恢复,并以退出码 1 结束;到时重新运行同一条命令,索引会从停下的地方接着做。退出码列在 imgsearch.py 的文件头。
新建索引后的第一次查询会在最后一行显示费用,例如 cost: 0.01 credits at list price, 0 charged, in 1 calls。
同一套代码由清单驱动时就是 harness 运行的程序(main.py --manifest M --out DIR)。它还会写出 DIR/index/(索引)、DIR/stats.json(耗时、嵌入了什么、运行是否在某个限额处停下、第二次建库的结果,该次必须不发出任何调用)以及符合 contract/result.schema.json 的 DIR/result.json:每张图库图片一项,每条文字查询一项,各自带有自己那几次调用的账本行。
完整数据集(400 张图片、100 个近重复变体、40 条文字查询,73 MB)是仓库的发布附件(见 datasets/README.md)。其清单已提交为 assets/full.json,图片和标准答案在压缩包里。把压缩包解压到 assets/,再在仓库根目录对参考实现运行完整数据集。完整运行需要 FOLDBUN_API_KEY(没有密钥时以退出码 2 结束):它规模大,而且用到上传和批处理。一次运行按标价最多 51 credits(500 张图片 x 0.10 加 40 条文本 x 0.01,合计 50.4),约 1,040 次调用。
tar xzf 05-image-library-search-full.tar.gz -C use-cases/05-image-library-search/assetsexport FOLDBUN_API_KEY="fbs_..."uv run python -m harness live --size full --uc 05 --max-credits 51 --dry-run # 只打印计划,不发送请求uv run python -m harness refresh --size full --uc 05 --max-credits 51 # 运行、打分,写入 expected/full.json 与 expected/full-summary.json,之后执行 `uv run python -m harness snippets`harness live 运行方式相同,只按 usecase.toml 的 [quality] 下限检查结果;指标即 evaluate.py --result out/result.json --truth assets/truth.json 的输出。
Go 实现是同一条管线的单个命令:cd use-cases/05-image-library-search/go && go run . --manifest ../assets/smoke/manifest.json --out out。它只用标准库,读写的索引格式与 Python 相同,因此一种语言建好的索引可以用 --resume 交给另一种语言继续。Go 没有对应 imgsearch.py 的部分(针对文件夹的命令只有 Python 版),并且在 Go 中 --limit 0 表示全部条目。
没人等结果时用异步作业
Section titled “没人等结果时用异步作业”给图库建索引是没人守着等结果的活。有密钥时加上 --async,每个模型提交一个异步作业(POST /v1/jobs),不再走批处理:图片照常先上传,作业在空闲算力上运行,价格是标价的十分之一,代码每 30 秒(--poll-seconds)轮询一次,读出结果并写入向量。
cd use-cases/05-image-library-search/pythonexport FOLDBUN_API_KEY="fbs_..."uv run imgsearch.py index ~/photos --async # 文件夹版本uv run main.py --manifest ../assets/full.json --out out --async # 完整数据集(先解压,见下文)cd ../go && go run . --manifest ../assets/smoke/manifest.json --out out --async # Go 里同样如此- 价格。 异步条目只要十分之一:完整数据集的 500 张图片用批处理约 50 credits,用异步作业约 5 credits(500 x 0.10 x 0.1)。文字查询仍然照原样发送,所以一次完整运行约 5.4 credits,而不是 50.4。
- 不承诺什么时候完成。 作业在有空闲算力时才运行,可能要几分钟,也可能要几个小时,所以
--async适合在后台建库,不适合有用户在等的场景。24 小时内还没开始的条目会过期且不扣费,已完成作业的结果保留 7 天。过期或失败的条目在索引里仍然缺着;再运行同一条命令,它们会作为新作业提交。运行中断后在 24 小时内重新运行:作业会按幂等键找到,不会重复提交。 - 需要密钥。 异步作业不属于免费层:没有
FOLDBUN_API_KEY时,--async会打印FOLDBUN_API_KEY is not set, and this needs a key: async jobs are not part of the free tier.并以退出码 2 结束。
客户端文件(stack_client.py、stackclient/client.go)是 clients/ 中对应文件的副本(遵循服务端 429 和限速响应头的重试与等待、带密钥调用的幂等键、额度上限和账本)。以下是本用例新增的部分。
遍历图库的循环。 缺少向量的图片被切成块,依次交给每个模型嵌入。两个模型逐块交替,这样运行被中途打断时,图库的前一部分两种向量都已齐备。
源码: python/embedder.py (在新标签页打开)
def index_library( client, index: LibraryIndex, root: Path, models=("dinov3", "siglip2"), *, batch_size=50, workers=4, log=print, async_mode=False, poll_s=POLL_SECONDS, sleep=time.sleep,) -> Report: """Embed every library image that is missing from the index, with every model, a chunk at a time (or as async jobs).""" check_free_size(client, len(index.items)) report = Report() paths = {it["sha256"]: root / it["path"] for it in index.items} if async_mode: run_async(client, index, models, paths, report, workers=workers, poll_s=poll_s, log=log, sleep=sleep) return report for model in models: pending = index.missing(model) # sha256 of the images that lack this model's vector, in catalogue order log(f"{len(index.items)} images in the library, {len(pending)} to embed with {model}") for start in range(0, len(pending), batch_size): if report.stopped: return report chunk = {sha: paths[sha] for sha in pending[start : start + batch_size]} embed_chunk(client, index, model, chunk, report, workers=workers) log(f" {min(start + batch_size, len(pending))}/{len(pending)}") return report// indexLibrary embeds every library image that is missing from the index, with every model, a chunk at a time.func indexLibrary(client *stackclient.Client, index *LibraryIndex, root string, models []string, batchSize, workers int, log func(string)) (*Report, error) { if err := checkFreeSize(client, len(index.Items)); err != nil { return nil, err } report := newReport() paths := map[string]string{} for _, it := range index.Items { paths[it.Sha256] = filepath.Join(root, it.Path) } for _, model := range models { pending := index.Missing(model) // sha256 of the images that lack this model's vector, in catalogue order log(fmt.Sprintf("%d images in the library, %d to embed with %s", len(index.Items), len(pending), model)) for start := 0; start < len(pending); start += batchSize { if report.Stopped != nil { return report, nil } chunk := pending[start:min(start+batchSize, len(pending))] if err := embedChunk(client, index, model, chunk, paths, report, workers); err != nil { return report, err } log(fmt.Sprintf(" %d/%d", min(start+batchSize, len(pending)), len(pending))) } } return report, nil}一个批处理。 流中的每一项到达时即处理。一项的账本行就是该批处理标签下的最后一行。因可能恢复的原因失败的项(image_not_found、model_busy、过期项)进入下一次尝试;没有 summary 就结束的流会留下一些没有结果的项,这样的项可能已经生成并扣费,因此不把它放进新批处理,而是用同样的请求体和同一个幂等键把同一个批处理再发一遍(服务端重放已完成的项,其余的照常运行);重复几次后仍没有结果的项记为 outcome_unknown,不会用新键重发;达到每日上限或额度上限则停止运行。批处理的幂等键由内容派生,因此重跑被中断的一块时,已完成的项会被重放,不再计费。
源码: python/embedder.py (在新标签页打开)
def _pause(seconds): time.sleep(seconds)
def run_batch(client, model, shas, handles, attempt, report): """One batch request (needs a key). Returns ([(sha256, vector)] that succeeded, [sha256] to send again).
A stream can end early (a dropped connection, a timeout) without a summary event. An image that got no outcome may already have been embedded and charged on the server, so it is never sent under a new key: the same batch is sent again with the same body and the same idempotency key, which replays what finished (no charge) and runs the rest. Only images the server reported as failed or expired, which are not charged, go into the next attempt.""" tag = f"{model}:{shas[0][:12]}:{attempt}" key = _key(model, handles, attempt) body = {"model": model, "items": [{"image": sc.image_handle(h)} for h in handles]} vectors, retry, failed = {}, set(), {} # by sha256; an image in none of them has no outcome yet for send in range(1 + RECOVERY_SENDS): try: with closing(client.batch("embeddings", body, key, tag=tag)) as events: for event, data in events: if event != "item": continue # progress heartbeats; the summary ends the stream sha = shas[data["index"]] if sha in vectors or sha in failed: continue # a replay repeats the items we already hold if data["status"] == "ok": vectors[sha] = data["result"]["embeddings"][0]["vector"] retry.discard(sha) report.rows.setdefault(sha, []).append(client.calls(tag)[-1]) # the ledger row of the item that just arrived elif data["status"] == "expired" or data["error"]["code"] in RETRY_CODES: retry.add(sha) # not billed; goes into the next attempt (an evicted handle is uploaded again) elif data["error"]["code"] in STOP_CODES: report.stopped = {**data["error"], "model": model} break # closing the stream stops the batch; the items that already finished are kept else: failed[sha] = data["error"] # for example an image the API cannot read: no retry helps except sc.StackError as err: if err.code in STOP_CODES: # refused before the stream started, or by the credit cap report.stopped = {"code": err.code, "message": err.message, "model": model} elif not err.retryable: raise if report.stopped or all(s in vectors or s in retry or s in failed for s in shas): break _pause(min(2**send, 10)) report.failed.update(failed) if not report.stopped: for sha in shas: if sha not in vectors and sha not in retry and sha not in failed: report.failed[sha] = { "code": "outcome_unknown", "message": "the stream ended before this image's outcome arrived, and every repeat under the same key failed too; " "it was not sent under a new key because it may already be charged", } return [(s, vectors[s]) for s in shas if s in vectors], [s for s in shas if s in retry]
def run_singles(client, model, paths, shas, report, workers): """The free tier: one `/v1/embeddings` call per image, the bytes inline as base64 (at most 4 MiB). Same return value as run_batch."""
def one(sha): if report.stopped: return sha, "retry", None # another call already hit a limit: send nothing more tag = f"{model}:{sha[:12]}" body = {"model": model, "image": sc.image_base64(paths[sha].read_bytes())} try: result = client.post("/v1/embeddings", body, tag=tag) # the client waits and retries 429 by itself except sc.MissingApiKey: raise except sc.StackError as err: if err.code == "model_locked": raise kind = "stop" if err.code in STOP_CODES else "retry" if err.code in RETRY_CODES else "fail" return sha, kind, {"code": err.code, "message": err.message} return sha, "ok", (result["embeddings"][0]["vector"], client.calls(tag)[-1])
done, retry = [], [] with ThreadPoolExecutor(workers) as pool: for sha, kind, value in pool.map(one, shas): if kind == "ok": done.append((sha, value[0])) report.rows.setdefault(sha, []).append(value[1]) elif kind == "retry": retry.append(sha) elif kind == "stop": report.stopped = report.stopped or {**value, "model": model} else: report.failed[sha] = value return done, retry
def embed_chunk(client, index: LibraryIndex, model: str, paths: dict[str, Path], report: Report, *, workers=4) -> None: """Embed the images `paths` (sha256 -> file) with `model`, commit them, and record the outcome in `report`.""" todo = list(paths) for attempt in range(ATTEMPTS): if not todo or report.stopped: break if client.free: done, retry = run_singles(client, model, paths, todo, report, workers) else: with ThreadPoolExecutor(workers) as pool: handles = list(pool.map(lambda sha: _upload(client, paths[sha]), todo)) # free of charge; also re-uploads evicted images done, retry = run_batch(client, model, todo, handles, attempt, report) if done: index.add(model, [s for s, _ in done], [v for _, v in done]) index.commit() report.embedded += len(done) todo = retry for sha in [] if report.stopped else todo: report.failed.setdefault(sha, {"code": "gave_up", "message": f"not embedded after {ATTEMPTS} attempts"})// runBatch sends one batch request (needs a key). It returns the embeddings that succeeded and the sha256 to send again.//// A stream can end early (a dropped connection, a timeout) without a summary event. An image that got no outcome may// already have been embedded and charged on the server, so it is never sent under a new key: the same batch is sent// again with the same body and the same idempotency key, which replays what finished (no charge) and runs the rest.// Only images the server reported as failed or expired, which are not charged, go into the next attempt.func runBatch(client *stackclient.Client, model string, shas, handles []string, attempt int, report *Report) ([]embedding, []string, error) { tag := fmt.Sprintf("%s:%s:%d", model, shas[0][:12], attempt) key := batchKey(model, handles, attempt) items := make([]map[string]any, len(handles)) for i, h := range handles { items[i] = map[string]any{"image": stackclient.ImageHandle(h)} } body := map[string]any{"model": model, "items": items} vectors := map[string][]float32{} // by sha256; an image in none of vectors, retry and failed has no outcome yet retry := map[string]bool{} failed := map[string]ErrInfo{} for send := 0; send <= recoverySends; send++ { err := client.Batch("embeddings", body, stackclient.Opts{Key: key, Tag: tag}, func(ev stackclient.Event) error { if ev.Name != "item" { return nil // progress heartbeats; the summary ends the stream } var data batchItem if err := json.Unmarshal(ev.Data, &data); err != nil { return err } sha := shas[data.Index] if _, held := vectors[sha]; held { return nil // a replay repeats the items we already hold } if _, bad := failed[sha]; bad { return nil } switch { case data.Status == "ok": if len(data.Result.Embeddings) == 0 { return errors.New("batch item without an embedding") } vectors[sha] = data.Result.Embeddings[0].Vector delete(retry, sha) rows := client.Calls(tag) report.Rows[sha] = append(report.Rows[sha], rows[len(rows)-1]) // the ledger row of the item that just arrived case data.Status == "expired" || (data.Error != nil && retryCodes[data.Error.Code]): retry[sha] = true // not billed; goes into the next attempt (an evicted handle is uploaded again) case data.Error != nil && stopCodes[data.Error.Code]: report.Stopped = &Stopped{data.Error.Code, data.Error.Message, model} return stackclient.ErrStop // ending the stream stops the batch; the items that already finished are kept case data.Error != nil: failed[sha] = *data.Error // for example an image the API cannot read: no retry helps default: failed[sha] = ErrInfo{"failed", "batch item " + data.Status} } return nil }) var apiErr *stackclient.Error if errors.As(err, &apiErr) { if stopCodes[apiErr.Code] { // refused before the stream started, or by the credit cap report.Stopped = &Stopped{apiErr.Code, apiErr.Message, model} } else if !apiErr.Retryable { return nil, nil, err } } else if err != nil { return nil, nil, err } if report.Stopped != nil || allDecided(shas, vectors, retry, failed) { break } pause(min(time.Duration(1<<send)*time.Second, 10*time.Second)) } for sha, e := range failed { report.Failed[sha] = e } var done []embedding var again []string for _, sha := range shas { _, ok := vectors[sha] _, bad := failed[sha] switch { case ok: done = append(done, embedding{sha, vectors[sha]}) case retry[sha]: again = append(again, sha) case !bad && report.Stopped == nil: report.Failed[sha] = ErrInfo{"outcome_unknown", "the stream ended before this image's outcome arrived, and every repeat under the same key failed too; it was not sent under a new key because it may already be charged"} } } return done, again, nil}
func allDecided(shas []string, vectors map[string][]float32, retry map[string]bool, failed map[string]ErrInfo) bool { for _, s := range shas { _, ok := vectors[s] _, bad := failed[s] if !ok && !bad && !retry[s] { return false } } return true}
// pause waits between two sends of the same batch; tests replace it.var pause = time.Sleep
// runSingles is the free tier: one /v1/embeddings call per image, the bytes inline as base64 (at most 4 MiB), at most// workers calls at a time. It returns what runBatch returns.func runSingles(client *stackclient.Client, model string, shas []string, paths map[string]string, report *Report, workers int) ([]embedding, []string, error) { type outcome struct { kind string // ok, retry, stop or fail vector []float32 row stackclient.Row info ErrInfo err error // anything but an API error, or an API error that ends the run } outcomes, stopped := make([]outcome, len(shas)), make(chan struct{}) var once sync.Once slots, wg := make(chan struct{}, max(workers, 1)), sync.WaitGroup{} for i, sha := range shas { wg.Add(1) slots <- struct{}{} go func() { defer func() { <-slots; wg.Done() }() select { case <-stopped: outcomes[i].kind = "retry" // another call already hit a limit: send nothing more return default: } data, err := os.ReadFile(paths[sha]) if err != nil { outcomes[i].err = err return } tag := model + ":" + sha[:12] body := map[string]any{"model": model, "image": stackclient.ImageBase64(data)} raw, err := client.Post("/v1/embeddings", body, stackclient.Opts{Tag: tag}) // the client waits and retries 429 by itself var apiErr *stackclient.Error switch { case errors.As(err, &apiErr) && apiErr.Code != "model_locked" && apiErr.Code != "missing_api_key": kind := "fail" if stopCodes[apiErr.Code] { kind = "stop" once.Do(func() { close(stopped) }) } else if retryCodes[apiErr.Code] { kind = "retry" } outcomes[i].kind, outcomes[i].info = kind, ErrInfo{apiErr.Code, apiErr.Message} case err != nil: outcomes[i].err = err default: var result struct { Embeddings []struct { Vector []float32 `json:"vector"` } `json:"embeddings"` } if err := json.Unmarshal(raw, &result); err != nil || len(result.Embeddings) == 0 { outcomes[i].err = errors.New("embeddings response without a vector") return } rows := client.Calls(tag) outcomes[i].kind, outcomes[i].vector, outcomes[i].row = "ok", result.Embeddings[0].Vector, rows[len(rows)-1] } }() } wg.Wait() var done []embedding var retry []string for i, sha := range shas { o := outcomes[i] switch { case o.err != nil: return nil, nil, o.err case o.kind == "ok": done = append(done, embedding{sha, o.vector}) report.Rows[sha] = append(report.Rows[sha], o.row) case o.kind == "retry": retry = append(retry, sha) case o.kind == "stop": if report.Stopped == nil { report.Stopped = &Stopped{o.info.Code, o.info.Message, model} } default: report.Failed[sha] = o.info } } return done, retry, nil}
// embedChunk embeds the images paths (sha256 -> file; order gives the batch order) with model, commits them, and// records the outcome in report.func embedChunk(client *stackclient.Client, index *LibraryIndex, model string, order []string, paths map[string]string, report *Report, workers int) error { todo := order for attempt := 0; attempt < attempts; attempt++ { if len(todo) == 0 || report.Stopped != nil { break } var done []embedding var retry []string var err error if client.Free() { done, retry, err = runSingles(client, model, todo, paths, report, workers) } else { var handles []string if handles, err = uploadAll(client, todo, paths, workers); err == nil { // free of charge; also uploads again what was evicted done, retry, err = runBatch(client, model, todo, handles, attempt, report) } } if err != nil { return err } if len(done) > 0 { shas, vectors := make([]string, len(done)), make([][]float32, len(done)) for i, d := range done { shas[i], vectors[i] = d.sha, d.vector } if err := index.Add(model, shas, vectors); err != nil { return err } if err := index.Commit(); err != nil { return err } report.Embedded += len(done) } todo = retry } if report.Stopped == nil { for _, sha := range todo { if _, ok := report.Failed[sha]; !ok { report.Failed[sha] = ErrInfo{"gave_up", fmt.Sprintf("not embedded after %d attempts", attempts)} } } } return nil}
// uploadAll uploads the files of shas with at most workers uploads at a time and returns the handles in the same order.func uploadAll(client *stackclient.Client, shas []string, paths map[string]string, workers int) ([]string, error) { handles, errs := make([]string, len(shas)), make([]error, len(shas)) slots, wg := make(chan struct{}, max(workers, 1)), sync.WaitGroup{} for i, sha := range shas { wg.Add(1) slots <- struct{}{} go func() { defer func() { <-slots; wg.Done() }() handles[i], errs[i] = uploadFile(client, paths[sha]) }() } wg.Wait() return handles, errors.Join(errs...)}磁盘上的索引。 向量文件从不原地修改。提交时先写新文件,再原子地替换 index.json,最后删除不再被引用的文件。崩溃后留下的是旧索引或新索引。
def commit(self) -> None: """Write what changed. Call it after every batch, so that a stop or a crash loses at most one batch.""" self.dir.mkdir(parents=True, exist_ok=True) keep = {it["sha256"] for it in self.items} self.generation += 1 for model in sorted(self._dirty): rows = self._rows[model] order = [sha for sha, _ in sorted(rows.items(), key=lambda kv: kv[1]) if sha in keep] matrix = self._vectors[model][[rows[sha] for sha in order]] if order else self._vectors[model][:0] self._vectors[model], self._rows[model] = matrix, {sha: i for i, sha in enumerate(order)} name = f"vectors-{model}-{self.generation}.f32" atomic_write(self.dir / name, np.ascontiguousarray(matrix, dtype="<f4").tobytes()) self.models[model].update(file=name, sha256s=order) self._dirty.clear() meta = {"format": FORMAT, "generation": self.generation, "items": self.items, "models": self.models} atomic_write(self.dir / "index.json", json.dumps(meta, indent=1).encode()) live = {info["file"] for info in self.models.values()} for stale in self.dir.glob("vectors-*.f32"): if stale.name not in live: stale.unlink()// Commit writes what changed. Call it after every batch, so that a stop or a crash loses at most one batch.func (x *LibraryIndex) Commit() error { if err := os.MkdirAll(x.Dir, 0o755); err != nil { return err } keep := map[string]bool{} for _, it := range x.Items { keep[it.Sha256] = true } x.Generation++ dirty := make([]string, 0, len(x.dirty)) for model := range x.dirty { dirty = append(dirty, model) } sort.Strings(dirty) for _, model := range dirty { rows, dims := x.rows[model], x.Models[model].Dimensions order := make([]string, 0, len(rows)) for sha := range rows { if keep[sha] { order = append(order, sha) } } sort.Slice(order, func(i, j int) bool { return rows[order[i]] < rows[order[j]] }) old, fresh := x.vectors[model], make(map[string]int, len(order)) kept, file := make([]float32, 0, len(order)*dims), make([]byte, 0, 4*len(order)*dims) for i, sha := range order { row := old[rows[sha]*dims : (rows[sha]+1)*dims] kept = append(kept, row...) for _, v := range row { file = binary.LittleEndian.AppendUint32(file, math.Float32bits(v)) } fresh[sha] = i } x.vectors[model], x.rows[model] = kept, fresh name := fmt.Sprintf("vectors-%s-%d.f32", model, x.Generation) if err := atomicWrite(filepath.Join(x.Dir, name), file); err != nil { return err } x.Models[model].File, x.Models[model].Sha256s = name, order } x.dirty = map[string]bool{} items := x.Items if items == nil { items = []Item{} } meta, err := json.MarshalIndent(indexMeta{Format: indexFormat, Generation: x.Generation, Items: items, Models: x.Models}, "", " ") if err != nil { return err } if err := atomicWrite(filepath.Join(x.Dir, "index.json"), meta); err != nil { return err } live := map[string]bool{} for _, info := range x.Models { live[info.File] = true } stale, _ := filepath.Glob(filepath.Join(x.Dir, "vectors-*.f32")) for _, path := range stale { if !live[filepath.Base(path)] { os.Remove(path) } } return nil}精确检索。 向量已做 L2 归一化,一次矩阵与向量的乘法即得全部余弦相似度。
源码: python/search.py (在新标签页打开)
def top_k(matrix: np.ndarray, query, k: int, exclude: int | None = None) -> list[tuple[int, float]]: """The `k` rows of `matrix` most similar to `query`, as (row, score), best first. `exclude` skips one row (the query itself).""" if len(matrix) == 0: return [] scores = matrix @ np.asarray(query, dtype=np.float32) if exclude is not None: scores[exclude] = -np.inf k = min(k, len(scores) - (exclude is not None)) if k <= 0: return [] best = np.argpartition(-scores, k - 1)[:k] if k < len(scores) else np.arange(len(scores)) best = best[np.lexsort((best, -scores[best]))] # by score, ties by row, so the order is stable return [(int(i), float(scores[i])) for i in best]// TopK returns the k rows of m most similar to query (which has m.Dim values), as (row, score), best first. exclude// skips one row (the query itself); pass -1 to skip none. Equal scores are ordered by row, so the order is stable.func TopK(m Matrix, query []float32, k int, exclude int) []Hit { if m.N == 0 { return []Hit{} } hits := make([]Hit, 0, m.N) for i := 0; i < m.N; i++ { if i != exclude { hits = append(hits, Hit{i, dot(m.Row(i), query)}) } } sort.Slice(hits, func(a, b int) bool { if hits[a].Score != hits[b].Score { return hits[a].Score > hits[b].Score } return hits[a].Row < hits[b].Row }) if k < 0 { k = 0 } return hits[:min(k, len(hits))]}近重复。 超过阈值的图片对按块查找(内存有界),再用并查集合并。
源码: python/search.py (在新标签页打开)
class UnionFind: def __init__(self, n: int): self.parent = list(range(n))
def find(self, i: int) -> int: while self.parent[i] != i: self.parent[i] = self.parent[self.parent[i]] # path halving i = self.parent[i] return i
def union(self, i: int, j: int) -> None: self.parent[self.find(j)] = self.find(i)
def similar_pairs(matrix: np.ndarray, threshold: float, block: int = 1024): """Yield (i, j, score) for every pair i < j with similarity >= threshold, one block of rows at a time (memory stays O(block x n)).""" n = len(matrix) for start in range(0, n, block): scores = matrix[start : start + block] @ matrix.T rows, cols = np.nonzero(scores >= threshold) for r, c in zip(rows, cols): if start + r < c: yield int(start + r), int(c), float(scores[r, c])
def duplicate_groups(matrix: np.ndarray, threshold: float) -> list[list[int]]: """Groups of rows that are near-duplicates of each other, found as connected components of the 'similar enough' graph.
Connected components are transitive: if A resembles B and B resembles C, all three end up in one group even when A and C are below the threshold. A high threshold keeps chains short; the groups are what a person reviews, not a verdict. """ uf = UnionFind(len(matrix)) for i, j, _ in similar_pairs(matrix, threshold): uf.union(i, j) groups: dict[int, list[int]] = {} for i in range(len(matrix)): groups.setdefault(uf.find(i), []).append(i) return sorted((g for g in groups.values() if len(g) > 1), key=lambda g: g[0])// UnionFind groups elements into connected components.type UnionFind struct{ parent []int }
func NewUnionFind(n int) *UnionFind { u := &UnionFind{parent: make([]int, n)} for i := range u.parent { u.parent[i] = i } return u}
func (u *UnionFind) Find(i int) int { for u.parent[i] != i { u.parent[i] = u.parent[u.parent[i]] // path halving i = u.parent[i] } return i}
func (u *UnionFind) Union(i, j int) { u.parent[u.Find(j)] = u.Find(i) }
// SimilarPairs returns every pair (i, j) with i < j whose similarity is at least threshold, ordered by i then j. Rows are// compared in parallel, in blocks.func SimilarPairs(m Matrix, threshold float64) [][2]int { const block = 64 limit := float32(threshold) // the scores are float32: compare in float32 blocks := (m.N + block - 1) / block found := make([][][2]int, blocks) var wg sync.WaitGroup next := make(chan int) for w := 0; w < runtime.NumCPU(); w++ { wg.Add(1) go func() { defer wg.Done() for b := range next { for i := b * block; i < min((b+1)*block, m.N); i++ { for j := i + 1; j < m.N; j++ { if dot(m.Row(i), m.Row(j)) >= limit { found[b] = append(found[b], [2]int{i, j}) } } } } }() } for b := 0; b < blocks; b++ { next <- b } close(next) wg.Wait() var pairs [][2]int for _, f := range found { pairs = append(pairs, f...) } return pairs}
// DuplicateGroups returns the groups of rows that are near-duplicates of each other, found as connected components of// the "similar enough" graph. Rows inside a group, and the groups themselves, are in row order.//// Connected components are transitive: if A resembles B and B resembles C, all three end up in one group even when A and// C are below the threshold. A high threshold keeps chains short; the groups are what a person reviews, not a verdict.func DuplicateGroups(m Matrix, threshold float64) [][]int { uf := NewUnionFind(m.N) for _, p := range SimilarPairs(m, threshold) { uf.Union(p[0], p[1]) } byRoot, groups := map[int][]int{}, [][]int{} for i := 0; i < m.N; i++ { root := uf.Find(i) byRoot[root] = append(byRoot[root], i) } for _, g := range byRoot { if len(g) > 1 { groups = append(groups, g) } } sort.Slice(groups, func(a, b int) bool { return groups[a][0] < groups[b][0] }) return groups}每次运行都检查增量重建。 建库之后,main.py 再建一次。此时没有缺失项,第二次必须不发出任何 API 调用;调用数记入 stats.json,发出调用则运行失败。
calls_before = len(client.ledger)if args.use_async and (report.stopped or report.failed): again = None # what is missing would only be submitted as new jobs; the next run does thatelse: again = embedder.index_library(client, index, base, MODELS, batch_size=args.batch_size, workers=args.concurrency, log=lambda *_: None, **mode)stats["reindex"] = ( {"apiCalls": len(client.ledger) - calls_before, "embedded": again.embedded} if again else None) # nothing is missing, so nothing is sentcallsBefore := len(client.Calls(""))var again *Reportif *useAsync && (report.Stopped != nil || len(report.Failed) > 0) { again = newReport() // what is missing would only be submitted as new jobs; the next run does that} else { again, err = runIndex(func(string) {})}if err == nil { st.Reindex.APICalls, st.Reindex.Embedded = len(client.Calls(""))-callsBefore, again.Embedded // nothing is missing, so nothing is sent}文字查询。 每条查询一次调用,使每条查询的费用和延迟落在各自的项里。文本也可以合并发送(每次最多 64 条),以少占用项目限额内的调用次数;两种方式的价格都按文本条数计。
text_items, text_ms = [], []siglip_items, siglip_matrix = index.matrix("siglip2")for q in manifest.get("queries", []): entry = {"id": q["id"], "calls": [], "output": None} try: vector, row = embedder.embed_text(client, q["text"], tag=q["id"]) t = time.monotonic() ranked = search.top_k(siglip_matrix, vector, TEXT_RESULTS) text_ms.append((time.monotonic() - t) * 1000) entry["status"], entry["calls"] = "ok", [row] entry["output"] = {"type": "query", "text": q["text"], "ranking": [siglip_items[i]["id"] for i, _ in ranked], "scores": [r4(s) for _, s in ranked]} except sc.StackError as err: print(f" {q['id']}: {err.status} {err.code}: {err.message}", file=sys.stderr) if err.code == "model_locked": return EXIT_TIER entry["status"] = "expired" if err.code in embedder.STOP_CODES else "error" entry["error"] = {"status": err.status, "code": err.code, "message": err.message} if err.code == "budget_exceeded": code = EXIT_BUDGET text_items.append(entry)var textMs []float64siglipItems, siglipMatrix := index.Matrix("siglip2")for _, q := range mf.Queries { entry := resultItem{ID: q.ID, Calls: []stackclient.Row{}} vector, row, err := embedText(client, q.Text, "siglip2", q.ID) if err == nil && siglipMatrix.N > 0 && len(vector) != siglipMatrix.Dim { err = fmt.Errorf("the query vector has %d values, the index %d", len(vector), siglipMatrix.Dim) } var apiErr *stackclient.Error switch { case err == nil: t := time.Now() hits := TopK(siglipMatrix, vector, textResults, -1) textMs = append(textMs, float64(time.Since(t).Microseconds())/1000) ranking, scores := []string{}, []float64{} for _, h := range hits { ranking, scores = append(ranking, siglipItems[h.Row].ID), append(scores, float64(h.Score)) } entry.Status, entry.Calls = "ok", []stackclient.Row{row} entry.Output = queryOutput{Type: "query", Text: q.Text, Ranking: ranking, Scores: r4(scores)} case errors.As(err, &apiErr): fmt.Fprintf(os.Stderr, " %s: %v\n", q.ID, apiErr) if apiErr.Code == "model_locked" { return exitTier } entry.Status = "error" if stopCodes[apiErr.Code] { entry.Status = "expired" } entry.Error = &errOutput{Status: apiErr.Status, Code: apiErr.Code, Message: apiErr.Message} if apiErr.Code == "budget_exceeded" { code = exitBudget } default: return fail(err) } items = append(items, entry)}Go 程序还有第二种模式:对先前运行写出的索引(Python 或 Go 写的都可以,格式相同)提供检索服务:go run . serve --index out/index。默认监听 127.0.0.1:8080(可用 --addr 修改),只读(不通过 HTTP 建库),返回 JSON,所有错误统一为 {"error": {"code": "...", "message": "..."}}。
curl "127.0.0.1:8080/search?q=a+photo+of+a+dog&k=10" # 符合一段文字的图片(siglip2)curl "127.0.0.1:8080/similar?id=5177b735e298df6c&k=10" # 与某张图库图片相似的图片(dinov3)curl "127.0.0.1:8080/duplicates?threshold=0.90" # 近重复分组(dinov3)curl "127.0.0.1:8080/healthz"/search 通过客户端用 siglip2 嵌入文字,因此每次查询是一次调用(标价 0.01 credits;没有设置 FOLDBUN_API_KEY 时服务使用免费层,不扣费;设置后按标价扣费)。/similar 和 /duplicates 读取已存的向量,不发出 API 调用。k 缺省为 10,超过 100 时按 100 处理;k 不合法、q 为空或过长、阈值不在 (0, 1] 内时返回 400,图片 id 不存在返回 404,GET 以外的方法返回 405,API 失败返回 502(项目限额、余额或额度上限用尽时为 429)。收到 SIGINT 或 SIGTERM 后,服务在处理中的请求结束后退出。
路由表和服务器设置(超时、请求头上限、平滑停止)就是服务的全部:
// handler routes the four read-only endpoints; any other method or path gets the standard error shape.func (s *service) handler() http.Handler { routes := map[string]http.HandlerFunc{"/search": s.search, "/similar": s.similar, "/duplicates": s.duplicates, "/healthz": s.healthz} return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { h, ok := routes[r.URL.Path] switch { case !ok: apiError(w, http.StatusNotFound, "not_found", "unknown path") case r.Method != http.MethodGet && r.Method != http.MethodHead: w.Header().Set("Allow", "GET, HEAD") apiError(w, http.StatusMethodNotAllowed, "method_not_allowed", "the service is read-only: use GET") default: h(w, r) } })}
// listenAndServe serves until ctx ends (SIGINT or SIGTERM in main), then lets running requests finish for up to 10 s.func listenAndServe(ctx context.Context, ln net.Listener, h http.Handler) error { srv := &http.Server{ Handler: h, ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 10 * time.Second, WriteTimeout: 2 * time.Minute, // a text query waits for the API, which may retry IdleTimeout: 60 * time.Second, MaxHeaderBytes: 16 << 10, } failed := make(chan error, 1) go func() { failed <- srv.Serve(ln) }() select { case err := <-failed: return err case <-ctx.Done(): } shutdown, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() return srv.Shutdown(shutdown)}检索是对全部向量的暴力遍历,几万张以内没有问题;更大的规模见下文的局限。
evaluate.py(仅用标准库,读取 result.json、stats.json 和真值)度量的内容:
- 以图搜图。 每个近重复变体作为一次查询;
i2i_recall_at_k是原图出现在最近 k 张图片中的变体所占比例。 - 文字搜图。 40 条形如 “a photo of a <class>” 的查询,对照 Open Images 对原图的人工图像级标签评分:
text_precision_at_5、text_precision_at_10、text_recall_at_10、text_map。变体按定义不相关,评分前先从排序中去掉。 - 近重复。 “相似度不低于阈值”这一判断在候选图片对上的精确率、召回率和 F1。阈值在数据的 a 半上选定(精确率保持在 0.99 以上的最低阈值),原样用于 b 半,并报告 b 半上的数字。
- 速度与成本。 本地检索耗时(不涉及网络)、第二次建库发出的调用数(应为零),以及每 1,000 张图片的标价和实扣。建库吞吐取决于读者到 API 的网络路径,因此不予发布。
下方数字由 expected/full-summary.json 生成,维护者在完整数据集(约 500 张图片)上做一次真实运行后记录。在此之前该区块如实标明。harness check 的离线运行使用假 API,其向量是确定性的但没有语义:离线运行只验证流程(建出索引、第二次建库不发送任何请求、查询返回形状正确的排序结果),不验证质量,因此质量下限不适用于离线运行。
| 指标 | 数值 |
|---|---|
i2i_recall_at_1 |
1 |
i2i_recall_at_5 |
1 |
i2i_recall_at_10 |
1 |
text_precision_at_5 |
0.905 |
text_precision_at_10 |
0.8075 |
text_recall_at_10 |
0.7963 |
text_map |
0.8438 |
text_queries |
40 |
dup_threshold |
0.9352 |
dup_precision |
1 |
dup_recall |
0.9149 |
dup_f1 |
0.9556 |
dup_pairs_a |
53 |
dup_pairs_b |
47 |
images |
500 |
images_embedded |
500 |
cost_per_1000_images_list |
100 |
cost_per_1000_images_charged |
90 |
cost_per_text_query_list |
0.01 |
search_ms_text |
0.389 |
search_ms_image |
0.003 |
second_pass_api_calls |
0 |
dinov3 每张图 0.05 credits,siglip2 每张图 0.05 credits,每条文本 0.01 credits。smoke 数据集(25 张图片、5 条查询)按标价为 25 x 0.10 + 5 x 0.01 = 2.55 credits,共 55 次调用:dinov3 25 次、siglip2 25 次、文字查询 5 次。在免费层上 smoke 运行不扣费:运行时会打印这笔标价并标注“免费层,不扣费”。完整数据集(约 500 张图片、40 条查询)按标价为 50.4 credits,约 1,040 次调用,由密钥扣费;每次停止后建库可继续。用 --async 时 500 张图片只要十分之一,即 5 credits 而不是 50 credits(加上查询约 5.4 credits);下表是批处理那次运行的实测值。
每次运行记录各自的标价和实扣:
| 运行 | 调用次数 | 标价(credits) | 实扣(credits) |
|---|---|---|---|
| smoke | 55 | 2.55 | 0 |
| full | 1040 | 45.4 | 45.4 |
每 1,000 张图片的建库标价为 100 credits,仅一次;之后只对新增和改动的图片计费。文字查询每条 0.01 credits,图库内图片的查询不计费。
局限与不适用的情形
Section titled “局限与不适用的情形”- 精确检索适用于这个量级。 一次查询遍历全部向量:每 1,000 张图片约 3 MB(768 个值,每个 4 字节),1 万张图片需要几毫秒。超过大约 5 万到 10 万张,或需要在负载下几毫秒内响应时,应改用近似索引(FAISS、hnswlib,或数据库里的 pgvector)。接口保持不变:
top_k(matrix, query, k)换成对索引的调用,向量文件仍是数据的来源,流程的其余部分不变。全量两两比较的近重复查找随图库规模平方增长;到那个规模时,使用索引的范围检索,或先按粗粒度哈希分组。 - 近重复是对相似度设的阈值。 默认值(0.90)只是起点。应在图库自己的带标注样本上选定,做法与
evaluate.py相同;裁剪和重度重新压缩的得分通常低于缩放。连通分量会成链:A 与 B 相似、B 与 C 相似,三者就进入同一组。 - 只有图像级语义。 两个模型描述的是整张图片,没有物体位置。
siglip2的输入是 224 px,小物体会丢失。文字查询以英文效果最好,得分是很小的数,只有排序有意义,因此不使用固定的截断值。 - 模型是索引的一部分。 不同模型的向量不可比较,模型升级后旧向量与新向量也会不一致。索引记录每个模型的向量长度,遇到不同长度就拒绝;模型变化时需要重建索引。
- 免费层限额。 免费层按模型的重量级别,对每个项目分别限制每分钟和每日的调用数,而 Cookbook 的共享项目由所有读者共用,上限可能被别人用完。代码依据
429和服务端返回的响应头行事,不假设具体数字。批处理中的各项依次执行,N 张图的批处理耗时约为单张处理时间的 N 倍;每块应远小于批处理 600 秒的上限。 - 上传的图片由客户端保留。 客户端文件在内存中保留已上传图片的字节,以便句柄被逐出时重新上传。图库很大时,请分多次运行建库。
- 参考文档:Embeddings and search (在新标签页打开)、DINOv3 (在新标签页打开)、SigLIP 2 (在新标签页打开) 和 Rate limits (在新标签页打开)。
dinov3的输出受 DINOv3 License 约束;基于它构建的产品应显示标签 “Built with DINOv3”。
smoke 数据集包含 20 张原图、5 个近重复变体和 5 条文字查询,来自 datasets/smoke/05-image-library-search/(assets/smoke/ 链接到它)。照片来自 Open Images V7 验证集(图片以 CC BY 2.0 由作者发布,标签为 CC BY 4.0);变体由它们按记录的变换派生。作者和许可证见 assets/smoke/ATTRIBUTION.csv。完整数据集(400 张图片、100 个变体、40 条查询)是发布附件,说明见 datasets/README.md。权利人如需移除某张图片或某个数据集,可通过联系页面 (在新标签页打开)提出。