Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit33e9b4f

Browse files
committed
Improve documentation, add tests
1 parentbbc9f63 commit33e9b4f

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

‎gpt3.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ type Client interface {
9191
SearchWithEngine(ctx context.Context,enginestring,requestSearchRequest) (*SearchResponse,error)
9292

9393
// Returns an embedding using the provided request.
94-
Embedding(ctx context.Context,requestEmbeddingRequest) (*EmbeddingResponse,error)
94+
Embeddings(ctx context.Context,requestEmbeddingsRequest) (*EmbeddingsResponse,error)
9595
}
9696

9797
typeclientstruct {
@@ -269,8 +269,10 @@ func (c *client) SearchWithEngine(ctx context.Context, engine string, request Se
269269
returnoutput,nil
270270
}
271271

272-
// EmbeddingWithEngine creates an embedding and allows overriding the engine
273-
func (c*client)Embedding(ctx context.Context,requestEmbeddingRequest) (*EmbeddingResponse,error) {
272+
// Embeddings creates text embeddings for a supplied slice of inputs with a provided model.
273+
//
274+
// See: https://beta.openai.com/docs/api-reference/embeddings
275+
func (c*client)Embeddings(ctx context.Context,requestEmbeddingsRequest) (*EmbeddingsResponse,error) {
274276
req,err:=c.newRequest(ctx,"POST","/embeddings",request)
275277
iferr!=nil {
276278
returnnil,err
@@ -280,11 +282,11 @@ func (c *client) Embedding(ctx context.Context, request EmbeddingRequest) (*Embe
280282
returnnil,err
281283
}
282284

283-
output:=new(EmbeddingResponse)
284-
iferr:=getResponseObject(resp,output);err!=nil {
285+
output:=EmbeddingsResponse{}
286+
iferr:=getResponseObject(resp,&output);err!=nil {
285287
returnnil,err
286288
}
287-
returnoutput,nil
289+
return&output,nil
288290
}
289291

290292
func (c*client)performRequest(req*http.Request) (*http.Response,error) {

‎gpt3_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ func TestRequestCreationFails(t *testing.T) {
106106
returnclient.SearchWithEngine(ctx,gpt3.AdaEngine, gpt3.SearchRequest{})
107107
},
108108
"Post\"https://api.openai.com/v1/engines/ada/search\": request error",
109+
}, {
110+
"Embedding",
111+
func() (interface{},error) {
112+
returnclient.Embeddings(ctx, gpt3.EmbeddingsRequest{})
113+
},
114+
"Post\"https://api.openai.com/v1/embeddings\": request error",
109115
},
110116
}
111117

@@ -246,6 +252,20 @@ func TestResponses(t *testing.T) {
246252
},
247253
},
248254
},
255+
}, {
256+
"Embedding",
257+
func() (interface{},error) {
258+
returnclient.Embeddings(ctx, gpt3.EmbeddingsRequest{})
259+
},
260+
&gpt3.EmbeddingsResponse{
261+
Object:"list",
262+
Data: []gpt3.EmbeddingsResult{{
263+
Object:"object",
264+
Embedding: []float64{0.1,0.2,0.3},
265+
Index:0,
266+
}},
267+
Model:gpt3.TextSimilarityBabbage001,
268+
},
249269
},
250270
}
251271

‎models.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,21 @@ type EditsRequest struct {
7777
N*int`json:"n"`
7878
}
7979

80-
// EmbeddingRequest is a request for the Embeddings API
81-
typeEmbeddingRequeststruct {
80+
// EmbeddingsRequest is a request for the Embeddings API
81+
typeEmbeddingsRequeststruct {
82+
// Input text to get embeddings for, encoded as a string or array of tokens. To get embeddings
83+
// for multiple inputs in a single request, pass an array of strings or array of token arrays.
84+
// Each input must not exceed 2048 tokens in length.
8285
Input []string`json:"input"`
83-
Modelstring`json:"model"`
84-
Userstring`json:"user,omitempty"`
86+
// ID of the model to use
87+
Modelstring`json:"model"`
88+
// The request user is an optional parameter meant to be used to trace abusive requests
89+
// back to the originating user. OpenAI states:
90+
// "The [user] IDs should be a string that uniquely identifies each user. We recommend hashing
91+
// their username or email address, in order to avoid sending us any identifying information.
92+
// If you offer a preview of your product to non-logged in users, you can send a session ID
93+
// instead."
94+
Userstring`json:"user,omitempty"`
8595
}
8696

8797
// LogprobResult represents logprob result of Choice
@@ -117,17 +127,17 @@ type EditsResponse struct {
117127
UsageEditsResponseUsage`json:"usage"`
118128
}
119129

120-
typeEmbeddingResultstruct {
130+
typeEmbeddingsResultstruct {
121131
Objectstring`json:"object"`
122132
Embedding []float64`json:"embedding"`
123133
Indexint`json:"index"`
124134
}
125135

126-
//EmbeddingResponse is the response from a Create embeddings request.
127-
typeEmbeddingResponsestruct {
128-
Objectstring`json:"object"`
129-
Data []EmbeddingResult`json:"data"`
130-
Modelstring`json:"model"`
136+
//EmbeddingsResponse is the response from a Create embeddings request.
137+
typeEmbeddingsResponsestruct {
138+
Objectstring`json:"object"`
139+
Data []EmbeddingsResult`json:"data"`
140+
Modelstring`json:"model"`
131141
}
132142

133143
// EditsResponseChoice is one of the choices returned in the response to the Edits API

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp