|
| 1 | +package cohere |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + // BaseURL is Cohere HTTP API base URL. |
| 15 | + BaseURL = "https://api.cohere.ai" |
| 16 | + // EmbedAPIVersion is the latest stable embedding API version. |
| 17 | + EmbedAPIVersion = "v1" |
| 18 | +) |
| 19 | + |
| 20 | +// Client is Cohere HTTP API client. |
| 21 | +type Client struct { |
| 22 | + apiKey string |
| 23 | + baseURL string |
| 24 | + version string |
| 25 | + hc *http.Client |
| 26 | +} |
| 27 | + |
| 28 | +// NewClient creates a new HTTP client and returns it. |
| 29 | +// It reads the Cohere API key from COHERE_API_KEY env var |
| 30 | +// and uses the default Go http.Client. |
| 31 | +// You can override the default options by using the |
| 32 | +// client methods. |
| 33 | +func NewClient() (*Client, error) { |
| 34 | + return &Client{ |
| 35 | + apiKey: os.Getenv("COHERE_API_KEY"), |
| 36 | + baseURL: BaseURL, |
| 37 | + version: EmbedAPIVersion, |
| 38 | + hc: &http.Client{}, |
| 39 | + }, nil |
| 40 | +} |
| 41 | + |
| 42 | +// WithAPIKey sets the API key. |
| 43 | +func (c *Client) WithAPIKey(apiKey string) *Client { |
| 44 | + c.apiKey = apiKey |
| 45 | + return c |
| 46 | +} |
| 47 | + |
| 48 | +// WithBaseURL sets the API base URL. |
| 49 | +func (c *Client) WithBaseURL(baseURL string) *Client { |
| 50 | + c.baseURL = baseURL |
| 51 | + return c |
| 52 | +} |
| 53 | + |
| 54 | +// WithVersion sets the API version. |
| 55 | +func (c *Client) WithVersion(version string) *Client { |
| 56 | + c.version = version |
| 57 | + return c |
| 58 | +} |
| 59 | + |
| 60 | +// WithHTTPClient sets the HTTP client. |
| 61 | +func (c *Client) WithHTTPClient(httpClient *http.Client) *Client { |
| 62 | + c.hc = httpClient |
| 63 | + return c |
| 64 | +} |
| 65 | + |
| 66 | +// ReqOption is http requestion functional option. |
| 67 | +type ReqOption func(*http.Request) |
| 68 | + |
| 69 | +// WithSetHeader sets the header key to value val. |
| 70 | +func WithSetHeader(key, val string) ReqOption { |
| 71 | + return func(req *http.Request) { |
| 72 | + if req.Header == nil { |
| 73 | + req.Header = make(http.Header) |
| 74 | + } |
| 75 | + req.Header.Set(key, val) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// WithAddHeader adds the val to key header. |
| 80 | +func WithAddHeader(key, val string) ReqOption { |
| 81 | + return func(req *http.Request) { |
| 82 | + if req.Header == nil { |
| 83 | + req.Header = make(http.Header) |
| 84 | + } |
| 85 | + req.Header.Add(key, val) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func (c *Client) newRequest(ctx context.Context, method, url string, body io.Reader, opts ...ReqOption) (*http.Request, error) { |
| 90 | + if ctx == nil { |
| 91 | + ctx = context.Background() |
| 92 | + } |
| 93 | + if body == nil { |
| 94 | + body = &bytes.Reader{} |
| 95 | + } |
| 96 | + |
| 97 | + req, err := http.NewRequestWithContext(ctx, method, url, body) |
| 98 | + if err != nil { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + |
| 102 | + for _, setOption := range opts { |
| 103 | + setOption(req) |
| 104 | + } |
| 105 | + |
| 106 | + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.apiKey)) |
| 107 | + req.Header.Set("Accept", "application/json; charset=utf-8") |
| 108 | + if body != nil { |
| 109 | + // if no content-type is specified we default to json |
| 110 | + if ct := req.Header.Get("Content-Type"); len(ct) == 0 { |
| 111 | + req.Header.Set("Content-Type", "application/json; charset=utf-8") |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return req, nil |
| 116 | +} |
| 117 | + |
| 118 | +func (c *Client) doRequest(req *http.Request) (*http.Response, error) { |
| 119 | + resp, err := c.hc.Do(req) |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + if resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusBadRequest { |
| 124 | + return resp, nil |
| 125 | + } |
| 126 | + defer resp.Body.Close() |
| 127 | + |
| 128 | + var apiErr APIError |
| 129 | + if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + |
| 133 | + return nil, apiErr |
| 134 | +} |
0 commit comments