11-19 更新 实时
首页/ AI 大模型/ 正文

神经符号智能体解析:Python 中的 Z3 示例

AI 大模型 来源:Towards Data Science 2026-11-18 23:00 0 阅读 机器翻译
神经符号智能体解析:Python 中的 Z3 示例

2022年11月,一位名叫 Moffatt 的乘客向 Air Canada 网站聊天机器人询问丧亲优惠票价。他的祖母刚刚去世,他需要从 Vancouver 飞往 Toronto 参加葬礼。聊天机器人告诉他,他可以购买普通票,并在 90 天内申请折扣。然而,这是错误的。该航空公司的实际政策不允许退款。事实上,聊天机器人甚至还附上了说明这一点的页面链接。

Air Canada 拒绝退款。当 Moffatt 将案件提交给仲裁庭时,该航空公司辩称,它不为其聊天机器人所说的话负责。2024年2月,仲裁庭驳回了这一论点,并命令该航空公司向他支付差价。

我认为这个案例凸显了语言模型智能体的核心问题之一。聊天机器人给出了一个自信但完全错误的答案。目前有多种正在实施的方法来缓解这一问题,而神经符号智能体就是对此问题的一种应对。它们把智能体的一部分推理交给符号工具,例如约束求解器或证明检查器,这些工具能够指出模型何时出错。研究结果很扎实,但这些智能体仍然存在一个大问题。

在本文中,我回顾什么是神经符号智能体,然后用 Python 构建一个小型示例,以说明当前的问题是什么。

什么是神经符号智能体?

17世纪80年代,Gottfried Wilhelm Leibniz 设想了一种形式语言,任何论证都可以用这种语言精确地写下来。在他的设想中,两个意见相左的人不再需要争论。他们会坐下来,说 Calculemus:让我们来计算。他相信,人类争端可以归结为精确的符号。

该计划因一个实际原因而失败。必须有人手动将日常问题翻译成形式语言,这似乎相当不可能。几十年来,AI 也撞上了同一堵墙。专家系统和规划器等符号系统能够可靠地推理,但每条规则都必须由人编写。神经网络从数据中学习,但无法保证其输出正确,因为它们仍然是黑箱。

大型语言模型具备改变这一现状的能力。它们可以将英语翻译成形式语言,例如 SQL 或 PDDL——AI 规划器的输入语言。

如今,“神经符号”一词涵盖了许多不同类型的系统,因此在本文中我尝试使用一个更狭义的定义:

神经符号智能体是一种智能体,其中至少有一个决策是由一个处理具有形式意义的显式符号的组件做出或检查的,从而可以在不信任神经网络的情况下验证该决策。

要更广泛地了解神经与符号传统如何融合,Henry Kautz 的 2020 年 Engelmore 讲座是一个很好的起点:

求解器实际检查什么?

设 x 为英语任务。一个参数为 θ 的语言模型将 x 翻译成形式规范 φ。然后符号引擎 Σ 返回一个答案 y 和一个证书 c,例如一个证明或一个可满足赋值:

φ=fθ(x),(y,c)=Σ(φ)\varphi = f_\theta(x), \qquad (y,\ c) = \Sigma(\varphi)φ=fθ​(x),(y,c)=Σ(φ)

证书证明答案满足规范。但它并不保证规范与任务相符:

c 证明 y⊨φ,但不证明 y⊨x c\ \text{certifies}\ y \models \varphi, \qquad \text{but not}\ \ y \models x

没有人能形式化地检验 y 是否就是用户的本意,因为“用户的本意”其实并没有正式定义。在本文中,我把 x 与 φ 之间的距离称为形式化鸿沟。

现在你可能会想:“为什么还要加一个检查器?”主要原因是错误会累积。如果任务每一步正确的概率为 p,且错误未被发现,那么一个 n 步任务成功的概率为 pⁿ。如果有一个可靠的检查器能捕获每个错误,并且智能体每一步最多可以尝试 k 次,成功率就会高得多:

Punchecked(n)=pn,Pchecked(n)=(1−(1−p)k)nP_{\text{unchecked}}(n) = p^{n}, \qquad P_{\text{checked}}(n) = \left(1 - (1 - p)^{k}\right)^{n}Punchecked​(n)=pn,Pchecked​(n)=(1−(1−p)k)n

例如,当 p = 0.95、共 20 步时,没有检查器的智能体只有 36% 的情况下能正确完成。有检查器且每一步可尝试三次时,它能 99.8% 的情况下正确完成。

