387 lines
11 KiB
Go
387 lines
11 KiB
Go
// Code generated by ogen, DO NOT EDIT.
|
|
|
|
package api
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"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"
|
|
"go.opentelemetry.io/otel/codes"
|
|
"go.opentelemetry.io/otel/metric"
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.39.0"
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
func trimTrailingSlashes(u *url.URL) {
|
|
u.Path = strings.TrimRight(u.Path, "/")
|
|
u.RawPath = strings.TrimRight(u.RawPath, "/")
|
|
}
|
|
|
|
// Invoker invokes operations described by OpenAPI v3 specification.
|
|
type Invoker interface {
|
|
// CreateMLEmbeddings invokes CreateMLEmbeddings operation.
|
|
//
|
|
// 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 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
|
|
CreateMLEmbeddings(ctx context.Context, request *EmbeddingRequest) (CreateMLEmbeddingsRes, error)
|
|
// GetMLHealthz invokes GetMLHealthz operation.
|
|
//
|
|
// Check whether the ML process is responding.
|
|
//
|
|
// GET /healthz
|
|
GetMLHealthz(ctx context.Context) (*MLHealthResponse, error)
|
|
// GetMLReadyz invokes GetMLReadyz operation.
|
|
//
|
|
// Check whether an inference runtime is available.
|
|
//
|
|
// GET /readyz
|
|
GetMLReadyz(ctx context.Context) (GetMLReadyzRes, error)
|
|
}
|
|
|
|
// Client implements OAS client.
|
|
type Client struct {
|
|
serverURL *url.URL
|
|
sec SecuritySource
|
|
baseClient
|
|
}
|
|
|
|
// NewClient initializes new Client defined by OAS.
|
|
func NewClient(serverURL string, sec SecuritySource, opts ...ClientOption) (*Client, error) {
|
|
u, err := url.Parse(serverURL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
trimTrailingSlashes(u)
|
|
|
|
c, err := newClientConfig(opts...).baseClient()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Client{
|
|
serverURL: u,
|
|
sec: sec,
|
|
baseClient: c,
|
|
}, nil
|
|
}
|
|
|
|
type serverURLKey struct{}
|
|
|
|
// WithServerURL sets context key to override server URL.
|
|
func WithServerURL(ctx context.Context, u *url.URL) context.Context {
|
|
return context.WithValue(ctx, serverURLKey{}, u)
|
|
}
|
|
|
|
func (c *Client) requestURL(ctx context.Context) *url.URL {
|
|
u, ok := ctx.Value(serverURLKey{}).(*url.URL)
|
|
if !ok {
|
|
return c.serverURL
|
|
}
|
|
return u
|
|
}
|
|
|
|
// CreateMLEmbeddings invokes CreateMLEmbeddings operation.
|
|
//
|
|
// 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 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
|
|
func (c *Client) CreateMLEmbeddings(ctx context.Context, request *EmbeddingRequest) (CreateMLEmbeddingsRes, error) {
|
|
res, err := c.sendCreateMLEmbeddings(ctx, request)
|
|
return res, err
|
|
}
|
|
|
|
func (c *Client) sendCreateMLEmbeddings(ctx context.Context, request *EmbeddingRequest) (res CreateMLEmbeddingsRes, err error) {
|
|
otelAttrs := []attribute.KeyValue{
|
|
otelogen.OperationID("CreateMLEmbeddings"),
|
|
semconv.HTTPRequestMethodKey.String("POST"),
|
|
semconv.URLTemplateKey.String("/v1/embeddings"),
|
|
}
|
|
otelAttrs = append(otelAttrs, c.cfg.Attributes...)
|
|
|
|
// Run stopwatch.
|
|
startTime := time.Now()
|
|
defer func() {
|
|
// Use floating point division here for higher precision (instead of Millisecond method).
|
|
elapsedDuration := time.Since(startTime)
|
|
c.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), metric.WithAttributes(otelAttrs...))
|
|
}()
|
|
|
|
// Increment request counter.
|
|
c.requests.Add(ctx, 1, metric.WithAttributes(otelAttrs...))
|
|
|
|
// Start a span for this request.
|
|
ctx, span := c.cfg.Tracer.Start(ctx, CreateMLEmbeddingsOperation,
|
|
trace.WithAttributes(otelAttrs...),
|
|
clientSpanKind,
|
|
)
|
|
// Track stage for error reporting.
|
|
var stage string
|
|
defer func() {
|
|
if err != nil {
|
|
span.RecordError(err)
|
|
span.SetStatus(codes.Error, stage)
|
|
c.errors.Add(ctx, 1, metric.WithAttributes(otelAttrs...))
|
|
}
|
|
span.End()
|
|
}()
|
|
|
|
stage = "BuildURL"
|
|
u := uri.Clone(c.requestURL(ctx))
|
|
var pathParts [1]string
|
|
pathParts[0] = "/v1/embeddings"
|
|
uri.AddPathParts(u, pathParts[:]...)
|
|
|
|
stage = "EncodeRequest"
|
|
r, err := ht.NewRequest(ctx, "POST", u)
|
|
if err != nil {
|
|
return res, errors.Wrap(err, "create request")
|
|
}
|
|
if err := encodeCreateMLEmbeddingsRequest(request, r); err != nil {
|
|
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 {
|
|
return res, errors.Wrap(err, "do request")
|
|
}
|
|
body := resp.Body
|
|
defer func() {
|
|
// Drain the body to EOF before closing, so the underlying
|
|
// connection can be reused by the Transport regardless of the
|
|
// response status code. See https://github.com/ogen-go/ogen/issues/1670.
|
|
_, _ = io.Copy(io.Discard, body)
|
|
_ = body.Close()
|
|
}()
|
|
|
|
stage = "DecodeResponse"
|
|
result, err := decodeCreateMLEmbeddingsResponse(resp)
|
|
if err != nil {
|
|
return res, errors.Wrap(err, "decode response")
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// GetMLHealthz invokes GetMLHealthz operation.
|
|
//
|
|
// Check whether the ML process is responding.
|
|
//
|
|
// GET /healthz
|
|
func (c *Client) GetMLHealthz(ctx context.Context) (*MLHealthResponse, error) {
|
|
res, err := c.sendGetMLHealthz(ctx)
|
|
return res, err
|
|
}
|
|
|
|
func (c *Client) sendGetMLHealthz(ctx context.Context) (res *MLHealthResponse, err error) {
|
|
otelAttrs := []attribute.KeyValue{
|
|
otelogen.OperationID("GetMLHealthz"),
|
|
semconv.HTTPRequestMethodKey.String("GET"),
|
|
semconv.URLTemplateKey.String("/healthz"),
|
|
}
|
|
otelAttrs = append(otelAttrs, c.cfg.Attributes...)
|
|
|
|
// Run stopwatch.
|
|
startTime := time.Now()
|
|
defer func() {
|
|
// Use floating point division here for higher precision (instead of Millisecond method).
|
|
elapsedDuration := time.Since(startTime)
|
|
c.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), metric.WithAttributes(otelAttrs...))
|
|
}()
|
|
|
|
// Increment request counter.
|
|
c.requests.Add(ctx, 1, metric.WithAttributes(otelAttrs...))
|
|
|
|
// Start a span for this request.
|
|
ctx, span := c.cfg.Tracer.Start(ctx, GetMLHealthzOperation,
|
|
trace.WithAttributes(otelAttrs...),
|
|
clientSpanKind,
|
|
)
|
|
// Track stage for error reporting.
|
|
var stage string
|
|
defer func() {
|
|
if err != nil {
|
|
span.RecordError(err)
|
|
span.SetStatus(codes.Error, stage)
|
|
c.errors.Add(ctx, 1, metric.WithAttributes(otelAttrs...))
|
|
}
|
|
span.End()
|
|
}()
|
|
|
|
stage = "BuildURL"
|
|
u := uri.Clone(c.requestURL(ctx))
|
|
var pathParts [1]string
|
|
pathParts[0] = "/healthz"
|
|
uri.AddPathParts(u, pathParts[:]...)
|
|
|
|
stage = "EncodeRequest"
|
|
r, err := ht.NewRequest(ctx, "GET", u)
|
|
if err != nil {
|
|
return res, errors.Wrap(err, "create request")
|
|
}
|
|
|
|
stage = "SendRequest"
|
|
resp, err := c.cfg.Client.Do(r)
|
|
if err != nil {
|
|
return res, errors.Wrap(err, "do request")
|
|
}
|
|
body := resp.Body
|
|
defer func() {
|
|
// Drain the body to EOF before closing, so the underlying
|
|
// connection can be reused by the Transport regardless of the
|
|
// response status code. See https://github.com/ogen-go/ogen/issues/1670.
|
|
_, _ = io.Copy(io.Discard, body)
|
|
_ = body.Close()
|
|
}()
|
|
|
|
stage = "DecodeResponse"
|
|
result, err := decodeGetMLHealthzResponse(resp)
|
|
if err != nil {
|
|
return res, errors.Wrap(err, "decode response")
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// GetMLReadyz invokes GetMLReadyz operation.
|
|
//
|
|
// Check whether an inference runtime is available.
|
|
//
|
|
// GET /readyz
|
|
func (c *Client) GetMLReadyz(ctx context.Context) (GetMLReadyzRes, error) {
|
|
res, err := c.sendGetMLReadyz(ctx)
|
|
return res, err
|
|
}
|
|
|
|
func (c *Client) sendGetMLReadyz(ctx context.Context) (res GetMLReadyzRes, err error) {
|
|
otelAttrs := []attribute.KeyValue{
|
|
otelogen.OperationID("GetMLReadyz"),
|
|
semconv.HTTPRequestMethodKey.String("GET"),
|
|
semconv.URLTemplateKey.String("/readyz"),
|
|
}
|
|
otelAttrs = append(otelAttrs, c.cfg.Attributes...)
|
|
|
|
// Run stopwatch.
|
|
startTime := time.Now()
|
|
defer func() {
|
|
// Use floating point division here for higher precision (instead of Millisecond method).
|
|
elapsedDuration := time.Since(startTime)
|
|
c.duration.Record(ctx, float64(elapsedDuration)/float64(time.Millisecond), metric.WithAttributes(otelAttrs...))
|
|
}()
|
|
|
|
// Increment request counter.
|
|
c.requests.Add(ctx, 1, metric.WithAttributes(otelAttrs...))
|
|
|
|
// Start a span for this request.
|
|
ctx, span := c.cfg.Tracer.Start(ctx, GetMLReadyzOperation,
|
|
trace.WithAttributes(otelAttrs...),
|
|
clientSpanKind,
|
|
)
|
|
// Track stage for error reporting.
|
|
var stage string
|
|
defer func() {
|
|
if err != nil {
|
|
span.RecordError(err)
|
|
span.SetStatus(codes.Error, stage)
|
|
c.errors.Add(ctx, 1, metric.WithAttributes(otelAttrs...))
|
|
}
|
|
span.End()
|
|
}()
|
|
|
|
stage = "BuildURL"
|
|
u := uri.Clone(c.requestURL(ctx))
|
|
var pathParts [1]string
|
|
pathParts[0] = "/readyz"
|
|
uri.AddPathParts(u, pathParts[:]...)
|
|
|
|
stage = "EncodeRequest"
|
|
r, err := ht.NewRequest(ctx, "GET", u)
|
|
if err != nil {
|
|
return res, errors.Wrap(err, "create request")
|
|
}
|
|
|
|
stage = "SendRequest"
|
|
resp, err := c.cfg.Client.Do(r)
|
|
if err != nil {
|
|
return res, errors.Wrap(err, "do request")
|
|
}
|
|
body := resp.Body
|
|
defer func() {
|
|
// Drain the body to EOF before closing, so the underlying
|
|
// connection can be reused by the Transport regardless of the
|
|
// response status code. See https://github.com/ogen-go/ogen/issues/1670.
|
|
_, _ = io.Copy(io.Discard, body)
|
|
_ = body.Close()
|
|
}()
|
|
|
|
stage = "DecodeResponse"
|
|
result, err := decodeGetMLReadyzResponse(resp)
|
|
if err != nil {
|
|
return res, errors.Wrap(err, "decode response")
|
|
}
|
|
|
|
return result, nil
|
|
}
|