1. 安装openai
- pip3 install openai
- ##########################
- Collecting openai
- Using 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 OpenAI
- client = 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 openai
- try:
- 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
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |