agentscope-ai / agentscope-ai/agentscope

fix(rag): embedding service always forwards `dimensions` even when the model does not support matryoshka

Đang mở
#2,344 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
31.5k
Fork
3.5k
Merge trung bình
1 ngày 23 giờ
Pull request đã merge (30 ngày)
95

Mô tả

## 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`

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.