以编程方式使用文档

官方 LangSmith Terraform 提供商 允许您将 LangSmith 组织和工作区资源作为代码进行管理——工作区、自定义角色、组织和工作区成员、评估器、运行规则和警报规则。这是 使用 API 管理您的组织.

安装和配置

将提供商添加到您的 Terraform 配置中并固定版本:

terraform {
  required_providers {
    langsmith = {
      source  = "langchain-ai/langsmith"
      version = "~> 0.0.2"
    }
  }
}

provider "langsmith" {
  # Cloud (US). Use https://eu.api.smith.langchain.com for the EU region,
  # or your self-hosted URL. Can also be set via LANGSMITH_ENDPOINT.
  api_url = "https://api.smith.langchain.com"

  # Optional: scope workspace-level resources to a specific workspace.
  workspace_id = "00000000-0000-0000-0000-000000000000"
}

然后运行 terraform init 以下载该提供商。

身份验证

该提供商解析凭证的方式与 LangSmith SDK 和 CLI 相同。优先使用环境变量或配置文件,而不是硬编码 api_key:

  • * **环境**—LANGSMITH_API_KEY, LANGSMITH_ENDPOINT (API URL), LANGSMITH_WORKSPACE_ID.
  • * **配置文件**—设置 profile (or LANGSMITH_PROFILE) 使用 LangSmith CLI 配置文件。
  • * **提供商参数**—api_key, api_url, workspace_id, profile.

在您的 LangSmith 设置中创建 API 密钥或 服务密钥 。请参阅 身份验证方法 了解可用的密钥类型。

示例

创建工作区

resource "langsmith_workspace" "demo" {
  display_name  = "Demo Workspace"
  tenant_handle = "demo-workspace"
}

管理角色和成员

使用数据源查找内置角色,然后分配它们。这会邀请用户加入组织并授予他们工作区管理员权限:

data "langsmith_org_role" "user" {
  name = "ORGANIZATION_USER"
}

data "langsmith_workspace_role" "admin" {
  name = "WORKSPACE_ADMIN"
}

resource "langsmith_org_membership" "alice" {
  email   = "alice@example.com"
  role_id = data.langsmith_org_role.user.id
}

resource "langsmith_workspace_membership" "alice_demo" {
  workspace_id = langsmith_workspace.demo.id
  email        = langsmith_org_membership.alice.email
  role_id      = data.langsmith_workspace_role.admin.id
}

您也可以定义自定义工作区角色,例如通过克隆现有角色的权限:

resource "langsmith_workspace_role" "issues_agent" {
  display_name = "Issues Agent"
  description  = data.langsmith_workspace_role.admin.description
  permissions  = data.langsmith_workspace_role.admin.permissions
}

自动化评估器、运行规则和警报

该提供商管理的不仅仅是账户。您可以将 在线代码评估器、应用它们的 运行规则警报 与您的工作区一起编写:

resource "langsmith_evaluator" "tool_calls" {
  workspace_id = langsmith_workspace.demo.id
  name         = "tool call counts"
  type         = "code"

  code_evaluator = {
    language = "javascript"
    code     = file("${path.module}/evaluator.js")
  }
}

# A run rule applies the evaluator to matching runs in a tracing project.
# Run rules can also add runs to a dataset or annotation queue, or call webhooks.
resource "langsmith_run_rule" "score_root_runs" {
  workspace_id  = langsmith_workspace.demo.id
  display_name  = "score root runs"
  session_id    = "00000000-0000-0000-0000-000000000000" # tracing project ID
  sampling_rate = 1
  filter        = "eq(is_root, true)"

  evaluator_id = langsmith_evaluator.tool_calls.id
}

resource "langsmith_alert_rule" "error_rate" {
  session_id     = "00000000-0000-0000-0000-000000000000" # tracing project ID
  name           = "run error count high"
  type           = "threshold"
  attribute      = "error_count"
  aggregation    = "sum"
  window_minutes = 15
  operator       = "gte"
  threshold      = 10
  filter         = "eq(is_root, true)"

  actions = [{
    target  = "webhook"
    url_env = "LANGSMITH_ALERTS_WEBHOOK_URL"
    config_json = jsonencode({
      body = jsonencode({ text = "Error rate elevated" })
    })
  }]
}

资源参考

资源和数据源的完整列表——包含每个参数和属性——在 Terraform Registry 上发布并保持同步:

LangSmith provider on the Terraform Registry

浏览所有资源和数据源的完整参考。