Skip to content

feat(embeddings): add base_url for OpenAI-compatible servers - #120

Open
weshofmann wants to merge 1 commit into
wandercom:mainfrom
weshofmann:feat/openai-embedding-base-url
Open

weshofmann wants to merge 1 commit into
wandercom:mainfrom
weshofmann:feat/openai-embedding-base-url

Conversation

@weshofmann

@weshofmann weshofmann commented Nov 18, 2026 •

Copy link
Copy Markdown

What

embedding.base_url lets the openai embedding provider talk to any OpenAI-compatible /v1/embeddings server instead of the hardcoded https://api.openai.com/v1/embeddings: Ollama, LM Studio, llama.cpp, vLLM, MLX servers, or a gateway.

embedding:
  provider: openai
  base_url: http://127.0.0.1:11434/v1   # Ollama
  model: nomic-embed-text
  dimensions: 768

Why

The local provider imports sentence-transformers and torch in every process that embeds. For short-lived CLI hooks such as kin prime at 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 each kin process does one HTTP call and never imports torch. It also opens the door to stronger local models than MiniLM.

Behavior

  • Unset or equal to https://api.openai.com/v1: nothing changes. Same URL, same required OPENAI_API_KEY, same dimensions handling, same urlopen path.
  • Custom base_url:
    • Requests go to <base_url>/embeddings; a trailing slash is tolerated.
    • API key is optional and is read only from an explicitly configured api_key_env. A default OPENAI_API_KEY in the environment is never sent to a non-OpenAI host. If api_key_env is set but empty, the call returns None without a request, same as today's missing-key path.
    • dimensions is sent only when configured. Many local servers reject or ignore it. dimensions still has to match what the server returns, because it sizes the vector table, and the existing length check rejects mismatched vectors.
    • Redirects are not followed. urllib forwards the Authorization header to a redirect target, so a 3xx surfaces as a provider_http_error with its status instead.
    • URL validation: only http/https with a host. URLs with userinfo, query or fragment are refused before any request. base_url is never logged.
  • Errors keep going through _provider_http_error, so quarantine classification (input_too_large, authentication_error) works the same against compatible servers.

Decision: base_url is not part of the embedding fingerprint

The 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_url to the fingerprint would also break identity on cosmetic changes (localhost vs 127.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 (default OPENAI_API_KEY not sent), dimensions omitted when unset, explicit key sent, missing explicit key makes no request, unsafe URLs refused without leaking userinfo to stderr, 3xx raises instead of following, _NoRedirect refuses, 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 Authorization header arrives with OPENAI_API_KEY set in the environment, and a 302 produces provider_http_error 302 without a second request to the target.

Full suite: 4968 passed, 4 skipped. tests/test_text_encoding.py's UTF-8 guard now treats build_opener(...).open(request) as a URL API, like webbrowser.open, with an allow-case test.

README (provider diagram, config reference, a short section with an example) and CHANGELOG [Unreleased] are updated.


Run Proof
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

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.

@adaptcom adaptcom Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread src/kindex/vectors.py
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,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/kindex/vectors.py
return None


def _urlopen_no_redirect(req: urllib.request.Request, timeout: float):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant