Repository navigation
feat(embeddings): add base_url for OpenAI-compatible servers - #120
weshofmann wants to merge 1 commit into
Conversation
The openai embedding provider hardcoded https://api.openai.com/v1. embedding.base_url now points it at any OpenAI-compatible /v1/embeddings server (Ollama, LM Studio, llama.cpp, vLLM, MLX servers). A long-running server keeps the model loaded, so short-lived CLI hooks embed queries without importing torch on every start. With a custom base_url: - the API key is optional and read only from an explicitly configured api_key_env, so a default OPENAI_API_KEY is never sent to another host - dimensions is sent only when configured - redirects are not followed (urllib would forward Authorization) - only http(s) URLs without userinfo, query or fragment are accepted Unset, or set to the OpenAI default, behavior is unchanged. base_url is not part of the embedding fingerprint.
There was a problem hiding this comment.
Confidence Score: 4/5
Summary
Adds embedding.base_url so the openai provider can target OpenAI-compatible servers, with optional key, no-redirect opener and URL validation; default OpenAI path unchanged. Low risk; behavior verified end to end against a local stub.
Important Files Changed
| File | Overview |
|---|---|
| src/kindex/vectors.py | Custom base_url path: URL validation, optional key, _NoRedirect opener, dimensions omitted when unset |
| src/kindex/config.py | New EmbeddingConfig.base_url field, default empty |
| tests/test_vectors.py | Nine tests covering default endpoint, key handling, unsafe URLs, redirects |
| tests/test_text_encoding.py | UTF-8 guard now treats build_opener(...).open as a URL API |
| README.md | Provider diagram, base_url section with Ollama example, config reference |
| CHANGELOG.md | Unreleased entry for embedding.base_url |
Findings
- kin.sample.yaml embedding block lacks the new
# base_url:line added to README's config reference; add it so the sample stays in sync.
Reviewed 528566c...1af1f77 · ↻ Re-run review · View in Adapt
| base_url = _openai_base_url(config) if provider == "openai" else "" | ||
| if base_url: | ||
| ec = config.embedding | ||
| return _embed_openai(redact_text(text), model, ec.dimensions, ec.api_key_env, |
There was a problem hiding this comment.
With base_url set but dimensions unset, storage still sizes 1536 (model defaults to text-embedding-3-small); a 768-d server then yields silent empty search; fail fast with a clear warning.
| return None | ||
|
|
||
|
|
||
| def _urlopen_no_redirect(req: urllib.request.Request, timeout: float): |
There was a problem hiding this comment.
Real 3xx handling is covered only by mocks and a redirect_request unit test; add a stub-server test asserting HTTPError 302 and no follow-up request.
What
embedding.base_urllets theopenaiembedding provider talk to any OpenAI-compatible/v1/embeddingsserver instead of the hardcodedhttps://api.openai.com/v1/embeddings: Ollama, LM Studio, llama.cpp, vLLM, MLX servers, or a gateway.Why
The
localprovider imports sentence-transformers and torch in every process that embeds. For short-lived CLI hooks such askin primeat session start, the import dominates: several seconds of torch import against a few milliseconds of embedding. A long-running local server keeps the model loaded, so eachkinprocess does one HTTP call and never imports torch. It also opens the door to stronger local models than MiniLM.Behavior
https://api.openai.com/v1: nothing changes. Same URL, same requiredOPENAI_API_KEY, samedimensionshandling, sameurlopenpath.base_url:<base_url>/embeddings; a trailing slash is tolerated.api_key_env. A defaultOPENAI_API_KEYin the environment is never sent to a non-OpenAI host. Ifapi_key_envis set but empty, the call returnsNonewithout a request, same as today's missing-key path.dimensionsis sent only when configured. Many local servers reject or ignore it.dimensionsstill has to match what the server returns, because it sizes the vector table, and the existing length check rejects mismatched vectors.Authorizationheader to a redirect target, so a 3xx surfaces as aprovider_http_errorwith its status instead.http/httpswith a host. URLs with userinfo, query or fragment are refused before any request.base_urlis never logged._provider_http_error, so quarantine classification (input_too_large,authentication_error) works the same against compatible servers.Decision:
base_urlis not part of the embedding fingerprintThe fingerprint stays provider/model/dimensions/strategy, so existing indexes keep their identity. A server that serves different weights under the same model name won't be detected; switching to a different model name or dimension still fails closed as today. Adding
base_urlto the fingerprint would also break identity on cosmetic changes (localhostvs127.0.0.1). Easy to flip if you'd rather be strict.Tests
tests/test_vectors.py: default endpoint unchanged (URL, header, body), compatible server without a key (defaultOPENAI_API_KEYnot sent),dimensionsomitted when unset, explicit key sent, missing explicit key makes no request, unsafe URLs refused without leaking userinfo to stderr, 3xx raises instead of following,_NoRedirectrefuses, explicit default URL keeps the default path.Also checked end to end against a stub HTTP server on 127.0.0.1 using real urllib: the vector round-trips, no
Authorizationheader arrives withOPENAI_API_KEYset in the environment, and a 302 producesprovider_http_error 302without a second request to the target.Full suite: 4968 passed, 4 skipped.
tests/test_text_encoding.py's UTF-8 guard now treatsbuild_opener(...).open(request)as a URL API, likewebbrowser.open, with an allow-case test.README (provider diagram, config reference, a short section with an example) and CHANGELOG
[Unreleased]are updated.Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.