呼叫文心一言API詢問httpx的使用方法

萝卜薰發表於2024-06-07

import requests
import json

def get_access_token():
url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=輸入自己的id&client_secret=輸入自己id的密碼"
payload = json.dumps("")
headers = {"Content-Type": "application/json","Accept": "application/json"}
response = requests.request("POST",url,headers=headers,data=payload)
return response.json().get("access_token") #獲取acces_token

def main():
url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=" + get_access_token()
payload = json.dumps({"messages": [{"role": "user","content": "如何使用httpx?" }]}) #提問
headers = {"Content-Type": "application/json"}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)

if name == "main":
main()

相關文章