@@ -113,10 +113,25 @@ type EmbeddingResponse struct {
113
113
Usage Usage `json:"usage"`
114
114
}
115
115
116
- // EmbeddingRequest is the input to a Create embeddings request.
116
+ type EmbeddingRequestConverter interface {
117
+ // Needs to be of type EmbeddingRequestStrings or EmbeddingRequestTokens
118
+ Convert () EmbeddingRequest
119
+ }
120
+
117
121
type EmbeddingRequest struct {
122
+ Input any `json:"input"`
123
+ Model EmbeddingModel `json:"model"`
124
+ User string `json:"user"`
125
+ }
126
+
127
+ func (r EmbeddingRequest ) Convert () EmbeddingRequest {
128
+ return r
129
+ }
130
+
131
+ // EmbeddingRequestStrings is the input to a create embeddings request with a slice of strings.
132
+ type EmbeddingRequestStrings struct {
118
133
// Input is a slice of strings for which you want to generate an Embedding vector.
119
- // Each input must not exceed 2048 tokens in length.
134
+ // Each input must not exceed 8192 tokens in length.
120
135
// OpenAPI suggests replacing newlines (\n) in your input with a single space, as they
121
136
// have observed inferior results when newlines are present.
122
137
// E.g.
@@ -129,15 +144,50 @@ type EmbeddingRequest struct {
129
144
User string `json:"user"`
130
145
}
131
146
132
- // CreateEmbeddings returns an EmbeddingResponse which will contain an Embedding for every item in |request.Input|.
147
+ func (r EmbeddingRequestStrings ) Convert () EmbeddingRequest {
148
+ return EmbeddingRequest {
149
+ Input : r .Input ,
150
+ Model : r .Model ,
151
+ User : r .User ,
152
+ }
153
+ }
154
+
155
+ type EmbeddingRequestTokens struct {
156
+ // Input is a slice of slices of ints ([][]int) for which you want to generate an Embedding vector.
157
+ // Each input must not exceed 8192 tokens in length.
158
+ // OpenAPI suggests replacing newlines (\n) in your input with a single space, as they
159
+ // have observed inferior results when newlines are present.
160
+ // E.g.
161
+ // "The food was delicious and the waiter..."
162
+ Input [][]int `json:"input"`
163
+ // ID of the model to use. You can use the List models API to see all of your available models,
164
+ // or see our Model overview for descriptions of them.
165
+ Model EmbeddingModel `json:"model"`
166
+ // A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse.
167
+ User string `json:"user"`
168
+ }
169
+
170
+ func (r EmbeddingRequestTokens ) Convert () EmbeddingRequest {
171
+ return EmbeddingRequest {
172
+ Input : r .Input ,
173
+ Model : r .Model ,
174
+ User : r .User ,
175
+ }
176
+ }
177
+
178
+ // CreateEmbeddings returns an EmbeddingResponse which will contain an Embedding for every item in |body.Input|.
133
179
// https://beta.openai.com/docs/api-reference/embeddings/create
134
- func (c * Client ) CreateEmbeddings (ctx context.Context , request EmbeddingRequest ) (resp EmbeddingResponse , err error ) {
135
- req , err := c .newRequest (ctx , http .MethodPost , c .fullURL ("/embeddings" , request .Model .String ()), withBody (request ))
180
+ //
181
+ // Body should be of type EmbeddingRequestStrings for embedding strings or EmbeddingRequestTokens
182
+ // for embedding groups of text already converted to tokens.
183
+ func (c * Client ) CreateEmbeddings (ctx context.Context , conv EmbeddingRequestConverter ) (res EmbeddingResponse , err error ) { //nolint:lll
184
+ baseReq := conv .Convert ()
185
+ req , err := c .newRequest (ctx , http .MethodPost , c .fullURL ("/embeddings" , baseReq .Model .String ()), withBody (baseReq ))
136
186
if err != nil {
137
187
return
138
188
}
139
189
140
- err = c .sendRequest (req , & resp )
190
+ err = c .sendRequest (req , & res )
141
191
142
192
return
143
193
}
0 commit comments