> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 如何在本地运行评估（仅限Python）

有时在本地运行评估而不将任何结果上传到LangSmith会很有帮助。例如，如果您正在快速迭代提示词并想在几个示例上进行快速测试，或者您正在验证目标函数和评估函数是否正确定义，您可能不希望记录这些评估。

您可以通过使用LangSmith Python SDK并在`evaluate()` / `aevaluate()`中传递`upload_results=False`来实现这一点。

这将完全按照常规方式运行您的应用程序和评估器并返回相同的输出，但不会有任何内容记录到LangSmith。这不仅包括实验结果，还包括应用程序和评估器的跟踪信息。

<Note>
  如果您想将结果上传到LangSmith，但同时需要在脚本中处理它们（用于质量门控、自定义聚合等），请参阅[在本地读取实验结果](/langsmith/read-local-experiment-results)。
</Note>

## 示例

让我们看一个示例：

需要 `langsmith>=0.2.0`。示例还使用了 `pandas`。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langsmith import Client

# 1. 创建和/或选择您的数据集
ls_client = Client()
dataset = ls_client.clone_public_dataset(
    "https://smith.langchain.com/public/a63525f9-bdf2-4512-83e3-077dc9417f96/d"
)

# 2. 定义一个评估器
def is_concise(outputs: dict, reference_outputs: dict) -> bool:
    return len(outputs["answer"]) < (3 * len(reference_outputs["answer"]))

# 3. 定义您的应用接口
def chatbot(inputs: dict) -> dict:
    return {"answer": inputs["question"] + " is a good question. I don't know the answer."}

# 4. 运行评估
experiment = ls_client.evaluate(
    chatbot,
    data=dataset,
    evaluators=[is_concise],
    experiment_prefix="my-first-experiment",
    # 'upload_results' 是相关参数。
    upload_results=False
)

# 5. 在本地分析结果
results = list(experiment)

# 检查 'is_concise' 是否返回了 False。
failed = [r for r in results if not r["evaluation_results"]["results"][0].score]

# 查看失败的输入和输出。
for r in failed:
    print(r["example"].inputs)
    print(r["run"].outputs)

# 将结果作为 Pandas DataFrame 进行查看。
# 必须已安装 'pandas'。
df = experiment.to_pandas()
df[["inputs.question", "outputs.answer", "reference.answer", "feedback.is_concise"]]
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{'question': 'What is the largest mammal?'}
{'answer': "What is the largest mammal? is a good question. I don't know the answer."}
{'question': 'What do mammals and birds have in common?'}
{'answer': "What do mammals and birds have in common? is a good question. I don't know the answer."}
```

|   | inputs.question                           | outputs.answer                                                                         | reference.answer           | feedback.is\_concise |
| - | ----------------------------------------- | -------------------------------------------------------------------------------------- | -------------------------- | -------------------- |
| 0 | What is the largest mammal?               | What is the largest mammal? is a good question. I don't know the answer.               | The blue whale             | False                |
| 1 | What do mammals and birds have in common? | What do mammals and birds have in common? is a good question. I don't know the answer. | They are both warm-blooded | False                |

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到Claude、VSCode等，通过MCP获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在GitHub上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/langsmith/local.mdx)或[提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
