接口用于在指定的数据集 Collection 内写入数据。指定写入的数据是一个map,允许单次插入一条数据或者多条数据,单次最多可插入100条数据。
注意
在执行upsert操作选择上传数据的fields字段时,跟创建的数据集类型有关。
若数据集带vector的字段(选择“已有向量数据”):请仅上传“vector”字段,不要上传“text”或 “image”字段。两者不能同时上传。
若数据集带vectorize字段(选择“从向量化开始”):请根据需求上传text、image字段中的一个或一起上传,不要上传“vector”字段;
若需要上传image图多模态类型字段,请先将图片上传至TOS,并将图片的tos存储路径传入"image"字段。
V2接口 | V1接口 | |
|---|---|---|
写入数据对应的参数 | data | fields |
返回模型消耗token量 | 自动返回 | 需手动设置 |
单次写入的数据量限制 | 如果数据集是带vectorize的,不超过1,时延更小;不带vectorize的,不超过100。 | 不超过100。 |
说明
请求向量数据库 VikingDB 的 OpenAPI 接口时,可以使用 ak、sk 构造签名进行鉴权。请参见数据面API调用流程,复制调用示例并填入必要信息
URI | /api/vikingdb/data/upsert | 统一资源标识符 |
|---|---|---|
方法 | POST | 客户端对向量数据库服务器请求的操作类型 |
请求头 | Content-Type: application/json | 请求消息类型 |
Authorization: HMAC-SHA256 *** | 鉴权 |
参数名 | 类型 | 必选 | 默认值 | 备注 |
|---|---|---|---|---|
resource_id | string | 2选1 | 资源id | |
collection_name | string | collection名称 | ||
data | array | 是 | 单次写入的数据数目不超过100。
| |
ttl | int | 否 | 0 | 正整数,负数无效 |
async | bool | 否 | false | 异步写入开关
|
说明
注意:数据插入时主键不能为0
字段类型 | 格式 | 说明 |
|---|---|---|
int64 | 整型数值 | 整数 |
float32 | 浮点数值 | 浮点数 |
string | 字符串 | 字符串。内容限制256byte |
bool | true/false | 布尔类型 |
list | 字符串数组 | 字符串数组 |
list | 整型数组 | 整数数组 |
vector |
| 稠密向量 |
sparse_vector | 输入格式<token_id ,token_weight>的字典列表,来表征稀疏稀疏向量的非零位下标及其对应的值, 其中 token_id 是 string 类型, token_weight 是float 类型 | 稀疏向量 |
text | 字符串 | 若为向量化字段,则值不能为空。(若否,可以为空) |
image | 字符串 | 若为向量化字段,则值不能为空。(若否,可以为空)
|
video | map | { |
date_time | string | 分钟级别: |
geo_point | string | 地理坐标 |
只能填写以下格式的其中一种,全部遵循RFC3339标准(https://datatracker.ietf.org/doc/html/rfc3339)
格式(string) | 示例 | 说明 | |
|---|---|---|---|
分钟级别 |
|
| 左侧示例都会解析为北京时间 |
秒级别 |
|
| 左侧示例都会解析为北京时间 |
毫秒级别 |
|
| 左侧示例都会解析为北京时间 |
公共响应体参数部分见
数据面API调用流程
参数名 | 类型 | 子参数 | 说明 | |
|---|---|---|---|---|
result | map | token_usage | map | 包括prompt_tokens、completion_tokens、image_tokens、total_tokens信息 |
req_path = "/api/vikingdb/data/upsert" req_body = { "collection_name": "test_coll", "data": [ { "f_id": "000135", #以下参数为选填,请根据coll创建字段填写 "f_vector": [0.1, 0.33, -0.88, 0.66], "f_city": "北京" } ] }
{ "code": "Success", "message": "The API call was executed successfully.", "request_id": "02175438839168500000000000000000000ffff0a003ee4fc3499", "result": null }
req_path = "/api/vikingdb/data/upsert" req_body = { "collection_name": "test_coll_with_vectorize", "data": [ { "f_id": "000135", "f_city": "北京", "f_text": "这是一件历史悠久的文物,具有1300年的历史", "f_image": "tos://my_bucket/good_000135.jpg" } ] }
{ "code": "Success", "message": "The API call was executed successfully.", "request_id": "02175438839168500000000000000000000ffff0a003ee4fc3499", "result": { "token_usage": { "doubao-embedding-vision__250615": { "prompt_tokens": 1325, "completion_tokens": 0, "image_tokens": 1312, "total_tokens": 1325 } } } }
req_path = "/api/vikingdb/data/upsert" req_body = { "collection_name": "test_coll_with_vectorize", "data": [ { "f_id": "000135", "f_city": "北京", "f_text": "这是一件历史悠久的文物,具有1300年的历史", "f_image": "tos://my_bucket/good_000135.jpg", "f_video": { "value": "tos://my_bucket/good_000135.mp4", "fps": 2.0, } } ] }
{ "code": "Success", "message": "The API call was executed successfully.", "request_id": "02175438839168500000000000000000000ffff0a003ee4fc3499", "result": { "token_usage": { "doubao-embedding-vision__250615": { "prompt_tokens": 1325, "completion_tokens": 0, "image_tokens": 1312, "total_tokens": 1325 } } } }
req_path = "/api/vikingdb/data/upsert" req_body = { "collection_name": "test", "data": [ {"f_id": 1, "f_time": "2025-08-12T12:34:56+08:00", "f_vector":[...]}, {"f_id": 2, "f_time": "2025-08-12T11:33:56+08:00", "f_vector":[...]}, {"f_id": 3, "f_time": "2025-08-12T04:32:56+08:00", "f_vector":[...]} ] }
req_path = "/api/vikingdb/data/upsert" req_body = { "collection_name": "test", "data": [ {"f_id": 1, "f_geo_point": "116.403874,39.914885", "f_vector":[...]}, {"f_id": 2, "f_geo_point": "116.412138,39.914912", "f_vector":[...]}, {"f_id": 3, "f_geo_point": "116.408108,39.915023", "f_vector":[...]} ] }
""" pip3 install volcengine """ import os from volcengine.auth.SignerV4 import SignerV4 from volcengine.Credentials import Credentials from volcengine.base.Request import Request import requests, json class ClientForDataApi: def __init__(self, ak, sk, host): self.ak = ak self.sk = sk self.host = host def prepare_request(self, method, path, params=None, data=None): r = Request() r.set_shema("https") r.set_method(method) r.set_connection_timeout(10) r.set_socket_timeout(10) mheaders = { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Host': self.host, } r.set_headers(mheaders) if params: r.set_query(params) r.set_host(self.host) r.set_path(path) if data is not None: r.set_body(json.dumps(data)) credentials = Credentials(self.ak, self.sk, 'vikingdb', 'cn-beijing') SignerV4.sign(r, credentials) return r def do_req(self, req_method, req_path, req_params, req_body): req = self.prepare_request(method=req_method, path=req_path, params=req_params, data=req_body) return requests.request(method=req.method, url="http://{}{}".format(self.host, req.path), headers=req.headers, data=req.body, timeout=10000) if __name__ == '__main__': client = ClientForDataApi( ak = "*",#替换为您的ak sk = "*",#替换为您的sk host = "api-vikingdb.vikingdb.cn-beijing.volces.com",#替换为您所在的域名 ) req_method = "POST" req_params = None req_path = "/api/vikingdb/data/upsert" req_body = { "collection_name": "test_coll_with_vectorize",#替换为您的collection名 "data": [ { "f_id": "000135", "f_city": "北京", "f_text": "这是一件历史悠久的文物,具有1300年的历史", "f_image": "tos://my_bucket/good_000135.jpg" } ] } result = client.do_req(req_method=req_method, req_path=req_path, req_params=req_params, req_body=req_body) print("req http status code: ", result.status_code) print("req result: \n", result.text)