Files
ai_academy/internal/models/providers/academycloud.go
T
runny 7ec21f35b0 chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、
Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。
今日开发的培训推送管理改动随后单独提交。
2026-11-16 18:12:23 +08:00

128 lines
5.3 KiB
Go

// Package providers registers the Academy managed model service.
//
// unverified: this is a first-party endpoint with no published API
// reference — https://weknora.weixin.qq.com/docs documents the Academy
// product, not the model gateway's wire protocol. Every fact below is taken
// from this repository's own clients rather than from vendor documentation,
// and is cited accordingly:
// - one host serves chat, embedding, rerank and VLM. Chat completions and
// VLM live at /api/v1/chat/completions rather than the OpenAI default
// path (internal/models/vlm/academycloud.go), embeddings at
// /api/v1/embeddings (internal/models/embedding/academycloud.go) and
// rerank at /api/v1/rerank (internal/models/rerank/academycloud.go).
// Only the chat path goes through the Endpoint hook; the other three
// clients build their own URLs;
// - requests are signed with the tenant's AppID / AppSecret through
// modelutils.Sign (internal/models/utils/signer.go), which sends
// X-APPID, X-API-Key, X-Request-ID, X-Timestamp, X-Nonce and an
// X-Signature that is the MD5 of the RFC3986-encoded, key-sorted
// parameter string including the MD5 of the body. There is no bearer
// key, hence the AuthSigned style and a fresh request id per call;
// - credentials are provisioned through the dedicated initialization
// endpoint (internal/handler/academycloud.go), so Validate performs no
// key check here;
// - the model names the product provisions are chat, embedding, rerank and
// vlm (frontend/src/utils/academyCloudModels.ts). models.json is left
// empty on purpose: no context window, output cap or price is published
// for them, and the embedding dimension is probed at runtime through
// GET /api/v1/models/academycloud/status.
//
// unverified: SupportsMultiContent is kept false so chat messages are
// flattened to plain text, but nothing documents that restriction and the
// VLM client posts image_url parts to the very same /api/v1/chat/completions
// path, so it may only apply to the chat model. The value is left as-is
// because the transport test pins it.
//
// unverified: PromptCacheAccounting is kept false; the gateway is not
// documented to report prompt-cache counters, and none have been observed.
package providers
import (
_ "embed"
"net/http"
"strings"
"academy/internal/models/api"
modelutils "academy/internal/models/utils"
"academy/internal/types"
"github.com/google/uuid"
)
//go:embed assets/academycloud.svg
var academyCloudIcon []byte
// AcademyCloudID is the provider identifier stored on model rows.
const AcademyCloudID = "academycloud"
// AcademyCloudBaseURL is the single service entry point; per-capability paths are
// appended by the Endpoint hook and the embedding / rerank clients.
const AcademyCloudBaseURL = "https://weknora.weixin.qq.com"
func newAcademyCloudProvider() *Definition {
return &Definition{
ID: AcademyCloudID,
Name: "Academy Cloud",
Names: map[string]string{"zh-CN": "Academy 云服务"},
Description: "Academy managed models: chat, embedding, rerank, vlm",
Descriptions: map[string]string{"zh-CN": "Academy云服务,模型:chat, embedding, rerank, vlm"},
Website: AcademyCloudBaseURL,
Icon: academyCloudIcon,
API: api.APIOpenAICompletions,
Order: 1,
RequiresAuth: true,
Auth: AuthSigned,
URLPatterns: []string{"weknora.weixin.qq.com"},
DefaultBaseURLs: map[types.ModelType]string{
types.ModelTypeKnowledgeQA: AcademyCloudBaseURL,
types.ModelTypeEmbedding: AcademyCloudBaseURL,
types.ModelTypeRerank: AcademyCloudBaseURL,
types.ModelTypeVLLM: AcademyCloudBaseURL,
},
ModelTypes: []types.ModelType{
types.ModelTypeKnowledgeQA,
types.ModelTypeEmbedding,
types.ModelTypeRerank,
types.ModelTypeVLLM,
},
Compat: VendorCompat{
Embeddings: api.EmbeddingsCompat{
// The OpenAI body on the service's own path, signed like the
// rest of it.
Path: api.Ptr("/api/v1/embeddings"),
DimensionsField: api.Ptr("dimensions"),
RequestTimeout: api.Ptr(60),
},
Rerank: api.RerankCompat{
Path: api.Ptr("/api/v1/rerank"),
// The only rerank vendor that has ever had a client deadline
// here. Without it a hung endpoint holds the retrieval stage
// for as long as the caller's context allows.
RequestTimeout: api.Ptr(60),
},
OpenAICompletions: api.OpenAICompletionsCompat{
SupportsMultiContent: api.Ptr(false),
PromptCacheAccounting: api.Ptr(false),
},
},
Signer: func(creds api.Credentials) api.AuthFunc {
return func(req *http.Request, body []byte) {
headers := modelutils.Sign(creds.AppID, creds.AppSecret, uuid.NewString(), string(body))
for k, v := range headers {
req.Header.Set(k, v)
}
}
},
Endpoint: func(r EndpointRequest) (string, map[string]string) {
switch r.ModelType {
case types.ModelTypeEmbedding, types.ModelTypeRerank, types.ModelTypeASR:
// Those clients build their own paths; fall back.
return "", nil
}
return strings.TrimRight(r.BaseURL, "/") + "/api/v1/chat/completions", nil
},
// AppID / AppSecret are written by the dedicated initialization
// endpoint; only structural checks happen there.
Validate: func(_ *Config) error { return nil },
}
}