Files
ai_academy/internal/models/api/embeddings.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

116 lines
4.5 KiB
Go

package api
import (
"context"
"encoding/base64"
"fmt"
)
// EmbeddingAPI names an embedding wire protocol. Like RerankAPI it is its own
// type, so a chat row's extra_config.api cannot name an embedding dialect and
// pass validation.
type EmbeddingAPI string
// The embedding protocols Academy speaks.
const (
// EmbeddingOpenAI is POST {base}/embeddings with {model, input[]} answering
// {data: [{embedding, index}]} — the shape OpenAI defined and every
// compatible gateway copied. Jina, NVIDIA, Azure and Academy Cloud speak
// it too, with their own optional fields, URL or credential.
EmbeddingOpenAI EmbeddingAPI = "openai-embeddings"
// EmbeddingDashScope is Alibaba Model Studio's native multimodal shape:
// input.contents, parameters.dimension, output.embeddings.
EmbeddingDashScope EmbeddingAPI = "dashscope-embeddings"
// EmbeddingArk is Volcengine Ark's multimodal shape, which fuses every
// input into one vector and therefore embeds one text per request.
EmbeddingArk EmbeddingAPI = "ark-embeddings"
// EmbeddingGoogle is Gemini's batchEmbedContents.
EmbeddingGoogle EmbeddingAPI = "google-embeddings"
)
// Known reports whether the value names a protocol this build implements.
func (a EmbeddingAPI) Known() bool {
switch a {
case EmbeddingOpenAI, EmbeddingDashScope, EmbeddingArk, EmbeddingGoogle:
return true
}
return false
}
// EmbedInputType distinguishes the two sides of a retrieval pair. Several
// vendors score them differently and want to be told which one they are
// embedding; the field they want it in differs, so the catalog names it.
type EmbedInputType string
const (
// EmbedDocument is text being indexed.
EmbedDocument EmbedInputType = "document"
// EmbedQuery is text being searched with.
EmbedQuery EmbedInputType = "query"
)
// Embedder is what every embedding protocol package implements. Callers pass
// the whole batch; splitting it to the vendor's documented ceiling is the
// shared layer's job, not the protocol's.
type Embedder interface {
Embed(ctx context.Context, texts []string, kind EmbedInputType) ([][]float32, error)
}
// EmbedImage is one image to embed. It travels inline rather than as a URL:
// the object store Academy reads images from is usually not reachable from
// the vendor, and every multimodal vendor in the catalog accepts base64.
type EmbedImage struct {
Data []byte
MIMEType string
}
// Base64 is the image in standard base64, Gemini's inlineData form.
func (i EmbedImage) Base64() string { return base64.StdEncoding.EncodeToString(i.Data) }
// DataURI is the data:{mime};base64,{data} form Ark, DashScope and Jina
// document.
func (i EmbedImage) DataURI() string { return "data:" + i.MIMEType + ";base64," + i.Base64() }
// ImageEmbedder is implemented by the protocols that can carry an image.
// Whether a given model maps images into the space of its text vectors is
// the catalog entry's input, not the protocol's, so the caller checks that
// first. Callers pass a batch within EmbeddingsSettings.ImageBatchLimit.
type ImageEmbedder interface {
// AcceptsImages reports whether this endpoint can carry an image at all.
AcceptsImages() bool
EmbedImages(ctx context.Context, images []EmbedImage, kind EmbedInputType) ([][]float32, error)
}
// PlaceEmbeddings puts returned vectors back in the order the texts were
// sent, using the index each vendor reports, and refuses a response that does
// not cover every input.
//
// Both halves matter. A vendor is free to answer out of order, so the index
// has to be honoured rather than assumed — and reading the wrong field for it
// silently collapses a whole batch onto slot 0, which is what
// Tencent/WeKnora#3484 was. An unfilled slot then reaches the index as an
// empty vector, where it is stored and poisons retrieval without anything
// failing, so a gap is an error instead. So is an index reported twice: one
// of the two vectors belongs to some other input.
func PlaceEmbeddings(want, got int, at func(i int) (int, []float32)) ([][]float32, error) {
out := make([][]float32, want)
seen := make([]bool, want)
for i := 0; i < got; i++ {
index, vector := at(i)
if index < 0 || index >= want {
return nil, fmt.Errorf("embedding index %d out of range for %d inputs", index, want)
}
if seen[index] {
return nil, fmt.Errorf("embedding index %d returned twice", index)
}
seen[index] = true
out[index] = vector
}
for i, vector := range out {
if len(vector) == 0 {
return nil, fmt.Errorf("no embedding returned for input %d of %d", i, want)
}
}
return out, nil
}