-
Notifications
You must be signed in to change notification settings - Fork 3
/
google.go
57 lines (46 loc) · 1.66 KB
/
google.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
package google
import (
"context"
"errors"
"fmt"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/net/gclient"
"github.com/iimeta/fastapi-sdk/logger"
"github.com/iimeta/fastapi-sdk/model"
"github.com/iimeta/fastapi-sdk/sdkerr"
)
type Client struct {
key string
baseURL string
path string
proxyURL string
isSupportSystemRole *bool
}
func NewClient(ctx context.Context, model, key, baseURL, path string, isSupportSystemRole *bool, proxyURL ...string) *Client {
logger.Infof(ctx, "NewClient Google model: %s, key: %s", model, key)
client := &Client{
key: key,
baseURL: "https://generativelanguage.googleapis.com/v1beta",
path: "/models/" + model,
isSupportSystemRole: isSupportSystemRole,
}
if baseURL != "" {
logger.Infof(ctx, "NewClient Google model: %s, baseURL: %s", model, baseURL)
client.baseURL = baseURL
}
if path != "" {
logger.Infof(ctx, "NewClient Google model: %s, path: %s", model, path)
client.path = path
}
if len(proxyURL) > 0 && proxyURL[0] != "" {
logger.Infof(ctx, "NewClient Google model: %s, proxyURL: %s", model, proxyURL[0])
client.proxyURL = proxyURL[0]
}
return client
}
func (c *Client) requestErrorHandler(ctx context.Context, response *gclient.Response) (err error) {
return sdkerr.NewRequestError(500, errors.New(fmt.Sprintf("error, status code: %d, response: %s", response.StatusCode, response.ReadAllString())))
}
func (c *Client) apiErrorHandler(response *model.GoogleChatCompletionRes) error {
return sdkerr.NewApiError(500, response.Error.Code, gjson.MustEncodeString(response), "api_error", "")
}