通过ModelScope开源多模态Embedding模型进行向量生成

2025-09-09 15:36:37 0点赞 0收藏 0评论

本文介绍如何通过ModelScope魔搭社区中的多模态表征开源模型进行多模态向量生成,并入库至向量检索服务DashVector中进行向量检索。

ModelScope魔搭社区旨在打造下一代开源的模型即服务共享平台,为泛AI开发者提供灵活、易用、低成本的一站式模型服务产品,让模型应用更简单。

ModelScope魔搭社区的愿景是汇集行业领先的预训练模型,减少开发者的重复研发成本,提供更加绿色环保、开源开放的AI开发环境和模型服务,助力绿色“数字经济”事业的建设。 ModelScope魔搭社区将以开源的方式提供多类优质模型,开发者可在平台上免费体验与下载使用。

在ModelScope魔搭社区,您可以:

  • 免费使用平台提供的预训练模型,支持免费下载运行

  • 一行命令实现模型预测,简单快速验证模型效果

  • 用自己的数据对模型进行调优,定制自己的个性化模型

  • 学习系统性的知识,结合实训,有效提升模型研发能力

  • 分享和贡献你的想法、评论与模型,让更多人认识你,在社区中成长

前提条件

CLIP模型

简介

本项目为CLIP模型的中文版本,使用大规模中文数据进行训练(~2亿图文对),可用于图文检索和图像、文本的表征提取,应用于搜索、推荐等应用场景。

通过ModelScope开源多模态Embedding模型进行向量生成

说明

关于CLIP模型更多信息请参考:CLIP模型

使用示例

说明

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

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

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

  3. 使用上表中模型ID替换示例中的{model_id}

  4. 使用上表中向量维度替换示例中的{model_dim}

Python

from modelscope.utils.constant import Tasks from modelscope.pipelines import pipeline from modelscope.preprocessors.image import load_image from typing import List from dashvector import Client pipeline = pipeline(task=Tasks.multi_modal_embedding, model='{model_id}') def generate_text_embeddings(texts: List[str]): inputs = {'text': texts} result = pipeline.forward(input=inputs) return result['text_embedding'].numpy() def generate_img_embeddings(img: str): input_img = load_image(img) inputs = {'img': input_img} result = pipeline.forward(input=inputs) return result['img_embedding'].numpy()[0] # 创建DashVector Client client = Client( api_key='{your-dashvector-api-key}', endpoint='{your-dashvector-cluster-endpoint}' ) # 创建DashVector Collection rsp = client.create('CLIP-embedding', dimension={model_dim}) assert rsp collection = client.get('CLIP-embedding') assert collection # 向量入库DashVector collection.insert( [ ('ID1', generate_text_embeddings(['阿里云向量检索服务DashVector是性能、性价比具佳的向量数据库之一'])[0]), ('ID2', generate_img_embeddings('https://clip-cn-beijing.oss-cn-beijing.aliyuncs.com/pokemon.jpeg')) ] ) # 向量检索 docs = collection.query( generate_text_embeddings(['The best vector database'])[0] ) print(docs)

展开 收起
0评论

当前文章无评论,是时候发表评论了
提示信息

取消
确认
评论举报

相关文章推荐

更多精彩文章
更多精彩文章
最新文章 热门文章
0
扫一下,分享更方便,购买更轻松