-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnull.go
More file actions
70 lines (53 loc) · 1.51 KB
/
Copy pathnull.go
File metadata and controls
70 lines (53 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package embeddings
import (
"context"
"net/url"
"strings"
"time"
)
// NullEmbedder implements the `Embedder` interface using an Null API endpoint to derive embeddings.
type NullEmbedder[T Float] struct {
Embedder[T]
precision string
scheme string
}
func init() {
ctx := context.Background()
RegisterEmbedder[float32](ctx, "null", NewNullEmbedder[float32])
RegisterEmbedder[float32](ctx, "null32", NewNullEmbedder[float32])
RegisterEmbedder[float64](ctx, "null64", NewNullEmbedder[float64])
}
func NewNullEmbedder[T Float](ctx context.Context, uri string) (Embedder[T], error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
precision := "float32"
switch {
case strings.HasSuffix(u.Scheme, "64"):
precision = "%s#as-float64"
}
e := &NullEmbedder[T]{
precision: precision,
scheme: u.Scheme,
}
return e, nil
}
func (e *NullEmbedder[T]) TextEmbeddings(ctx context.Context, req *EmbeddingsRequest) (EmbeddingsResponse[T], error) {
return e.nullEmbeddings(ctx, req)
}
func (e *NullEmbedder[T]) ImageEmbeddings(ctx context.Context, req *EmbeddingsRequest) (EmbeddingsResponse[T], error) {
return e.nullEmbeddings(ctx, req)
}
func (e *NullEmbedder[T]) nullEmbeddings(ctx context.Context, req *EmbeddingsRequest) (EmbeddingsResponse[T], error) {
now := time.Now()
ts := now.Unix()
rsp := &CommonEmbeddingsResponse[T]{
CommonId: req.Id,
CommonEmbeddings: make([]T, 0),
CommonModel: "null",
CommonCreated: ts,
CommonPrecision: e.precision,
}
return rsp, nil
}