|
1 | 1 | from abc import abstractmethod |
| 2 | +from collections.abc import Sequence |
2 | 3 |
|
3 | 4 | from dify_plugin.entities.model import ModelType |
4 | | -from dify_plugin.entities.model.rerank import RerankResult |
| 5 | +from dify_plugin.entities.model.rerank import MultiModalRerankResult, RerankResult |
| 6 | +from dify_plugin.entities.model.text_embedding import MultiModalContent |
5 | 7 | from dify_plugin.interfaces.model.ai_model import AIModel |
6 | 8 |
|
7 | 9 |
|
@@ -41,6 +43,23 @@ def _invoke( |
41 | 43 | """ |
42 | 44 | raise NotImplementedError |
43 | 45 |
|
| 46 | + def _invoke_multimodal( |
| 47 | + self, |
| 48 | + model: str, |
| 49 | + credentials: dict, |
| 50 | + query: MultiModalContent, |
| 51 | + docs: Sequence[MultiModalContent], |
| 52 | + score_threshold: float | None = None, |
| 53 | + top_n: int | None = None, |
| 54 | + user: str | None = None, |
| 55 | + ) -> MultiModalRerankResult: |
| 56 | + """Invoke a multimodal rerank model.""" |
| 57 | + |
| 58 | + raise NotImplementedError( |
| 59 | + f"{self.__class__.__name__} does not implement `_invoke_multimodal`. " |
| 60 | + "Implement this method to support multimodal rerank invocations." |
| 61 | + ) |
| 62 | + |
44 | 63 | ############################################################ |
45 | 64 | # For executor use only # |
46 | 65 | ############################################################ |
@@ -73,3 +92,31 @@ def invoke( |
73 | 92 | return self._invoke(model, credentials, query, docs, score_threshold, top_n, user) |
74 | 93 | except Exception as e: |
75 | 94 | raise self._transform_invoke_error(e) from e |
| 95 | + |
| 96 | + def invoke_multimodal( |
| 97 | + self, |
| 98 | + model: str, |
| 99 | + credentials: dict, |
| 100 | + query: MultiModalContent, |
| 101 | + docs: Sequence[MultiModalContent], |
| 102 | + score_threshold: float | None = None, |
| 103 | + top_n: int | None = None, |
| 104 | + user: str | None = None, |
| 105 | + ) -> MultiModalRerankResult: |
| 106 | + """Invoke a multimodal rerank model.""" |
| 107 | + |
| 108 | + with self.timing_context(): |
| 109 | + try: |
| 110 | + return self._invoke_multimodal( |
| 111 | + model, |
| 112 | + credentials, |
| 113 | + query, |
| 114 | + docs, |
| 115 | + score_threshold, |
| 116 | + top_n, |
| 117 | + user, |
| 118 | + ) |
| 119 | + except NotImplementedError: |
| 120 | + raise |
| 121 | + except Exception as e: |
| 122 | + raise self._transform_invoke_error(e) from e |
0 commit comments