openai元件一直改版,可直接用post打api url使用
import requests
import urllib3
API_KEY = "*********"
version = "2024-10-21"
model = "gpt-4o"
url = "https://****.openai.azure.com/"
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
headers = {"Content-Type": "application/json","api-key": API_KEY}
ENDPOINT = f"{url}openai/deployments/{model}/chat/completions?api-version={version}"
payload = {"messages": [{"role": "user", "content": "我國台灣總統是誰"}]}
try:
response = requests.post(ENDPOINT, headers=headers, json=payload, verify=False)
response.raise_for_status()
print(response.json()['choices'][0]['message']['content'])
except Exception as e:
print(f"錯誤: {e}")
若有錯誤:[InsecureRequestWarning: Unverified HTTPS request is being made to host]
可加urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
若有錯誤: HTTPSConnectionPool(host='.openai.azure.com', port=443): Max retries exceeded with url:/…/…/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain
可在requests.post
加verify=False
Taiwan is a country. 臺灣是我的國家