Ready-to-Run Code Examples
Complete Chat Completions programs in Python and Node.js, covering environment setup, model selection, error handling, and execution commands.
The two programs below do the same thing: read environment variables, make a single non-streaming text request, and output the response or an identifiable error. The examples disable automatic retries so you can confirm the result of a single request.
1. Prepare Environment Variables
Section titled “1. Prepare Environment Variables”Set them in the same terminal where you run the program; ONEZHE_MODEL can be replaced with an ID from models with available keys .
export ONEZHE_API_KEY="sk-你的Key"export ONEZHE_MODEL="gpt-5.6-sol"$env:ONEZHE_API_KEY = "sk-你的Key"$env:ONEZHE_MODEL = "gpt-5.6-sol"2. Choose a Language and Run
Section titled “2. Choose a Language and Run”Python
Section titled “Python”Install python -m pip install openai, save as chat.py, and execute python chat.py.
import osimport sysimport openai
api_key = os.environ.get("ONEZHE_API_KEY")if not api_key: sys.exit("请先设置 ONEZHE_API_KEY 环境变量。")
client = openai.OpenAI( api_key=api_key, base_url="https://1zheai.com/v1", timeout=120.0, max_retries=0,)try: response = client.chat.completions.create( model=os.environ.get("ONEZHE_MODEL", "gpt-5.6-sol"), messages=[{"role": "user", "content": "用一句中文解释 DNS。"}], ) if not response.choices: sys.exit("响应没有 choices,请检查响应协议。") choice = response.choices[0] print(choice.message.content or "本次响应没有普通文本。") print("finish_reason:", choice.finish_reason) print("response_id:", response.id)except openai.APIStatusError as exc: print("HTTP:", exc.status_code, "request_id:", exc.request_id, file=sys.stderr) print("请查看控制台日志和错误码文档;不要公开密钥。", file=sys.stderr) sys.exit(1)except openai.APIConnectionError: sys.exit("连接失败或超时:检查网络与代理,再核对调用日志。")Node.js
Section titled “Node.js”Install npm install openai, save as chat.mjs, and execute node chat.mjs. Using the .mjs extension directly enables ES Modules.
import OpenAI from "openai";
if (!process.env.ONEZHE_API_KEY) { console.error("请先设置 ONEZHE_API_KEY 环境变量。"); process.exit(1);}const client = new OpenAI({ apiKey: process.env.ONEZHE_API_KEY, baseURL: "https://1zheai.com/v1", timeout: 120_000, maxRetries: 0,});try { const response = await client.chat.completions.create({ model: process.env.ONEZHE_MODEL || "gpt-5.6-sol", messages: [{ role: "user", content: "用一句中文解释 DNS。" }], }); const choice = response.choices[0]; if (!choice) throw new Error("响应没有 choices,请检查响应协议。"); console.log(choice.message.content || "本次响应没有普通文本。"); console.log("finish_reason:", choice.finish_reason); console.log("response_id:", response.id);} catch (error) { if (error instanceof OpenAI.APIError) { console.error("HTTP:", error.status, "request_id:", error.request_id); console.error("请检查网络、控制台日志和错误码文档。"); } else { console.error(error instanceof Error ? error.message : "未知错误"); } process.exitCode = 1;}3. Verify the Results
Section titled “3. Verify the Results”You should see a response and a finish reason. When finish_reason is not stop, check whether it is a tool call or a truncation. The SDK’s request ID may be empty; the server may also place the trace ID in the error body, so supplement it from the redacted raw response when debugging.
Go to call logs to verify calls. The program’s exit status and whether there is plain text do not cover all business success conditions; check structure and content according to your task.
Continue Extending
Section titled “Continue Extending”- Chat Completions : multi-turn history and field descriptions.
- Messages : Claude-native content blocks.
- Responses : GPT Responses and streaming events.
- Image Generation : generate and save image files.
- Timeouts and Retries : configure failure handling before going live.
