|
| 1 | +from http import HTTPStatus |
| 2 | +from types import SimpleNamespace |
| 3 | +import sys |
| 4 | + |
1 | 5 | from llama_index.core.embeddings.multi_modal_base import MultiModalEmbedding |
2 | 6 | from llama_index.embeddings.dashscope import DashScopeEmbedding |
| 7 | +from llama_index.embeddings.dashscope.base import get_multimodal_embedding |
3 | 8 |
|
4 | 9 |
|
5 | 10 | def test_dashscope_embedding_class(): |
6 | 11 | names_of_base_classes = [b.__name__ for b in DashScopeEmbedding.__mro__] |
7 | 12 | 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