接口实现按主键id检索,会以这条主键的向量值为检索条件。
请求向量数据库 VikingDB 的 OpenAPI 接口时,可以使用API Key(推荐)或者 AK、SK 构造签名进行鉴权。请参见数据面API调用流程,复制调用示例并填入必要信息
URI | /api/vikingdb/data/search/id | 统一资源标识符 |
|---|---|---|
方法 | POST | 客户端对向量数据库服务器请求的操作类型 |
请求头 | Content-Type: application/json | 请求消息类型 |
Authorization: *** | 鉴权 |
仅列出本接口特有的参数。更多信息请参见检索公共参数。
参数名 | 必选 | 类型 | 备注 |
|---|---|---|---|
id | 是 | int64/string | 主键id。会以这条主键的向量值为检索条件。 |
req_path = "/api/vikingdb/data/search/id" req_body = { "collection_name": "test_coll", "index_name": "idx_1", "id": "uid_001", "limit": 2 }
{ "code": "Success", "message": "The API call was executed successfully.", "request_id": "02175438839168500000000000000000000ffff0a003ee4fc3499", "result": { "data": [ { "id": "uid_001", "fields": { "f_good_id": "uid_001", "f_price": 999000 }, "score": 1.000000, "ann_score": 1.000000 }, { "id": "uid_002", "fields": { "f_good_id": "uid_002", "f_price": 309000 }, "score": 0.88232, "ann_score": 0.88232, } ], "total_return_count": 2 } }
""" pip3 install volcengine """ from volcengine.base.Request import Request import requests, json class ClientForDataApi: def __init__(self, api_key=None, host=None): """ 初始化ClientForDataApi类 :param api_key: VikingDB的API Key :param host: VikingDB的API Host """ if not api_key or not host: raise ValueError("api_key and host are required") self.api_key = api_key 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, "Authorization": f"Bearer {self.api_key}", } 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)) 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__": import os client = ClientForDataApi( api_key=os.environ.get("VIKINGDB_API_KEY", "***"), # 从环境变量中获取API Key,若未设置则使用默认值 host="api-vikingdb.vikingdb.cn-beijing.volces.com", # 替换为您所在的域名 ) req_method = "POST" req_params = None req_path = "/api/vikingdb/data/search/id" req_body = { "collection_name": "test_coll", "index_name": "idx_1", "id": "uid_001", "limit": 2, } 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)