-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
53 lines (40 loc) · 752 Bytes
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package huggingface
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
)
type Client struct {
*Embedder
*Completer
}
func New(url string, options ...Option) (*Client, error) {
e, err := NewEmbedder(url, options...)
if err != nil {
return nil, err
}
c, err := NewCompleter(url, options...)
if err != nil {
return nil, err
}
return &Client{
Embedder: e,
Completer: c,
}, nil
}
func convertError(resp *http.Response) error {
data, _ := io.ReadAll(resp.Body)
if len(data) == 0 {
return errors.New(http.StatusText(resp.StatusCode))
}
return errors.New(string(data))
}
func jsonReader(v any) io.Reader {
b := new(bytes.Buffer)
enc := json.NewEncoder(b)
enc.SetEscapeHTML(false)
enc.Encode(v)
return b
}