Đi tới nội dung
Tất cả tài liệu

Mã

Python

Gói openai chính thức dùng được ngay. Chỉ cần đổi base URL và khóa; mọi thứ còn lại là SDK của OpenAI.

Đã kiểm tra 26 tháng 10, 2026

Cài đặt và kết nối

Shell
pip install openai
export NOKEEP_API_KEY="sk-nokeep-test"  # your own key once it connects
client.py
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://nokeep.ai/v1",
    api_key=os.environ["NOKEEP_API_KEY"],
)

Gửi một tin nhắn

Python
reply = client.chat.completions.create(
    model="deepseek-v4.1-flash-uncensored",
    messages=[
        {"role": "system", "content": "You are a blunt editor."},
        {"role": "user", "content": "Cut this opening line to ten words: ..."},
    ],
)
print(reply.choices[0].message.content)
print("credits:", reply.usage.model_extra.get("nokeep_credits"))

Nhận luồng

Python
stream = client.chat.completions.create(
    model="deepseek-v4.1-flash-uncensored",
    messages=[{"role": "user", "content": "Write a limerick about a cat."}],
    stream=True,
)
for chunk in stream:
    if chunk.choices:
        print(chunk.choices[0].delta.content or "", end="", flush=True)

Bất đồng bộ

Python
import asyncio
import os
from openai import AsyncOpenAI

client = AsyncOpenAI(base_url="https://nokeep.ai/v1", api_key=os.environ["NOKEEP_API_KEY"])

async def main():
    reply = await client.chat.completions.create(
        model="deepseek-v4.1-flash-uncensored",
        messages=[{"role": "user", "content": "Hello"}],
    )
    print(reply.choices[0].message.content)

asyncio.run(main())

Lỗi

SDK ném ra một ngoại lệ cho mỗi loại lỗi và tự động thử lại các mã 429 và 5xx, chờ theo thời gian Retry-After. Trường code cho biết điều gì đã xảy ra; Lỗi liệt kê tất cả.

Python
import openai

try:
    reply = client.chat.completions.create(model="deepseek-v4.1-flash-uncensored", messages=messages)
except openai.APIStatusError as error:
    print(error.status_code, error.body["error"]["code"])

Các trường mà SDK không có tham số riêng

Gửi min_p, top_k và repetition_penalty trong extra_body={...}. Xem Bộ lấy mẫu cho truyện hư cấu.