Skip to main content
我们可以通过 SDK 使用 aevaluate() 异步运行评估,该函数接受与 evaluate() 相同的所有参数,但要求应用程序函数是异步的。了解更多信息,请参阅如何使用 evaluate() 函数
本指南仅在使用 Python SDK 时相关。在 JS/TS 中,evaluate() 函数已经是异步的。更多信息,请参阅评估 LLM 应用程序

使用 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-4.1-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-4.1-mini-baseline"  # 可选,默认为随机。
)

相关