- Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathembedding.go
206 lines (184 loc) · 8.03 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
// 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/requestconfig"
"github.com/openai/openai-go/option"
"github.com/openai/openai-go/packages/param"
"github.com/openai/openai-go/packages/resp"
"github.com/openai/openai-go/shared/constant"
)
// 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.
typeEmbeddingServicestruct {
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.
funcNewEmbeddingService(opts...option.RequestOption) (rEmbeddingService) {
r=EmbeddingService{}
r.Options=opts
return
}
// Creates an embedding vector representing the input text.
func (r*EmbeddingService) New(ctx context.Context, bodyEmbeddingNewParams, opts...option.RequestOption) (res*CreateEmbeddingResponse, errerror) {
opts=append(r.Options[:], opts...)
path:="embeddings"
err=requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
typeCreateEmbeddingResponsestruct {
// The list of embeddings generated by the model.
Data []Embedding`json:"data,required"`
// The name of the model used to generate the embedding.
Modelstring`json:"model,required"`
// The object type, which is always "list".
Object constant.List`json:"object,required"`
// The usage information for the request.
UsageCreateEmbeddingResponseUsage`json:"usage,required"`
// Metadata for the response, check the presence of optional fields with the
// [resp.Field.IsPresent] method.
JSONstruct {
Data resp.Field
Model resp.Field
Object resp.Field
Usage resp.Field
ExtraFieldsmap[string]resp.Field
rawstring
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (rCreateEmbeddingResponse) RawJSON() string { returnr.JSON.raw }
func (r*CreateEmbeddingResponse) UnmarshalJSON(data []byte) error {
returnapijson.UnmarshalRoot(data, r)
}
// The usage information for the request.
typeCreateEmbeddingResponseUsagestruct {
// The number of tokens used by the prompt.
PromptTokensint64`json:"prompt_tokens,required"`
// The total number of tokens used by the request.
TotalTokensint64`json:"total_tokens,required"`
// Metadata for the response, check the presence of optional fields with the
// [resp.Field.IsPresent] method.
JSONstruct {
PromptTokens resp.Field
TotalTokens resp.Field
ExtraFieldsmap[string]resp.Field
rawstring
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (rCreateEmbeddingResponseUsage) RawJSON() string { returnr.JSON.raw }
func (r*CreateEmbeddingResponseUsage) UnmarshalJSON(data []byte) error {
returnapijson.UnmarshalRoot(data, r)
}
// Represents an embedding vector returned by embedding endpoint.
typeEmbeddingstruct {
// 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.
Indexint64`json:"index,required"`
// The object type, which is always "embedding".
Object constant.Embedding`json:"object,required"`
// Metadata for the response, check the presence of optional fields with the
// [resp.Field.IsPresent] method.
JSONstruct {
Embedding resp.Field
Index resp.Field
Object resp.Field
ExtraFieldsmap[string]resp.Field
rawstring
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (rEmbedding) RawJSON() string { returnr.JSON.raw }
func (r*Embedding) UnmarshalJSON(data []byte) error {
returnapijson.UnmarshalRoot(data, r)
}
typeEmbeddingModel=string
const (
EmbeddingModelTextEmbeddingAda002EmbeddingModel="text-embedding-ada-002"
EmbeddingModelTextEmbedding3SmallEmbeddingModel="text-embedding-3-small"
EmbeddingModelTextEmbedding3LargeEmbeddingModel="text-embedding-3-large"
)
typeEmbeddingNewParamsstruct {
// 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. Some models may also impose a limit on total number of
// tokens summed across inputs.
InputEmbeddingNewParamsInputUnion`json:"input,omitzero,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.
ModelEmbeddingModel`json:"model,omitzero,required"`
// The number of dimensions the resulting output embeddings should have. Only
// supported in `text-embedding-3` and later models.
Dimensions param.Opt[int64] `json:"dimensions,omitzero"`
// 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.Opt[string] `json:"user,omitzero"`
// The format to return the embeddings in. Can be either `float` or
// [`base64`](https://pypi.org/project/pybase64/).
//
// Any of "float", "base64".
EncodingFormatEmbeddingNewParamsEncodingFormat`json:"encoding_format,omitzero"`
paramObj
}
// IsPresent returns true if the field's value is not omitted and not the JSON
// "null". To check if this field is omitted, use [param.IsOmitted].
func (fEmbeddingNewParams) IsPresent() bool { return!param.IsOmitted(f) &&!f.IsNull() }
func (rEmbeddingNewParams) MarshalJSON() (data []byte, errerror) {
typeshadowEmbeddingNewParams
returnparam.MarshalObject(r, (*shadow)(&r))
}
// Only one field can be non-zero.
//
// Use [param.IsOmitted] to confirm if a field is set.
typeEmbeddingNewParamsInputUnionstruct {
OfString param.Opt[string] `json:",omitzero,inline"`
OfArrayOfStrings []string`json:",omitzero,inline"`
OfArrayOfTokens []int64`json:",omitzero,inline"`
OfArrayOfTokenArrays [][]int64`json:",omitzero,inline"`
paramUnion
}
// IsPresent returns true if the field's value is not omitted and not the JSON
// "null". To check if this field is omitted, use [param.IsOmitted].
func (uEmbeddingNewParamsInputUnion) IsPresent() bool { return!param.IsOmitted(u) &&!u.IsNull() }
func (uEmbeddingNewParamsInputUnion) MarshalJSON() ([]byte, error) {
returnparam.MarshalUnion[EmbeddingNewParamsInputUnion](u.OfString, u.OfArrayOfStrings, u.OfArrayOfTokens, u.OfArrayOfTokenArrays)
}
func (u*EmbeddingNewParamsInputUnion) asAny() any {
if!param.IsOmitted(u.OfString) {
return&u.OfString.Value
} elseif!param.IsOmitted(u.OfArrayOfStrings) {
return&u.OfArrayOfStrings
} elseif!param.IsOmitted(u.OfArrayOfTokens) {
return&u.OfArrayOfTokens
} elseif!param.IsOmitted(u.OfArrayOfTokenArrays) {
return&u.OfArrayOfTokenArrays
}
returnnil
}
// The format to return the embeddings in. Can be either `float` or
// [`base64`](https://pypi.org/project/pybase64/).
typeEmbeddingNewParamsEncodingFormatstring
const (
EmbeddingNewParamsEncodingFormatFloatEmbeddingNewParamsEncodingFormat="float"
EmbeddingNewParamsEncodingFormatBase64EmbeddingNewParamsEncodingFormat="base64"
)