-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
97 lines (85 loc) · 1.72 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
package openai_scheduler
import (
"github.com/sashabaranov/go-openai"
"strings"
)
type Status int
const (
OK Status = iota
Banned
OutOfService
OutOfQuota
)
func (s Status) String() string {
switch s {
case OK:
return "OK"
case Banned:
return "BANNED"
case OutOfService:
return "OOS"
case OutOfQuota:
return "OOQ"
default:
return "Unknown"
}
}
type Client struct {
Raw *openai.Client
Status Status
Identity string
}
func NewClient(token string) (g *Client) {
return &Client{
Raw: openai.NewClient(token),
Status: OK,
Identity: token,
}
}
func NewClientWithIdentity(identity, token string) (g *Client) {
return &Client{
Raw: openai.NewClient(token),
Status: OK,
Identity: identity,
}
}
func NewWithConfig(identity string, config openai.ClientConfig) (g *Client) {
return &Client{
Raw: openai.NewClientWithConfig(config),
Status: OK,
Identity: identity,
}
}
func (c *Client) IsOk() bool {
return c.Status == OK
}
func (c *Client) IsBanned() bool {
return c.Status == Banned
}
func (c *Client) IsOutOfService() bool {
return c.Status == OutOfService
}
func (c *Client) StatusAdjust(e error) {
if e == nil {
return
}
err := e.Error()
if strings.Contains(err, "status code: 200") {
return
}
if strings.Contains(err, "status code: 429") {
if strings.Contains(err, "Your access was terminated due to violation of our policies") {
c.Status = Banned
} else {
c.Status = OutOfService
}
} else if strings.Contains(err, "status code: 403") {
c.Status = Banned
} else if strings.Contains(err, "status code: 401") {
c.Status = Banned
} else if strings.Contains(err, "status code: 503") {
c.Status = OutOfService
} else {
c.Status = OutOfService
}
}