-
Notifications
You must be signed in to change notification settings - Fork 0
/
embeddings.go
73 lines (63 loc) · 1.78 KB
/
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
package together
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
type EmbeddingsRequest struct {
Model string `json:"model"`
Input string `json:"input"`
}
type EmbeddingsResponse struct {
Status string `json:"status"`
Prompt []string `json:"prompt"`
Model string `json:"model"`
ModelOwner string `json:"model_owner"`
Tags Tags `json:"tags"`
NumReturns int `json:"num_returns"`
Args Args `json:"args"`
Subjobs []Subjob `json:"subjobs"`
Output Output `json:"output"`
}
// Embeddings is the endpoint for embedding models on Together AI.
//
// API Reference: https://docs.together.ai/reference/embeddings
func (api *API) Embeddings(ctx context.Context, model string, input string, request EmbeddingsRequest) (EmbeddingsResponse, error) {
if ctx == nil {
return EmbeddingsResponse{}, fmt.Errorf("no context provided")
}
if model == "" {
return EmbeddingsResponse{}, fmt.Errorf("no model provided")
}
if input == "" {
return EmbeddingsResponse{}, fmt.Errorf("no input provided")
}
request.Model = model
request.Input = input
uri := defaultBasePath + Version + "/embeddings"
reqBody, err := json.Marshal(request)
if err != nil {
return EmbeddingsResponse{}, err
}
res, err := api.request(ctx, "POST", uri, bytes.NewBuffer(reqBody), nil)
if err != nil {
return EmbeddingsResponse{}, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return EmbeddingsResponse{}, err
}
if res.StatusCode != http.StatusOK {
return EmbeddingsResponse{}, fmt.Errorf("HTTP request failed: %s", string(body))
}
var embeddingsResponse EmbeddingsResponse
err = json.Unmarshal(body, &embeddingsResponse)
if err != nil {
return EmbeddingsResponse{}, err
}
return embeddingsResponse, nil
}