Chuyển đến nội dung chính
LiteLLM cung cấp một API Python thống nhất cho hơn 100 nhà cung cấp LLM. CometAPI được hỗ trợ nguyên bản — sử dụng tiền tố cometapi/ để định tuyến các request thông qua danh mục model của CometAPI.

Điều kiện tiên quyết

  • Python 3.6+
  • Một tài khoản CometAPI với API key đang hoạt động — lấy tại đây
1

Cài đặt LiteLLM

pip install litellm
2

Đặt API key của bạn

Đặt API key dưới dạng biến môi trường (khuyến nghị) hoặc truyền trực tiếp inline:
import os
from litellm import completion

# Recommended: environment variable
os.environ["COMETAPI_KEY"] = "<COMETAPI_KEY>"

# Alternative: pass inline
api_key = "<COMETAPI_KEY>"
Sử dụng biến môi trường để tránh hardcode thông tin xác thực nhạy cảm trong script của bạn.
3

Thực hiện lệnh gọi completion

Sử dụng định dạng cometapi/<model-name> để chỉ định model. Bạn có thể truyền key qua biến môi trường hoặc chỉ định tường minh:
messages = [{"content": "Hello, how are you?", "role": "user"}]

# Method 1: environment variable (recommended)
response = completion(model="cometapi/your-model-id", messages=messages)

# Method 2: explicit API key
response = completion(model="cometapi/your-model-id", messages=messages, api_key=api_key)

print(response.choices[0].message.content)
4

Lệnh gọi async và Streaming

Sử dụng acompletion với stream=True để nhận phản hồi theo thời gian thực, không chặn:
from litellm import acompletion
import asyncio, traceback

async def stream_call():
    try:
        response = await acompletion(
      model="cometapi/your-model-id",
            messages=[{"content": "Hello, how are you?", "role": "user"}],
            stream=True,
        )
        async for chunk in response:
            print(chunk)
    except Exception:
        print(f"Error: {traceback.format_exc()}")

asyncio.run(stream_call())
  • Định dạng model: Các model của CometAPI sử dụng tiền tố cometapi/<model-name>, ví dụ cometapi/your-model-id. Xem trang CometAPI Models để biết các model khả dụng.
  • Tinh chỉnh phản hồi: LiteLLM hỗ trợ temperature, max_tokens, và top_p — thêm chúng vào bất kỳ lệnh gọi completion() nào, ví dụ completion(..., temperature=0.7).
  • Xử lý lỗi: Bọc các lệnh gọi trong try/except để bắt lỗi key không hợp lệ hoặc sự cố mạng.
  • Bảo mật: Không bao giờ commit API key vào hệ thống quản lý phiên bản. Hãy dùng biến môi trường hoặc trình quản lý secrets.
  • Giới hạn tần suất: Theo dõi mức sử dụng trong CometAPI console.
  • Tài liệu thêm: tài liệu LiteLLMbắt đầu nhanh với CometAPI