-
Notifications
You must be signed in to change notification settings - Fork 3
/
text_embeddings.go
95 lines (83 loc) · 2.13 KB
/
text_embeddings.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package vertexai
import (
"context"
"errors"
"fmt"
"net/http"
)
// TextEmbeddingRequest is the request for the TextEmbedding method
type TextEmbeddingRequest struct {
ProjectID string
EndpointID string
/*
Content is Deprecated. Use Inputs instead
*/
Content string
Inputs []string
}
// TextEmbeddingResponse is the response for the TextEmbedding method
type TextEmbeddingResponse struct {
Predictions []Predictions `json:"predictions"`
}
// Predictions is the generated response
type Predictions struct {
Embedding Embedding `json:"embeddings"`
}
// Embedding is the embedding for text
type Embedding struct {
Values []float64 `json:"values"`
Statistics EmbeddingStats `json:"statistics"`
}
// EmbeddingStats define the statistics for a text embedding
type EmbeddingStats struct {
TokenCount int `json:"token_count"`
Truncated bool `json:"truncated"`
}
const (
textEmbeddingEndpoint = ":predict"
)
func (c *client) TextEmbedding(
ctx context.Context,
req TextEmbeddingRequest,
) (*TextEmbeddingResponse, error) {
payload, err := c.getTextEmbeddingPayload(req)
if err != nil {
return nil, err
}
httpReq, err := c.requestBuilder.build(
ctx,
http.MethodPost,
req.ProjectID,
req.EndpointID,
textGenerationEndpoint,
payload,
)
if err != nil {
return nil, fmt.Errorf("failed to create http request for predict endpoint: %v", err)
}
resp := &TextEmbeddingResponse{}
err = c.sendRequest(httpReq, resp)
if err != nil {
return nil, fmt.Errorf("failed to send request to predict endpoint: %v", err)
}
return resp, nil
}
func (c *client) getTextEmbeddingPayload(
req TextEmbeddingRequest,
) (payload, error) {
payload := payload{}
if req.Content != "" && len(req.Inputs) > 0 {
return payload, errors.New("received Content and Inputs both, only expected one")
}
if req.Content != "" {
payload.Instances = []inputInstances{{Content: req.Content}}
} else { // Use Inputs instead of Content
payload.Instances = make([]inputInstances, 0, len(req.Inputs))
for _, input := range req.Inputs {
payload.Instances = append(payload.Instances, inputInstances{
Content: input,
})
}
}
return payload, nil
}