API多轮对话的实现
半醉半醒半疯Z 发布于2018-07-23 15:50 浏览:2371 回复:19
8
收藏
最后编辑于2018-11-14

使用API对话接口时,想要实现多轮对话,需要传递bot_session或session_id的信息。

第一轮对话bot_session需要置空,置空的写法是"bot_session": ""。从第二轮开始,多轮对话的每轮都需将上轮返回结果里的bot_session(或session_id)的值完整不动的传给本轮的bot_session。

1.传bot_session的值

      返回数据里bot_session的形式如下:

     "bot_session": "{\"bot_id\":\"1201\",......\"timestamp\":\"  request time   \"}],\"session_id\":\"     value   \"}\n"

      可以将bot_session的值(整个双引号里的内容)完整不做任何修改的传入请求参数的bot_session里。也可以去掉最后的  \n  。其他部分不需要做任何修改。

2.传session_id的值

     如果传的是session_id,则请求参数形式是:"bot_session": "{\"session_id\":\" value \"}"

     返回数据里bot_session的形式如下:

     "bot_session": "{\"bot_id\":\"1201\",......,\"session_id\":\"     value   \"}\n"

     可以直接将     \"session_id\":\"value\"    取出,使用  {}  包起来,然后再传入入参的bot_session里。

      格式: "bot_session": "{\"session_id\":\"value\"}"

收藏
点赞
8
个赞
共19条回复 最后由inderzhu回复于2018-11-14 16:22
#20inderzhu回复于2018-11-14 16:22:54
#19 荒墨丶迷失回复
按道理不会出现这个问题的呀,除非sessionID一样了

请求百度ai把上次对话的bot_session也传进去可以达到多轮对话的实现,这个是没问题的。但我遇到一个问题:我用电脑A访问UI界面和机器人对话是没问题,但当我又用另一台电脑B访问UI界面和机器人对话,此时B的对话会出现A的对话回答缓存,这是为什么?我的后台程序做了session用户的区分,当是新用户的时候把bot_session给置空,相当于访问后台程序bot_session第一次传空参,以后bot_session就传上次对话的bot_session,这样做还是不行,他们的对话回答还是会串在一起,大神能指点指点么?告诉我怎么解决!!

0
#19荒墨丶迷失回复于2018-11-12 21:27:08
#18 inderzhu回复
我传sessionid保存上下文的对话,为什么会出现A用户和B用户同时访问出现回复混乱的样子??求解答
展开

按道理不会出现这个问题的呀,除非sessionID一样了

1
#18inderzhu回复于2018-11-12 18:39:21

我传sessionid保存上下文的对话,为什么会出现A用户和B用户同时访问出现回复混乱的样子??求解答

0
#17love卵疼回复于2018-11-03 14:33:22

这是我 一次对话百度json返回的bot_session内容,我不确定,在第二轮对话的时候是否需要把这么多再次传回去(如果我语文学的没错的情况下,楼主应该就是这个意思)

 

