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

DashVector · · 200 次点击 · · 开始浏览    

本文介绍如何通过[模型服务灵积DashScope](https://dashscope.aliyun.com)进行 **多模态向量生成** ,并入库至向量检索服务DashVector中进行向量检索。 [模型服务灵积DashScope](https://dashscope.aliyun.com),通过灵活、易用的模型API服务,让各种模态模型的能力,都能方便的为AI开发者所用。通过灵积API,开发者不仅可以直接集成大模型的强大能力,也可以对模型进行训练微调,实现模型定制化。 前提条件 --------------------- * DashVector: * 已创建Cluster * 已获得API-KEY * 已安装最新版SDK * 已开通服务并获得API-KEY * 已安装最新版SDK ONE-PEACE多模态向量表征 --------------------------------- ### 简介 [ONE-PEAC](https://help.aliyun.com/zh/dashscope/developer-reference/one-peace-multimodal-embedding-quick-start?spm=a2c4g.2673784.0.0.5ddc5e17x54Jn9)是一个 **图文音三模态** 通用表征模型,在语义分割、音文检索、音频分类和视觉定位几个任务都达到了新SOTA表现,在视频分类、图像分类、图文检索、以及多模态经典benchmark也都取得了比较领先的结果。 **说明** 关于灵积ONE-PEACE多模态向量表征更多信息请参考:[ONE-PEACE多模态向量表征](https://help.aliyun.com/zh/dashscope/developer-reference/one-peace-multimodal-embedding-root/?spm=a2c4g.2673784.0.0.51ab250eH1sbSw)。 ### **使用示例** **说明** 需要进行如下替换代码才能正常运行: 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 ```python import dashscope from dashscope import MultiModalEmbedding from dashvector import Client dashscope.api_key = '{your-dashscope-api-key}' # 调用DashScope ONE-PEACE模型,将各种模态素材embedding为向量 def generate_embeddings(text: str = None, image: str = None, audio: str = None): input = [] if text: input.append({'text': text}) if image: input.append({'image': image}) if audio: input.append({'audio': audio}) result = MultiModalEmbedding.call( model=MultiModalEmbedding.Models.multimodal_embedding_one_peace_v1, input=input, auto_truncation=True ) if result.status_code != 200: raise Exception(f"ONE-PEACE failed to generate embedding of {input}, result: {result}") return result.output["embedding"] # 创建DashVector Client client = Client( api_key='{your-dashvector-api-key}', endpoint='{your-dashvector-cluster-endpoint}' ) # 创建DashVector Collection rsp = client.create('one-peace-embedding', 1536) assert rsp collection = client.get('one-peace-embedding') assert collection # 向量入库DashVector collection.insert( [ ('ID1', generate_embeddings(text='阿里云向量检索服务DashVector是性能、性价比具佳的向量数据库之一')), ('ID2', generate_embeddings(image='https://dashscope.oss-cn-beijing.aliyuncs.com/images/256_1.png')), ('ID3', generate_embeddings(audio='https://dashscope.oss-cn-beijing.aliyuncs.com/audios/cow.flac')), ('ID4', generate_embeddings( text='阿里云向量检索服务DashVector是性能、性价比具佳的向量数据库之一', image='https://dashscope.oss-cn-beijing.aliyuncs.com/images/256_1.png', audio='https://dashscope.oss-cn-beijing.aliyuncs.com/audios/cow.flac' )) ] ) # 向量检索 docs = collection.query( generate_embeddings(text='The best vector database') ) print(docs) ``` ### 相关最佳实践 * [DashVector + DashScope升级多模态检索](https://help.aliyun.com/document_detail/2568028.html?spm=a2c4g.2673784.0.0.69053550bQTmY5)

有疑问加站长微信联系(非本文作者))

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

200 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传