chore: 初始化仓库,导入项目基线代码

Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、
Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。
今日开发的培训推送管理改动随后单独提交。
This commit is contained in:
runny committed 2026-11-16 18:12:23 +08:00
commit 7ec21f35b0
5767 files changed
+1436788

No files matched your search

@@ -0,0 +1,163 @@
// Package googleembeddings implements Gemini's batchEmbedContents: one POST
// carrying a request per text, answering embeddings[].values in order.
//
// Three facts shape it. The per-request `model` must be the fully qualified
// `models/{id}`, not the bare id the URL already carries. The per-request
// options — `taskType`, `outputDimensionality` — belong inside
// `embedContentConfig`; the reference marks the same names at the top level
// of the request "Deprecated: Please use EmbedContentConfig…". And
// `taskType` exists on gemini-embedding-001 but not on gemini-embedding-2,
// which takes its task instruction in the prompt instead — so the field is
// declared per model rather than for the vendor.
//
// Each text is its own request with a single part: gemini-embedding-2 fuses
// the parts of one Content into one vector.
//
// https://ai.google.dev/api/embeddings
package googleembeddings
import (
"context"
"fmt"
"strings"
"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 batchEmbedContents to one endpoint.
type Client struct {
cfg Config
}
// New builds a client.
func New(cfg Config) *Client { return &Client{cfg: cfg} }
const method = ":batchEmbedContents"
// configField holds the per-request options.
const configField = "embedContentConfig"
// qualifiedModel is the `models/{id}` form the per-request model field needs.
func (c *Client) qualifiedModel() string {
return "models/" + strings.TrimPrefix(c.cfg.Endpoint.Model, "models/")
}
// openAIFacade is the sub-path of the same API version that serves the
// OpenAI-compatible surface. Rows that chat through it carry it in their base
// URL; the native method lives one level up.
const openAIFacade = "/openai"
// url is {base}/models/{id}:batchEmbedContents.
func (c *Client) url() string {
if c.cfg.Endpoint.URL != "" {
return c.cfg.Endpoint.Resolve("")
}
endpoint := c.cfg.Endpoint
endpoint.BaseURL = strings.TrimSuffix(strings.TrimRight(endpoint.BaseURL, "/"), openAIFacade)
return endpoint.Resolve("/" + c.qualifiedModel() + method)
}
type response struct {
Embeddings []struct {
Values []float32 `json:"values"`
} `json:"embeddings"`
Error *struct {
Message string `json:"message"`
} `json:"error,omitempty"`
}
// BuildRequestBody is the golden-test entry point.
func (c *Client) BuildRequestBody(texts []string, kind api.EmbedInputType) map[string]any {
parts := make([]any, 0, len(texts))
for _, text := range texts {
parts = append(parts, map[string]any{"text": text})
}
return c.body(parts, kind)
}
// BuildImageRequestBody is the golden-test entry point for images: an image
// is an inlineData part carrying its MIME type and base64 bytes.
func (c *Client) BuildImageRequestBody(images []api.EmbedImage, kind api.EmbedInputType) map[string]any {
parts := make([]any, 0, len(images))
for _, img := range images {
parts = append(parts, map[string]any{
"inlineData": map[string]any{"mimeType": img.MIMEType, "data": img.Base64()},
})
}
return c.body(parts, kind)
}
// body wraps every part in its own request, so each gets its own vector.
func (c *Client) body(parts []any, kind api.EmbedInputType) map[string]any {
s := c.cfg.Settings
requests := make([]any, 0, len(parts))
for _, part := range parts {
req := map[string]any{
"model": c.qualifiedModel(),
"content": map[string]any{"parts": []any{part}},
}
config := map[string]any{}
if field := s.InputTypeField; field != "" {
config[field] = s.InputTypeValue(kind)
}
if s.DimensionsField != "" && c.cfg.Dimensions > 0 {
config[s.DimensionsField] = c.cfg.Dimensions
}
if len(config) > 0 {
req[configField] = config
}
requests = append(requests, req)
}
body := map[string]any{"requests": requests}
for k, v := range s.ExtraBody {
if _, exists := body[k]; !exists {
body[k] = v
}
}
return body
}
// Embed vectorizes one batch. The reply carries no index: embeddings come
// back positionally, one per request, so a short reply is an error rather
// than a partially filled result.
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: inlineData 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
err := c.cfg.Endpoint.PostJSONWithRetry(ctx, c.url(), body, &decoded, c.cfg.Retry, "embedding")
if err != nil {
return nil, err
}
if decoded.Error != nil && decoded.Error.Message != "" {
return nil, fmt.Errorf("gemini embedding error: %s", decoded.Error.Message)
}
if len(decoded.Embeddings) != want {
return nil, fmt.Errorf(
"gemini returned %d embeddings for %d inputs", len(decoded.Embeddings), want)
}
return api.PlaceEmbeddings(want, len(decoded.Embeddings), func(i int) (int, []float32) {
return i, decoded.Embeddings[i].Values
})
}
@@ -0,0 +1,100 @@
package googleembeddings
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 batchEmbedContents response shape:
// https://ai.google.dev/api/embeddings — embeddings[].values, positional.
const documentedResponse = `{"embeddings":[{"values":[0.1,0.2]},{"values":[0.3,0.4]}]}`
var retrievalTasks = map[string]string{"document": "RETRIEVAL_DOCUMENT", "query": "RETRIEVAL_QUERY"}
func newClient(url, model string, settings api.EmbeddingsSettings, dims int) *Client {
return New(Config{
Endpoint: api.Endpoint{BaseURL: url, Model: model, Auth: api.HeaderAuth("x-goog-api-key", "k")},
Settings: settings,
Dimensions: dims,
})
}
// gemini-embedding-001 takes taskType, and each request's model must be the
// fully qualified models/{id}.
func TestRequestBodyForATaskTypeModel(t *testing.T) {
c := newClient("https://generativelanguage.googleapis.com/v1beta", "gemini-embedding-001",
api.EmbeddingsSettings{
InputTypeField: "taskType", InputTypeValues: retrievalTasks,
DimensionsField: "outputDimensionality",
}, 768)
// The options sit in embedContentConfig: the same names at the top level
// of the request are marked deprecated in the reference.
body := c.BuildRequestBody([]string{"a"}, api.EmbedDocument)
assert.Equal(t, map[string]any{"requests": []any{map[string]any{
"model": "models/gemini-embedding-001",
"content": map[string]any{"parts": []any{map[string]any{"text": "a"}}},
"embedContentConfig": map[string]any{
"taskType": "RETRIEVAL_DOCUMENT",
"outputDimensionality": 768,
},
}}}, body)
q := c.BuildRequestBody([]string{"q"}, api.EmbedQuery)["requests"].([]any)[0].(map[string]any)
assert.Equal(t, "RETRIEVAL_QUERY", q["embedContentConfig"].(map[string]any)["taskType"])
}
// gemini-embedding-2 does not support task_type: its reference says to put
// the task instruction in the text instead. Sending the field anyway is an
// undocumented parameter, so a model that declares none sends none.
func TestNoTaskTypeWhereTheModelHasNone(t *testing.T) {
c := newClient("https://generativelanguage.googleapis.com/v1beta", "gemini-embedding-2",
api.EmbeddingsSettings{}, 0)
req := c.BuildRequestBody([]string{"a"}, api.EmbedDocument)["requests"].([]any)[0].(map[string]any)
assert.NotContains(t, req, "embedContentConfig", "no option declared, so no config object")
assert.NotContains(t, req, "taskType")
}
func TestPostsToTheModelsMethod(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()
// An id stored with the prefix must not become models/models/…
c := newClient(server.URL+"/v1beta", "models/gemini-embedding-001", api.EmbeddingsSettings{}, 0)
got, err := c.Embed(context.Background(), []string{"a", "b"}, api.EmbedDocument)
require.NoError(t, err)
assert.Equal(t, [][]float32{{0.1, 0.2}, {0.3, 0.4}}, got)
assert.Equal(t, "/v1beta/models/gemini-embedding-001:batchEmbedContents", path)
// A row that chats through the OpenAI-compatible facade stores
// .../v1beta/openai; its embeddings still go to the native method.
c = newClient(server.URL+"/v1beta/openai/", "gemini-embedding-001", api.EmbeddingsSettings{}, 0)
_, err = c.Embed(context.Background(), []string{"a", "b"}, api.EmbedDocument)
require.NoError(t, err)
assert.Equal(t, "/v1beta/models/gemini-embedding-001:batchEmbedContents", path)
}
// The reply is positional with no index, so a short one cannot be mapped back.
func TestShortReplyIsAnError(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(`{"embeddings":[{"values":[0.1]}]}`))
}))
defer server.Close()
_, err := newClient(server.URL+"/v1beta", "gemini-embedding-001", api.EmbeddingsSettings{}, 0).
Embed(context.Background(), []string{"a", "b"}, api.EmbedDocument)
require.Error(t, err)
assert.Contains(t, err.Error(), "1 embeddings for 2 inputs")
}