{{"bot_id":"12968","bot_views":{"bernard_res":[{"action_list":[{"action_id":"","confidence":0.0,"custom_reply":"","refine_detail":{"clarify_reason":"","interact":"","option_list":[]},"say":"","type":"understood"}],"msg":"ok","qu_res":{"candidates":[{"confidence":100.0,"domain_confidence":0.0,"extra_info":{"group_id":"5","real_threshold":"1","threshold":"0.7"},"from_who":"pow-slu-lev1","intent":"QUERYPOSITION","intent_confidence":100.0,"intent_need_clarify":false,"match_info":"{\"group_id\":\"5\",\"id\":\"739186\",\"match_keywords\":\" kw_querypositionyuqi:在哪里\",\"match_pattern\":\"[D:user_queryposition]\\t[D:kw_querypositionyuqi]\",\"ori_pattern\":\"[D:user_queryposition]\\t[D:kw_querypositionyuqi]\",\"ori_slots\":{\"confidence\":100.0,\"domain_confidence\":0.0,\"extra_info\":{},\"from_who\":\"smart_qu\",\"intent\":\"QUERYPOSITION\",\"intent_confidence\":100.0,\"intent_need_clarify\":false,\"match_info\":\"[D:user_queryposition] \\t[D:kw_querypositionyuqi] kw_querypositionyuqi:在哪里\",\"slots\":[{\"begin\":0,\"confidence\":100.0,\"father_idx\":-1,\"length\":4,\"name\":\"user_queryposition\",\"need_clarify\":false,\"normalized_word\":\"\",\"original_word\":\"电梯\",\"word_type\":\"\"},{\"begin\":4,\"confidence\":100.0,\"father_idx\":-1,\"length\":6,\"name\":\"kw_querypositionyuqi\",\"need_clarify\":false,\"normalized_word\":\"\",\"original_word\":\"在哪里\",\"word_type\":\"\"}]},\"real_threshold\":1.0,\"threshold\":0.6999999880790710}","slots":[{"begin":0,"confidence":100.0,"father_idx":-1,"length":4,"name":"user_queryposition","need_clarify":false,"normalized_word":"电梯","original_word":"电梯","word_type":""}]}],"lexical_analysis":[{"basic_word":["电梯"],"etypes":["[D:user_queryposition]"],"term":"电梯","type":"user_queryposition","weight":0.6080},{"basic_word":["在"],"etypes":[],"term":"在","type":"28","weight":0.130},{"basic_word":["哪里"],"etypes":["[D:COREF_LOCATION]","sys_coref_location","sys_coref_location"],"term":"哪里","type":"sys_coref_location","weight":0.260}],"qu_res_chosen":"{\"confidence\":100.0,\"domain_confidence\":0.0,\"extra_info\":{\"group_id\":\"5\",\"real_threshold\":\"1\",\"threshold\":\"0.7\"},\"from_who\":\"pow-slu-lev1\",\"intent\":\"QUERYPOSITION\",\"intent_confidence\":100.0,\"intent_need_clarify\":false,\"match_info\":\"{\\\"group_id\\\":\\\"5\\\",\\\"id\\\":\\\"739186\\\",\\\"match_keywords\\\":\\\" kw_querypositionyuqi:在哪里\\\",\\\"match_pattern\\\":\\\"[D:user_queryposition]\\\\t[D:kw_querypositionyuqi]\\\",\\\"ori_pattern\\\":\\\"[D:user_queryposition]\\\\t[D:kw_querypositionyuqi]\\\",\\\"ori_slots\\\":{\\\"confidence\\\":100.0,\\\"domain_confidence\\\":0.0,\\\"extra_info\\\":{},\\\"from_who\\\":\\\"smart_qu\\\",\\\"intent\\\":\\\"QUERYPOSITION\\\",\\\"intent_confidence\\\":100.0,\\\"intent_need_clarify\\\":false,\\\"match_info\\\":\\\"[D:user_queryposition] \\\\t[D:kw_querypositionyuqi] kw_querypositionyuqi:在哪里\\\",\\\"slots\\\":[{\\\"begin\\\":0,\\\"confidence\\\":100.0,\\\"father_idx\\\":-1,\\\"length\\\":4,\\\"name\\\":\\\"user_queryposition\\\",\\\"need_clarify\\\":false,\\\"normalized_word\\\":\\\"\\\",\\\"original_word\\\":\\\"电梯\\\",\\\"word_type\\\":\\\"\\\"},{\\\"begin\\\":4,\\\"confidence\\\":100.0,\\\"father_idx\\\":-1,\\\"length\\\":6,\\\"name\\\":\\\"kw_querypositionyuqi\\\",\\\"need_clarify\\\":false,\\\"normalized_word\\\":\\\"\\\",\\\"original_word\\\":\\\"在哪里\\\",\\\"word_type\\\":\\\"\\\"}]},\\\"real_threshold\\\":1.0,\\\"threshold\\\":0.6999999880790710}\",\"slots\":[{\"begin\":0,\"confidence\":100.0,\"father_idx\":-1,\"length\":4,\"name\":\"user_queryposition\",\"need_clarify\":false,\"normalized_word\":\"电梯\",\"original_word\":\"电梯\",\"word_type\":\"\"}]}\n","raw_query":"电梯在哪里","sentiment_analysis":{"label":"1","pval":0.9960},"status":0,"timestamp":0},"schema":{"domain_confidence":0.0,"intent":"QUERYPOSITION","intent_confidence":100.0,"slots":[{"begin":0,"confidence":100.0,"length":4,"merge_method":"updated","name":"user_queryposition","normalized_word":"电梯","original_word":"电梯","session_offset":0,"sub_slots":[],"word_type":""}]},"status":0}],"bernard_status":[{"index":0,"step":"AFTER_DM_TRIGGER"}],"intervention":{"interv_qu_res":"","interv_query":"","qu_res_interved":"","qu_res_original":"","query_original":"","type":"","user_id":""},"user_slots":{"user_queryposition":{"state":2,"static_slot":{"default_state":0,"default_tag_name":"DEFAULT","extensible":true,"name":"user_queryposition","print":["区域位置"],"type":0,"update_type":"single_turn","weight":1.0},"tag_map":{"电梯":{"begin":0,"confidence":100.0,"length":4,"merge_method":"updated","normalized_name":"电梯","original_name":"电梯","state":2,"turn":0,"word_type":""}}}}},"dialog_state":{"contexts":{},"intents":[{"index":0,"name":"QUERYPOSITION"}],"user_slots":{"user_queryposition":{"attrs":{"default_state":0,"default_tag_name":"DEFAULT","extensible":true,"name":"user_queryposition","print":["区域位置"],"type":0,"update_type":"single_turn","weight":1.0},"state":2,"values":{"电梯":{"begin":0,"confidence":100.0,"length":4,"merge_method":"updated","normalized_name":"电梯","original_name":"电梯","session_offest":0,"state":2,"word_type":""}}}}},"interactions":[{"interaction_id":"interaction-1541226572696-746135415-8013-497","request":{"bernard_level":1,"client_session":"{\"client_results\":\"\", \"candidate_options\":[]}","hyper_params":{"slu_tags":[]},"query":"电梯在哪里","query_info":{"asr_candidates":[],"source":"KEYBOARD","type":"TEXT"},"updates":"","user_id":"99635"},"response":{"action_list":[{"action_id":"queryposition_satisfy","confidence":100.0,"custom_reply":"","refine_detail":{"clarify_reason":"","interact":"","option_list":[]},"say":"结果请查看地图","type":"satisfy"}],"msg":"ok","qu_res":{"candidates":[{"confidence":100.0,"domain_confidence":0.0,"extra_info":{"group_id":"5","real_threshold":"1","threshold":"0.7"},"from_who":"pow-slu-lev1","intent":"QUERYPOSITION","intent_confidence":100.0,"intent_need_clarify":false,"match_info":"{\"group_id\":\"5\",\"id\":\"739186\",\"match_keywords\":\" kw_querypositionyuqi:在哪里\",\"match_pattern\":\"[D:user_queryposition]\\t[D:kw_querypositionyuqi]\",\"ori_pattern\":\"[D:user_queryposition]\\t[D:kw_querypositionyuqi]\",\"ori_slots\":{\"confidence\":100.0,\"domain_confidence\":0.0,\"extra_info\":{},\"from_who\":\"smart_qu\",\"intent\":\"QUERYPOSITION\",\"intent_confidence\":100.0,\"intent_need_clarify\":false,\"match_info\":\"[D:user_queryposition] \\t[D:kw_querypositionyuqi] kw_querypositionyuqi:在哪里\",\"slots\":[{\"begin\":0,\"confidence\":100.0,\"father_idx\":-1,\"length\":4,\"name\":\"user_queryposition\",\"need_clarify\":false,\"normalized_word\":\"\",\"original_word\":\"电梯\",\"word_type\":\"\"},{\"begin\":4,\"confidence\":100.0,\"father_idx\":-1,\"length\":6,\"name\":\"kw_querypositionyuqi\",\"need_clarify\":false,\"normalized_word\":\"\",\"original_word\":\"在哪里\",\"word_type\":\"\"}]},\"real_threshold\":1.0,\"threshold\":0.6999999880790710}","slots":[{"begin":0,"confidence":100.0,"father_idx":-1,"length":2,"name":"user_queryposition","need_clarify":false,"normalized_word":"电梯","original_word":"电梯","word_type":""}]}],"lexical_analysis":[{"basic_word":["电梯"],"etypes":["[D:user_queryposition]"],"term":"电梯","type":"user_queryposition","weight":0.6080},{"basic_word":["在"],"etypes":[],"term":"在","type":"28","weight":0.130},{"basic_word":["哪里"],"etypes":["[D:COREF_LOCATION]","sys_coref_location","sys_coref_location"],"term":"哪里","type":"sys_coref_location","weight":0.260}],"qu_res_chosen":"{\"confidence\":100.0,\"domain_confidence\":0.0,\"extra_info\":{\"group_id\":\"5\",\"real_threshold\":\"1\",\"threshold\":\"0.7\"},\"from_who\":\"pow-slu-lev1\",\"intent\":\"QUERYPOSITION\",\"intent_confidence\":100.0,\"intent_need_clarify\":false,\"match_info\":\"{\\\"group_id\\\":\\\"5\\\",\\\"id\\\":\\\"739186\\\",\\\"match_keywords\\\":\\\" kw_querypositionyuqi:在哪里\\\",\\\"match_pattern\\\":\\\"[D:user_queryposition]\\\\t[D:kw_querypositionyuqi]\\\",\\\"ori_pattern\\\":\\\"[D:user_queryposition]\\\\t[D:kw_querypositionyuqi]\\\",\\\"ori_slots\\\":{\\\"confidence\\\":100.0,\\\"domain_confidence\\\":0.0,\\\"extra_info\\\":{},\\\"from_who\\\":\\\"smart_qu\\\",\\\"intent\\\":\\\"QUERYPOSITION\\\",\\\"intent_confidence\\\":100.0,\\\"intent_need_clarify\\\":false,\\\"match_info\\\":\\\"[D:user_queryposition] \\\\t[D:kw_querypositionyuqi] kw_querypositionyuqi:在哪里\\\",\\\"slots\\\":[{\\\"begin\\\":0,\\\"confidence\\\":100.0,\\\"father_idx\\\":-1,\\\"length\\\":4,\\\"name\\\":\\\"user_queryposition\\\",\\\"need_clarify\\\":false,\\\"normalized_word\\\":\\\"\\\",\\\"original_word\\\":\\\"电梯\\\",\\\"word_type\\\":\\\"\\\"},{\\\"begin\\\":4,\\\"confidence\\\":100.0,\\\"father_idx\\\":-1,\\\"length\\\":6,\\\"name\\\":\\\"kw_querypositionyuqi\\\",\\\"need_clarify\\\":false,\\\"normalized_word\\\":\\\"\\\",\\\"original_word\\\":\\\"在哪里\\\",\\\"word_type\\\":\\\"\\\"}]},\\\"real_threshold\\\":1.0,\\\"threshold\\\":0.6999999880790710}\",\"slots\":[{\"begin\":0,\"confidence\":100.0,\"father_idx\":-1,\"length\":4,\"name\":\"user_queryposition\",\"need_clarify\":false,\"normalized_word\":\"电梯\",\"original_word\":\"电梯\",\"word_type\":\"\"}]}\n","raw_query":"电梯在哪里","sentiment_analysis":{"label":"1","pval":0.9960},"status":0,"timestamp":0},"schema":{"domain_confidence":0.0,"intent":"QUERYPOSITION","intent_confidence":100.0,"slots":[{"begin":0,"confidence":100.0,"length":2,"merge_method":"updated","name":"user_queryposition","normalized_word":"电梯","original_word":"电梯","session_offset":0,"sub_slots":[],"word_type":""}]},"status":0},"timestamp":"2018-11-03 14:29:32.696"}],"session_id":"session-1541226572612-746135415-8013-490"}}  

 

