如何使用开发人员代理通过 GitHub Actions 跟踪语言模型使用情况和成本

概览
目标: 在 GitHub Actions 中跟踪 LLM 成本
时间: 20 分钟
Plugins:OpenAITelemetryPlugin
先决条件:设置开发代理、GitHub 存储库

若要将 Dev Proxy 集成到 GitHub Actions 工作流中,请使用 Dev Proxy Actions

注释

此功能目前以预览版提供,仅在最新 Beta 版开发代理中可用。

配置开发代理以跟踪语言模型使用情况

若要跟踪语言模型使用情况,请使用 OpenAITelemetryPlugin 在项目中配置开发代理配置文件。 若要生成包含总成本和使用情况详细信息的报表,请包括 MarkdownReporter 插件。 请在配置文件的 urlsToWatch 节中包含您要跟踪的 OpenAI 兼容 API 的 URL。

文件: devproxyrc.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.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 节中。

文件: devproxyrc.json(urlsToWatch 部分)

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

下表列出了可以使用开发代理跟踪的一些常用 OpenAI 兼容 API:

提供者 可查看的 URL DESCRIPTION
Anthropic https://api.anthropic.com/* 克劳德语言模型
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/v2.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/v2.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/v2.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/v2.0.0/openaitelemetryplugin.schema.json",
    "includeCosts": true,
    "pricesFile": "prices.json",
    "currency": "EUR"
  }
}

更改使用情况报告标题

默认情况下,用于创建报表标题的格式 LLM usage report for <application> in <environment>。 若要更改名称和环境值,请在 OpenAITelemetryPlugin 配置中设置 applicationenvironment 属性。 默认值分别为 defaultdevelopment

文件: devproxyrc.json(openAITelemetryPlugin 部分)

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

在 GitHub Actions 工作流中设置开发代理

若要安装和启动 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

触发要记录的请求

若要与应用程序交互并触发开发代理可以记录的请求,请使用 Playwright 等端到端测试框架。 自动设置操作会将 http_proxyhttps_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 浏览器中安装开发代理证书

如果在 Linux 运行程序上使用基于 Chromium 的浏览器来生成要记录的开发代理的请求,则需要安装开发代理证书以避免 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操作在工作流中手动停止Dev 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*

另请参阅