From 250a43354debd9cfac065c1ceaa48374a1a4a248 Mon Sep 17 00:00:00 2001 From: Simone Vellei Date: Sat, 9 Sep 2023 19:05:10 +0200 Subject: [PATCH] chore: refactor code to simplify the understanding --- embeddings.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/embeddings.go b/embeddings.go index ef708b05a..5ba91f235 100644 --- a/embeddings.go +++ b/embeddings.go @@ -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 }