agentscope-ai / agentscope-ai/agentscope
fix(rag): embedding service always forwards `dimensions` even when the model does not support matryoshka
- 主要語言
- Python
- 星號
- 31.6k
- 分支
- 3.5k
- 平均合併
- 1 天 16 小時
- 30 天內合併 PR
- 103
描述
## Bug
In `src/agentscope/app/_service/_embedding.py`, the `build_embedding_model` helper unconditionally sets `kwargs["pass_dimensions"]` based on whether `supported_dimensions` is present on the model card.
This logic is **OpenAI-specific**: only `OpenAIEmbeddingModel` exposes a `pass_dimensions` flag in its `__init__` (see `src/agentscope/embedding/_openai/_model.py:38`). The other three providers (`DashScopeTextEmbedding`, `GeminiTextEmbedding`, `OllamaTextEmbedding`) do **not** accept this kwarg — calling `embedding_cls(**kwargs)` on them raises `TypeError: __init__() got an unexpected keyword argument`.
More fundamentally, when a non-OpenAI provider or a non-matryoshka model is used, forwarding a user-specified `dimensions` can either:
1. **Trigger a provider-side error** if the model has no matryoshka support (BGE, M3E, most OSS embedding models).
2. **Silently produce a different vector size** than the user expects, breaking downstream vector index compatibility.
## Repro
```python
from agentscope.app._service._embedding import build_embedding_model
from agentscope.app.storage import CredentialRecord, EmbeddingModelConfig
credential = CredentialRecord(data={...}, ...)
config = EmbeddingModelConfig(
type="dashscope",
model="text-embedding-v3", # no matryoshka
dimensions=1024, # user-specified
parameters={},
credential_id="...",
)
model = build_embedding_model(credential, config)
# / / With the buggy code: TypeError
# / / dashscope model does not accept `pass_dimensions`
```
## Expected
- Matryoshka-capable models (OpenAI `text-embedding-3-*`, etc.) → forward `dimensions` as configured.
- Non-matryoshka models (BGE, M3E, etc.) → **skip** `dimensions` and use the model default.
- Non-OpenAI providers → no `TypeError`; the service should silently omit the `pass_dimensions` flag for them.
## Proposed Fix
PR #2343 (companion PR):
1. Read `supported_dimensions` from `EmbeddingModelCard` (already exposed in `src/agentscope/embedding/_embedding_model_card.py:66`).
2. Use `inspect.signature(embedding_cls.__init__).parameters` to check whether the embedding class accepts `pass_dimensions`. Only set the kwarg when supported.
3. Set `pass_dimensions=True` only when the model card declares `supported_dimensions`.
This is a 17-line, single-file change (`src/agentscope/app/_service/_embedding.py`) with:
- 100% backward compatibility for OpenAI matryoshka usage.
- Defensive check that future-proofs the service against new providers adding the same flag.
- No behavior change for OpenAI when the model supports matryoshka.
## Environment
- AgentScope 2.0 (current `main` branch, SHA `d3e6bd7`)
- Discovered during 8-week source code deep-dive of the embedding service module.
## Links
- PR: https://github.com/agentscope-ai/agentscope/pull/2343
- Code anchor: `src/agentscope/app/_service/_embedding.py:71-99`
貢獻指南
研究方向
Start in `src/agentscope/app/_service/_embedding.py` at `build_embedding_model` (around lines 71-99) where `kwargs['pass_dimensions']` is currently set and passed to `embedding_cls(**kwargs)`. Then check `src/agentscope/embedding/_embedding_model_card.py` for `supported_dimensions` and confirm constructor support in `src/agentscope/embedding/_openai/_model.py` and other provider constructors used by this service. Re-run the issue's reproduction with a DashScope config to verify no `TypeError`, then test an OpenAI matryoshka case to confirm configured dimensions still flow; done when `pass_dimensions` is only forwarded for supported OpenAI matryoshka models.
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- backend
- Issue 類型
- 缺陷
- 難度
- 1/5
- 預估耗時
- 1 小時以內
- 活躍度
- 活躍
- 描述清晰度
- 描述清楚
- 新手友好度
- 85/100