补充:client_session参数可以不写,省事。
使用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\"}"
另外:DMKIT如果进行多轮对话,需要传入整个bot_session的内容,不能只传session_id。
String params =
"{\"bot_session\":\"{\\\"session_id\\\":\\\" " + session_id + "\\\"}\"," +
"\"log_id\":\"" + 1106648 + "\"" +
",\"request\":" +
"{\"bernard_level\":1" +
",\"client_session\":\"" +
"{\\\"client_results\\\":\\\"\\\", " +
"\\\"candidate_options\\\":[]}\"," +
"\"query\":\"" + content + "\"," +
"\"query_info\":" +
"{\"asr_candidates\":[]," +
"\"source\":\"ASR\"," +
"\"type\":\"TEXT\"}," +
"\"updates\":\"\"," +
"\"user_id\":\"" + 88888 + "\"}," +
"\"bot_id\":\"S54982\"}," +
"\"version\":\"2.0\"}";
报错 :ult":{"ref_id":"q9cn6_20211214105107_4257016938"},"error_code":282008,"error_msg":"The request content type is illegal."}
传入上轮的bot_session后出现类型非法。。。。
不知道该怎么该了
{'error_code': 282008, 'error_msg': 'The request content type is illegal.'}
实现多轮对话更适合与人的交流
干活
谢谢无私的分享。
能把多轮调用这部分代码分享么,谢谢啦
简单易懂,学会了,谢谢
已经解决
请求百度ai把上次对话的bot_session也传进去可以达到多轮对话的实现,这个是没问题的。但我遇到一个问题:我用电脑A访问UI界面和机器人对话是没问题,但当我又用另一台电脑B访问UI界面和机器人对话,此时B的对话会出现A的对话回答缓存,这是为什么?我的后台程序做了session用户的区分,当是新用户的时候把bot_session给置空,相当于访问后台程序bot_session第一次传空参,以后bot_session就传上次对话的bot_session,这样做还是不行,他们的对话回答还是会串在一起,大神能指点指点么?告诉我怎么解决!!
按道理不会出现这个问题的呀,除非sessionID一样了
我传sessionid保存上下文的对话,为什么会出现A用户和B用户同时访问出现回复混乱的样子??求解答
这是我 一次对话百度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"}}
把这么多的玩意再次传回去。。 确定吗?
拼接参数...:
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\"}";
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直接传进去就行了。
“转移”应该是错别字,应该是“转义”。引号写成”\"“
这前后空格是你自己加上去的?还是怎么弄进去的?
首先 bot_session 和 client_session 都是 json格式的字符串类型,所以需要对这两个json值的引号进行转移,可以参考 9 楼的同学的写法
你的上轮返回的session_id 值的前后就包含空格吗??"bot_session": "{\"session_id\":\" session-1536030295392-1220742608-8013-218 \"}"
好烦 一直是 请求数据不对
"{ "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"}"
都不知道这个拼接哪里有错。
问一下,传入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"
}