-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathvertexadapter.go
95 lines (83 loc) · 2.24 KB
/
vertexadapter.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
package anthropic
import (
"encoding/json"
"fmt"
"net/http"
)
var _ ClientAdapter = (*VertexAdapter)(nil)
type VertexAdapter struct {
}
func (v *VertexAdapter) TranslateError(resp *http.Response, body []byte) (error, bool) {
switch resp.StatusCode {
case http.StatusUnauthorized,
http.StatusForbidden,
http.StatusNotFound,
http.StatusTooManyRequests:
var errRes VertexAIErrorResponse
err := json.Unmarshal(body, &errRes)
if err != nil {
// it could be an array
var errResArr []VertexAIErrorResponse
err = json.Unmarshal(body, &errResArr)
if err == nil && len(errResArr) > 0 {
errRes = errResArr[0]
}
}
if err != nil || errRes.Error == nil {
reqErr := RequestError{
StatusCode: resp.StatusCode,
Err: err,
Body: body,
}
return &reqErr, true
}
return fmt.Errorf(
"error, status code: %d, message: %w",
resp.StatusCode,
errRes.Error,
), true
}
return nil, false
}
func (v *VertexAdapter) fullURL(baseUrl string, suffix string, model Model) string {
// replace the first slash with a colon
return fmt.Sprintf("%s/%s:%s", baseUrl, model.asVertexModel(), suffix[1:])
}
func (v *VertexAdapter) translateUrlSuffix(suffix string, stream bool) (string, error) {
switch suffix {
case "/messages":
if stream {
return ":streamRawPredict", nil
} else {
return ":rawPredict", nil
}
}
return "", fmt.Errorf("unknown suffix: %s", suffix)
}
func (v *VertexAdapter) PrepareRequest(
c *Client,
method string,
urlSuffix string,
body any,
) (string, error) {
// if the body implements the ModelGetter interface, use the model from the body
model := Model("")
if body != nil {
if vertexAISupport, ok := body.(VertexAISupport); ok {
model = vertexAISupport.GetModel()
vertexAISupport.SetAnthropicVersion(c.config.APIVersion)
var err error
urlSuffix, err = v.translateUrlSuffix(urlSuffix, vertexAISupport.IsStreaming())
if err != nil {
return "", err
}
} else {
return "", fmt.Errorf("this call is not supported by the Vertex AI API")
}
}
return v.fullURL(c.config.BaseURL, urlSuffix, model), nil
}
func (v *VertexAdapter) SetRequestHeaders(c *Client, req *http.Request) error {
req.Header.Set("Authorization", "Bearer "+c.config.GetApiKey())
return nil
}