Repository navigation
feat(inference): add MiniMax native embedding adapter #1653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PsiACE
merged 7 commits into
oceanbase:master
from
AlexStocks:feat/minimax-embedding-adapter
Sep 20, 2026
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2ebdcb5
feat(inference): add MiniMax native embedding adapter
AlexStocks 8f3678d
fix(inference): use query embeddings for retrieval
AlexStocks 93ce29c
fix(inference): preserve MiniMax adapter error semantics
AlexStocks 1a2cab0
fix(inference): preserve MiniMax provider diagnostics
AlexStocks 1d6d984
fix(inference): honor MiniMax embedding timeouts
AlexStocks 3a89ae7
fix(service): allow slower native startup
AlexStocks a4b0641
fix(service): stabilize native startup in PR #1653
AlexStocks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| # Copyright (c) 2026 OceanBase. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """MiniMax embedding backend adapter. | ||
|
|
||
| MiniMax publishes embeddings under the OpenAI path prefix but does not implement | ||
| the OpenAI embeddings contract. Its ``/v1/embeddings`` endpoint requires the | ||
| native request shape ``{"model", "texts": [...], "type"}`` instead of OpenAI's | ||
| ``{"input": [...]}``, and returns ``{"vectors": [...], "base_resp": {"status_code"}}`` | ||
| instead of ``{"data": [{"embedding": [...]}]}``. It also answers HTTP 200 with a | ||
| non-zero ``base_resp.status_code`` on error rather than a 4xx/5xx. This adapter | ||
| speaks the MiniMax-native shape directly so PowerContext can use MiniMax as an | ||
| embedding provider without a custom Pydantic AI provider shim. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from collections.abc import Mapping, Sequence | ||
|
|
||
| import httpx | ||
|
|
||
| from powercontext.builtin.artifacts.memory.canonical import canonical_embedding | ||
| from powercontext.builtin.artifacts.memory.models import EmbeddingProfile | ||
| from powercontext.builtin.inference.errors import ( | ||
| InferenceConfigurationError, | ||
| InferenceTimeoutError, | ||
| InferenceUnavailableError, | ||
| InvalidInferenceOutputError, | ||
| ) | ||
| from powercontext.builtin.inference.models import EmbeddingResult, InferenceUsage | ||
|
|
||
| _MINIMAX_DOCUMENT_TYPE = "db" | ||
| _MINIMAX_QUERY_TYPE = "query" | ||
|
|
||
|
|
||
| class MiniMaxEmbeddingModel: | ||
| """Embed an ordered text batch with MiniMax ``embo`` via ``/v1/embeddings``.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| base_url: str, | ||
| model: str, | ||
| headers: Mapping[str, str] | None = None, | ||
| profile: EmbeddingProfile, | ||
| batch_size: int = 10, | ||
| timeout_seconds: float = 30.0, | ||
| http_client: httpx.AsyncClient | None = None, | ||
| ) -> None: | ||
| if not model or not model.strip(): | ||
| raise InferenceConfigurationError("embedding-model-empty") | ||
| if profile.dimension < 1: | ||
| raise InferenceConfigurationError("embedding-dimension-positive") | ||
| if batch_size < 1: | ||
| raise InferenceConfigurationError("embedding-batch-size-positive") | ||
| if not profile.profile_id.strip() or not profile.model.strip() or not profile.normalization.strip(): | ||
| raise InferenceConfigurationError("embedding-profile-identifiers") | ||
| self.profile = profile | ||
| self._model = model | ||
| self._batch_size = batch_size | ||
| self._timeout = timeout_seconds | ||
| self._endpoint = f"{base_url.rstrip('/')}/embeddings" | ||
| request_headers: dict[str, str] = {"Content-Type": "application/json"} | ||
| if headers: | ||
| request_headers.update(headers) | ||
| self._headers = request_headers | ||
| self._client = http_client or httpx.AsyncClient() | ||
|
|
||
| async def aclose(self) -> None: | ||
| """Close the underlying HTTP client (registered with the runtime stack).""" | ||
|
|
||
| await self._client.aclose() | ||
|
|
||
| async def embed(self, texts: tuple[str, ...], /) -> EmbeddingResult: | ||
| """Embed documents, validating order, count, dimension, and finite values.""" | ||
|
|
||
| return await self._embed(texts, embedding_type=_MINIMAX_DOCUMENT_TYPE) | ||
|
|
||
| async def embed_query(self, texts: tuple[str, ...], /) -> EmbeddingResult: | ||
| """Embed retrieval queries, validating order, count, dimension, and finite values.""" | ||
|
|
||
| return await self._embed(texts, embedding_type=_MINIMAX_QUERY_TYPE) | ||
|
|
||
| async def _embed(self, texts: tuple[str, ...], *, embedding_type: str) -> EmbeddingResult: | ||
| """Embed one MiniMax document or query batch.""" | ||
|
|
||
| if not texts: | ||
| return EmbeddingResult(vectors=()) | ||
|
|
||
| try: | ||
| result = await asyncio.wait_for( | ||
| self._embed_batches(texts, embedding_type=embedding_type), timeout=self._timeout | ||
| ) | ||
| except asyncio.CancelledError: | ||
| raise | ||
| except (InvalidInferenceOutputError, InferenceConfigurationError): | ||
| raise | ||
| except TimeoutError as error: | ||
| raise InferenceTimeoutError("embed", self._timeout) from error | ||
| except httpx.HTTPError as error: | ||
| raise InferenceUnavailableError("embed") from error | ||
| except Exception as error: | ||
| raise InferenceUnavailableError("embed") from error | ||
| return result | ||
|
|
||
| async def _embed_batches(self, texts: tuple[str, ...], *, embedding_type: str) -> EmbeddingResult: | ||
| vectors: list[tuple[float, ...]] = [] | ||
| requests = 0 | ||
| input_tokens = 0 | ||
| for start in range(0, len(texts), self._batch_size): | ||
| batch = texts[start : start + self._batch_size] | ||
| rows, tokens = await self._embed_one(batch, embedding_type=embedding_type) | ||
| vectors.extend(self._validated_vectors(batch, rows)) | ||
| requests += 1 | ||
| input_tokens += tokens | ||
| return EmbeddingResult( | ||
| vectors=tuple(vectors), | ||
| usage=InferenceUsage(requests=requests, input_tokens=input_tokens, output_tokens=None), | ||
| ) | ||
|
|
||
| async def _embed_one(self, batch: Sequence[str], *, embedding_type: str) -> tuple[list[list[float]], int]: | ||
| payload = {"model": self._model, "texts": list(batch), "type": embedding_type} | ||
| response = await self._client.post(self._endpoint, json=payload, headers=self._headers) | ||
| # MiniMax returns HTTP 200 with a non-zero base_resp.status_code on error; | ||
| # only a real transport/HTTP failure reaches raise_for_status first. | ||
| response.raise_for_status() | ||
| data = response.json() | ||
| if not isinstance(data, Mapping): | ||
| raise InvalidInferenceOutputError("embed", "provider response was not a JSON object") | ||
| base_resp = data.get("base_resp") | ||
| if isinstance(base_resp, Mapping): | ||
| status_code = base_resp.get("status_code") | ||
| if status_code is not None and status_code != 0: | ||
| raise InferenceUnavailableError("embed") | ||
| vectors = data.get("vectors") | ||
| if not isinstance(vectors, list) or len(vectors) != len(batch): | ||
| raise InvalidInferenceOutputError("embed", "provider returned no vectors or the wrong vector count") | ||
| total_tokens = data.get("total_tokens") | ||
| tokens = int(total_tokens) if isinstance(total_tokens, int) else 0 | ||
| return vectors, tokens | ||
|
|
||
| def _validated_vectors( | ||
| self, texts: Sequence[str], rows: Sequence[Sequence[float]] | ||
| ) -> tuple[tuple[float, ...], ...]: | ||
| out: list[tuple[float, ...]] = [] | ||
| for row in rows: | ||
| try: | ||
| out.append( | ||
| canonical_embedding( | ||
| tuple(row), | ||
| dimension=self.profile.dimension, | ||
| normalization=self.profile.normalization, | ||
| ) | ||
| ) | ||
| except (TypeError, ValueError) as error: | ||
| raise InvalidInferenceOutputError("embed", str(error)) from error | ||
| return tuple(out) | ||
|
|
||
|
|
||
| def _embedding_model_name(model: str | None) -> str: | ||
| """Strip a provider prefix such as ``openai:embo-01`` or ``minimax:embo-01``.""" | ||
|
|
||
| if model is None: | ||
| return "" | ||
| return model.partition(":")[2] or model | ||
|
|
||
|
|
||
| def is_minimax_embedding(base_url: str | None, model: str | None) -> bool: | ||
| """Detect a MiniMax embedding endpoint by host or explicit model prefix.""" | ||
|
|
||
| base = base_url or "" | ||
| if any(fragment in base.lower() for fragment in ("minimaxi.com", "minimax.io")): | ||
| return True | ||
| return "minimax" in (model or "").lower() | ||
|
AlexStocks marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| __all__ = ["MiniMaxEmbeddingModel", "is_minimax_embedding"] | ||
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.