Skip to content

Commit 227130a

Browse files
committed
fix: support DashScope multimodal embeddings response
1 parent db30f53 commit 227130a

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

llama-index-integrations/embeddings/llama-index-embeddings-dashscope/llama_index/embeddings/dashscope/base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,13 @@ def get_multimodal_embedding(
147147
model=model, input=input, api_key=api_key, kwargs=kwargs
148148
)
149149
if response.status_code == HTTPStatus.OK:
150-
return response.output["embedding"]
150+
if "embedding" in response.output:
151+
return response.output["embedding"]
152+
embeddings = response.output.get("embeddings") or []
153+
if embeddings:
154+
return embeddings[0]["embedding"]
155+
logger.error("Calling MultiModalEmbedding returned no embeddings, details: %s" % response)
156+
return []
151157
else:
152158
logger.error("Calling MultiModalEmbedding failed, details: %s" % response)
153159
return []
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,64 @@
1+
from http import HTTPStatus
2+
from types import SimpleNamespace
3+
import sys
4+
15
from llama_index.core.embeddings.multi_modal_base import MultiModalEmbedding
26
from llama_index.embeddings.dashscope import DashScopeEmbedding
7+
from llama_index.embeddings.dashscope.base import get_multimodal_embedding
38

49

510
def test_dashscope_embedding_class():
611
names_of_base_classes = [b.__name__ for b in DashScopeEmbedding.__mro__]
712
assert MultiModalEmbedding.__name__ in names_of_base_classes
13+
14+
15+
def test_get_multimodal_embedding_accepts_current_dashscope_shape(monkeypatch):
16+
class FakeMultiModalEmbedding:
17+
@staticmethod
18+
def call(**_kwargs):
19+
return SimpleNamespace(
20+
status_code=HTTPStatus.OK,
21+
output={
22+
"embeddings": [
23+
{
24+
"embedding": [0.1, 0.2, 0.3],
25+
"index": 0,
26+
"type": "text",
27+
}
28+
]
29+
},
30+
)
31+
32+
monkeypatch.setitem(
33+
sys.modules,
34+
"dashscope",
35+
SimpleNamespace(MultiModalEmbedding=FakeMultiModalEmbedding),
36+
)
37+
38+
assert get_multimodal_embedding("multimodal-embedding-v1", [{"text": "hello"}]) == [
39+
0.1,
40+
0.2,
41+
0.3,
42+
]
43+
44+
45+
def test_get_multimodal_embedding_preserves_legacy_dashscope_shape(monkeypatch):
46+
class FakeMultiModalEmbedding:
47+
@staticmethod
48+
def call(**_kwargs):
49+
return SimpleNamespace(
50+
status_code=HTTPStatus.OK,
51+
output={"embedding": [0.4, 0.5, 0.6]},
52+
)
53+
54+
monkeypatch.setitem(
55+
sys.modules,
56+
"dashscope",
57+
SimpleNamespace(MultiModalEmbedding=FakeMultiModalEmbedding),
58+
)
59+
60+
assert get_multimodal_embedding("multimodal-embedding-v1", [{"text": "hello"}]) == [
61+
0.4,
62+
0.5,
63+
0.6,
64+
]

0 commit comments

Comments
 (0)