-
Notifications
You must be signed in to change notification settings - Fork 13
/
client.go
133 lines (114 loc) · 2.94 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package mistral
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"time"
)
const (
Endpoint = "https://api.mistral.ai"
CodestralEndpoint = "https://codestral.mistral.ai"
DefaultMaxRetries = 5
DefaultTimeout = 120 * time.Second
)
var retryStatusCodes = map[int]bool{
429: true,
500: true,
502: true,
503: true,
504: true,
}
type MistralClient struct {
apiKey string
endpoint string
maxRetries int
timeout time.Duration
}
func NewMistralClient(apiKey string, endpoint string, maxRetries int, timeout time.Duration) *MistralClient {
if apiKey == "" {
apiKey = os.Getenv("MISTRAL_API_KEY")
}
if endpoint == "" {
endpoint = Endpoint
}
if maxRetries == 0 {
maxRetries = DefaultMaxRetries
}
if timeout == 0 {
timeout = DefaultTimeout
}
return &MistralClient{
apiKey: apiKey,
endpoint: endpoint,
maxRetries: maxRetries,
timeout: timeout,
}
}
// NewMistralClientDefault creates a new Mistral API client with the default endpoint and the given API key. Defaults to using MISTRAL_API_KEY from the environment.
func NewMistralClientDefault(apiKey string) *MistralClient {
if apiKey == "" {
apiKey = os.Getenv("MISTRAL_API_KEY")
}
return NewMistralClient(apiKey, Endpoint, DefaultMaxRetries, DefaultTimeout)
}
// NewCodestralClientDefault creates a new Codestral API client with the default endpoint and the given API key. Defaults to using CODESTRAL_API_KEY from the environment.
func NewCodestralClientDefault(apiKey string) *MistralClient {
if apiKey == "" {
apiKey = os.Getenv("CODESTRAL_API_KEY")
}
return NewMistralClient(apiKey, CodestralEndpoint, DefaultMaxRetries, DefaultTimeout)
}
func (c *MistralClient) request(method string, jsonData map[string]interface{}, path string, stream bool, params map[string]string) (interface{}, error) {
uri, err := url.Parse(c.endpoint)
if err != nil {
return nil, err
}
uri.Path = path
jsonValue, _ := json.Marshal(jsonData)
req, err := http.NewRequest(method, uri.String(), bytes.NewBuffer(jsonValue))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+c.apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{
Timeout: c.timeout,
}
var resp *http.Response
for i := 0; i < c.maxRetries; i++ {
resp, err = client.Do(req)
if err != nil {
if i == c.maxRetries-1 {
return nil, err
}
continue
}
if _, ok := retryStatusCodes[resp.StatusCode]; ok {
time.Sleep(time.Duration(i+1) * 500 * time.Millisecond)
continue
}
break
}
if resp.StatusCode >= 400 {
responseBytes, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("(HTTP Error %d) %s", resp.StatusCode, string(responseBytes))
}
if stream {
return resp.Body, nil
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var result map[string]interface{}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return result, nil
}