把这么多的玩意再次传回去。。 确定吗?

0
#16smarthomeLYZ回复于2018-10-29 17:47:43

拼接参数...:

String params =
"{\"bot_session\":\"{\\\"session_id\\\":\\\" " + session_id + "\\\"}\"," +
"\"log_id\":\"" + logId + "\"" +
",\"request\":" +
"{\"bernard_level\":1" +
",\"client_session\":\"" +
"{\\\"client_results\\\":\\\"\\\", " +
"\\\"candidate_options\\\":[]}\"," +
"\"query\":\"" + content + "\"," +
"\"query_info\":" +
"{\"asr_candidates\":[]," +
"\"source\":\"ASR\"," +
"\"type\":\"TEXT\"}," +
"\"updates\":\"\"," +
"\"user_id\":\"" + user_id + "\"}," +
"\"bot_id\":" + Constants.BAIDU_UNIT_BOT_ID + "," +
"\"version\":\"2.0\"}";

0
#15glary_augus回复于2018-10-24 17:55:34

String params = "{\"bot_session\":"+bot_session+",\"log_id\":\"7758521\",\"request\":{\"bernard_level\":1,\"client_session\":\"{\\\"client_results\\\":\\\"\\\", \\\"candidate_options\\\":[]}\",\"query\":\""+ask+"\",\"query_info\":{\"asr_candidates\":[],\"source\":\"KEYBOARD\",\"type\":\"TEXT\"},\"updates\":\"\",\"user_id\":\"88888\"},\"bot_id\":\"你自己的bot_id\",\"version\":\"2.0\"}";