Subbarao Kambhampati 在亚利桑那州立大学的团队认为,语言模型无法可靠地验证自己的计划。这是因为模型检查自身时,会带有与编写计划的模型相同的盲点。他们提出 LLM-Modulo 框架,由模型提出计划,外部验证器进行评判。他在演讲中用不到二十分钟解释了这一论点:

这取决于检查器被放置在智能体循环的哪个位置。近期研究将其放在四个位置。

第一种方案是让求解器给出答案。在 Logic-LM [2] 中,语言模型把问题写成符号形式,由求解器完成推理。如果求解器报错,错误信息会回传给模型,让它修正自己的转换。在五个逻辑推理数据集上,Logic-LM 平均比标准提示(standard prompting)高出 39.2%,比思维链提示(chain-of-thought prompting)高出 18.4%。随着问题需要更多推理步骤,它的领先幅度还会扩大,这正是上文所述复合效应论证所预测的结果。

第二种方案是在智能体执行每个动作之前先做检查。Agent-C [3] 用一种小型规则语言编写诸如“在访问用户数据前先对用户进行身份验证”这样的规则,将其转换为逻辑,并用 SMT 求解器阻止违反规则的 tool call(工具调用)。在零售和航空客服基准测试上,它搭配 Claude Sonnet 4.5 和 GPT-5 都达到了 100% 的策略合规率。其任务效用得分也有所提升。这与“安全规则会让智能体变得不那么有用”的普遍假设相悖。

第三种方案是检查最终答案。Amazon 的 Automated Reasoning checks [4] 是 Bedrock Guardrails 的一部分,它把成文的策略转换为逻辑,并据此检查模型的每一次回复。每条回复会被多次翻译,只有在各个版本结果一致时才会采信该结果。作者报告其可靠性(soundness)超过 99%,且几乎没有误报。

第四种方案是在训练过程中把检查器用作奖励信号。当前最强的定理证明器正是这样训练出来的。

在语言任务之外,符号组件同样胜过纯神经网络模型。一篇 2026 年对 178 篇论文的综述 [5] 报告称,机器人任务完成率提升 23%,自主导航成功率达到 95.4%,而神经网络基线仅为 18.6%,且训练数据需求小得多。这里需要指出的是,这些研究比较的是差异很大的系统。

Python 中的一个小例子

我为一项调度任务构建了一个小型智能体。请求用通俗英文写成,智能体必须把它转换为 Z3(微软开源的 SMT 求解器,pip install z3-solver)的约束:

在我们的单个 GPU 节点上调度四个作业:数据摄入、训练、评估和部署。数据摄入在训练和评估之前运行。评估在训练之后、12:00 维护窗口之前运行。12:00 不运行任何任务。部署在下午进行,至少在评估之后两小时。

模型不应自己编写 Z3 代码。相反,它填写一个小型带类型的格式,每句英文对应一条具名约束,再由代码把该格式转换为 Z3。这样,模型编写的代码永远不会被执行。在下面的代码中,模型把一句话翻译反了:

注意:为保持代码片段简短,它从编译后的约束开始,跳过带类型的格式。

from z3 import Int, Solver, Distinct, And, satjobs = ["ingest", "train", "evaluate", "deploy"]t = {j: Int(j) for j in jobs} # 每个任务的开始小时def base(): """09:00 到 17:00 的一小时时段,每个时段一个任务。""" return [And(t[j] >= 9, t[j] <= 17) for j in jobs] + [Distinct(*t.values())]def schedule(spec): s = Solver() s.add(base()) for name, c in spec.items(): # 每句话一个命名约束 s.assert_and_track(c, name) if s.check() == sat: m = s.model() return {j: m[t[j]].as_long() for j in jobs} return sorted(str(c) for c in s.unsat_core())llm_spec = { # LLM 的翻译 "ingest_first": And(t["ingest"] < t["train"], t["ingest"] < t["evaluate"]), "eval_after_train": t["train"] < t["evaluate"], "eval_before_noon": t["evaluate"] < 12, "nothing_at_noon": And([t[j] != 12 for j in jobs]), "deploy_afternoon": t["deploy"] >= 13, "deploy_gap": t["evaluate"] >= t["deploy"] + 2, # 反转}print(schedule(llm_spec))

['deploy_afternoon', 'deploy_gap', 'eval_before_noon']

求解器不仅仅是否决该规范。它列出了六条约束中无法同时成立的三条。每个名称对应一句英文句子,因此智能体可以准确告诉模型要重新查看哪些句子。这样的反馈比另一个语言模型给出的自由文本评论有用得多。在模型修复了被反转的约束后,求解器找到了一个排程:

原文较长,译文为节选;译文由英文原文机器翻译,术语按行业惯例统一,可在文末展开原文核对。

查看英文原文(Neurosymbolic Agents Explained, With a Z3 Example in Python)

In November 2022, a passenger named Moffatt asked Air Canada’s website chatbot about bereavement fares. His grandmother had just died, and he needed to fly from Vancouver to Toronto for the funeral. The chatbot told him he could buy a regular ticket and apply for the discount within 90 days. However, that was wrong. The airline’s actual policy did not allow refunds. In fact, the chatbot had even attached a link to the page that said so.

Air Canada refused the refund. When Moffatt took the case to a tribunal, the airline argued that it was not responsible for what its chatbot said. In February 2024, the tribunal rejected that argument and ordered the airline to pay him the difference.

I think this case highlights one of the core problems with language-model agents. The chatbot gave a confident answer that was completely wrong. There are multiple methods being implemented to mitigate this, and neurosymbolic agents are one response to this problem. They hand part of the agent’s reasoning to a symbolic tool, such as a constraint solver or a proof checker, that can show when the model is wrong. The research results are strong, but these agents still have a large problem.

In this article, I review what neurosymbolic agents are and then build a small one in Python to illustrate what the current issue is.

What is a neurosymbolic agent?

In the 1680s, Gottfried Wilhelm Leibniz imagined a formal language in which any argument could be written down precisely. In his world, two people who disagreed would no longer need to debate. They would sit down and say Calculemus: let us calculate. He believed that human disputes could be reduced to precise symbols.

The plan failed for a practical reason. Someone had to translate everyday questions into the formal language by hand, which seems pretty impossible. AI hit the same wall for decades. Symbolic systems such as expert systems and planners reasoned reliably, but a person had to write every rule. Neural networks learned from data, but gave no guarantee that their outputs were correct, as they are still black boxes.

Large language models possess the ability to change this. They can translate English into formal languages such as SQL or PDDL, the input language of AI planners.

Today the word “neurosymbolic” covers many different kinds of systems, so I try to use a narrower definition in this article:

A neurosymbolic agent is an agent in which at least one decision is made or checked by a component that works on explicit symbols with a formal meaning, so that the decision can be verified without trusting the neural network.

For a broader view of how the neural and symbolic traditions are coming together, Henry Kautz’s 2020 Engelmore lecture is a good place to start:

What does the solver actually check?

Let x be the task in English. A language model with parameters θ translates x into a formal specification φ. A symbolic engine Σ then returns an answer y and a certificate c, for example a proof or a satisfying assignment:

φ=fθ(x),(y,c)=Σ(φ)\varphi = f_\theta(x), \qquad (y,\ c) = \Sigma(\varphi)φ=fθ​(x),(y,c)=Σ(φ)

The certificate proves that the answer satisfies the specification. But it doesn’t guarantee that the specification matches the task:

c certifies y⊨φ,but not  y⊨xc\ \text{certifies}\ y \models \varphi, \qquad \text{but not}\ \ y \models xccertifiesy⊨φ,but noty⊨x

Nobody can formally check whether y is what the user meant, because “what the user meant” doesn’t really have a formal definition. In this article, I call the distance between x and φ the formalization gap.

Diagram: an LLM translates the task in English into a formal specification, and a solver checks it to produce an answer with a certificate. Only the second step is checked; the first is the formalization gap.

Now you may be wondering, “Why add a checker at all?” The main reason is that errors compound. If each step of a task is correct with probability p and errors go unnoticed, an n-step task succeeds with probability pⁿ. If a sound checker catches each error and the agent can try each step up to k times, the success rate is much higher:

Punchecked(n)=pn,Pchecked(n)=(1−(1−p)k)nP_{\text{unchecked}}(n) = p^{n}, \qquad P_{\text{checked}}(n) = \left(1 - (1 - p)^{k}\right)^{n}Punchecked​(n)=pn,Pchecked​(n)=(1−(1−p)k)n

For example, with p = 0.95 and 20 steps, the agent without a checker finishes correctly only 36% of the time. With a checker and three tries per step, it finishes correctly 99.8% of the time.

Subbarao Kambhampati’s group at Arizona State University argues that language models cannot reliably verify their own plans. This is because a model checking itself has the same blind spots as the model that wrote the plan. They propose LLM-Modulo frameworks, in which the model suggests plans and external verifiers judge them. His talk explains the argument in under twenty minutes:

