1.?安裝openai
pip3 install openai##########################
Collecting openaiUsing cached openai-1.61.1-py3-none-any.whl.metadata (27 kB)
Collecting anyio<5,>=3.5.0 (from openai)Using cached anyio-4.8.0-py3-none-any.whl.metadata (4.6 kB)
Collecting distro<2,>=1.7.0 (from openai)Using cached distro-1.9.0-py3-none-any.whl.metadata (6.8 kB)
Collecting httpx<1,>=0.23.0 (from openai)Using cached httpx-0.28.1-py3-none-any.whl.metadata (7.1 kB)
Collecting jiter<1,>=0.4.0 (from openai)Using cached jiter-0.8.2-cp313-cp313-win_amd64.whl.metadata (5.3 kB)
Collecting pydantic<3,>=1.9.0 (from openai)Using cached pydantic-2.10.6-py3-none-any.whl.metadata (30 kB)
Collecting sniffio (from openai)Using cached sniffio-1.3.1-py3-none-any.whl.metadata (3.9 kB)
Collecting tqdm>4 (from openai)Using cached tqdm-4.67.1-py3-none-any.whl.metadata (57 kB)
Collecting typing-extensions<5,>=4.11 (from openai)Using cached typing_extensions-4.12.2-py3-none-any.whl.metadata (3.0 kB)
Collecting idna>=2.8 (from anyio<5,>=3.5.0->openai)Using cached idna-3.10-py3-none-any.whl.metadata (10 kB)
Collecting certifi (from httpx<1,>=0.23.0->openai)Using cached certifi-2025.1.31-py3-none-any.whl.metadata (2.5 kB)
Collecting httpcore==1.* (from httpx<1,>=0.23.0->openai)Using cached httpcore-1.0.7-py3-none-any.whl.metadata (21 kB)
Collecting h11<0.15,>=0.13 (from httpcore==1.*->httpx<1,>=0.23.0->openai)Using cached h11-0.14.0-py3-none-any.whl.metadata (8.2 kB)
Collecting annotated-types>=0.6.0 (from pydantic<3,>=1.9.0->openai)Using cached annotated_types-0.7.0-py3-none-any.whl.metadata (15 kB)
Collecting pydantic-core==2.27.2 (from pydantic<3,>=1.9.0->openai)Using cached pydantic_core-2.27.2-cp313-cp313-win_amd64.whl.metadata (6.7 kB)
Collecting colorama (from tqdm>4->openai)Using cached colorama-0.4.6-py2.py3-none-any.whl.metadata (17 kB)
Using cached openai-1.61.1-py3-none-any.whl (463 kB)
Using cached anyio-4.8.0-py3-none-any.whl (96 kB)
Using cached distro-1.9.0-py3-none-any.whl (20 kB)
Using cached httpx-0.28.1-py3-none-any.whl (73 kB)
Using cached httpcore-1.0.7-py3-none-any.whl (78 kB)
Using cached jiter-0.8.2-cp313-cp313-win_amd64.whl (203 kB)
Using cached pydantic-2.10.6-py3-none-any.whl (431 kB)
Downloading pydantic_core-2.27.2-cp313-cp313-win_amd64.whl (2.0 MB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.0/2.0 MB 11.9 kB/s eta 0:00:00
Downloading sniffio-1.3.1-py3-none-any.whl (10 kB)
Downloading tqdm-4.67.1-py3-none-any.whl (78 kB)
Downloading typing_extensions-4.12.2-py3-none-any.whl (37 kB)
Downloading annotated_types-0.7.0-py3-none-any.whl (13 kB)
Downloading idna-3.10-py3-none-any.whl (70 kB)
Downloading certifi-2025.1.31-py3-none-any.whl (166 kB)
Downloading colorama-0.4.6-py2.py3-none-any.whl (25 kB)
Downloading h11-0.14.0-py3-none-any.whl (58 kB)
Installing collected packages: typing-extensions, sniffio, jiter, idna, h11, distro, colorama, certifi, annotated-types, tqdm, pydantic-core, httpcore, anyio, pydantic, httpx, openai
Successfully installed annotated-types-0.7.0 anyio-4.8.0 certifi-2025.1.31 colorama-0.4.6 distro-1.9.0 h11-0.14.0 httpcore-1.0.7 httpx-0.28.1 idna-3.10 jiter-0.8.2 openai-1.61.1 pydantic-2.10.6 pydantic-core-2.27.2 sniffio-1.3.1 tqdm-4.67.1 typing-extensions-4.12.2
2.?First API Call
from openai import OpenAIclient = OpenAI(api_key="<DeepSeek API Key>", base_url="https://api.deepseek.com")response = client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "Hello"},
],
stream=False
)print(response.choices[0].message.content)
3. Create API Key
- 訪問:DeepSeek API keys
- 創建 API Key
4.?執行第一次API調用
#####################################raise self._make_status_error_from_response(err.response) from None
openai.APIStatusError: Error code: 402 - {'error': {'message': 'Insufficient Balance', 'type': 'unknown_error', 'param': None, 'code': 'invalid_request_error'}}402 - Insufficient Balance Cause: You have run out of balance.
Solution: Please check your account's balance, and go to the Top up page to add funds.## 賬戶余額不足,請續費
5. 問問 DeepSeek什么問題
中文回答:penai.APIStatusError: Error code: 402 - {'error': {'message': 'Insufficient Balance'
openai.APIStatusError: Error code: 402 - {'error': {'message': 'Insufficient Balance'}} 這個錯誤表示你的 OpenAI 賬戶余額不足,無法完成當前的 API 請求。以下是解決此問題的步驟:1. 檢查賬戶余額
登錄 OpenAI 平臺(https://platform.openai.com)。
進入 Billing(賬單)頁面,查看當前賬戶余額。
如果余額為 0 或不足以支付當前請求,需要充值。2. 設置支付方式
在 Billing 頁面,確保你已經綁定了有效的支付方式(如信用卡)。
如果沒有綁定支付方式,或者支付方式失效,OpenAI 會拒絕處理請求。3. 檢查 API 使用情況
在 Usage(使用情況)頁面,查看你的 API 使用量和費用。
如果你使用的是按量付費(Pay-As-You-Go)模式,確保賬戶中有足夠的余額來支付當前請求。4. 充值或升級計劃
如果余額不足,可以通過 OpenAI 的支付頁面進行充值。
如果你需要更高的使用額度,可以考慮升級到更高級的訂閱計劃。5. 聯系 OpenAI 支持
如果你確認賬戶余額充足,但仍然遇到此錯誤,可能是系統問題。
聯系 OpenAI 支持團隊(https://help.openai.com),提供以下信息:
錯誤信息(包括錯誤代碼 402)。
你的賬戶郵箱或 API Key(確保不泄露完整 Key)。
相關請求的詳細信息。6. 代碼中處理錯誤
在你的代碼中,可以捕獲此錯誤并提示用戶檢查賬戶余額。例如:import openaitry:response = openai.Completion.create(engine="davinci",prompt="Translate the following English text to French: '{}'",max_tokens=60)print(response.choices[0].text.strip())
except openai.error.APIStatusError as e:if e.code == 402:print("錯誤:賬戶余額不足,請充值后再試。")else:print(f"API 請求失敗: {e}")
except Exception as e:print(f"發生未知錯誤: {e}")總結
錯誤代碼 402 通常是由于賬戶余額不足或支付方式問題導致的。通過檢查余額、綁定有效支付方式或充值,可以解決此問題。如果問題仍然存在,建議聯系 OpenAI 支持團隊獲取進一步幫助。
要錢。。。。。。那就等等,先用起來再增強。
參考:
- Your First API Call | DeepSeek API Docs
- Error Codes | DeepSeek API Docs