跳到内容

DashScope

DashScopeEmbedding #

基类: MultiModalEmbedding

用于文本嵌入的 DashScope 类。

参数

名称 类型 描述 默认值
model_name str

嵌入的模型名称。默认为 DashScopeTextEmbeddingModels.TEXT_EMBEDDING_V2。选项包括:

- DashScopeTextEmbeddingModels.TEXT_EMBEDDING_V1
- DashScopeTextEmbeddingModels.TEXT_EMBEDDING_V2
TEXT_EMBEDDING_V2
text_type str

输入类型,['query', 'document']。对于检索等非对称任务,为了获得更好的检索结果,建议区分查询文本 (query) 和基础文本 (document) 类型,分类等对称任务无需特别指定,可使用系统默认值 "document"。

'document'
api_key str

DashScope API 密钥。

源代码位于 llama-index-integrations/embeddings/llama-index-embeddings-dashscope/llama_index/embeddings/dashscope/base.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
class DashScopeEmbedding(MultiModalEmbedding):
    """
    DashScope class for text embedding.

    Args:
        model_name (str): Model name for embedding.
            Defaults to DashScopeTextEmbeddingModels.TEXT_EMBEDDING_V2.
                Options are:

                - DashScopeTextEmbeddingModels.TEXT_EMBEDDING_V1
                - DashScopeTextEmbeddingModels.TEXT_EMBEDDING_V2
        text_type (str): The input type, ['query', 'document'],
            For asymmetric tasks such as retrieval, in order to achieve better
            retrieval results, it is recommended to distinguish between query
            text (query) and base text (document) types, clustering Symmetric
            tasks such as classification and classification do not need to
            be specially specified, and the system default
            value "document" can be used.
        api_key (str): The DashScope api key.

    """

    _api_key: Optional[str] = PrivateAttr()
    _text_type: Optional[str] = PrivateAttr()

    def __init__(
        self,
        model_name: str = DashScopeTextEmbeddingModels.TEXT_EMBEDDING_V2,
        text_type: str = "document",
        api_key: Optional[str] = None,
        embed_batch_size: int = EMBED_MAX_BATCH_SIZE,
        **kwargs: Any,
    ) -> None:
        super().__init__(
            model_name=model_name,
            embed_batch_size=embed_batch_size,
            **kwargs,
        )
        self._api_key = api_key
        self._text_type = text_type

    @classmethod
    def class_name(cls) -> str:
        return "DashScopeEmbedding"

    def _get_query_embedding(self, query: str) -> List[float]:
        """Get query embedding."""
        emb = get_text_embedding(
            self.model_name,
            query,
            api_key=self._api_key,
            text_type="query",
        )
        if len(emb) > 0 and emb[0] is not None:
            return emb[0]
        else:
            return []

    def _get_text_embedding(self, text: str) -> List[float]:
        """Get text embedding."""
        emb = get_text_embedding(
            self.model_name,
            text,
            api_key=self._api_key,
            text_type=self._text_type,
        )
        if len(emb) > 0 and emb[0] is not None:
            return emb[0]
        else:
            return []

    def _get_text_embeddings(self, texts: List[str]) -> List[List[float]]:
        """Get text embeddings."""
        return get_text_embedding(
            self.model_name,
            texts,
            api_key=self._api_key,
            text_type=self._text_type,
        )

    # TODO: use proper async methods
    async def _aget_text_embedding(self, query: str) -> List[float]:
        """Get text embedding."""
        return self._get_text_embedding(query)

    # TODO: user proper async methods
    async def _aget_query_embedding(self, query: str) -> List[float]:
        """Get query embedding."""
        return self._get_query_embedding(query)

    def get_batch_query_embedding(self, embedding_file_url: str) -> Optional[str]:
        """
        Get batch query embeddings.

        Args:
            embedding_file_url (str): The url of the file to embedding which with lines of text to embedding.

        Returns:
            str: The url of the embedding result, format ref:
                 https://help.aliyun.com/zh/dashscope/developer-reference/text-embedding-async-api-details.

        """
        return get_batch_text_embedding(
            self.model_name,
            embedding_file_url,
            api_key=self._api_key,
            text_type=self._text_type,
        )

    def get_batch_text_embedding(self, embedding_file_url: str) -> Optional[str]:
        """
        Get batch text embeddings.

        Args:
            embedding_file_url (str): The url of the file to embedding which with lines of text to embedding.

        Returns:
            str: The url of the embedding result, format ref:
                 https://help.aliyun.com/zh/dashscope/developer-reference/text-embedding-async-api-details.

        """
        return get_batch_text_embedding(
            self.model_name,
            embedding_file_url,
            api_key=self._api_key,
            text_type=self._text_type,
        )

    def _get_image_embedding(self, img_file_path: ImageType) -> List[float]:
        """
        Embed the input image synchronously.
        """
        input = [{"image": img_file_path}]
        return get_multimodal_embedding(
            self.model_name, input=input, api_key=self._api_key
        )

    async def _aget_image_embedding(self, img_file_path: ImageType) -> List[float]:
        """
        Embed the input image asynchronously.

        """
        return self._get_image_embedding(img_file_path=img_file_path)

    def get_multimodal_embedding(
        self, input: List[Dict], auto_truncation: bool = False
    ) -> List[float]:
        """
        Call DashScope multimodal embedding.
        ref: https://help.aliyun.com/zh/dashscope/developer-reference/one-peace-multimodal-embedding-api-details.

        Args:
            input (str): The input of the multimodal embedding, eg:
                [{'factor': 1, 'text': '你好'},
                {'factor': 2, 'audio': 'https://dashscope.oss-cn-beijing.aliyuncs.com/audios/cow.flac'},
                {'factor': 3, 'image': 'https://dashscope.oss-cn-beijing.aliyuncs.com/images/256_1.png'}]

        Raises:
            ImportError: Need install dashscope package.

        Returns:
            List[float]: The embedding result

        """
        return get_multimodal_embedding(
            self.model_name,
            input=input,
            api_key=self._api_key,
            auto_truncation=auto_truncation,
        )

