github.com/weaviate/weaviate@v1.24.6/modules/text2vec-gpt4all/clients/gpt4all.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package clients 13 14 import ( 15 "bytes" 16 "context" 17 "encoding/json" 18 "fmt" 19 "io" 20 "net/http" 21 "time" 22 23 "github.com/pkg/errors" 24 "github.com/sirupsen/logrus" 25 "github.com/weaviate/weaviate/modules/text2vec-gpt4all/ent" 26 ) 27 28 type client struct { 29 origin string 30 httpClient *http.Client 31 logger logrus.FieldLogger 32 } 33 34 func New(origin string, timeout time.Duration, logger logrus.FieldLogger) *client { 35 return &client{ 36 origin: origin, 37 httpClient: &http.Client{ 38 Timeout: timeout, 39 }, 40 logger: logger, 41 } 42 } 43 44 func (c *client) Vectorize(ctx context.Context, text string) (*ent.VectorizationResult, error) { 45 body, err := json.Marshal(vecRequest{text}) 46 if err != nil { 47 return nil, errors.Wrapf(err, "marshal body") 48 } 49 50 req, err := http.NewRequestWithContext(ctx, "POST", c.url("/vectorize"), 51 bytes.NewReader(body)) 52 if err != nil { 53 return nil, errors.Wrap(err, "create POST request") 54 } 55 56 res, err := c.httpClient.Do(req) 57 if err != nil { 58 return nil, errors.Wrap(err, "send POST request") 59 } 60 defer res.Body.Close() 61 62 bodyBytes, err := io.ReadAll(res.Body) 63 if err != nil { 64 return nil, errors.Wrap(err, "read response body") 65 } 66 67 var resBody vecResponse 68 if err := json.Unmarshal(bodyBytes, &resBody); err != nil { 69 return nil, errors.Wrap(err, "unmarshal response body") 70 } 71 72 if res.StatusCode != 200 { 73 if resBody.Error != "" { 74 return nil, errors.Errorf("failed with status: %d error: %v", res.StatusCode, resBody.Error) 75 } 76 return nil, errors.Errorf("failed with status: %d", res.StatusCode) 77 } 78 79 return &ent.VectorizationResult{ 80 Vector: resBody.Vector, 81 Dimensions: resBody.Dim, 82 Text: resBody.Text, 83 }, nil 84 } 85 86 func (c *client) url(path string) string { 87 return fmt.Sprintf("%s%s", c.origin, path) 88 } 89 90 type vecRequest struct { 91 Text string `json:"text"` 92 } 93 94 type vecResponse struct { 95 Text string `json:"text"` 96 Vector []float32 `json:"vector"` 97 Dim int `json:"dim"` 98 Error string `json:"error"` 99 }