feat(embedding): multi-provider abstraction layer#62
Merged
Conversation
d4fbf04 to
7a76a85
Compare
Add embedding.NewProvider factory supporting OpenAI, Ollama, and Cohere
via a unified ProviderConfig. Providers self-register via init() so
callers only need a blank import.
New packages:
pkg/embedding/ollama - local Ollama server (/api/embeddings)
pkg/embedding/cohere - Cohere API (embed-english-v3.0 default)
New files:
pkg/embedding/registry.go - NewProvider, RegisterFactory,
ProviderConfig, SupportedProviders
pkg/embedding/openai/register.go - registers OpenAI into the factory
pkg/embedding/ollama/register.go - registers Ollama into the factory
pkg/embedding/cohere/register.go - registers Cohere into the factory
pkg/embedding/registry_test.go - custom provider, unknown type,
ollama resolution, cache wrapping
CacheSize=-1 disables the in-memory cache; 0 uses the default (10k).
Existing cmd/ code continues to use openai.NewClient directly and is
unaffected.
Co-authored-by: Ona <[email protected]>
7a76a85 to
c4ff45f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #33
What
Adds a unified
embedding.NewProviderfactory that supports OpenAI, Ollama, and Cohere via a singleProviderConfig. Providers self-register viainit()so callers only need a blank import. Custom providers can be registered at startup viaRegisterFactory.Changes
pkg/embedding/registry.go(new)ProviderConfig:Type,APIKey,Model,BaseURL,CacheSizeNewProvider(cfg): resolves provider from registry, wraps inCachedProviderwhenCacheSize >= 0RegisterFactory(type, fn): registers a custom provider factorySupportedProviders(): returns["openai", "ollama", "cohere"]pkg/embedding/ollama/(new)Client: calls/api/embeddingson a local Ollama serverEmbedBatchis sequential (Ollama has no batch API)nomic-embed-text; default URL:http://localhost:11434register.go: self-registers into the factory viainit()pkg/embedding/cohere/(new)Client: calls Cohere/v1/embedwithinput_typesupportEmbedBatchsends all texts in a single API callembed-english-v3.0(1024 dims)ErrRateLimited/ErrInvalidAPIKeyon 429/401register.go: self-registers into the factory viainit()pkg/embedding/openai/register.go(new)pkg/embedding/registry_test.go(new) — 5 tests: custom provider, unknown type, empty type, Ollama resolution, cache wrappingUsage
Compatibility
Existing
cmd/code usesopenai.NewClientdirectly and is unaffected. The registry is additive — nothing breaks if a provider package is not imported.