首页›Real Python›文章

LLM Evaluation in Python: Build an Eval Harness From Scratch

Real Python · 2026年11月25日

AI 摘要 AI 生成,可能有误

这篇 Real Python 教程教读者如何使用 Python 从零开始构建一个 LLM 评估工具(harness),并以客服聊天机器人作为贯穿全篇的示例。这个与框架无关的评估工具将确定性的代码检查与 LLM 评判器(已通过人工标注样本进行校准)相结合,从正确性、安全性、有用性和语气等标准对输出进行评分。它在相同的测试用例上比较两个提示词版本,并返回 PASS、FAIL 或 REVIEW 的发布决定以及一个适用于 CI 的退出码,从而阻止任何引入新的阻断性失败的更改。

You change the system prompt behind an LLM feature, try a few examples, and think you’ve fixed the bug. The new outputs look better when you inspect them manually, so you deploy the change. All is well. That is, until you discover that a separate class of requests has regressed or that responses seem worse on average. This is the problem with making ad hoc prompt changes without systematic LLM evaluation.

Ad hoc changes and manual inspection are useful while you’re experimenting, but they don’t give you a repeatable regression signal. You also find it increasingly difficult to distinguish stochastic variation from real improvement. In this tutorial, you’ll build an LLM evaluation harness from scratch that catches potential output regressions and turns product expectations into verifiable tests.

By the end of this tutorial, you’ll understand that:

继续阅读全文

为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 HorizonReader 里读:划线做笔记、让 AI 帮忙。

更多来自 Real Python

GLM 5.4 Draws a Python Reading a Book

AI 摘要Real Python ran GLM 5.4 through an informal five-task benchmark of everyday Python skills, including using the turtle module to draw a python reading a book, which the model completed successfully with 172 lines of code. Across the tasks, the model wrote Python at a 3.10 level with some 3.11 features, avoided all ten unrelated edits in a cleanup test, correctly identified that itertools.flatten() doesn't exist, and cost $0.16 total. The full results are available on Real Python's website as part of their ongoing AI benchmark series.

Amazon Ergo Draws a Python Reading a Book

AI 摘要This article presents Real Python's informal AI benchmark of Amazon Ergo, which was tasked with drawing a python reading a book using Python's turtle module. Across five tasks, the model drew the snake with 3,205 drawing commands, used modern Python 3.11 features, correctly flagged that itertools.flatten() doesn't exist, and made zero unnecessary edits to a script. The full run took about 2.5 minutes and cost $0.18, with the model reporting Python 3.15.1 as the newest release.

Quiz: Build an AI Agent From Scratch in Python: The Agentic Loop

AI 摘要This article introduces a quiz that tests comprehension of a tutorial on building an AI agent from scratch in Python. The quiz revisits core concepts that turn a language model into an agent, including the harness, the conversation context, the tools the model can request, and the agentic loop that chains tool calls until an answer is reached.

Python Microservices With gRPC

AI 摘要This tutorial teaches readers how to implement Python microservices that communicate with each other using gRPC, explaining why splitting a monolithic application into independently deployed services offers benefits like modularity, flexibility, and robustness. It covers building middleware for monitoring, performing unit and integration tests, and deploying microservices to a production environment with Kubernetes, using a web app and a Recommendations microservice as a running example.

Plotting Declarative Charts With Altair

AI 摘要The article introduces Altair, a declarative Python visualization library where users describe what their data means—such as which columns map to axes, colors, and interactivity—and the library generates the chart, unlike Matplotlib's approach of scripting every visual detail. Altair produces web-native charts (HTML and JavaScript) that render in notebooks and can be saved as standalone files or embedded in web pages, making it ideal for interactive exploratory analysis. It's not a replacement for Matplotlib, which remains the better choice for pixel-precise publication figures or 3D plots, so the two libraries serve complementary use cases.