智能召回引擎搭建完成后,通过API(Application Programming Interface)方式完成对召回引擎的读请求,以及及对详情表的写请求。
通过API可以进行如下操作:
对运行中的x2i、向量的服务进行查询。
对详情表进行增量写入。
域名
智能召回引擎搭建完成后,可以从控制台上获取域名。格式为${实例id}.aime.aliyuncs.com。
目前智能引擎对请求来源做了安全限制,该域名仅能在您购买实例关联的VPC内进行访问。
签名
智能召回引擎使用http auth对读写请求进行验权,需要用户将设置的用户名密码加到请求的http header中。
基于Apache http client的实现:
String auth = userName + ":" + passWord;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1));
String authHeader = "Basic " + new String(encodedAuth);
String query = "aime query here";
HttpGet httpGet = new HttpGet(query);
httpGet.setHeader("Authorization", authHeader);
curl请求
curl "http://aime-cn-xxxx.aime.aliyuncs.com/be?biz_name=searcher&p=test_x2i_biz&s=test_x2i_biz&return_count=10&outfmt=json2&trigger_list=trigger_key:1,trigger_key2:1.5" -u ${userName}:{passWord}
读请求
请求语法
GET
http://${实例id}.aime.aliyuncs.com/be?biz_name=searcher
&p=${服务名}
&s=${服务名}
&return_count=${返回doc个数}
&outfmt=json2
&trigger_list=${trigger列表}
服务名:智能召回引擎中处于运行状态的服务名称
返回doc个数:本次查询需要返回的召回doc个数
trigger列表:
x2i服务:为召回引擎中trigger的值,多个trigger用','分割。其中每个trigger需要带上打分的加权值,类型为数值型,以':'分割。例如"trigger_key:1,trigger_key2:1.5",表示需要召回trigger_key1和trigger_key2关联的doc列表,其中trigger_key1召回的doc的得分最终会乘基数1,trigger_key2召回的doc得分最终会乘基数1.5。
向量服务:为向量对应维度的向量,向量维度间用','分割,多个向量用';'分割。
x2i example
http://aime-cn-xxxx.aime.aliyuncs.com/be?biz_name=searcher&p=test_x2i_biz&s=test_x2i_biz&return_count=10&outfmt=json2&trigger_list=trigger_key:1,trigger_key2:1.5
向量 example
http://aime-cn-xxxx.aime.aliyuncs.com/be?biz_name=searcher&p=test_vec_biz&s=test_vec_biz&return_count=10&outfmt=json2&trigger_list=-0.5430353283882141,-0.0292476424574852,-0.3608616292476654,-0.35043397545814514,-0.23355364799499512,-0.24787241220474243,0.27063214778900146,-0.40489235520362854,0.3900103271007538,0.54123854637146,-0.006652768235653639,0.26998400688171387,-0.08973824232816696,0.5796284079551697,0.1965138018131256,0.6804714798927307,-0.3605094254016876,-0.12562842667102814,0.543321430683136,0.4717116057872772,-0.30192506313323975,0.2214985191822052,0.1516236513853073,0.21026527881622314,-0.6978874206542969,-0.11705683171749115,0.2574385106563568,-0.3885158896446228,-0.4346419870853424,0.1547151654958725
返回结果:
errorCode: 请求错误码,成功为0
errorMessage: 请求错误详情
matchItems:召回结果集
fieldNames:召回字段列表
fieldValues:召回doc内容列表
example
{
"errorCode":0,
"errorMessage":"none",
"matchItems":{
"fieldNames":[
"__score__",
"title",
"id",
"match_type",
"score",
"trigger_id",
"trigger_score"
],
"fieldValues":[
[
"1.0",
"title_1",
"3489628730026797560",
"1",
"0.000009883646271191537",
"indexall_common",
"1.0"
],
[
"1.0",
"title_2",
"-3094066460264965022",
"1",
"0.000005524287644220749",
"indexall_common",
"1.0"
]
]
}
}
写请求
请求语法
GET
http://${实例id}.aime.aliyuncs.com/sendmsg?
&table=${table名称}
&h=${内容哈希值}
&msg=${增量内容}
table名称:智能召回引擎开启增量模式的详细表名称
内容哈希值:增量的分片参数,一般取发送内容的哈希值即可
增量内容:发送的增量内容
格式要求,增量以kv对的格式发送给召回引擎。kv对内使用
%3D
分割,多个kv对之间使用%1F%0A
分割,kv对要以%1F%0A
结尾。增量支持add和delete消息,需要将
CMD%3Dadd
或CMD%3Ddelete
加到多个kv对的首个位置。
add消息需要填充详细表中的所有字段,否则会因为数据不完整导致消息消费失败。
delete消息需要包含详情表的主键字段
add example
http://aime-cn-xxxx.aime.aliyuncs.com:80/sendmsg?table=detail_test_inc_table&h=2035815278&msg=CMD%3Dadd%1F%0A_item_hash_id%3D-5656019669429076362%1F%0Aauthor%3Dtest%1F%0Acategory%3D1%1F%0Acategory_level%3D5%1F%0Acategory_level1%3D1%1F%0Acategory_path%3D1_1_1_1_1%1F%0Achannel%3Dnone%1F%0Acity%3Dnone%1F%0Aclick_cnt%3D-1%1F%0Acollect_cnt%3D-1%1F%0Acomm_cnt%3D-1%1F%0Acountry%3Dnone%1F%0Adownload_cnt%3D-1%1F%0Aduration%3D-1%1F%0Aexpire_time%3D4102416000%1F%0Afeatures%3Dnone%1F%0Aid%3D-5656019669429076362%1F%0A
delete example
http://aime-cn-xxxx.aime.aliyuncs.com/sendmsg?table=detail_test_inc_table&h=-71900929&msg=CMD%3Ddelete%1F%0Aid%3D-5656019669429076362%1F%0A
返回结果
成功:{"success":true}
失败:{"errno":1,"errmsg":"Error"},具体错误原因请联系技术支持