Chuyển đến nội dung chính

Documentation Index

Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt

Use this file to discover all available pages before exploring further.

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 request qua danh mục model của CometAPI.

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

  • Python 3.6+
  • 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

Thiết lập 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 một 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 truyền 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 CometAPI sử dụng tiền tố cometapi/<model-name>, ví dụ: cometapi/your-model-id. Xem trang Models của CometAPI để biết các model hiện có.
  • Tinh chỉnh phản hồi: LiteLLM hỗ trợ temperature, max_tokenstop_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 sử dụng biến môi trường hoặc trình quản lý secrets.
  • Giới hạn tốc độ: 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