feat(ml): fuse two embedding models and protect the ML service with a token

This commit is contained in:
shira committed 2026-10-11 14:40:52 +03:00
1 parent bc00d6901f
commit b24e5ff906
63 files changed
+2287 -276

No files matched your search

@@ -11,6 +11,7 @@ import (
"github.com/go-faster/errors"
ht "github.com/ogen-go/ogen/http"
"github.com/ogen-go/ogen/ogenerrors"
"github.com/ogen-go/ogen/otelogen"
"github.com/ogen-go/ogen/uri"
"go.opentelemetry.io/otel/attribute"
@@ -29,13 +30,16 @@ func trimTrailingSlashes(u *url.URL) {
type Invoker interface {
// CreateMLEmbeddings invokes CreateMLEmbeddings operation.
//
// Internal operation for the Go backend; it is not reachable from browsers and needs no credentials on
// the private network. The request carries only text that the caller is allowed to process, and the
// service never logs it.
// Internal operation for the Go backend; it is not reachable from browsers and needs the service
// bearer token. The request carries only text that the caller is allowed to process, and the service
// never logs it.
//
// The service answers 503 service_unavailable until the model is loaded from the image and a warm-up
// encode has finished; `/readyz` reports the same state. A request above the body limit answers 413,
// and a request that breaks the schema, such as too many texts or a text over 8000 characters, or
// The service may load several pinned models, for example the members of an ensemble; `model` selects
// one, and the response names the model, revision, dimension, and prompt version that produced the
// vectors. The service answers 503 service_unavailable until every configured model is loaded and a
// warm-up encode has finished, and when the device runs out of memory for a request; `/readyz` reports
// the same state. A request without a valid token answers 401, a request above the body limit answers
// 413, and a request that breaks the schema, such as too many texts or a text over 8000 characters, or
// names a model that the service does not serve, answers 400.
//
// POST /v1/embeddings
@@ -57,11 +61,12 @@ type Invoker interface {
// Client implements OAS client.
type Client struct {
serverURL *url.URL
sec SecuritySource
baseClient
}
// NewClient initializes new Client defined by OAS.
func NewClient(serverURL string, opts ...ClientOption) (*Client, error) {
func NewClient(serverURL string, sec SecuritySource, opts ...ClientOption) (*Client, error) {
u, err := url.Parse(serverURL)
if err != nil {
return nil, err
@@ -74,6 +79,7 @@ func NewClient(serverURL string, opts ...ClientOption) (*Client, error) {
}
return &Client{
serverURL: u,
sec: sec,
baseClient: c,
}, nil
}
@@ -95,13 +101,16 @@ func (c *Client) requestURL(ctx context.Context) *url.URL {
// CreateMLEmbeddings invokes CreateMLEmbeddings operation.
//
// Internal operation for the Go backend; it is not reachable from browsers and needs no credentials on
// the private network. The request carries only text that the caller is allowed to process, and the
// service never logs it.
// Internal operation for the Go backend; it is not reachable from browsers and needs the service
// bearer token. The request carries only text that the caller is allowed to process, and the service
// never logs it.
//
// The service answers 503 service_unavailable until the model is loaded from the image and a warm-up
// encode has finished; `/readyz` reports the same state. A request above the body limit answers 413,
// and a request that breaks the schema, such as too many texts or a text over 8000 characters, or
// The service may load several pinned models, for example the members of an ensemble; `model` selects
// one, and the response names the model, revision, dimension, and prompt version that produced the
// vectors. The service answers 503 service_unavailable until every configured model is loaded and a
// warm-up encode has finished, and when the device runs out of memory for a request; `/readyz` reports
// the same state. A request without a valid token answers 401, a request above the body limit answers
// 413, and a request that breaks the schema, such as too many texts or a text over 8000 characters, or
// names a model that the service does not serve, answers 400.
//
// POST /v1/embeddings
@@ -160,6 +169,39 @@ func (c *Client) sendCreateMLEmbeddings(ctx context.Context, request *EmbeddingR
return res, errors.Wrap(err, "encode request")
}
{
type bitset = [1]uint8
var satisfied bitset
{
stage = "Security:BearerAuth"
switch err := c.securityBearerAuth(ctx, CreateMLEmbeddingsOperation, r); {
case err == nil: // if NO error
satisfied[0] |= 1 << 0
case errors.Is(err, ogenerrors.ErrSkipClientSecurity):
// Skip this security.
default:
return res, errors.Wrap(err, "security \"BearerAuth\"")
}
}
if ok := func() bool {
nextRequirement:
for _, requirement := range []bitset{
{0b00000001},
} {
for i, mask := range requirement {
if satisfied[i]&mask != mask {
continue nextRequirement
}
}
return true
}
return false
}(); !ok {
return res, ogenerrors.ErrSecurityRequirementIsNotSatisfied
}
}
stage = "SendRequest"
resp, err := c.cfg.Client.Do(r)
if err != nil {