分享
  1. 首页
  2. 文章

如何通过Python SDK 向 Collection 中插入 Doc

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

本文介绍如何通过Python SDK向Collection中插入Doc。 **说明** 1. 插入Doc时若指定id已存在,已存在的Doc不会被覆盖,本次插入Doc操作无效。 2. 插入Doc时若不指定id,则在插入过程中会自动生成id,并在返回结果中携带id信息。 前提条件 --------------------- * 已创建Cluster * 已获得API-KEY * 已安装最新版SDK **接口定义** ----------------- Python ```python Collection.insert( docs: Union[Doc, List[Doc], Tuple, List[Tuple]], partition: Optional[str] = None, async_req: False ) -> DashVectorResponse ``` **使用示例** ----------------- **说明** 1. 需要使用您的api-key替换示例中的YOUR_API_KEY、您的Cluster Endpoint替换示例中的YOUR_CLUSTER_ENDPOINT,代码才能正常运行。 2. 本示例需要参考[新建Collection-使用示例](https://help.aliyun.com/document_detail/2510242.html#hivl6)的Collection。 Python ```python import dashvector from dashvector import Doc import numpy as np client = dashvector.Client( api_key='YOUR_API_KEY', endpoint='YOUR_CLUSTER_ENDPOINT' ) collection = client.get(name='quickstart') ``` ### **插入Doc** Python ```python # 通过Doc对象insert ret = collection.insert( Doc( id='1', vector=[0.1, 0.2, 0.3, 0.4] ) ) # 判断insert是否成功 assert ret # 简化形式:通过Tuple insert ret = collection.insert( ('2', [0.1, 0.1, 0.1, 0.1]) # (id, vector) ) ``` ### 插入不带有Id的Doc Python ```python # 插入Doc不携带id ret = collection.insert( Doc(vector=[0.1, 0.2, 0.3, 0.4]) ) # 简化形式:通过Tuple insert ret = collection.insert( ([0.1, 0.1, 0.1, 0.1],) ) ``` ### 插入带有Fields的Doc Python ```python # insert单条数据,并设置Fields Value ret = collection.insert( Doc( id='3', vector=np.random.rand(4), fields={ # 设置创建Collection时预定义的Fields Value # name:str, weight:float, age:int, id:dashvector.long 'name': 'zhangsan', 'weight':70.0, 'age':30, 'id':1234567890, # 设置Schema-Free的Field & Value 'anykey1': 'str-value', 'anykey2': 1, 'anykey3': True, 'anykey4': 3.1415926 } ) ) # insert单条数据,并设置Fields Value ret = collection.insert( ('4', np.random.rand(4), {'foo': 'bar'}) # (id, vector, fields) ) ``` ### **批量插入Doc** Python ```python # 通过Doc对象,批量insert 10条数据 ret = collection.insert( [ Doc(id=str(i+5), vector=np.random.rand(4)) for i in range(10) ] ) # 简化形式:通过Tuple,批量insert 3条数据 ret = collection.insert( [ ('15', [0.2,0.7,0.8,1.3], {'age': 20}), ('16', [0.3,0.6,0.9,1.2], {'age': 30}), ('17', [0.4,0.5,1.0,1.1], {'age': 40}) ] # List[(id, vector, fields)] ) # 判断批量insert是否成功 assert ret ``` ### 异步插入Doc Python ```python # 异步批量insert 10条数据 ret_funture = collection.insert( [ Doc(id=str(i+18), vector=np.random.rand(4), fields={'name': 'foo' + str(i)}) for i in range(10) ], async_req=True ) # 等待并获取异步insert结果 ret = ret_funture.get() ``` ### **插入带有Sparse Vector的Doc** Python ```python ret = collection.insert( Doc( id='28', vector=[0.1, 0.2, 0.3, 0.4], sparse_vector={1:0.4, 10000:0.6, 222222:0.8} ) ) ``` ### **插入多向量的Doc** Python ```python collection = client.get(name='multi_vector_demo') docs = [] docs.append(Doc(id='1', vectors={"title": [0.3, 0.4, 0.5, 0.6], "content": [0.3, 0.4, 0.5, 0.6, 0.7, 0.8]})) docs.append(Doc(id='2', vectors={"title": [0.3, 0.4, 0.5, 0.6]}, fields={'author':'zhangsan'})) docs.append(Doc(id='3', vectors={"content": [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]}, fields={'anykey':'anyvalue'})) docs.append(Doc(id='4', vectors={"title": [0.3, 0.4, 0.5, 0.6], "content": [0.3, 0.4, 0.5, 0.6, 0.7, 0.8]}, sparse_vectors={"abstruct": {0: 0.1, 1: 0.32, 2: 0.482}})) docs.append(Doc(id='5', sparse_vectors={"abstruct": {0: 0.3, 1: 0.232, 2: 0.4482, 3: 0.6672}}, fields={'author':'lisi'})) ret = collection.insert(docs) print(ret) ``` **说明** 多向量Collection中,稠密向量+稀疏向量字段总共不能超过4条 [点击了解更多关于向量检索服务 DashVector信息,可进行免费试用~](https://www.aliyun.com/product/ai/dashvector)

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

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

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

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