It depends on where the checker is placed in the agent loop. Recent work puts it in four places.

The first option is to let the solver produce the answer. In Logic-LM [2], a language model writes the problem in symbolic form and a solver does the reasoning. If the solver reports an error, the message goes back to the model so it can fix its translation. On five logical reasoning datasets, Logic-LM beat standard prompting by 39.2% on average and chain-of-thought prompting by 18.4%. Its lead grew as problems needed more reasoning steps, which is what the compounding argument above predicts.

The second option is to check each action before the agent takes it. Agent-C [3] writes rules such as “authenticate the user before accessing their data” in a small rule language, converts them into logic, and uses an SMT solver to block tool calls that break a rule. On retail and airline customer-service benchmarks, it reached 100% policy conformance with both Claude Sonnet 4.5 and GPT-5. Its task utility score also went up. This goes against the common assumption that safety rules make agents less useful.

The third option is to check the final answer. Amazon’s Automated Reasoning checks [4], part of Bedrock Guardrails, turn a written policy into logic and check each model response against it. Each response is translated several times, and the versions have to agree before the result is trusted. The authors report soundness above 99% with almost no false positives.

The fourth option is to use the checker as a reward during training. This is how the strongest current theorem provers are trained.

Symbolic components also beat purely neural models outside of language tasks. A 2026 review of 178 papers [5] reports a 23% improvement in robot task completion, 95.4% success in autonomous navigation against 18.6% for neural baselines, and much smaller training data requirements. It is important to note here that these studies compare very different systems.

A small example in Python

I built a small agent for a scheduling task. The request is written in plain English, and the agent has to turn it into constraints for Z3, Microsoft’s open-source SMT solver (pip install z3-solver):

Schedule four jobs on our single GPU node: ingestion, training, evaluation and deployment. Ingestion runs before training and evaluation. Evaluation runs after training and before the 12:00 maintenance window. Nothing runs at 12:00. Deployment happens in the afternoon, at least two hours after evaluation.

The model should not write Z3 code itself. Instead, it fills in a small typed format with one named constraint per English sentence, and turns that format into Z3 using code. This way, no model-written code ever runs. In the code below, the model has translated one sentence backwards:

Note: To keep the snippet short, it starts from the compiled constraints and skips the typed format.

from z3 import Int, Solver, Distinct, And, satjobs = ["ingest", "train", "evaluate", "deploy"]t = {j: Int(j) for j in jobs} # start hour of each jobdef base(): """One-hour slots from 09:00 to 17:00, one job per slot.""" return [And(t[j] >= 9, t[j] <= 17) for j in jobs] + [Distinct(*t.values())]def schedule(spec): s = Solver() s.add(base()) for name, c in spec.items(): # one named constraint per sentence s.assert_and_track(c, name) if s.check() == sat: m = s.model() return {j: m[t[j]].as_long() for j in jobs} return sorted(str(c) for c in s.unsat_core())llm_spec = { # the LLM's translation "ingest_first": And(t["ingest"] < t["train"], t["ingest"] < t["evaluate"]), "eval_after_train": t["train"] < t["evaluate"], "eval_before_noon": t["evaluate"] < 12, "nothing_at_noon": And([t[j] != 12 for j in jobs]), "deploy_afternoon": t["deploy"] >= 13, "deploy_gap": t["evaluate"] >= t["deploy"] + 2, # reversed}print(schedule(llm_spec))

['deploy_afternoon', 'deploy_gap', 'eval_before_noon']

The solver does more than reject the specification. It names three of the six constraints that cannot all hold together. Each name maps to one English sentence, so the agent can tell the model exactly which sentences to look at again. That feedback is substantially more useful than a free-text comment from another language model. After the model fixes the reversed constraint, the solver finds a schedule:

llm_spec["deploy_gap"] = t["deploy"] >= t["evaluate"] + 2 # after feedbackprint(schedule(llm_spec))

{'ingest': 9, 'train': 10, 'evaluate': 11, 'deploy': 13}

The schedule is now verified, in the sense that it satisfies every constraint. However, as I mentioned previously the solver cannot guarantee whether the constraints mean what the user meant. Recent research shows that this is where these systems often fail.

Planetarium [1] tested this directly. Researchers asked GPT-4o to translate English planning problems into PDDL. Almost all of its outputs were valid PDDL and could be solved by a planner. But only about a quarter described the problem that was actually asked. For the rest, a planner would return a correct plan with a certificate for the wrong problem.

