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
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名) - 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块) - docreader proto 描述符采用等长替换,保持序列化字节布局有效 - Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像 - 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
120 lines
3.9 KiB
Go
120 lines
3.9 KiB
Go
// Package dashscopeembeddings implements Alibaba Model Studio's native
|
|
// multimodal embedding shape: input.contents, parameters.dimension, and
|
|
// results under output.embeddings.
|
|
//
|
|
// Only the multimodal models use it. DashScope's text embeddings are served
|
|
// on the OpenAI-compatible endpoint, which is why the vendor's default
|
|
// protocol is the OpenAI one and the multimodal entries override it.
|
|
//
|
|
// https://help.aliyun.com/zh/model-studio/multimodal-embedding-api-reference
|
|
package dashscopeembeddings
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"academy/internal/models/api"
|
|
)
|
|
|
|
// Config is everything the client needs, already resolved by the api.
|
|
type Config struct {
|
|
Endpoint api.Endpoint
|
|
Settings api.EmbeddingsSettings
|
|
Dimensions int
|
|
Retry api.RetryPolicy
|
|
}
|
|
|
|
// Client talks DashScope's native multimodal embedding to one endpoint.
|
|
type Client struct {
|
|
cfg Config
|
|
}
|
|
|
|
// New builds a client.
|
|
func New(cfg Config) *Client { return &Client{cfg: cfg} }
|
|
|
|
type response struct {
|
|
Output struct {
|
|
Embeddings []struct {
|
|
// The position field is `index`. DashScope's *text* embedding API
|
|
// one page over calls it `text_index`, and decoding that name
|
|
// here yields 0 for every element, collapsing a whole batch onto
|
|
// slot 0 (Tencent/WeKnora#3484).
|
|
Index int `json:"index"`
|
|
Embedding []float32 `json:"embedding"`
|
|
Type string `json:"type"`
|
|
} `json:"embeddings"`
|
|
} `json:"output"`
|
|
Code string `json:"code"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
// BuildRequestBody is the golden-test entry point.
|
|
func (c *Client) BuildRequestBody(texts []string, _ api.EmbedInputType) map[string]any {
|
|
contents := make([]any, 0, len(texts))
|
|
for _, text := range texts {
|
|
contents = append(contents, map[string]any{"text": text})
|
|
}
|
|
return c.body(contents)
|
|
}
|
|
|
|
// BuildImageRequestBody is the golden-test entry point for images. Each
|
|
// content is {"image": URL or "data:image/{format};base64,{data}"}.
|
|
func (c *Client) BuildImageRequestBody(images []api.EmbedImage, _ api.EmbedInputType) map[string]any {
|
|
contents := make([]any, 0, len(images))
|
|
for _, img := range images {
|
|
contents = append(contents, map[string]any{"image": img.DataURI()})
|
|
}
|
|
return c.body(contents)
|
|
}
|
|
|
|
func (c *Client) body(contents []any) map[string]any {
|
|
body := map[string]any{
|
|
"model": c.cfg.Endpoint.Model,
|
|
"input": map[string]any{"contents": contents},
|
|
}
|
|
// The width lives under parameters, not at the top level, and the field
|
|
// is singular: `dimension`.
|
|
if c.cfg.Settings.DimensionsField != "" && c.cfg.Dimensions > 0 {
|
|
body["parameters"] = map[string]any{c.cfg.Settings.DimensionsField: c.cfg.Dimensions}
|
|
}
|
|
for k, v := range c.cfg.Settings.ExtraBody {
|
|
if _, exists := body[k]; !exists {
|
|
body[k] = v
|
|
}
|
|
}
|
|
return body
|
|
}
|
|
|
|
// Embed vectorizes one batch.
|
|
func (c *Client) Embed(
|
|
ctx context.Context, texts []string, kind api.EmbedInputType,
|
|
) ([][]float32, error) {
|
|
return c.post(ctx, c.BuildRequestBody(texts, kind), len(texts))
|
|
}
|
|
|
|
// AcceptsImages is always true: the image part is part of the schema.
|
|
func (c *Client) AcceptsImages() bool { return true }
|
|
|
|
// EmbedImages vectorizes one batch of images, each its own vector.
|
|
func (c *Client) EmbedImages(
|
|
ctx context.Context, images []api.EmbedImage, kind api.EmbedInputType,
|
|
) ([][]float32, error) {
|
|
return c.post(ctx, c.BuildImageRequestBody(images, kind), len(images))
|
|
}
|
|
|
|
func (c *Client) post(ctx context.Context, body map[string]any, want int) ([][]float32, error) {
|
|
var decoded response
|
|
url := c.cfg.Endpoint.Resolve(c.cfg.Settings.Path)
|
|
err := c.cfg.Endpoint.PostJSONWithRetry(ctx, url, body, &decoded, c.cfg.Retry, "embedding")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// DashScope reports some failures in the body with a 200.
|
|
if decoded.Code != "" {
|
|
return nil, fmt.Errorf("DashScope embedding error %s: %s", decoded.Code, decoded.Message)
|
|
}
|
|
return api.PlaceEmbeddings(want, len(decoded.Output.Embeddings), func(i int) (int, []float32) {
|
|
return decoded.Output.Embeddings[i].Index, decoded.Output.Embeddings[i].Embedding
|
|
})
|
|
}
|