chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、 Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。 今日开发的培训推送管理改动随后单独提交。
This commit is contained in:
commit
7ec21f35b0
5767 files changed
+1436788
No files matched your search
@@ -0,0 +1,140 @@
|
||||
// Package arkembeddings implements Volcengine Ark's multimodal embedding
|
||||
// shape: an input array of typed parts answering a single `data` object.
|
||||
//
|
||||
// The response is one fused vector for the whole input, not one per element —
|
||||
// the reference's own example returns
|
||||
//
|
||||
// "data": { "embedding": [...], "object": "embedding" }
|
||||
//
|
||||
// for a request carrying a video, an image and a text. So a batch of N texts
|
||||
// is N requests, which the vendor declares as a batch size of one rather than
|
||||
// this package looping.
|
||||
//
|
||||
// It is the only embedding API Ark still documents. Its retired text
|
||||
// endpoint (/api/v3/embeddings) was the plain OpenAI shape, which is why rows
|
||||
// naming a text model resolve to that protocol instead.
|
||||
//
|
||||
// https://docs.volcengine.com/docs/ark/multimodal-vectorization-api
|
||||
package arkembeddings
|
||||
|
||||
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 Ark's 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 {
|
||||
Data struct {
|
||||
Embedding []float32 `json:"embedding"`
|
||||
Object string `json:"object"`
|
||||
} `json:"data"`
|
||||
Error *struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
} `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// BuildRequestBody is the golden-test entry point.
|
||||
func (c *Client) BuildRequestBody(texts []string, _ api.EmbedInputType) map[string]any {
|
||||
input := make([]any, 0, len(texts))
|
||||
for _, text := range texts {
|
||||
input = append(input, map[string]any{"type": "text", "text": text})
|
||||
}
|
||||
return c.body(input)
|
||||
}
|
||||
|
||||
// BuildImageRequestBody is the golden-test entry point for images. The
|
||||
// reference takes an image as a URL or as
|
||||
// "data:image/{图片格式};base64,{图片Base64编码}" under image_url.url.
|
||||
func (c *Client) BuildImageRequestBody(images []api.EmbedImage, _ api.EmbedInputType) map[string]any {
|
||||
input := make([]any, 0, len(images))
|
||||
for _, img := range images {
|
||||
input = append(input, map[string]any{
|
||||
"type": "image_url", "image_url": map[string]any{"url": img.DataURI()},
|
||||
})
|
||||
}
|
||||
return c.body(input)
|
||||
}
|
||||
|
||||
func (c *Client) body(input []any) map[string]any {
|
||||
body := map[string]any{
|
||||
"model": c.cfg.Endpoint.Model,
|
||||
"input": input,
|
||||
}
|
||||
if c.cfg.Settings.SendEncodingFormat {
|
||||
body["encoding_format"] = "float"
|
||||
}
|
||||
if c.cfg.Settings.DimensionsField != "" && c.cfg.Dimensions > 0 {
|
||||
body[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 input. The vendor fuses everything it is sent into a
|
||||
// single vector, so the caller must hand over exactly one text; the shared
|
||||
// batching layer guarantees that through the declared batch size.
|
||||
func (c *Client) Embed(
|
||||
ctx context.Context, texts []string, kind api.EmbedInputType,
|
||||
) ([][]float32, error) {
|
||||
if len(texts) != 1 {
|
||||
return nil, fmt.Errorf(
|
||||
"ark embedding fuses its input into one vector, so it takes one text per request, got %d",
|
||||
len(texts),
|
||||
)
|
||||
}
|
||||
return c.post(ctx, c.BuildRequestBody(texts, kind))
|
||||
}
|
||||
|
||||
// AcceptsImages is always true: the image part is part of the schema.
|
||||
func (c *Client) AcceptsImages() bool { return true }
|
||||
|
||||
// EmbedImages vectorizes one image, for the same reason Embed takes one text.
|
||||
func (c *Client) EmbedImages(
|
||||
ctx context.Context, images []api.EmbedImage, kind api.EmbedInputType,
|
||||
) ([][]float32, error) {
|
||||
if len(images) != 1 {
|
||||
return nil, fmt.Errorf(
|
||||
"ark embedding fuses its input into one vector, so it takes one image per request, got %d",
|
||||
len(images),
|
||||
)
|
||||
}
|
||||
return c.post(ctx, c.BuildImageRequestBody(images, kind))
|
||||
}
|
||||
|
||||
func (c *Client) post(ctx context.Context, body map[string]any) ([][]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
|
||||
}
|
||||
if decoded.Error != nil && decoded.Error.Message != "" {
|
||||
return nil, fmt.Errorf("ark embedding error %s: %s", decoded.Error.Code, decoded.Error.Message)
|
||||
}
|
||||
if len(decoded.Data.Embedding) == 0 {
|
||||
return nil, fmt.Errorf("ark embedding returned no vector")
|
||||
}
|
||||
return [][]float32{decoded.Data.Embedding}, nil
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package arkembeddings
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"academy/internal/models/api"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// documentedResponse is the response example from Volcengine's multimodal
|
||||
// embedding reference, copied from the rendered page:
|
||||
// https://docs.volcengine.com/docs/ark/multimodal-vectorization-api
|
||||
//
|
||||
// `data` is one object, not an array. The request that produced it carried a
|
||||
// video, an image and a text; the answer is a single fused vector.
|
||||
const documentedResponse = `{
|
||||
"created": 1743575029,
|
||||
"data": {
|
||||
"embedding": [-0.123046875, -0.35546875, -0.318359375],
|
||||
"object": "embedding"
|
||||
},
|
||||
"id": "021743575029461acbe49a31755bec77b2f09448eb15fa9a88e47",
|
||||
"model": "doubao-embedding-vision-251215",
|
||||
"object": "list",
|
||||
"usage": {"prompt_tokens": 13987, "total_tokens": 13987}
|
||||
}`
|
||||
|
||||
const endpoint = "/api/v3/embeddings/multimodal"
|
||||
|
||||
func newClient(url string, dims int) *Client {
|
||||
return New(Config{
|
||||
Endpoint: api.Endpoint{BaseURL: url, Model: "doubao-embedding-vision-251215", Auth: api.BearerAuth("k")},
|
||||
Settings: api.EmbeddingsSettings{
|
||||
SendEncodingFormat: true, DimensionsField: "dimensions",
|
||||
},
|
||||
Dimensions: dims,
|
||||
})
|
||||
}
|
||||
|
||||
func TestRequestBodyMatchesTheDocumentedSchema(t *testing.T) {
|
||||
body := newClient("https://ark.cn-beijing.volces.com"+endpoint, 2048).
|
||||
BuildRequestBody([]string{"视频和图片里有什么"}, api.EmbedDocument)
|
||||
assert.Equal(t, map[string]any{
|
||||
"model": "doubao-embedding-vision-251215",
|
||||
"encoding_format": "float",
|
||||
"dimensions": 2048,
|
||||
"input": []any{map[string]any{"type": "text", "text": "视频和图片里有什么"}},
|
||||
}, body)
|
||||
}
|
||||
|
||||
func TestDecodesTheDocumentedResponse(t *testing.T) {
|
||||
t.Setenv("SSRF_WHITELIST", "127.0.0.1")
|
||||
var path string
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
path = r.URL.Path
|
||||
_, _ = w.Write([]byte(documentedResponse))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
got, err := newClient(server.URL+endpoint, 0).Embed(context.Background(), []string{"a"}, api.EmbedDocument)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, [][]float32{{-0.123046875, -0.35546875, -0.318359375}}, got)
|
||||
assert.Equal(t, endpoint, path)
|
||||
}
|
||||
|
||||
// The vendor fuses everything it is sent. Handing it two texts would return
|
||||
// one vector for both, silently, so the protocol refuses rather than guess.
|
||||
func TestRefusesMoreThanOneText(t *testing.T) {
|
||||
_, err := newClient("https://ark.cn-beijing.volces.com"+endpoint, 0).
|
||||
Embed(context.Background(), []string{"a", "b"}, api.EmbedDocument)
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "one text per request")
|
||||
}
|
||||
|
||||
func TestEmptyVectorIsAnError(t *testing.T) {
|
||||
t.Setenv("SSRF_WHITELIST", "127.0.0.1")
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
_, _ = w.Write([]byte(`{"data":{"embedding":[],"object":"embedding"}}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
_, err := newClient(server.URL+endpoint, 0).Embed(context.Background(), []string{"a"}, api.EmbedDocument)
|
||||
require.Error(t, err)
|
||||
}
|
||||
Reference in new issue
Block a user