get_batch_query_embedding #

get_batch_query_embedding(embedding_file_url: str) -> Optional[str]

获取批量查询嵌入。

参数

名称 类型 描述 默认值
embedding_file_url str

包含待嵌入文本行的文件的 URL。

必需

返回值

名称 类型 描述
str 可选[str]

嵌入结果文件的 URL,格式参考:https://help.aliyun.com/zh/dashscope/developer-reference/text-embedding-async-api-details。

源代码位于 llama-index-integrations/embeddings/llama-index-embeddings-dashscope/llama_index/embeddings/dashscope/base.py
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
def get_batch_query_embedding(self, embedding_file_url: str) -> Optional[str]:
    """
    Get batch query embeddings.

    Args:
        embedding_file_url (str): The url of the file to embedding which with lines of text to embedding.

    Returns:
        str: The url of the embedding result, format ref:
             https://help.aliyun.com/zh/dashscope/developer-reference/text-embedding-async-api-details.

    """
    return get_batch_text_embedding(
        self.model_name,
        embedding_file_url,
        api_key=self._api_key,
        text_type=self._text_type,
    )

get_batch_text_embedding #

get_batch_text_embedding(embedding_file_url: str) -> Optional[str]

获取批量文本嵌入。

参数

名称 类型 描述 默认值
embedding_file_url str

包含待嵌入文本行的文件的 URL。

必需

返回值

名称 类型 描述
str 可选[str]

嵌入结果文件的 URL,格式参考:https://help.aliyun.com/zh/dashscope/developer-reference/text-embedding-async-api-details。

源代码位于 llama-index-integrations/embeddings/llama-index-embeddings-dashscope/llama_index/embeddings/dashscope/base.py
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
def get_batch_text_embedding(self, embedding_file_url: str) -> Optional[str]:
    """
    Get batch text embeddings.

    Args:
        embedding_file_url (str): The url of the file to embedding which with lines of text to embedding.

    Returns:
        str: The url of the embedding result, format ref:
             https://help.aliyun.com/zh/dashscope/developer-reference/text-embedding-async-api-details.

    """
    return get_batch_text_embedding(
        self.model_name,
        embedding_file_url,
        api_key=self._api_key,
        text_type=self._text_type,
    )

get_multimodal_embedding #

get_multimodal_embedding(input: List[Dict], auto_truncation: bool = False) -> List[float]

调用 DashScope 多模态嵌入。参考:https://help.aliyun.com/zh/dashscope/developer-reference/one-peace-multimodal-embedding-api-details。

参数

名称 类型 描述 默认值
input str

多模态嵌入的输入,例如:[{'factor': 1, 'text': '你好'}, {'factor': 2, 'audio': 'https://dashscope.oss-cn-beijing.aliyuncs.com/audios/cow.flac'}, {'factor': 3, 'image': 'https://dashscope.oss-cn-beijing.aliyuncs.com/images/256_1.png'}]

必需

抛出

类型 描述
ImportError

需要安装 dashscope 包。

返回值

类型 描述
列表[浮点数]

List[float]: 嵌入结果

源代码位于 llama-index-integrations/embeddings/llama-index-embeddings-dashscope/llama_index/embeddings/dashscope/base.py
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
def get_multimodal_embedding(
    self, input: List[Dict], auto_truncation: bool = False
) -> List[float]:
    """
    Call DashScope multimodal embedding.
    ref: https://help.aliyun.com/zh/dashscope/developer-reference/one-peace-multimodal-embedding-api-details.

    Args:
        input (str): The input of the multimodal embedding, eg:
            [{'factor': 1, 'text': '你好'},
            {'factor': 2, 'audio': 'https://dashscope.oss-cn-beijing.aliyuncs.com/audios/cow.flac'},
            {'factor': 3, 'image': 'https://dashscope.oss-cn-beijing.aliyuncs.com/images/256_1.png'}]

    Raises:
        ImportError: Need install dashscope package.

    Returns:
        List[float]: The embedding result

    """
    return get_multimodal_embedding(
        self.model_name,
        input=input,
        api_key=self._api_key,
        auto_truncation=auto_truncation,
    )