Skip to content

feat: add OpenAI-compatible embedding backend#12

Open
zh820952173 wants to merge 1 commit into
modelscope:mainfrom
zh820952173:feat/openai-embedding-backend
Open

feat: add OpenAI-compatible embedding backend#12
zh820952173 wants to merge 1 commit into
modelscope:mainfrom
zh820952173:feat/openai-embedding-backend

Conversation

@zh820952173

@zh820952173 zh820952173 commented Jun 8, 2026

Copy link
Copy Markdown

Summary

Adds a third embedding backend openai alongside the existing dashscope (default) and local backends. It works with any OpenAI-compatible /embeddings endpoint — OpenAI, Zhipu/GLM (embedding-3), self-hosted vLLM/TEI, etc. — configured via base_url + api_key + model.

Default behavior is unchanged: existing users stay on dashscope with no action required.

Changes

  • ultron/core/embeddings.py: EmbeddingService now supports openai. New _init_openai_client() (lazy-imports openai, validates base_url/api_key) and _embed_openai() with robust error handling for call failures, empty data, empty rows, and input/output count mismatches. The openai branch is wired into embed_text / embed_texts / is_available / get_model_info.
  • ultron/config.py: new embedding_base_url and embedding_api_key fields. They fall back to the LLM endpoint/key (ULTRON_BASE_URL, ULTRON_API_KEY / OPENAI_API_KEY), so most users need no extra setup.
  • ultron/api/sdk/ultron.py: passes the new config through to EmbeddingService.
  • Docs & .env.example: document the new backend and env vars (ULTRON_EMBEDDING_BASE_URL, ULTRON_EMBEDDING_API_KEY) in both EN and ZH.
  • Tests: tests/core/test_embeddings.py covers the openai backend success and error paths (skipped when openai is not installed).

New environment variables

Variable Maps to Fallback
ULTRON_EMBEDDING_BACKEND=openai embedding_backend dashscope
ULTRON_EMBEDDING_BASE_URL embedding_base_url ULTRON_BASE_URL
ULTRON_EMBEDDING_API_KEY embedding_api_key ULTRON_API_KEY / OPENAI_API_KEY

Notes

  • One ULTRON_DATA_DIR can only use a single embedding backend/model/dimension; switching requires reset_all() (existing constraint, unchanged).
  • Requires the openai package only when this backend is selected.

Test plan

  • python -m pytest tests/core/test_embeddings.py
  • Manual smoke test against a Zhipu embedding-3 / OpenAI endpoint

Add a third embedding backend ("openai") alongside the existing
"dashscope" and "local" options. It talks to any OpenAI-compatible
/embeddings endpoint (OpenAI, Zhipu/GLM, vLLM/TEI, ...) via
base_url + api_key + model.

- EmbeddingService: new _init_openai_client / _embed_openai with
  robust handling for call failures, empty data/rows, and count
  mismatches; openai branch wired into embed_text/embed_texts/
  is_available/get_model_info.
- Config: new embedding_base_url / embedding_api_key fields that fall
  back to the LLM endpoint/key (ULTRON_BASE_URL, ULTRON_API_KEY /
  OPENAI_API_KEY) so no duplicate setup is needed.
- SDK: pass the new config through to EmbeddingService.
- Tests: cover the openai backend success and error paths.
- Docs/.env.example: document the new backend and env vars (EN/ZH).

Default behavior is unchanged (still dashscope).

Co-authored-by: Cursor <cursoragent@cursor.com>

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request introduces support for an OpenAI-compatible embedding backend (openai) alongside the existing dashscope and local backends, updating the configuration, documentation, and SDK implementation, and adding corresponding unit tests. Feedback on these changes suggests ensuring that EmbeddingService falls back to environment variables for base_url and api_key when instantiated directly, sorting the returned embedding data by index to handle potential out-of-order responses from some providers, and isolating configuration tests from host environment variables to prevent flakiness.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread ultron/core/embeddings.py
Comment thread ultron/core/embeddings.py
Comment thread tests/core/test_embeddings.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant