在 LibreChat 中使用 AI 聚合

LibreChat 是高度可設定的開源 AI 用戶端,可用 librechat.yaml 描述服務提供者、模型與路由規則。它支援自訂 OpenAI 相容端點,因此可以接入 AI 聚合。本文介紹設定流程。

申請流程

要在 LibreChat 中接入 AI 聚合,首先到 AI 聚合控制台,取得您的 API Token,留作備用。

取得 AI 聚合 API Key

如果您尚未登入或註冊,會自動跳轉到登入頁面邀請您註冊和登入,登入註冊之後會自動返回目前頁面。

在首次申請時會有免費額度贈送,可以免費體驗 AI 聚合的模型服務。

部署 LibreChat

LibreChat 透過 Docker Compose 部署,先準備目錄並下載官方設定:

mkdir -p ~/librechat && cd ~/librechat
curl -O https://raw.githubusercontent.com/danny-avila/LibreChat/main/docker-compose.yml
curl -O https://raw.githubusercontent.com/danny-avila/LibreChat/main/.env.example
mv .env.example .env

在 .env 中用 openssl rand -hex 32 產生 JWT_SECRET、JWT_REFRESH_SECRET、CREDS_KEY、CREDS_IV 等安全金鑰,並依需求設定 ALLOW_REGISTRATION。

設定 AI 聚合

建立 librechat.yaml,把 AI 聚合作為自訂 OpenAI 相容端點(完整欄位說明見官方 Custom Endpoints 文件,把 {token} 替換為您的 Token):

version: 1.0.5
cache: true

endpoints:
  custom:
    - name: "Ace Data Cloud"
      apiKey: "{token}"
      baseURL: "https://api.yxai-tech.top/v1"
      models:
        default:
          - "MODEL_ID"
        fetch: true
      titleConvo: true
      titleModel: "MODEL_ID"
      modelDisplayLabel: "Ace Data Cloud"

在 docker-compose.yml 的 api 服務下把該檔案掛載到容器,然後用 docker compose up -d 啟動:

    volumes:
      - type: bind
        source: ./librechat.yaml
        target: /app/librechat.yaml

注意 baseURL 的路徑規則:

baseURL 實際請求 結果
https://api.yxai-tech.top/v1 https://api.yxai-tech.top/v1/chat/completions 正確
https://api.yxai-tech.top/openai https://api.yxai-tech.top/openai/chat/completions 也可用
https://api.yxai-tech.top/openai/v1 https://api.yxai-tech.top/openai/v1/chat/completions 404(/openai 下沒有 /v1)
https://api.yxai-tech.top https://api.yxai-tech.top/chat/completions 404(缺少 /v1)

選擇模型

模型目錄會持續更新。優先使用用戶端自動載入的模型清單;需要手動填寫時,先請求 GET https://api.yxai-tech.top/v1/models 取得目前的模型 ID,再依用戶端支援的上下文、圖片和工具呼叫能力選擇。

驗證串接

如果不確定問題出在 LibreChat 還是網路,可以先用 curl 直接驗證端點(把 {token} 替換為您的 Token):

curl -X POST 'https://api.yxai-tech.top/v1/chat/completions' \
  -H 'Authorization: Bearer {token}' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "MODEL_ID",
    "messages": [{"role": "user", "content": "ping"}]
  }'

回傳 OpenAI 相容的 chat.completion 物件即表示 Token 與端點都已就緒;若回傳 HTTP 403 used_up 則表示 Token 有效但餘額不足,點擊「儲值」按鈕即可。

進階:yaml 設定技巧

librechat.yaml 支援多個 endpoint 分組,可以用 apiKey: "user_provided" 讓每個使用者填寫自己的 Key;titleModel 指定產生對話標題的模型;dropParams 可以統一刪除目標 API 不支援的參數。詳見 librechat.yaml 設定詳解。

常見問題

啟動後看不到 AI 聚合端點

通常是 yaml 沒有掛載到容器或縮排錯誤。檢查 docker-compose.yml 的 volumes,並驗證 yaml 縮排。

提示 404

baseURL 寫成了 .../openai/v1 或漏了 /v1。改成 https://api.yxai-tech.top/v1。

模型下拉選單為空

fetch: true 但 /v1/models 失敗時,改為 fetch: false 並手動列出 default。

修改 yaml 後未生效

需重新啟動容器:docker compose restart api。

了解更多