以编程方式使用文档

默认情况下,LangSmith 会为每个 运行分配一个随机 ID。当需要时,您可以用自定义 ID 覆盖此设置:

  • - 提前知道运行 ID(例如,在 反馈 后立即附加反馈)。
  • - 将 LangSmith 运行与外部系统的 ID 关联。
  • - 通过重用确定性 ID 使运行具有幂等性。

接受任何 UUID v7 字符串。您可以传递由 LangSmith SDK 辅助函数生成的 UUID v7,如果您的系统已使用 UUID v7 标识符,也可以使用您自己的。

Python

使用 @traceable

在调用 run_id 函数时传递 langsmith_extra :[@traceable] 函数:

    from langsmith import traceable, uuid7

    @traceable
    def my_pipeline(question: str) -> str:
        return "answer"

    run_id = uuid7()
    my_pipeline("What is the capital of France?", langsmith_extra={"run_id": run_id})

    # run_id can now be used to attach feedback, query the run, etc.
    

使用 trace 上下文管理器

run_id 直接传递给 trace 上下文管理器构造函数,以为该追踪块设置 ID:

    from langsmith import trace, uuid7

    run_id = uuid7()

    with trace("my-pipeline", run_id=run_id) as run:
        result = "answer"
        run.end(outputs={"result": result})

    # run_id can now be used to attach feedback, query the run, etc.
    

TypeScript

使用 traceable

在传递给 id 的配置对象中传递 traceable:

    const runId = uuid7();

    const myPipeline = traceable(
      async (question: string) => {
        return "answer";
      },
      { name: "my-pipeline", id: runId }
    );

    await myPipeline("What is the capital of France?");

    // runId can now be used to attach feedback, query the run, etc.
    

相关