我们可以使用 aevaluate() 通过 SDK 异步运行评估,它接受与 evaluate() 相同的所有参数,但期望应用程序函数是异步的。您可以在此处了解有关如何使用 evaluate() 函数的更多信息。
本指南仅在使用 Python SDK 时相关。在 JS/TS 中,evaluate() 函数已经是异步的。您可以在此处查看如何使用它。

使用 aevaluate()

  • Python
需要 langsmith>=0.3.13
from langsmith import wrappers, Client
from openai import AsyncOpenAI

# 可选择包装 OpenAI 客户端以跟踪所有模型调用。
oai_client = wrappers.wrap_openai(AsyncOpenAI())

# 可选择添加 'traceable' 装饰器以跟踪此函数的输入/输出。
@traceable
async def researcher_app(inputs: dict) -> str:
    instructions = """您是一位出色的研究人员。给定一个高级研究想法,\
列出应该调查的 5 个具体问题以确定该想法是否值得追求。"""

    response = await oai_client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": instructions},
            {"role": "user", "content": inputs["idea"]},
        ],
    )
    return response.choices[0].message.content

# 评估器函数可以是同步或异步的
def concise(inputs: dict, outputs: dict) -> bool:
    return len(outputs["output"]) < 3 * len(inputs["idea"])

ls_client = Client()
ideas = [
    "universal basic income",
    "nuclear fusion",
    "hyperloop",
    "nuclear powered rockets",
]
dataset = ls_client.create_dataset("research ideas")
ls_client.create_examples(
    dataset_name=dataset.name,
    examples=[{"inputs": {"idea": i}} for i in ideas],
)

# 可以等效地直接使用 'aevaluate' 函数:
# from langsmith import aevaluate
# await aevaluate(...)
results = await ls_client.aevaluate(
    researcher_app,
    data=dataset,
    evaluators=[concise],
    # 可选,添加并发。
    max_concurrency=2,  # 可选,添加并发。
    experiment_prefix="gpt-4o-mini-baseline"  # 可选,默认为随机。
)

相关


Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.