|
| 1 | +// Package cohere provides an embedding.Provider backed by the Cohere API. |
| 2 | +package cohere |
| 3 | + |
| 4 | +import ( |
| 5 | + "bytes" |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/Siddhant-K-code/distill/pkg/embedding" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + defaultBaseURL = "https://api.cohere.ai/v1" |
| 18 | + defaultModel = "embed-english-v3.0" |
| 19 | + defaultTimeout = 30 * time.Second |
| 20 | +) |
| 21 | + |
| 22 | +// InputType controls how Cohere classifies the input for retrieval tasks. |
| 23 | +type InputType string |
| 24 | + |
| 25 | +const ( |
| 26 | + InputTypeSearchDocument InputType = "search_document" |
| 27 | + InputTypeSearchQuery InputType = "search_query" |
| 28 | + InputTypeClassification InputType = "classification" |
| 29 | + InputTypeClustering InputType = "clustering" |
| 30 | +) |
| 31 | + |
| 32 | +// Model dimensions for common Cohere embedding models. |
| 33 | +var modelDimensions = map[string]int{ |
| 34 | + "embed-english-v3.0": 1024, |
| 35 | + "embed-multilingual-v3.0": 1024, |
| 36 | + "embed-english-light-v3.0": 384, |
| 37 | +} |
| 38 | + |
| 39 | +// Config holds Cohere client configuration. |
| 40 | +type Config struct { |
| 41 | + // APIKey is the Cohere API key (required). |
| 42 | + APIKey string |
| 43 | + |
| 44 | + // Model is the embedding model. Default: embed-english-v3.0 |
| 45 | + Model string |
| 46 | + |
| 47 | + // InputType controls retrieval optimisation. Default: search_document |
| 48 | + InputType InputType |
| 49 | + |
| 50 | + // Timeout for API requests. Default: 30s |
| 51 | + Timeout time.Duration |
| 52 | +} |
| 53 | + |
| 54 | +// Client implements embedding.Provider for Cohere. |
| 55 | +type Client struct { |
| 56 | + cfg Config |
| 57 | + httpClient *http.Client |
| 58 | + dimension int |
| 59 | +} |
| 60 | + |
| 61 | +// NewClient creates a new Cohere embedding client. |
| 62 | +func NewClient(cfg Config) (*Client, error) { |
| 63 | + if cfg.APIKey == "" { |
| 64 | + return nil, fmt.Errorf("Cohere API key is required") |
| 65 | + } |
| 66 | + if cfg.Model == "" { |
| 67 | + cfg.Model = defaultModel |
| 68 | + } |
| 69 | + if cfg.InputType == "" { |
| 70 | + cfg.InputType = InputTypeSearchDocument |
| 71 | + } |
| 72 | + if cfg.Timeout <= 0 { |
| 73 | + cfg.Timeout = defaultTimeout |
| 74 | + } |
| 75 | + dim := modelDimensions[cfg.Model] |
| 76 | + return &Client{ |
| 77 | + cfg: cfg, |
| 78 | + httpClient: &http.Client{Timeout: cfg.Timeout}, |
| 79 | + dimension: dim, |
| 80 | + }, nil |
| 81 | +} |
| 82 | + |
| 83 | +type embedRequest struct { |
| 84 | + Texts []string `json:"texts"` |
| 85 | + Model string `json:"model"` |
| 86 | + InputType InputType `json:"input_type"` |
| 87 | +} |
| 88 | + |
| 89 | +type embedResponse struct { |
| 90 | + Embeddings [][]float32 `json:"embeddings"` |
| 91 | +} |
| 92 | + |
| 93 | +// Embed returns the embedding for a single text. |
| 94 | +func (c *Client) Embed(ctx context.Context, text string) ([]float32, error) { |
| 95 | + if text == "" { |
| 96 | + return nil, embedding.ErrEmptyInput |
| 97 | + } |
| 98 | + results, err := c.EmbedBatch(ctx, []string{text}) |
| 99 | + if err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + return results[0], nil |
| 103 | +} |
| 104 | + |
| 105 | +// EmbedBatch embeds multiple texts in a single API call. |
| 106 | +func (c *Client) EmbedBatch(ctx context.Context, texts []string) ([][]float32, error) { |
| 107 | + if len(texts) == 0 { |
| 108 | + return nil, nil |
| 109 | + } |
| 110 | + |
| 111 | + body, err := json.Marshal(embedRequest{ |
| 112 | + Texts: texts, |
| 113 | + Model: c.cfg.Model, |
| 114 | + InputType: c.cfg.InputType, |
| 115 | + }) |
| 116 | + if err != nil { |
| 117 | + return nil, fmt.Errorf("marshal request: %w", err) |
| 118 | + } |
| 119 | + |
| 120 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, |
| 121 | + defaultBaseURL+"/embed", bytes.NewReader(body)) |
| 122 | + if err != nil { |
| 123 | + return nil, fmt.Errorf("build request: %w", err) |
| 124 | + } |
| 125 | + req.Header.Set("Authorization", "Bearer "+c.cfg.APIKey) |
| 126 | + req.Header.Set("Content-Type", "application/json") |
| 127 | + |
| 128 | + resp, err := c.httpClient.Do(req) |
| 129 | + if err != nil { |
| 130 | + return nil, fmt.Errorf("cohere request: %w", err) |
| 131 | + } |
| 132 | + defer resp.Body.Close() |
| 133 | + |
| 134 | + if resp.StatusCode == http.StatusTooManyRequests { |
| 135 | + return nil, embedding.ErrRateLimited |
| 136 | + } |
| 137 | + if resp.StatusCode == http.StatusUnauthorized { |
| 138 | + return nil, embedding.ErrInvalidAPIKey |
| 139 | + } |
| 140 | + if resp.StatusCode != http.StatusOK { |
| 141 | + b, _ := io.ReadAll(resp.Body) |
| 142 | + return nil, fmt.Errorf("cohere %d: %s", resp.StatusCode, string(b)) |
| 143 | + } |
| 144 | + |
| 145 | + var result embedResponse |
| 146 | + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 147 | + return nil, fmt.Errorf("decode response: %w", err) |
| 148 | + } |
| 149 | + if len(result.Embeddings) != len(texts) { |
| 150 | + return nil, fmt.Errorf("expected %d embeddings, got %d", len(texts), len(result.Embeddings)) |
| 151 | + } |
| 152 | + return result.Embeddings, nil |
| 153 | +} |
| 154 | + |
| 155 | +// Dimension returns the embedding dimension for the configured model. |
| 156 | +func (c *Client) Dimension() int { return c.dimension } |
| 157 | + |
| 158 | +// ModelName returns the configured model name. |
| 159 | +func (c *Client) ModelName() string { return c.cfg.Model } |
0 commit comments