|
| 1 | +package vertexai |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + |
| 12 | + "golang.org/x/oauth2" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + // BaseURL is Vertex AI HTTP API base URL |
| 17 | + BaseURL = "https://us-central1-aiplatform.googleapis.com/v1/projects" |
| 18 | + // ModelURI is Vertex AI HTTP API model URI. |
| 19 | + ModelURI = "locations/us-central1/publishers/google/models" |
| 20 | + // EmbedAction is embedding API action. |
| 21 | + EmbedAction = ":predict" |
| 22 | +) |
| 23 | + |
| 24 | +// Client is vertex AI HTTP API client. |
| 25 | +type Client struct { |
| 26 | + token string |
| 27 | + tokenSrc oauth2.TokenSource |
| 28 | + projectID string |
| 29 | + modelID string |
| 30 | + baseURL string |
| 31 | + hc *http.Client |
| 32 | +} |
| 33 | + |
| 34 | +// NewClient creates a new HTTP client and returns it. |
| 35 | +// It reads the Google API token from VERTEXAI_TOKEN env var |
| 36 | +// just like the project ID is read from GOOGLE_PROJECT_ID env var |
| 37 | +// and uses the default Go http.Client. |
| 38 | +// You can override the default options by using the |
| 39 | +// client methods. |
| 40 | +func NewClient() (*Client, error) { |
| 41 | + return &Client{ |
| 42 | + token: os.Getenv("VERTEXAI_TOKEN"), |
| 43 | + modelID: os.Getenv("VERTEXAI_MODEL_ID"), |
| 44 | + projectID: os.Getenv("GOOGLE_PROJECT_ID"), |
| 45 | + baseURL: BaseURL, |
| 46 | + hc: &http.Client{}, |
| 47 | + }, nil |
| 48 | +} |
| 49 | + |
| 50 | +// WithToken sets the API key. |
| 51 | +func (c *Client) WithToken(token string) *Client { |
| 52 | + c.token = token |
| 53 | + return c |
| 54 | +} |
| 55 | + |
| 56 | +// WithTokenSrc sets the API token source. |
| 57 | +func (c *Client) WithTokenSrc(ts oauth2.TokenSource) *Client { |
| 58 | + c.tokenSrc = ts |
| 59 | + return c |
| 60 | +} |
| 61 | + |
| 62 | +// WithProjectID sets the project ID. |
| 63 | +func (c *Client) WithProjectID(id string) *Client { |
| 64 | + c.projectID = id |
| 65 | + return c |
| 66 | +} |
| 67 | + |
| 68 | +// WithModelID sets the model ID. |
| 69 | +func (c *Client) WithModelID(id string) *Client { |
| 70 | + c.modelID = id |
| 71 | + return c |
| 72 | +} |
| 73 | + |
| 74 | +// WithBaseURL sets the API base URL. |
| 75 | +func (c *Client) WithBaseURL(baseURL string) *Client { |
| 76 | + c.baseURL = baseURL |
| 77 | + return c |
| 78 | +} |
| 79 | + |
| 80 | +// WithHTTPClient sets the HTTP client. |
| 81 | +func (c *Client) WithHTTPClient(httpClient *http.Client) *Client { |
| 82 | + c.hc = httpClient |
| 83 | + return c |
| 84 | +} |
| 85 | + |
| 86 | +// ReqOption is http requestion functional option. |
| 87 | +type ReqOption func(*http.Request) |
| 88 | + |
| 89 | +// WithSetHeader sets the header key to value val. |
| 90 | +func WithSetHeader(key, val string) ReqOption { |
| 91 | + return func(req *http.Request) { |
| 92 | + if req.Header == nil { |
| 93 | + req.Header = make(http.Header) |
| 94 | + } |
| 95 | + req.Header.Set(key, val) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// WithAddHeader adds the val to key header. |
| 100 | +func WithAddHeader(key, val string) ReqOption { |
| 101 | + return func(req *http.Request) { |
| 102 | + if req.Header == nil { |
| 103 | + req.Header = make(http.Header) |
| 104 | + } |
| 105 | + req.Header.Add(key, val) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func (c *Client) newRequest(ctx context.Context, method, url string, body io.Reader, opts ...ReqOption) (*http.Request, error) { |
| 110 | + if ctx == nil { |
| 111 | + ctx = context.Background() |
| 112 | + } |
| 113 | + if body == nil { |
| 114 | + body = &bytes.Reader{} |
| 115 | + } |
| 116 | + |
| 117 | + req, err := http.NewRequestWithContext(ctx, method, url, body) |
| 118 | + if err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + |
| 122 | + for _, setOption := range opts { |
| 123 | + setOption(req) |
| 124 | + } |
| 125 | + |
| 126 | + if c.token == "" { |
| 127 | + var err error |
| 128 | + c.token, err = GetToken(c.tokenSrc) |
| 129 | + if err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token)) |
| 135 | + req.Header.Set("Accept", "application/json; charset=utf-8") |
| 136 | + if body != nil { |
| 137 | + // if no content-type is specified we default to json |
| 138 | + if ct := req.Header.Get("Content-Type"); len(ct) == 0 { |
| 139 | + req.Header.Set("Content-Type", "application/json; charset=utf-8") |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return req, nil |
| 144 | +} |
| 145 | + |
| 146 | +func (c *Client) doRequest(req *http.Request) (*http.Response, error) { |
| 147 | + resp, err := c.hc.Do(req) |
| 148 | + if err != nil { |
| 149 | + return nil, err |
| 150 | + } |
| 151 | + if resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusBadRequest { |
| 152 | + return resp, nil |
| 153 | + } |
| 154 | + defer resp.Body.Close() |
| 155 | + |
| 156 | + var apiErr APIError |
| 157 | + if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { |
| 158 | + return nil, err |
| 159 | + } |
| 160 | + |
| 161 | + return nil, apiErr |
| 162 | +} |
0 commit comments