A 2026 study on neuro-formal verification [6] found a worse case. A coding agent was asked whether Python programs were correct, and it turned each question into a proof task for Dafny, a program verifier. When the agent and the verifier were simply connected, the agent “proved” 98% of the correct programs and 98% of the buggy ones. The verifier never accepted a wrong proof. It turns out the agent had proved statements that no longer described the program. Precision only rose to 92% when the translation was split into separate stages that could not see which verdict was wanted. Even then, the system could only decide 57% of the cases.

Can the solver catch a bad translation?

My scheduling example has the same problem. “In the afternoon” can mean 13:00 or later, or 14:00 or later. The model picked one reading without saying so. Instead of trusting a single translation, the agent can write down both readings and ask the solver whether they ever disagree:

others = [c for name, c in llm_spec.items() if name != "deploy_afternoon"]reading_a = t["deploy"] >= 13 # "afternoon" begins at 13:00reading_b = t["deploy"] >= 14 # "afternoon" begins at 14:00def find(*extra): s = Solver() s.add(base() + others + list(extra)) if s.check() == sat: m = s.model() return {j: m[t[j]].as_long() for j in jobs}print(find(reading_a != reading_b)) # a schedule on which the readings disagreeprint(find(reading_a, reading_b)) # a schedule both readings accept

{'ingest': 9, 'train': 10, 'evaluate': 11, 'deploy': 13}{'ingest': 9, 'train': 10, 'evaluate': 11, 'deploy': 14}

The schedule where the two readings disagree, with deployment at 13:00, is the same schedule the solver “verified” earlier.

The fix could be simple: the agent can ask the user a specific question, such as “Does 13:00 count as the afternoon?” Or it can choose the schedule that both readings accept, which moves deployment to 14:00. If such a schedule exists, the ambiguity does not really affect the decision, and so the agent can move forward without asking. Amazon’s Automated Reasoning checks use the same idea in production when they translate each response several times and check that the versions agree [4].

Neurosymbolic agents work when a task has a formal definition of what is correct, such as a logic puzzle. The weak point is the translation from English into logic. I find this very interesting.

Based on the research and the example above, I would suggest four practices for anyone building neurosymbolic agents:

Have the model fill in a typed specification instead of writing solver code.

Name each constraint after the sentence it came from, so that you are able to trace failures back to the original text.

Translate the task at least twice. Let the solver look for contradictions before trusting either version.

Spend human review time on the specification.

M. Zuo, F. Piedrahita Velez, X. Li, M. L. Littman, S. H. Bach, Planetarium: A Rigorous Benchmark for Translating Text to Structured Planning Languages (2025), NAACL 2025

L. Pan, A. Albalak, X. Wang, W. Y. Wang, Logic-LM: Empowering Large Language Models with Symbolic Solvers for Faithful Logical Reasoning (2023), Findings of EMNLP 2023

A. Kamath, S. Zhang, C. Xu, S. Ugare, G. Singh, S. Misailovic, Enforcing Temporal Constraints for LLM Agents (2025), arXiv:2512.23738

C. An, S. Bayless, S. Buliani et al., A Neurosymbolic Approach to Natural Language Formalization and Verification (2025), arXiv:2511.09008

Create and distribute a simple NLP application with Python

AI agents can now write more code than any of us can read. That changed how I think about AI.

October 9, 202616 min read

Causal AI, exploring the integration of causal reasoning into machine learning

June 21, 20247 min read

Discover the optimal transformations to apply on the standard [0,1] uniform random generator for uniformly sampling a 2D disk

September 16, 202413 min read

How I built four Claude Code mods to protect API keys, track plan progress, catch untested code, and monitor context usage — with 135 passing tests and up to 100× less overhead than traditional hooks — and why they can outperform traditional hooks.

October 30, 202624 min read

What our over-dependence on external consulting teaches us about delegating our minds to machines

July 10, 202620 min read

How to qualitatively evaluate Neo4j graph embeddings

June 25, 20216 min read

A brief history of how I learned about the forgotten core of artificial intelligence.

March 2, 20204 min read

You'll only need 10 minutes. And a M1 Mac.

June 10, 20214 min read

原文为英文,正文由自动抓取并保留原文。

本文由采集器自动同步

原文发布于 Towards Data Science(Neurosymbolic Agents Explained, With a Z3 Example in Python),标题、摘要与正文由机器翻译,术语按行业惯例统一,未经人工逐句校订。

阅读原文 ↗