-
Notifications
You must be signed in to change notification settings - Fork 34
/
embedding.go
240 lines (202 loc) · 8.26 KB
/
embedding.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package openai
import (
"context"
"net/http"
"github.com/openai/openai-go/internal/apijson"
"github.com/openai/openai-go/internal/param"
"github.com/openai/openai-go/internal/requestconfig"
"github.com/openai/openai-go/option"
)
// EmbeddingService contains methods and other services that help with interacting
// with the openai 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
}
// Creates an embedding vector representing the input text.
func (r *EmbeddingService) New(ctx context.Context, body EmbeddingNewParams, opts ...option.RequestOption) (res *CreateEmbeddingResponse, err error) {
opts = append(r.Options[:], opts...)
path := "embeddings"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
type CreateEmbeddingResponse struct {
// The list of embeddings generated by the model.
Data []Embedding `json:"data,required"`
// The name of the model used to generate the embedding.
Model string `json:"model,required"`
// The object type, which is always "list".
Object CreateEmbeddingResponseObject `json:"object,required"`
// The usage information for the request.
Usage CreateEmbeddingResponseUsage `json:"usage,required"`
JSON createEmbeddingResponseJSON `json:"-"`
}
// createEmbeddingResponseJSON contains the JSON metadata for the struct
// [CreateEmbeddingResponse]
type createEmbeddingResponseJSON struct {
Data apijson.Field
Model apijson.Field
Object apijson.Field
Usage apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *CreateEmbeddingResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r createEmbeddingResponseJSON) RawJSON() string {
return r.raw
}
// The object type, which is always "list".
type CreateEmbeddingResponseObject string
const (
CreateEmbeddingResponseObjectList CreateEmbeddingResponseObject = "list"
)
func (r CreateEmbeddingResponseObject) IsKnown() bool {
switch r {
case CreateEmbeddingResponseObjectList:
return true
}
return false
}
// The usage information for the request.
type CreateEmbeddingResponseUsage struct {
// The number of tokens used by the prompt.
PromptTokens int64 `json:"prompt_tokens,required"`
// The total number of tokens used by the request.
TotalTokens int64 `json:"total_tokens,required"`
JSON createEmbeddingResponseUsageJSON `json:"-"`
}
// createEmbeddingResponseUsageJSON contains the JSON metadata for the struct
// [CreateEmbeddingResponseUsage]
type createEmbeddingResponseUsageJSON struct {
PromptTokens apijson.Field
TotalTokens apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *CreateEmbeddingResponseUsage) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r createEmbeddingResponseUsageJSON) RawJSON() string {
return r.raw
}
// Represents an embedding vector returned by embedding endpoint.
type Embedding struct {
// The embedding vector, which is a list of floats. The length of vector depends on
// the model as listed in the
// [embedding guide](https://platform.openai.com/docs/guides/embeddings).
Embedding []float64 `json:"embedding,required"`
// The index of the embedding in the list of embeddings.
Index int64 `json:"index,required"`
// The object type, which is always "embedding".
Object EmbeddingObject `json:"object,required"`
JSON embeddingJSON `json:"-"`
}
// embeddingJSON contains the JSON metadata for the struct [Embedding]
type embeddingJSON struct {
Embedding apijson.Field
Index apijson.Field
Object apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *Embedding) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r embeddingJSON) RawJSON() string {
return r.raw
}
// The object type, which is always "embedding".
type EmbeddingObject string
const (
EmbeddingObjectEmbedding EmbeddingObject = "embedding"
)
func (r EmbeddingObject) IsKnown() bool {
switch r {
case EmbeddingObjectEmbedding:
return true
}
return false
}
type EmbeddingModel = string
const (
EmbeddingModelTextEmbeddingAda002 EmbeddingModel = "text-embedding-ada-002"
EmbeddingModelTextEmbedding3Small EmbeddingModel = "text-embedding-3-small"
EmbeddingModelTextEmbedding3Large EmbeddingModel = "text-embedding-3-large"
)
type EmbeddingNewParams struct {
// Input text to embed, encoded as a string or array of tokens. To embed multiple
// inputs in a single request, pass an array of strings or array of token arrays.
// The input must not exceed the max input tokens for the model (8192 tokens for
// `text-embedding-ada-002`), cannot be an empty string, and any array must be 2048
// dimensions or less.
// [Example Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken)
// for counting tokens.
Input param.Field[EmbeddingNewParamsInputUnion] `json:"input,required"`
// ID of the model to use. You can use the
// [List models](https://platform.openai.com/docs/api-reference/models/list) API to
// see all of your available models, or see our
// [Model overview](https://platform.openai.com/docs/models) for descriptions of
// them.
Model param.Field[EmbeddingModel] `json:"model,required"`
// The number of dimensions the resulting output embeddings should have. Only
// supported in `text-embedding-3` and later models.
Dimensions param.Field[int64] `json:"dimensions"`
// The format to return the embeddings in. Can be either `float` or
// [`base64`](https://pypi.org/project/pybase64/).
EncodingFormat param.Field[EmbeddingNewParamsEncodingFormat] `json:"encoding_format"`
// A unique identifier representing your end-user, which can help OpenAI to monitor
// and detect abuse.
// [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
User param.Field[string] `json:"user"`
}
func (r EmbeddingNewParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// Input text to embed, encoded as a string or array of tokens. To embed multiple
// inputs in a single request, pass an array of strings or array of token arrays.
// The input must not exceed the max input tokens for the model (8192 tokens for
// `text-embedding-ada-002`), cannot be an empty string, and any array must be 2048
// dimensions or less.
// [Example Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken)
// for counting tokens.
//
// Satisfied by [shared.UnionString], [EmbeddingNewParamsInputArrayOfStrings],
// [EmbeddingNewParamsInputArrayOfTokens],
// [EmbeddingNewParamsInputArrayOfTokenArrays].
type EmbeddingNewParamsInputUnion interface {
ImplementsEmbeddingNewParamsInputUnion()
}
type EmbeddingNewParamsInputArrayOfStrings []string
func (r EmbeddingNewParamsInputArrayOfStrings) ImplementsEmbeddingNewParamsInputUnion() {}
type EmbeddingNewParamsInputArrayOfTokens []int64
func (r EmbeddingNewParamsInputArrayOfTokens) ImplementsEmbeddingNewParamsInputUnion() {}
type EmbeddingNewParamsInputArrayOfTokenArrays [][]int64
func (r EmbeddingNewParamsInputArrayOfTokenArrays) ImplementsEmbeddingNewParamsInputUnion() {}
// The format to return the embeddings in. Can be either `float` or
// [`base64`](https://pypi.org/project/pybase64/).
type EmbeddingNewParamsEncodingFormat string
const (
EmbeddingNewParamsEncodingFormatFloat EmbeddingNewParamsEncodingFormat = "float"
EmbeddingNewParamsEncodingFormatBase64 EmbeddingNewParamsEncodingFormat = "base64"
)
func (r EmbeddingNewParamsEncodingFormat) IsKnown() bool {
switch r {
case EmbeddingNewParamsEncodingFormatFloat, EmbeddingNewParamsEncodingFormatBase64:
return true
}
return false
}