Skip to content

Commit

Permalink
chore: refactor code to simplify the understanding
Browse files Browse the repository at this point in the history
  • Loading branch information
henomis committed Sep 9, 2023
1 parent 6089ca0 commit 250a433
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions embeddings.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,25 +277,27 @@ func (r EmbeddingRequestTokens) Convert() EmbeddingRequest {
//
// Body should be of type EmbeddingRequestStrings for embedding strings or EmbeddingRequestTokens
// for embedding groups of text already converted to tokens.
func (c *Client) CreateEmbeddings(ctx context.Context, conv EmbeddingRequestConverter) (EmbeddingResponse, error) { //nolint:lll
func (c *Client) CreateEmbeddings(
ctx context.Context,
conv EmbeddingRequestConverter,
) (res EmbeddingResponse, err error) {
baseReq := conv.Convert()
response := EmbeddingResponse{}

req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/embeddings", baseReq.Model.String()), withBody(baseReq))
if err != nil {
return response, err
return
}

var embeddingResponse any = &response
if baseReq.EncodingFormat == EmbeddingEncodingFormatBase64 {
embeddingResponse = &EmbeddingResponseBase64{}
if baseReq.EncodingFormat != EmbeddingEncodingFormatBase64 {
err = c.sendRequest(req, &res)
return
}

err = c.sendRequest(req, embeddingResponse)

if baseReq.EncodingFormat == EmbeddingEncodingFormatBase64 {
response, err = embeddingResponse.(*EmbeddingResponseBase64).ToEmbeddingResponse()
base64Response := &EmbeddingResponseBase64{}
err = c.sendRequest(req, base64Response)
if err != nil {
return
}

return response, err
res, err = base64Response.ToEmbeddingResponse()
return
}

0 comments on commit 250a433

Please sign in to comment.