UNIT2.0 调用示例(Python3)
林夕画晴 发布于2018-07-11 18:07 浏览:154 回复:1
1
收藏

官网给出的调用示例是Python2的代码,Py3直接使用会报错,经过自己的摸索,使用Py3改写成功了,分享给大家(*^▽^*)

官网Python2源代码链接:https://ai.baidu.com/docs#/UNIT-v2-API/top

# -*- coding: utf-8 -*-
import urllib.request as ul_re
import json
url = 'https://aip.baidubce.com/rpc/2.0/unit/bot/chat?access_token=' + 【你的access_token】
post_data  = {
	"bot_session": "",
	"log_id": "7758521",
	"request": {
		"bernard_level": 0,
		"client_session": "{\"client_results\":\"\", \"candidate_options\":[]}",
		"query": "【你的输入语句】",
		"query_info": {
			"asr_candidates": [],
			"source": "KEYBOARD",
			"type": "TEXT"
		},
		"updates": "",
		"user_id": "88888"
	},
	"bot_id": 【你的BOTID】,
	"version": "2.0"
}
encoded_data = json.dumps(post_data).encode('utf-8')
headers = {'Content-Type': 'application/json'}

request = ul_re.Request(url, data=encoded_data, headers=headers)
response = ul_re.urlopen(request)
content = response.read()
result = str(content,'utf-8')
print(result)
收藏
点赞
1
个赞
共1条回复 最后由伊茨米可回复于2018-07-12 10:15
#2伊茨米可回复于2018-07-12

感谢感谢~

官网的文档也会尽快更新

0
TOP
切换版块