-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedding.go
More file actions
141 lines (124 loc) · 5.08 KB
/
embedding.go
File metadata and controls
141 lines (124 loc) · 5.08 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package together
import (
"context"
"net/http"
"slices"
"github.com/togethercomputer/together-go/internal/apijson"
"github.com/togethercomputer/together-go/internal/requestconfig"
"github.com/togethercomputer/together-go/option"
"github.com/togethercomputer/together-go/packages/param"
"github.com/togethercomputer/together-go/packages/respjson"
"github.com/togethercomputer/together-go/shared/constant"
)
// EmbeddingService contains methods and other services that help with interacting
// with the together API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewEmbeddingService] method instead.
type EmbeddingService struct {
Options []option.RequestOption
}
// NewEmbeddingService generates a new service that applies the given options to
// each request. These options are applied after the parent client's options (if
// there is one), and before any request-specific options.
func NewEmbeddingService(opts ...option.RequestOption) (r EmbeddingService) {
r = EmbeddingService{}
r.Options = opts
return
}
// Generate vector embeddings for one or more text inputs. Returns numerical arrays
// representing semantic meaning, useful for search, classification, and retrieval.
func (r *EmbeddingService) New(ctx context.Context, body EmbeddingNewParams, opts ...option.RequestOption) (res *Embedding, err error) {
opts = slices.Concat(r.Options, opts)
path := "embeddings"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return res, err
}
type Embedding struct {
Data []EmbeddingData `json:"data" api:"required"`
Model string `json:"model" api:"required"`
// The object type, which is always `list`.
Object constant.List `json:"object" default:"list"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Data respjson.Field
Model respjson.Field
Object respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r Embedding) RawJSON() string { return r.JSON.raw }
func (r *Embedding) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type EmbeddingData struct {
Embedding []float64 `json:"embedding" api:"required"`
Index int64 `json:"index" api:"required"`
// The object type, which is always `embedding`.
Object constant.Embedding `json:"object" default:"embedding"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Embedding respjson.Field
Index respjson.Field
Object respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r EmbeddingData) RawJSON() string { return r.JSON.raw }
func (r *EmbeddingData) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type EmbeddingNewParams struct {
// A string providing the text for the model to embed.
Input EmbeddingNewParamsInputUnion `json:"input,omitzero" api:"required"`
// The name of the embedding model to use.
//
// [See all of Together AI's embedding models](https://docs.together.ai/docs/serverless-models#embedding-models)
Model EmbeddingNewParamsModel `json:"model,omitzero" api:"required"`
paramObj
}
func (r EmbeddingNewParams) MarshalJSON() (data []byte, err error) {
type shadow EmbeddingNewParams
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *EmbeddingNewParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Only one field can be non-zero.
//
// Use [param.IsOmitted] to confirm if a field is set.
type EmbeddingNewParamsInputUnion struct {
OfString param.Opt[string] `json:",omitzero,inline"`
OfStringArray []string `json:",omitzero,inline"`
paramUnion
}
func (u EmbeddingNewParamsInputUnion) MarshalJSON() ([]byte, error) {
return param.MarshalUnion(u, u.OfString, u.OfStringArray)
}
func (u *EmbeddingNewParamsInputUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, u)
}
func (u *EmbeddingNewParamsInputUnion) asAny() any {
if !param.IsOmitted(u.OfString) {
return &u.OfString.Value
} else if !param.IsOmitted(u.OfStringArray) {
return &u.OfStringArray
}
return nil
}
// The name of the embedding model to use.
//
// [See all of Together AI's embedding models](https://docs.together.ai/docs/serverless-models#embedding-models)
type EmbeddingNewParamsModel string
const (
EmbeddingNewParamsModelWhereIsAIUaeLargeV1 EmbeddingNewParamsModel = "WhereIsAI/UAE-Large-V1"
EmbeddingNewParamsModelBaaiBgeLargeEnV1_5 EmbeddingNewParamsModel = "BAAI/bge-large-en-v1.5"
EmbeddingNewParamsModelBaaiBgeBaseEnV1_5 EmbeddingNewParamsModel = "BAAI/bge-base-en-v1.5"
EmbeddingNewParamsModelTogethercomputerM2Bert80M8kRetrieval EmbeddingNewParamsModel = "togethercomputer/m2-bert-80M-8k-retrieval"
)