如何使用 Dev Proxy 透過 GitHub Actions 追蹤語言模型使用方式和成本

一目了然
目標: 在 GitHub Actions 中追蹤 LLM(大型語言模型)成本
時間: 20分鐘
插件:OpenAITelemetryPlugin
前置條件:設定 Dev Proxy,GitHub 倉庫

若要將 Dev Proxy 整合到 GitHub Actions 流程中,請使用 Dev Proxy Actions。

設定開發代理來監測語言模型的使用情況

若要追蹤語言模型使用方式,請使用 OpenAITelemetryPlugin 在您的項目中設定 Dev Proxy 組態檔。 若要產生總成本和使用量詳細數據的報告,請包含 MarkdownReporter 外掛程式。 在配置檔的urlsToWatch區段中,包含您想要追蹤的 OpenAI 相容 API 的 URL。

檔案: devproxyrc.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/rc.schema.json",
  "plugins": [
    {
      "name": "OpenAITelemetryPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll"
    },
    {
      "name": "MarkdownReporter",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll"
    }
  ],
  "urlsToWatch": [
    "https://*.openai.azure.com/*"
  ]
}

追蹤 OpenAI 相容 API 的請求

若要追蹤來自任何 OpenAI 相容 API 的要求,請將要求中使用的 URL 新增至 urlsToWatch 開發 Proxy 組態檔的 區段。

檔案: devproxyrc.json(urlsToWatch 區段)

{
  "urlsToWatch": [
    "https://*.openai.azure.com/*"
  ]
}

下表列出您可以使用 Dev Proxy 追蹤的一些熱門 OpenAI 相容 API:

供應商 需監看的 URL 說明
人類學的 https://api.anthropic.com/* Claude 語言模型
GitHub https://models.github.com/* GitHub 模型 API
微軟 https://*.openai.azure.com/* Microsoft Azure OpenAI 服務 API
OpenAI https://api.openai.com/* GPT 語言模型
x.ai https://api.x.ai/* Grok 語言模型

設定 OpenAITelemetryPlugin 以追蹤語言模型使用成本

若要追蹤語言模型使用成本,請新增OpenAITelemetryPlugin的設定區段。 將 includeCosts 屬性設定為 true 以啟用成本追蹤。 請在 pricesFile 屬性中指定包含模型價格的 JSON 檔案路徑。

檔案: devproxyrc.json(附成本追蹤)

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/rc.schema.json",
  "plugins": [
    {
      "name": "OpenAITelemetryPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
      "configSection": "openAITelemetryPlugin"
    },
    {
      "name": "MarkdownReporter",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll"
    }
  ],
  "openAITelemetryPlugin": {
    "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/openaitelemetryplugin.schema.json",
    "includeCosts": true,
    "pricesFile": "prices.json"
  },
  "urlsToWatch": [
    "https://*.openai.azure.com/*"
  ]
}

針對您使用的模型,使用輸入和輸出成本建立價格檔案(每百萬個令牌的價格)。 價格檔案中的模型名稱必須符合 API 回應中傳回的模型名稱,才能正確計算成本。

檔案: prices.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/openaitelemetryplugin.pricesfile.schema.json",
  "prices": {
    "gpt-4": {
      "input": 0.03,
      "output": 0.06
    }
  }
}

變更使用報告成本所使用的貨幣

若要變更使用量報表成本中使用的貨幣,請在OpenAITelemetryPlugin組態中設定 currency 屬性。 預設值是 USD。

檔案: devproxyrc.json(openAITelemetryPlugin 區段)

{
  "openAITelemetryPlugin": {
    "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/openaitelemetryplugin.schema.json",
    "includeCosts": true,
    "pricesFile": "prices.json",
    "currency": "EUR"
  }
}

變更使用量報告標題

依預設,此格式用於建立報表標題LLM usage report for <application> in <environment>。 若要變更名稱和環境值,請在 OpenAITelemetryPlugin 組態中設定 application 和 environment 屬性。 預設值分別為 default 與 development。

檔案: devproxyrc.json(openAITelemetryPlugin 區段)

{
  "openAITelemetryPlugin": {
    "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/openaitelemetryplugin.schema.json",
    "application": "My application",
    "environment": "Staging"
  }
}

在 GitHub Actions 工作流程中設定 Dev Proxy

若要安裝和啟動 Dev Proxy,請使用 setup 動作。 若要進入錄製狀態以擷取 OpenAITelemetryPlugin 處理的要求,請將輸入設定 auto-record 為 true。 若要在工作流程作業摘要中包含使用量報告,請將變數傳遞 $GITHUB_STEP_SUMMARY 至 report-job-summary 輸入。

檔案: .github/workflows/your-workflow.yml(步驟)

- name: Setup Dev Proxy
  uses: dev-proxy-tools/actions/setup@v1
  with:
    auto-record: true
    report-job-summary: $GITHUB_STEP_SUMMARY
    version: v1.0.0-beta.6

觸發要記錄的要求

若要與您的應用程式互動,並觸發 Dev Proxy 可以記錄的要求,請使用 Playwright 之類的端對端測試架構。 設定動作會自動設定 http_proxy 和 https_proxy 環境變數,以透過Dev Proxy路由要求。

檔案: .github/workflows/your-workflow.yml(步驟)

- name: Setup Dev Proxy
  uses: dev-proxy-tools/actions/setup@v1
  with:
    auto-record: true
    report-job-summary: $GITHUB_STEP_SUMMARY

- name: Run Playwright tests
  run: npx playwright test

在 Chromium 瀏覽器中安裝開發 Proxy 憑證

如果您在 Linux 執行器上使用基於 Chromium 的瀏覽器來生成供 Dev Proxy 記錄的請求,您需要安裝 Dev Proxy 憑證以避免 SSL 錯誤。 若要安裝憑證,請使用 chromium-cert 動作。

檔案: .github/workflows/your-workflow.yml(步驟)

- name: Setup Dev Proxy
  uses: dev-proxy-tools/actions/setup@v1
  with:
    auto-record: true
    report-job-summary: $GITHUB_STEP_SUMMARY

- name: Install Dev Proxy certificate for Chromium browsers
  uses: dev-proxy-tools/actions/chromium-cert@v1

- name: Run Playwright tests
  run: npx playwright test

將使用量報告上傳至工件

若要產生使用報告,請使用 stop 動作來手動停止工作流程中的開發 Proxy。 若要將使用量報告上傳為成品,請使用 actions/upload-artifact 動作。

檔案: .github/workflows/your-workflow.yml(步驟)

- name: Stop recording
  uses: dev-proxy-tools/actions/stop@v1

- name: Upload Dev Proxy reports
  uses: actions/upload-artifact@v4
  with:
    name: Reports
    path: ./*Reporter*

另請參閱