这么拼接就行了。然后第一次传的bot_session="\"\"";

之后的bot_session就把从result中取出的bot_session直接传进去就行了。

0
#14redlavi回复于2018-10-16 17:22:06
#12 sun_lei0422回复
首先 bot_session 和 client_session 都是 json格式的字符串类型,所以需要对这两个json值的引号进行转移,可以参考 9 楼的同学的写法
展开

“转移”应该是错别字,应该是“转义”。引号写成”\"“

0
#13tiancorn回复于2018-10-12 15:58:59
#9 星空下hh回复
问一下,传入session_id但是两轮对话还是没有关联,是怎么回事啊   { "bot_session": "{\"session_id\":\" session-1536030295392-1220742608-8013-218 \"}", "log_id": "199606", "request": { "bernard_level": 0, "client_session": "{\"client_results\":\"\", \"candidate_options\":[]}", "query": "流水账单下载", "query_info": { "asr_candidates": [ ], "source": "KEYBOARD", "type": "TEXT" }, "updates": "", "user_id": "1234" }, "bot_id": "10729", "version": "2.0" }
展开

这前后空格是你自己加上去的?还是怎么弄进去的?

0
#12sun_lei0422回复于2018-10-12 15:50:36
#10 love卵疼回复
好烦  一直是 请求数据不对 "{ "bot_session":{ "session_id":"session-1539308171714-2261805925-8013-24"},"log_id":"7758521","request":{ "bernard_level":2,"client_session":"{"client_results":"", "candidate_options":[]}","query":"hyc在不在","query_info":{ "asr_candidates":[],"source":"KEYBOARD","type":"TEXT"},"updates":"","user_id":"99635"},"bot_id":12752,"version":"2.0"}"  都不知道这个拼接哪里有错。
展开

