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 ✓ 健康检查 ✓
104 lines
3.5 KiB
Go
104 lines
3.5 KiB
Go
// Package tencentlkeap implements Tencent RunRerank through its official SDK.
|
|
package tencentlkeap
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"academy/internal/models/api"
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
|
lkeap "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lkeap/v20240522"
|
|
)
|
|
|
|
// SDK defaults follow the managed rerank service.
|
|
const (
|
|
// LKEAPRerankEndpoint 腾讯云知识引擎原子能力 Rerank API 域名
|
|
LKEAPRerankEndpoint = "lkeap.tencentcloudapi.com"
|
|
// LKEAPDefaultRegion RunRerank 支持的地域,默认广州
|
|
LKEAPDefaultRegion = "ap-guangzhou"
|
|
// LKEAPDefaultRerankModel 默认 rerank 模型名
|
|
LKEAPDefaultRerankModel = "lke-reranker-base"
|
|
)
|
|
|
|
// Config contains resolved connection parameters for the SDK client.
|
|
type Config struct{ SecretID, SecretKey, Region, Model, Endpoint, Scheme string }
|
|
|
|
// lkeapClient calls Tencent Cloud's RunRerank action through the official
|
|
// SDK, which owns the TC3 signing this API requires. It implements
|
|
// api.Reranker and nothing else: the per-request ceilings (60 documents,
|
|
// 2000 characters for Query plus Docs together) are declared on the vendor
|
|
// and enforced by protocolReranker, like every other vendor's.
|
|
type lkeapClient struct {
|
|
modelName string
|
|
client *lkeap.Client
|
|
}
|
|
|
|
// New creates an authenticated rerank protocol client.
|
|
func New(config Config) (api.Reranker, error) {
|
|
secretID, secretKey := strings.TrimSpace(config.SecretID), strings.TrimSpace(config.SecretKey)
|
|
if secretID == "" || secretKey == "" {
|
|
return nil, fmt.Errorf("secret_id and secret_key are required for LKEAP rerank (set API Key and Secret Key)")
|
|
}
|
|
region := strings.TrimSpace(config.Region)
|
|
if region == "" {
|
|
region = LKEAPDefaultRegion
|
|
}
|
|
if config.Endpoint == "" {
|
|
config.Endpoint = LKEAPRerankEndpoint
|
|
}
|
|
if config.Scheme == "" {
|
|
config.Scheme = "HTTPS"
|
|
}
|
|
|
|
credential := common.NewCredential(secretID, secretKey)
|
|
cpf := profile.NewClientProfile()
|
|
cpf.HttpProfile.Endpoint = config.Endpoint
|
|
cpf.HttpProfile.Scheme = config.Scheme
|
|
|
|
client, err := lkeap.NewClient(credential, region, cpf)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create LKEAP client: %w", err)
|
|
}
|
|
|
|
modelName := strings.TrimSpace(config.Model)
|
|
if modelName == "" {
|
|
modelName = LKEAPDefaultRerankModel
|
|
}
|
|
return &lkeapClient{modelName: modelName, client: client}, nil
|
|
}
|
|
|
|
// Rerank scores one batch. RunRerank answers a bare score list in input
|
|
// order rather than an indexed, sorted result set.
|
|
func (r *lkeapClient) Rerank(
|
|
ctx context.Context, query string, documents []string,
|
|
) ([]api.RerankResult, error) {
|
|
req := lkeap.NewRunRerankRequest()
|
|
req.Query = common.StringPtr(query)
|
|
req.Docs = common.StringPtrs(documents)
|
|
req.Model = common.StringPtr(r.modelName)
|
|
|
|
resp, err := r.client.RunRerankWithContext(ctx, req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("LKEAP RunRerank: %w", err)
|
|
}
|
|
if resp == nil || resp.Response == nil || len(resp.Response.ScoreList) == 0 {
|
|
return nil, fmt.Errorf("LKEAP rerank API returned empty score list")
|
|
}
|
|
scores := resp.Response.ScoreList
|
|
if len(scores) != len(documents) {
|
|
return nil, fmt.Errorf("LKEAP rerank score count mismatch: got %d scores for %d documents",
|
|
len(scores), len(documents))
|
|
}
|
|
|
|
results := make([]api.RerankResult, len(documents))
|
|
for i, score := range scores {
|
|
results[i] = api.RerankResult{Index: i, Text: documents[i]}
|
|
if score != nil {
|
|
results[i].Score = *score
|
|
}
|
|
}
|
|
return results, nil
|
|
}
|