Environment:0xranx OpenContext Embedding API
| Knowledge Sources | |
|---|---|
| Domains | Infrastructure, Embeddings |
| Last Updated | 2026-10-24 10:00 GMT |
Overview
OpenAI-compatible embedding API access for generating vector embeddings during index build and query search.
Description
This environment provides access to an OpenAI-compatible embedding API endpoint. The default model is `text-embedding-3-small` (1536 dimensions) from OpenAI, but the configuration supports any compatible provider (e.g., DashScope `text-embedding-v4` at 1024 dimensions) via the `EMBEDDING_API_BASE` setting. The client auto-detects actual embedding dimensions from the first API response and re-initializes the vector store if there is a mismatch.
Usage
Use this environment for any index build (`oc index build`) or vector/hybrid search operation. It is the mandatory prerequisite for `NativeIndexer_buildIndex` and `EmbeddingClient` implementations. The API key must be configured before indexing.
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| OS | Any | API is accessed via HTTP |
| Hardware | Any | Network access required |
| Network | Internet access to API endpoint | Configurable via `EMBEDDING_API_BASE` |
Dependencies
Rust Packages
- `reqwest` >= 0.12 (with `json` feature)
- `tokio` >= 1 (async runtime)
npm Packages
- `openai` >= 6.13.0
Credentials
The following environment variables must be set (priority order shown):
- `EMBEDDING_API_KEY` — API key for embedding service (preferred)
- `OPENAI_API_KEY` — Fallback API key (legacy compatibility)
- `OPENAI_KEY` — Second fallback (Rust core only)
Optional configuration:
- `EMBEDDING_API_BASE` / `OPENAI_API_BASE` — API base URL (default: `https://api.openai.com/v1`)
- `EMBEDDING_MODEL` — Model name (default: `text-embedding-3-small`)
Quick Install
# Set API key via environment variable
export EMBEDDING_API_KEY="sk-..."
# Or via CLI config (stored in ~/.opencontext/config.json)
oc config set EMBEDDING_API_KEY "sk-..."
# For DashScope or other providers
oc config set EMBEDDING_API_BASE "https://dashscope.aliyuncs.com/compatible-mode/v1"
oc config set EMBEDDING_MODEL "text-embedding-v4"
Code Evidence
API key resolution with fallbacks from `crates/opencontext-core/src/search/config.rs:62-72`:
pub fn get_api_key(&self) -> SearchResult<String> {
if let Some(ref key) = self.api_key {
if !key.is_empty() {
return Ok(key.clone());
}
}
std::env::var("OPENAI_API_KEY")
.or_else(|_| std::env::var("OPENAI_KEY"))
.map_err(|_| SearchError::ApiKeyMissing)
}
Auto-detection of embedding dimensions from `embedding.rs:198-213`:
if let Some(first) = data.first() {
let detected_dim = first.embedding.len();
let current = self.actual_dimensions.load(Ordering::Relaxed);
if current == 0 {
self.actual_dimensions
.store(detected_dim, Ordering::Relaxed);
log::info!("Auto-detected embedding dimensions: {}", detected_dim);
} else if current != detected_dim {
log::warn!(
"Embedding dimension mismatch: expected {}, got {}",
current, detected_dim
);
}
}
Config priority chain from `src/core/config.js:112-140`:
function get(key) {
const keyInfo = CONFIG_KEYS[key];
// 1. Check primary environment variable
if (keyInfo?.envVar && process.env[keyInfo.envVar]) {
return process.env[keyInfo.envVar];
}
// 2. Check fallback environment variable (backward compatibility)
if (keyInfo?.fallbackEnvVar && process.env[keyInfo.fallbackEnvVar]) {
return process.env[keyInfo.fallbackEnvVar];
}
// 3. Check config file (new key name first, then old key name for migration)
const config = loadConfig();
if (config[key] !== undefined) return config[key];
// 4. Return default value
return keyInfo?.default;
}
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| `ApiKeyMissing` | No API key configured | Run `oc config set EMBEDDING_API_KEY "sk-..."` |
| `API error (401): Unauthorized` | Invalid or expired API key | Update the API key in config |
| `Embedding count mismatch` | API returned fewer embeddings than requested | Check for rate limiting; reduce batch size |
| `Embedding dimension mismatch` | Model changed dimensions | Delete LanceDB index and rebuild |
Compatibility Notes
- DashScope: Default batch size is 10 because DashScope limits batch size. The `dimensions` parameter is only sent for `text-embedding-3-*` models; other APIs may reject it.
- Ollama: Can be used as a local embedding provider by setting `EMBEDDING_API_BASE` to the Ollama endpoint.
- Text truncation: Chunks are truncated to 2000 characters before sending to the API to avoid token limit errors across all languages.