首先 bot_session 和 client_session 都是 json格式的字符串类型,所以需要对这两个json值的引号进行转移,可以参考 9 楼的同学的写法

1
#11sun_lei0422回复于2018-10-12 15:46:31
#9 星空下hh回复
问一下,传入session_id但是两轮对话还是没有关联,是怎么回事啊   { "bot_session": "{\"session_id\":\" session-1536030295392-1220742608-8013-218 \"}", "log_id": "199606", "request": { "bernard_level": 0, "client_session": "{\"client_results\":\"\", \"candidate_options\":[]}", "query": "流水账单下载", "query_info": { "asr_candidates": [ ], "source": "KEYBOARD", "type": "TEXT" }, "updates": "", "user_id": "1234" }, "bot_id": "10729", "version": "2.0" }
展开

你的上轮返回的session_id 值的前后就包含空格吗??"bot_session": "{\"session_id\":\" session-1536030295392-1220742608-8013-218 \"}"

0
#10love卵疼回复于2018-10-12 10:01:07

好烦  一直是 请求数据不对

"{ "bot_session":{ "session_id":"session-1539308171714-2261805925-8013-24"},"log_id":"7758521","request":{ "bernard_level":2,"client_session":"{"client_results":"", "candidate_options":[]}","query":"hyc在不在","query_info":{ "asr_candidates":[],"source":"KEYBOARD","type":"TEXT"},"updates":"","user_id":"99635"},"bot_id":12752,"version":"2.0"}" 

都不知道这个拼接哪里有错。

0
#9星空下hh回复于2018-09-04 11:24:45

问一下,传入session_id但是两轮对话还是没有关联,是怎么回事啊

 

{
"bot_session": "{\"session_id\":\" session-1536030295392-1220742608-8013-218 \"}",
"log_id": "199606",
"request": {
"bernard_level": 0,
"client_session": "{\"client_results\":\"\", \"candidate_options\":[]}",
"query": "流水账单下载",
"query_info": {
"asr_candidates": [ ],
"source": "KEYBOARD",
"type": "TEXT"
},
"updates": "",
"user_id": "1234"
},
"bot_id": "10729",
"version": "2.0"
}

0
#8tiancorn回复于2018-08-14 18:56:10
#7 小瘦子吃的少回复
是不是必须要设置对话意图才可以进行多轮对话呢?好像单独的问答意图是不行的。
展开

是的,问答只是单轮的。对话意图是多轮的。

0
#7小瘦子吃的少回复于2018-08-14 18:51:43

是不是必须要设置对话意图才可以进行多轮对话呢?好像单独的问答意图是不行的。

0
#6何必固執回复于2018-07-31 09:42:37

希望可以上点代码实例供参考,感谢贴主分享

0
#5lixk_way回复于2018-07-26 20:07:22

干货~~

0
#4choleraa回复于2018-07-24 19:33:03

干货, 再多点完整样例代码就更好了.

0
#3rose20135188回复于2018-07-24 17:50:55

写得易懂,

0
#2伊茨米可回复于2018-07-24 15:04:08

干货~~

0
TOP
切换版块