99from dify_plugin .entities .model .text_embedding import EmbeddingUsage , MultiModalContent , MultiModalContentType , MultiModalEmbeddingResult , TextEmbeddingResult
1010from dify_plugin .errors .model import CredentialsValidateFailedError
1111from dify_plugin .interfaces .model .text_embedding_model import TextEmbeddingModel
12- from models ._common import _CommonTongyi
12+ from models ._common import _CommonTongyi , get_http_base_address
1313from ..constant import BURY_POINT_HEADER
1414
1515vision_models = dict ()
@@ -37,8 +37,7 @@ def _invoke(
3737 :param input_type: input type
3838 :return: embeddings result
3939 """
40- if credentials .get ("use_international_endpoint" , "false" ) == "true" :
41- dashscope .base_http_api_url = "https://dashscope-intl.aliyuncs.com/api/v1"
40+ http_base_address = get_http_base_address (credentials )
4241 credentials_kwargs = self ._to_credential_kwargs (credentials )
4342 context_size = self ._get_context_size (model , credentials )
4443 max_chunks = self ._get_max_chunks (model , credentials )
@@ -57,7 +56,10 @@ def _invoke(
5756 _iter = range (0 , len (inputs ), max_chunks )
5857 for i in _iter :
5958 (embeddings_batch , embedding_used_tokens ) = self .embed_documents (
60- credentials_kwargs = credentials_kwargs , model = model , texts = inputs [i : i + max_chunks ]
59+ credentials_kwargs = credentials_kwargs ,
60+ model = model ,
61+ texts = inputs [i : i + max_chunks ],
62+ base_address = http_base_address ,
6163 )
6264 used_tokens += embedding_used_tokens
6365 batched_embeddings += embeddings_batch
@@ -90,12 +92,23 @@ def validate_credentials(self, model: str, credentials: dict) -> None:
9092 """
9193 try :
9294 credentials_kwargs = self ._to_credential_kwargs (credentials )
93- self .embed_documents (credentials_kwargs = credentials_kwargs , model = model , texts = ["ping" ])
95+ http_base_address = get_http_base_address (credentials )
96+ self .embed_documents (
97+ credentials_kwargs = credentials_kwargs ,
98+ model = model ,
99+ texts = ["ping" ],
100+ base_address = http_base_address ,
101+ )
94102 except Exception as ex :
95103 raise CredentialsValidateFailedError (str (ex ))
96104
97105 @staticmethod
98- def embed_documents (credentials_kwargs : dict , model : str , texts : list [str ]) -> tuple [list [list [float ]], int ]:
106+ def embed_documents (
107+ credentials_kwargs : dict ,
108+ model : str ,
109+ texts : list [str ],
110+ base_address : str ,
111+ ) -> tuple [list [list [float ]], int ]:
99112 """Call out to Tongyi's embedding endpoint.
100113
101114 Args:
@@ -111,7 +124,12 @@ def embed_documents(credentials_kwargs: dict, model: str, texts: list[str]) -> t
111124 # transfer and call embed_multimodal_documents
112125 if TongyiTextEmbeddingModel ._is_vision_model (model ):
113126 documents = [MultiModalContent (content_type = MultiModalContentType .TEXT , content = text ) for text in texts ]
114- return TongyiTextEmbeddingModel .embed_multimodal_documents (credentials_kwargs , model , documents )
127+ return TongyiTextEmbeddingModel .embed_multimodal_documents (
128+ credentials_kwargs ,
129+ model ,
130+ documents ,
131+ base_address ,
132+ )
115133
116134 embeddings = []
117135 embedding_used_tokens = 0
@@ -124,14 +142,16 @@ def call_embedding_api(text):
124142 api_key = credentials_kwargs ["dashscope_api_key" ],
125143 model = model ,
126144 input = [{"text" : text }],
145+ base_address = base_address ,
127146 )
128147 else :
129148 return dashscope .TextEmbedding .call (
130149 api_key = credentials_kwargs ["dashscope_api_key" ],
131150 model = model ,
132151 input = text ,
133152 headers = BURY_POINT_HEADER ,
134- text_type = "document"
153+ text_type = "document" ,
154+ base_address = base_address ,
135155 )
136156 except Exception as e :
137157 # Return the exception to be handled by the caller
@@ -236,11 +256,13 @@ def _invoke_multimodal(
236256 :param input_type: input type
237257 :return: embeddings result
238258 """
239- if credentials .get ("use_international_endpoint" , "false" ) == "true" :
240- dashscope .base_http_api_url = "https://dashscope-intl.aliyuncs.com/api/v1"
259+ http_base_address = get_http_base_address (credentials )
241260 credentials_kwargs = self ._to_credential_kwargs (credentials )
242261 (embeddings_batch , embedding_used_tokens ) = self .embed_multimodal_documents (
243- credentials_kwargs = credentials_kwargs , model = model , documents = documents
262+ credentials_kwargs = credentials_kwargs ,
263+ model = model ,
264+ documents = documents ,
265+ base_address = http_base_address ,
244266 )
245267 usage = self ._calc_response_usage (model = model , credentials = credentials , tokens = embedding_used_tokens )
246268 return MultiModalEmbeddingResult (
@@ -250,7 +272,12 @@ def _invoke_multimodal(
250272 )
251273
252274 @staticmethod
253- def embed_multimodal_documents (credentials_kwargs : dict , model : str , documents : list [MultiModalContent ]) -> tuple [list [list [float ]], int ]:
275+ def embed_multimodal_documents (
276+ credentials_kwargs : dict ,
277+ model : str ,
278+ documents : list [MultiModalContent ],
279+ base_address : str ,
280+ ) -> tuple [list [list [float ]], int ]:
254281 """Call out to Tongyi's embedding endpoint.
255282
256283 Args:
@@ -292,7 +319,8 @@ def call_embedding_api(input):
292319 return dashscope .MultiModalEmbedding .call (
293320 api_key = credentials_kwargs ["dashscope_api_key" ],
294321 model = model ,
295- input = [input ],
322+ input = [input ],
323+ base_address = base_address ,
296324 )
297325 except Exception as e :
298326 # Return the exception to be handled by the caller
0 commit comments