smartSpeaker/baidu.py

66 lines
1.9 KiB
Python
Raw Normal View History

2023-12-22 09:04:16 +00:00
import requests
import json
def get_access_token():
"""
使用 API KeySecret Key 获取access_token替换下列示例中的应用API Key应用Secret Key
"""
url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=tqsbQSj3DUGlebWsPwrQOCqg&client_secret=A7TV1Xs788sAaKt03xIGPE3AFGmMsxjh"
payload = json.dumps("")
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.json().get("access_token"))
return response.json().get("access_token")
def baidutext():
token = '24.18fd91a916f1c97f2bc70e9d1e7ea932.2592000.1705819202.282335-45506921'
url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=" + token
payload = json.dumps({
"messages": [
{
"role": "user",
"content": "介绍一下你自己"
}
]
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
return response.json().get("result")
def bdgettext(text):
token = '24.18fd91a916f1c97f2bc70e9d1e7ea932.2592000.1705819202.282335-45506921'
url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=" + token
payload = json.dumps({
"messages": [
{
"role": "user",
"content": text
}
]
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
return response.json().get("result")
if __name__ == '__main__':
print(baidutext())