|
| 1 | +//go:build (darwin && arm64) || (linux && amd64) |
| 2 | + |
| 3 | +package nomic |
| 4 | + |
| 5 | +import "path/filepath" |
| 6 | + |
| 7 | +// Client provides nomic embeddings, transparently using the daemon when |
| 8 | +// available or falling back to in-process embedding. |
| 9 | +type Client struct { |
| 10 | + daemon *daemonClient |
| 11 | + embedder *Embedder |
| 12 | +} |
| 13 | + |
| 14 | +// NewClient creates a Client that tries the daemon first for fast embedding. |
| 15 | +// If the daemon is unavailable, it falls back to loading the model in-process. |
| 16 | +// gitRoot is the git repository root (used to locate .rekal/nomic/). |
| 17 | +func NewClient(gitRoot string) (*Client, error) { |
| 18 | + if !Supported() { |
| 19 | + return nil, ErrNotSupported |
| 20 | + } |
| 21 | + |
| 22 | + // Try daemon first. |
| 23 | + dc, err := ensureDaemon(gitRoot) |
| 24 | + if err == nil { |
| 25 | + return &Client{daemon: dc}, nil |
| 26 | + } |
| 27 | + |
| 28 | + // Fall back to in-process. |
| 29 | + cacheDir := filepath.Join(gitRoot, ".rekal", "nomic") |
| 30 | + embedder, err := NewEmbedder(cacheDir) |
| 31 | + if err != nil { |
| 32 | + return nil, err |
| 33 | + } |
| 34 | + return &Client{embedder: embedder}, nil |
| 35 | +} |
| 36 | + |
| 37 | +// EmbedQuery embeds text with the "search_query: " prefix. |
| 38 | +func (c *Client) EmbedQuery(text string) ([]float64, error) { |
| 39 | + if c.daemon != nil { |
| 40 | + return c.daemon.EmbedQuery(text) |
| 41 | + } |
| 42 | + return c.embedder.EmbedQuery(text) |
| 43 | +} |
| 44 | + |
| 45 | +// EmbedDocument embeds text with the "search_document: " prefix. |
| 46 | +func (c *Client) EmbedDocument(text string) ([]float64, error) { |
| 47 | + if c.daemon != nil { |
| 48 | + return c.daemon.EmbedDocument(text) |
| 49 | + } |
| 50 | + return c.embedder.EmbedDocument(text) |
| 51 | +} |
| 52 | + |
| 53 | +// EmbedSessions embeds multiple sessions in batch. |
| 54 | +func (c *Client) EmbedSessions(sessions map[string]string) (map[string][]float64, error) { |
| 55 | + if c.daemon != nil { |
| 56 | + return c.daemon.EmbedSessions(sessions) |
| 57 | + } |
| 58 | + return c.embedder.EmbedSessions(sessions) |
| 59 | +} |
| 60 | + |
| 61 | +// Close releases resources. |
| 62 | +func (c *Client) Close() { |
| 63 | + if c.daemon != nil { |
| 64 | + c.daemon.Close() |
| 65 | + } |
| 66 | + if c.embedder != nil { |
| 67 | + c.embedder.Close() |
| 68 | + } |
| 69 | +} |
0 commit comments