import requests import json def get_access_token(): """ 使用 API Key,Secret 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())