当前位置:   article > 正文

通过DashScope API调用将多种模态转换为向量

dashscope api

本文介绍如何通过模型服务灵积DashScope进行多模态向量生成,并入库至向量检索服务DashVector中进行向量检索。

模型服务灵积DashScope,通过灵活、易用的模型API服务,让各种模态模型的能力,都能方便的为AI开发者所用。通过灵积API,开发者不仅可以直接集成大模型的强大能力,也可以对模型进行训练微调,实现模型定制化。

前提条件

ONE-PEACE多模态向量表征

简介

ONE-PEAC是一个图文音三模态通用表征模型,在语义分割、音文检索、音频分类和视觉定位几个任务都达到了新SOTA表现,在视频分类、图像分类、图文检索、以及多模态经典benchmark也都取得了比较领先的结果。

模型名称

向量维度

度量方式

向量数据类型

备注

multimodal-embedding-one-peace-v1

1536

Cosine

Float32

  • 图片:图像格式目前支持bmp, jpg, jpeg, png 和 tiff;文件大小不超过5M

  • 音频:当前支持最大音频时长为15s,超出该时长的音频内容在 auto-truncation 功能打开的情况下会被截断继续计算向量,auto-truncation 功能关闭的时候本次请求会报错返回;语音格式目前支持 wav, mp3 和 flac;文件大小不超过5M

  • 文本:当前支持最大文本长度为70 字,超出该长度的文本内容在 auto-truncation 功能打开的情况下会被截断继续计算向量,auto-truncation 功能关闭的时候本次请求会报错返回;

说明

关于灵积ONE-PEACE多模态向量表征更多信息请参考:ONE-PEACE多模态向量表征

使用示例

说明

需要进行如下替换代码才能正常运行:

  1. DashVector api-key替换示例中的{your-dashvector-api-key}

  2. DashVector Cluster Endpoint替换示例中的{your-dashvector-cluster-endpoint}

  3. DashScope api-key替换示例中的{your-dashscope-api-key}

Python示例:

  1. import dashscope
  2. from dashscope import MultiModalEmbedding
  3. from dashvector import Client
  4. dashscope.api_key = '{your-dashscope-api-key}'
  5. # 调用DashScope ONE-PEACE模型,将各种模态素材embedding为向量
  6. def generate_embeddings(text: str = None, image: str = None, audio: str = None):
  7. input = []
  8. if text:
  9. input.append({'text': text})
  10. if image:
  11. input.append({'image': image})
  12. if audio:
  13. input.append({'audio': audio})
  14. result = MultiModalEmbedding.call(
  15. model=MultiModalEmbedding.Models.multimodal_embedding_one_peace_v1,
  16. input=input,
  17. auto_truncation=True
  18. )
  19. if result.status_code != 200:
  20. raise Exception(f"ONE-PEACE failed to generate embedding of {input}, result: {result}")
  21. return result.output["embedding"]
  22. # 创建DashVector Client
  23. client = Client(
  24. api_key='{your-dashvector-api-key}',
  25. endpoint='{your-dashvector-cluster-endpoint}'
  26. )
  27. # 创建DashVector Collection
  28. rsp = client.create('one-peace-embedding', 1536)
  29. assert rsp
  30. collection = client.get('one-peace-embedding')
  31. assert collection
  32. # 向量入库DashVector
  33. collection.insert(
  34. [
  35. ('ID1', generate_embeddings(text='阿里云向量检索服务DashVector是性能、性价比具佳的向量数据库之一')),
  36. ('ID2', generate_embeddings(image='https://dashscope.oss-cn-beijing.aliyuncs.com/images/256_1.png')),
  37. ('ID3', generate_embeddings(audio='https://dashscope.oss-cn-beijing.aliyuncs.com/audios/cow.flac')),
  38. ('ID4', generate_embeddings(
  39. text='阿里云向量检索服务DashVector是性能、性价比具佳的向量数据库之一',
  40. image='https://dashscope.oss-cn-beijing.aliyuncs.com/images/256_1.png',
  41. audio='https://dashscope.oss-cn-beijing.aliyuncs.com/audios/cow.flac'
  42. ))
  43. ]
  44. )
  45. # 向量检索
  46. docs = collection.query(
  47. generate_embeddings(text='The best vector database')
  48. )
  49. print(docs)

相关最佳实践


阿里云向量检索服务 DashVector 免费试用进行中,玩转大模型搜索,快来试试吧~
了解更多信息,请点击:https://www.aliyun.com/product/ai/dashvector 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/869702
推荐阅读
相关标签
  

闽ICP备14008679号