Pydantic AI与Temporal:构建持久化、容错的AI智能体工作流 AI Engineer 2025-11-24

Pydantic AI、Temporal与Logfire简介

大家好,我是来自Pydantic的Samuel,今天我将演示Pydantic AI(一个用于构建AI智能体的库)、Temporal(一个分布式工作流编排平台)和Logfire(一个用于可观测性的工具)。我还会介绍Pydantic Evals(一个用于评估大型语言模型性能的工具)。

View/Hide Original English

Hi, I'm Samuel from Pantic and today I'm going to give a demo of Pyantic AI temporal and Pantic logfire. I'll also cover Pyantic evals.

在Pydantic AI中,我们支持Temporal和Deboss(另一个持久化执行框架)这两种持久化执行框架。

View/Hide Original English

So we have in Pantic AI support for temporal and Deboss to durable execution frameworks.

我们实际上正在增加更多支持;我认为我们已经收到了大约五个添加其他持久化执行或工作流编排后端的拉取请求。

View/Hide Original English

We're actually adding a bunch more. I think we've had something like five pull requests to add other durable execution or like workflow orchestration backends.

但目前主要是这两种,我认为可以公平地说,Temporal是这个领域的主要参与者,它们是这种执行方式的领导者。

View/Hide Original English

But at the moment it's it's these two. And I think it's fair to say temporal are like the big incumbent in this space and they're they're kind of I guess leaders leaders in how you do this.

为了演示这一点,一个简单的例子是,我向一个LLM(Large Language Model: 大型语言模型)提问,它会回复,这通常都能正常工作,我们不需要持久化执行组件。

View/Hide Original English

To demonstrate this a simple example of like I go ask an LLM question it replies mostly just works and we don't need the durable execution component.

但是,一旦我们进入长时间运行的工作流,问题就真正出现了。

View/Hide Original English

So, but once we get into longer running workflows, that's where it really becomes a problem.

特别是在我们已经进行了足够的计算,不想丢失它,或者我们已经在这项计算上花费了足够的时间,真的不想让用户从头开始的时候。

View/Hide Original English

In particular, where we've done enough compute that we don't want to lose it or we've spent enough time on that compute that we really don't want to have to start again for the user.

例如,我认为OpenAI公开表示他们使用Temporal进行深度研究,而且我认为其他一些LLM深度研究也做同样的事情。

View/Hide Original English

That's what for example I think OpenAI it's I think it's public that OpenAI use temporal for their um deep research and I think some of the other LLM deep research do the same thing.

所以,我将从一个“玩具”示例开始,然后转向一个更像是深度研究的示例,事实上,就是一个深度研究示例。

View/Hide Original English

So, I'll I'll start with a kind of toy example and then I'll move on to a more deep research type example. in fact a deep research example.

但在我们深入研究之前,让我先运行一个没有Temporal的示例。

View/Hide Original English

But before we get into that, let me let me run this example without.

“20个问题”游戏:一个“玩具”示例

这是一个由两个智能体玩“20个问题”游戏的例子。

View/Hide Original English

So this is a uh two agents that play um 20 questions.

所以,它们不仅仅是给出“是”或“否”的答案,还可以给出更多细节,比如“是,有点”、“不完全是”、“不,完全错误”。

View/Hide Original English

Um so instead of just a yes no answer, they get to give a little bit more detail like yes, kind of not really, no, completely wrong.

之所以这样设计,是因为我厌倦了等待它们花费大量时间才能成功地只用“是”或“否”来回答。

View/Hide Original English

Um that was because I was getting bored of waiting for them to take ages to succeed on the with the just the yes no.

我们有一个回答智能体,它运行一个相对较小的模型Hiku 3.5,因为我直到大约一小时前才知道4.5版本已经发布。

View/Hide Original English

So, we have an answer agent which is runs a relatively small model Hiku 3.5 because I didn't know 4.5 came out until about an hour ago.

它基本上会接收一个问题,并用这些答案中的一个来回答,然后它会将其上下文(即你正在寻找的秘密对象,在这个例子中是“土豆”)添加到其上下文中。

View/Hide Original English

And and this basically takes a question and answers yes well with with one of these answers and it it gets added into its context the the like secret object that you're looking for um which in this example is a potato.

然后我们有提问智能体,或者说玩家智能体,它对它将要做的事情有更多的上下文。

View/Hide Original English

So, and then we have the um questioner agent or the player agent has a bit more context on what it's going to go and do.

你不需要阅读所有这些内容,这些代码现在是公开的,它是一个拉取请求,但我稍后会合并它,你应该能理解其思想。

View/Hide Original English

You don't need to read read through all of this stuff. This code is is public now. It's a pull request, but I'll I'll merge that afterwards, but you should get the idea.

提问智能体提问的方式是调用一个工具“ask a question”。

View/Hide Original English

Um, and the way that the questioner agent gets to ask its questions is by calling a tool, ask a question.

在这个工具内部,我们运行另一个智能体,即回答智能体,来决定这个问题的答案,然后我们进行回复。

View/Hide Original English

Inside that tool, we run the other agent, the answer agent to basically decide the answer to this question, and then we respond.

它需要一点时间来运行。你可以看到,在这个例子中,它运行得很快。

View/Hide Original English

And it takes a little bit of time to run. You can see in this case, it succeeded pretty quickly.

有时令人惊讶的是,即使是这些非常简单的问题,即使是非常智能的LLM也会完全困惑,走上奇怪的轨道,变得非常困惑。

View/Hide Original English

Sometimes it it's amazing how even these very simple questions, even very intelligent LLMs get themselves completely confused and go down some weird track and and like get very confused.

但你可以看到,在上次运行中,它问了一堆问题,最终问到“这是水果还是蔬菜?是水果吗?不是。”

View/Hide Original English

But you can see in the last run that it asked a bunch of questions, got down to like is this a fruit or vegetable? Is it a fruit? No.

所以它知道是蔬菜,“是橙色的吗?”然后它找出了答案是土豆。

View/Hide Original English

So it knew it was a vegetable, is it orange? And it worked out the answer was was potato.

但显然,它又在运行了。我不知道它会采取多少步。

View/Hide Original English

But obviously and here it is running again. I don't know how many steps it's going to take.

有时可能需要多达50步才能正确回答这个问题。

View/Hide Original English

Sometimes it can take up to like 50 steps to get this question right.

显然,问题在于如果这个过程因为系统内部不可靠的端点,或者因为我们在云端运行并且Kubernetes(一个开源的容器编排系统)决定进行扩缩容或其他任何原因而崩溃,那么我们再次运行它时,就必须从头开始。

View/Hide Original English

And obviously the problem is if this dies either because we have some unreliable uh endpoint within our system or because we're running in the cloud and Kubernetes decides it wants to scale or whatever it might be.

这在这种情况下是有问题的。

View/Hide Original English

If we run this again, we obviously have to start from scratch. That is problematic in this case.

但你可以想象,随着任务越来越长,重新启动它会变得越来越麻烦。

View/Hide Original English

Um but you can imagine as the tasks get longer and longer just restarting it gets more and more problematic.

所以,我认为关于这个“20个问题”示例的另一件事是,尽管它很容易理解,感觉像个玩具,但它实际上直接等同于一个深度研究案例。

View/Hide Original English

So um I think the other thing to say about this this like um 20 questions example is although it's pretty simple to understand and it feels like a toy, it is actually directly equivalent to a deep research case.

在这种情况下,智能体实际上是在进行一项任务,去寻找一个问题的答案,它需要向“花园底部的巨魔”提问,以获得下一个谜题的答案,从而到达下一个终点,对吧?

View/Hide Original English

Where effectively the agent is is like going off on a quest to go and find an answer to a question where it needs to ask the like you know troll at the bottom of the garden the other the like question to the next riddle to get to the next endpoint, right?

深度研究实际上就是这个“20个问题”游戏,只是你的中间步骤可能是网络搜索(web search)或RAG(Retrieval-Augmented Generation: 检索增强生成)或其他任何东西。

View/Hide Original English

Like deep research is effectively this 20 questions just with like web like web search or rag or whatever it might be is your intermediate steps.

引入Temporal:构建持久化智能体

那么,让我们把这个“20个问题”游戏变成一个持久化智能体(Durable Agent: 能够在故障后恢复状态并继续执行的智能体)。

View/Hide Original English

So let's turn that 20 questions into a durable agent.

为了简单起见,这大部分代码是相同的。我实际上是复制了它,但你可以看到我们有我们的回答智能体。

View/Hide Original English

Um so this is mostly the same code for simplicity. I've actually copied it here but you see we have our answer agent.

我们需要将其封装在Temporal Agent中,这会给我们另一个行为类似于Pydantic AI智能体的东西。

View/Hide Original English

We need to wrap it in this temporal agent which gives us another thing that behaves like an agent like a pantic AI agent.

所以它也是抽象智能体的一个子类。我们对提问智能体也做同样的事情。

View/Hide Original English

So it's also a subclass of abstract agent. We do the same with our questionnaire agent.

为了保持简单,我们没有做关于在上下文中传递答案的相同操作。

View/Hide Original English

To keep things simple, we aren't doing the same um stuff about passing around the answer in context.

我们只是将答案硬编码到回答智能体的系统提示中。

View/Hide Original English

We've just hardcoded the answer into the system prompt here for the for the answer agent.

但除了添加Temporal封装器之外,你也可以像我稍后将展示的那样,稍后应用持久化执行。

View/Hide Original English

But apart from these like adding the temporal wrappers, you can as I will show later just um apply durable execution later.

这就是Temporal发挥作用的地方。

View/Hide Original English

But here's where the the temporal bit comes in.

我不是Temporal的销售人员。虽然他们做的底层工作很棒,但我确实认为他们的一些Python抽象有点丑陋。

View/Hide Original English

And I'm not a salesperson for temporal. And although the underlying stuff they do is amazing, I do think some of their Python abstractions are kind of ugly.

但无论如何,Temporal的原则是你有工作流(Workflows: 业务逻辑的定义)和活动(Activities: 工作流中执行的独立任务)。

View/Hide Original English

But anyway, the the principle of temporal is that you have workflows and activities.

工作流需要完全是确定性的(Deterministic: 每次执行相同输入都会产生相同输出),而活动则需要做任何非确定性的事情,特别是IO(Input/Output: 输入/输出操作)。

View/Hide Original English

And workflows need to be entirely deterministic and activities then need to do anything that is non-deterministic like IO in particular.

所以,你基本上可以在工作流内部做任何事情,除了IO和调用随机数。

View/Hide Original English

So you can basically do anything inside a workflow other than IO and calling random.

如果满足这个条件,那么你就有了一个确定性系统。

View/Hide Original English

And if you're if if that's the case, then you have a deterministic system.

你可以认为Temporal在后台所做的是,它在运行工作流时,基本上会记录每个运行的活动,包括其输入和输出。

View/Hide Original English

And what you can think of what temporal is doing in the background as it's running that workflow and it's basically recording the every activity that runs and both the the inputs to that and the outputs.

因此,如果你想从头到某个特定点重新运行它,它基本上可以插入这些答案。

View/Hide Original English

And so if you want to rerun it from like from the beginning to a certain point, it can basically plug in those answers.

我将展示它看起来像什么。

View/Hide Original English

And I'll show what that looks like.

这就是我们定义工作流的方式。这里的活动是隐式的。

View/Hide Original English

So this is how we define our workflow. The activities here are implicit.

关键是这个Temporal智能体会负责将所有调用LLM所需的IO操作转换为后台活动,包括工具调用。

View/Hide Original English

The point is that this temporal agent takes care of turning all of the IO that you need to do to call an LLM into activities in the background, including tool calls.

OpenAI声称支持Temporal,但他们不支持将工具调用作为活动,这对我来说有点像“巧克力茶壶”(指中看不中用)。

View/Hide Original English

So, OpenAI claim to have temporal support, but they don't support tool calls as activities, which to me makes it slightly a chocolate teapot.

也就是说,如果没有工具调用,这些东西实际上就没有什么意义。

View/Hide Original English

Like, there's actually no point in having any of these things without without tool calling or little little point.

但我们像这样定义工作流。我认为你大部分时间可以直接复制粘贴他们的定义。

View/Hide Original English

But we define our workflow like this. I think you can for the most part just copy paste their their definitions of how to do it.

这里我们有我们的“游戏”机制。

View/Hide Original English

Here we have our play mechanism.

这里的关键是我们将连接到Temporal服务器,它将记录我们任务或智能体执行时的状态,并能够恢复等。

View/Hide Original English

The point here is we're going to we're going to connect to the temporal server which is what's going to record the state of our task or our agent as it's executing and be able to resume and stuff.

我在这里本地运行着Temporal。这只是Temporal的开源版本,它作为单独的进程运行,我可以重新启动它来清除状态,这就是我们连接到本地主机的原因。

View/Hide Original English

I have temporal running locally here. This is just the open source version of temporal which runs as a separate process and I can restart that to kind of kill the state and that's why we're connecting to local host.

在生产环境中,你会使用Temporal的云服务,这就是他们赚大钱的原因。

View/Hide Original English

In production you use temporal's cloud. that's why they make so much money.

这里我们运行工作者。这是我们实际启动工作流的地方。

View/Hide Original English

Um, and here we we run the worker. This is where we're actually going to kick off our workflow.

通常,我们只需使用execute workflow来启动工作流。

View/Hide Original English

In general, we just kick off our workflow with execute workflow.

我们传入要运行的工作流,传入输入。在这种情况下没有输入,因为我们只是开始。

View/Hide Original English

We pl pass in the the workflow that we want to run. We pass the inputs. There aren't any inputs in this case because we just start.

所以这里没有任何输入。然后它就会运行。

View/Hide Original English

So, there aren't any here. And it will then go and run.

因此,如果我运行它,你就会看到它开始执行。

View/Hide Original English

And so, if I run this, we will you will see it start to to execute.

你会看到它正在运行。需要注意的几件事是,有一些日志消息。

View/Hide Original English

You'll see it running. The only couple of things to note, there's a couple of log messages.

啊,我们立即遇到了这个“broken”异常。

View/Hide Original English

Ah, and we immediately have this exception broken.

那是因为为了模拟系统内部不可靠的情况,我在工具中添加了20%的几率会发生故障。

View/Hide Original English

And that was because to simulate some system that's unreliable inside the tool I added 20% of the time it's going to it's going to break.

你会看到Temporal立即处理了故障后的继续执行。

View/Hide Original English

What you will see is that temporal has immediately taken care of continuing after that.

所以即使发生了故障,它也会继续运行。

View/Hide Original English

So even though this broke, it will continue to run.

我可能把20%的故障率设置得太高了,因为它现在总是失败,但它实际上会继续处理这些运行时错误,并继续正常运行。

View/Hide Original English

And I may have set 20% to be too high um because it's now failing all the time, but it's actually going to continue and deal with those runtime errors and just continue to operate absolutely fine.

然而,我想我刚才把20%调得太高了。

View/Hide Original English

Let me However, I think I dialed up 20% too high just before.

所以,我将看看它是否会继续运行。

View/Hide Original English

So, I'm going to actually see if this is going to continue to operate.

显然,当你进行演示时,一切都会突然停止。

View/Hide Original English

Obviously, when you give a demo, everything suddenly grinds to a halt.

有人最近说他们讨厌一切都按计划进行的演示,我说你永远不需要担心我会有这样的问题。

View/Hide Original English

Someone recently said they hate demos where everything goes to plan, and I said you'll never need to worry about that with me.

我不知道为什么它会完全停止。

View/Hide Original English

Um, I don't know why that has actually ground to a complete halt.

我不知道是不是它只是反复失败。

View/Hide Original English

I don't know whether that's it just repeatedly failing.

我将在这里关闭Temporal服务器并重新启动它,这样我们就不会存储状态。

View/Hide Original English

Let me I'm going to kill temporal server here and restart it so that we don't have the state stored.

我将清除这里并再次运行它。

View/Hide Original English

And I will clear this and run it again.

现在你应该会看到它大部分时间都能成功,10%的时间会失败。

View/Hide Original English

And you should now see it succeeding most of the time and failing 10% of the time.

所以,是的,你现在看到它正在提问,偶尔会中断,但会继续。

View/Hide Original English

So yeah, you see it now asking questions and occasionally breaking. Good timing, but continuing.

这就是Temporal所做的事情之一。它只是执行重试逻辑,你可以在没有Temporal的情况下实现,但他们做得非常好。

View/Hide Original English

Um, so that's one of the things Temporal does. It just does the like retry logic that is like you could implement without temporal, but they do it very nicely.

但还有更强大的功能。

View/Hide Original English

But there are more powerful things.

所以,假设这个正在运行的进程被Kubernetes杀死了。

View/Hide Original English

So let's say this process that's in the middle of running gets killed by Kubernetes.

现在,我们到这里,然后我们杀死它。

View/Hide Original English

Now, so we go across here and we we just like kill it.

进程被杀死了。

View/Hide Original English

Process gets killed.

我没有向你展示的是,我还用Logfire(一个用于应用程序可观测性的工具)对它进行了检测。

View/Hide Original English

Now, what I didn't show you is I also instrumented this with logfire.

所以,如果我们查看我们的工作流,我们可以看到这里发生了什么。

View/Hide Original English

So if we look at our our workflow, we can see exactly what was going on here.

我们可以看到这里发生了什么。

View/Hide Original English

Um, and we we can see what's going on here.

所以我们有对Claude(Anthropic开发的一系列大型语言模型)的不同调用,然后我们正在运行活动,它又在运行另一个智能体。

View/Hide Original English

So we have the different calls to claude and then inside that we have we're running the activity which is then running the um other agent.

但特别是如果我们来到工作流的顶层开始,我可以获取工作流ID。

View/Hide Original English

But in particular if we come to the top level start for the workflow I can take the the workflow ID.

如果我们回到代码这里,你会看到我有一些代码,基本上允许我使用给定的恢复ID来继续工作流。

View/Hide Original English

If we come back over to code here you'll see I had some some code in here to basically allow me to continue with a given uh resume ID and to continue a workflow.

现在,大部分情况下你不需要这样做。这只是为了演示。

View/Hide Original English

Now for the most part you wouldn't have to do this. This is just for the sake of the demo.

如果我再次运行脚本,它会再次启动这个工作流,并并行运行两个。

View/Hide Original English

If I just reran the script again, it would kick off this workflow again and it would run the two in parallel.

那看起来会非常混乱。

View/Hide Original English

That would look really confusing.

所以,我没有这样做,而是专门等待一个特定的工作流完成,这样你就可以看到发生了什么。

View/Hide Original English

So instead of doing that, I'm I'm specifically hanging on a particular workflow to finish. So you can see what's going on.

所以,如果我再次运行我的脚本,但我给它正在进行的工作流ID。

View/Hide Original English

So if I if I run my script again, but I give it the workflow that was ongoing.

现在你看到它已经迅速前进到第六个问题,并且正在继续运行。

View/Hide Original English

Now you see it's already whizzed forward to question six and it's continuing to operate.

所以,我们让它基本上在不向实际智能体代码中添加任何恢复代码的情况下恢复了。

View/Hide Original English

So, we've got it to basically resume without having to add any resume code anywhere in our actual agent code.

我们只是设置了Temporal,它就工作了。

View/Hide Original English

We just set up temporal and it works.

如果你查看Logfire,你可以看到到底发生了什么。

View/Hide Original English

And you can see exactly what's happened if you um look at logfire.

你会看到,对LLM的那第一批调用在大约5毫秒内就响应了。

View/Hide Original English

What you will see is that that whole that first bunch of um calls to the LLM responded in like 5 milliseconds.

所以,这些实际上并没有发送给LLM。

View/Hide Original English

So, these were not actually sent to the LLM.

Temporal只是返回了它已经为这些情况缓存的结果。

View/Hide Original English

Temporal just returned the result, the kind of cached result that it already had for each of these cases.

所以我们能够有效地迅速前进到它继续调用LLM的点。

View/Hide Original English

So we're able to effectively zoom forward to the point where it then continues to to call the LLM.

这就像你在所有进行IO的地方都设置了缓存,这样你就可以运行你的代码。

View/Hide Original English

It's it's as if you've gone through everywhere that you're doing IO and you've set up uh caching on each individual call so that you can run your code.

我看到有些人点头,这让我对解释这个感觉好多了。

View/Hide Original English

Uh I see some people nodding which is making me feel a bit better about explaining this.

但我们不需要进行推理,我们不需要等待时间。

View/Hide Original English

But we don't have to do the inference. We don't have to wait the time.

我们基本上可以运行我们的工作流代码,它通常非常快,因为它没有IO,只是程序性的。

View/Hide Original English

We can basically run our workflow code that's generally very fast because it's no IO. It's just procedural.

它会立即不断获取结果,直到它需要继续的点。

View/Hide Original English

and it will just keep getting results instantly until it gets to the point where it needs to needs to continue.

你看到在这种情况下,它完全困惑了,正在思考这个东西是不是沙拉碗。

View/Hide Original English

And you see in this case, it's got itself completely confused and it's off um wondering about whether this thing is a salad bowl.

所以,你看到LLM有时表现很好,有时表现很糟糕。

View/Hide Original English

So, you see how sometimes the LLM does well, sometimes it does does terribly.

我将让它继续运行,看看它是否——你看,它知道这与食物有关,但它真的困惑了。

View/Hide Original English

Um I'm going to actually I'll leave that running to see whether it you see it's it knows it's related to food, but it's got itself really confused.

模型评估:Pydantic Evals的洞察

我只想说,你可能会感兴趣,就在这之前,我一直在思考不同的模型会如何表现。

View/Hide Original English

Um I will just say as as it might interest you just before this I was wondering how the different models would perform.

所以我使用Pydantic Evals(Pydantic AI中的一个评估工具)对这些不同的案例进行了一些评估。

View/Hide Original English

And so I I ran some evals with padantic evals on these different cases.

你可以在这里看到,屏幕上有点难读,但有GPT 4.1(OpenAI的语言模型)、Gemini(Google的语言模型)和Claude Sonnet 4.5(Anthropic的语言模型)。

View/Hide Original English

And you can see here we have it's a bit hard to read on the screen but uh GPT 4.1 Gemini and Claude Sonnet 4.5.

你可以看到每个案例的不同断言,它们是通过还是失败了,以及平均成本。

View/Hide Original English

And you can see the different assertions for each case whether they passed or failed here and you can see the the average cost.

你可以看到Gemini便宜得多,也快得多。

View/Hide Original English

You can see Gemini was way way cheaper, way way faster.

在某个地方,如果我们看一个单独的案例,我们应该有一个关于成功所需步数的指标。

View/Hide Original English

And somewhere we should have, if we look at an individual case, we have a a metric for how many steps it took to succeed.

我想也许我们需要滚动一下。是的,问题计数。

View/Hide Original English

I think maybe we have to scroll over. Yeah. Question count.

你可以在这里看到,Gemini每次都快得多。

View/Hide Original English

You can see here Gemini was way quicker each time.

我后来检查结果发现,Gemini之所以快得多,回答得更快,是因为它只是编造了一个错误的答案,而我没有检查。

View/Hide Original English

I discovered subsequently having having checked the results that actually the reason Gemini is way faster and answers much more quickly is it just invents an answer that's wrong and I wasn't checking it.

所以,这还不完美,但这绝对是评估的一个有趣案例,可以找出哪个模型实际上更好,因为它们默认情况下肯定不是特别好。

View/Hide Original English

So, this is not perfect yet, but like it's uh the the EV this is definitely an interesting case for evals and seeing and like working out which model is actually better because they're definitely not particularly good at it by default.

但是,是的,在我天真的情况下,Gemini表现得更好,但这不具有代表性。

View/Hide Original English

But yeah, in my naive case, Gemini did way better, but that's not representative.

无论如何,我将离开它,因为它已经进行了46步,但仍然无法找出那个东西是土豆。

View/Hide Original English

Anyway, I'm going to leave that because it's got 46 steps in and it's still failing to work out that that thing's a potato.

如果你愿意,我可以展示评估案例,但我认为查看深度研究案例可能更有趣,这是一个更具意义的持久化执行用例。

View/Hide Original English

I I can show the evals case if you want, but I think it might be more interesting to look at a deep research case, which is a kind of more meaningful example of where you would run durable execution.

而且还可以并行执行,这也是Temporal开箱即用的功能之一,你无需编写任何代码。

View/Hide Original English

Um, and also doing stuff in parallel, which is also one of the things that like just works out of the box with temporal without you having to write any any code.

深度研究案例:构建多智能体工作流

所以,这是我昨晚几个小时内快速尝试构建的深度研究。

View/Hide Original English

So, this is my very quick last night hours attempt at building deep research.

我真心认为它和许多实际的深度研究系统一样好。

View/Hide Original English

I honestly think it's as good as lots of the actual deep research systems.

我们定义了深度研究的计划,这实际上就是我们的深度研究计划。

View/Hide Original English

So we have we define our plan for deep research and this is this is effectively our deep research plan.

它有一个执行摘要,你将有效地将其输出给用户,说明我将要做什么。

View/Hide Original English

So it has an executive summary what you would effectively pump out to the user about what I'm going to go and do.

然后我们有一个网络搜索步骤列表。我们这里最多有五个,这样就不会永远持续下去。

View/Hide Original English

Then we have a list of web search steps. We maximum we have a maximum of five here so it doesn't take forever.

然后我们有分析指令。

View/Hide Original English

And then we have analysis instructions.

关键是,我认为这是今年AI领域的一大变化,回答了我之前在另一个Zoom会议上的问题。

View/Hide Original English

And the point is that like I think this is one of the big change in AI this year answering a bit the question I had on the other zoom.

我认为我们已经从认为智能体(就其定义而言)——智能体有三个定义:AI定义是LLM在一个循环中调用工具;技术定义是微服务(Microservice: 一种软件架构风格);然后是商业定义,即可以取代人类的东西。

View/Hide Original English

I think we've moved from thinking that like agents in the sense of so there are three definitions of agents. There is the like AI definition which is LLM's calling tools in a loop. There is the tech definition which is a micros service and then there is the business definition which is something that can replace a human.

暂时忽略商业定义。如果你考虑AI和工程定义,我们今年年初认为,每个微服务中会有一个AI智能体,一个LLM在一个循环中调用工具。

View/Hide Original English

Um ignoring the business definition for a minute. If you think about the AI and the like engineering definitions, we thought at the beginning of this year you would have one AI agent, one LLM calling tools in the loop within each microser.

我认为我们越来越多地认为智能体实际上是开发的“量子”(quantum),它们是微任务,你通过构建它们来形成大多数人所认为的智能体,即实际自主完成任务的东西。

View/Hide Original English

I think we've moved more and more to think that the agents are actually the kind of quantum of development. They are the the micro tasks that are doing that you build up to to form a like what most people would think of as an agent, something that actually goes and autonomously completes a task.

所以我们的深度研究智能体实际上由多个智能体组成。

View/Hide Original English

And so our deep research agent is actually made up of multiple agents.

我们有一个计划智能体,它带着一个提示出发,并返回一个实例化的结构化数据提取,它会给你一个Pydantic模型的实例,这就是你的运行计划。

View/Hide Original English

So we have this plan agent which goes off with a prompt and it returns an instance structured data extraction gives you an instance of this pyantic model which is your plan to run it.

然后你有搜索智能体,在这种情况下它可以访问搜索工具,或者我将展示在另一种情况下使用Tavily(一个AI驱动的搜索API),它在这种情况下使用更快的模型Gemini Flash

View/Hide Original English

Then you have the search agent which has access in this case to search tool or I'll show using tavilli in the other case um which is using a a faster model gemini flash uh in this case.

然后在这种情况下,我使用Claude Sonnet 4.5进行最终的分析阶段。

View/Hide Original English

And then you're using in this case I'm using claude son 4.5 for the final analysis stage.

所以我想这有点像人们谈论倾向于图(graphs)的时候。

View/Hide Original English

So I suppose this is a bit what people talk about when they're leaning towards graphs.

我没有用图来构建它,尽管我可以,因为它不需要图。

View/Hide Original English

I haven't built this in a graph although I could because it doesn't need a graph.

它不够复杂,不需要图。

View/Hide Original English

It's not complex enough to need a graph.

持久化执行是一种更好的快照方式,但它对持久化执行有更细粒度的支持。

View/Hide Original English

And durable execution is a way better way of getting snapshotting, but like much more granular support for for durable execution.

我们添加了一个工具,允许分析智能体如果真的需要,可以进行更多的网络搜索。

View/Hide Original English

We added a tool that allowed the analysis agent to do a bit more web search if it really wanted to.

我不认为它会使用它,但这是实际的深度研究代码。

View/Hide Original English

I don't think it uses it, but this is the actual deep research code.

所以你可以看到它多么简洁。我们运行计划智能体,我们得到我们的计划。

View/Hide Original English

So you can see how concise it is. We run the plan agent. We get back our plan.

我们并行运行所有搜索智能体。

View/Hide Original English

We run in parallel all of the search agents.

所以,我们只是使用Python的任务组(Task Group: 用于管理一组并发任务)来运行所有这些。

View/Hide Original English

So, we're just using a task group from Python to run all of these.

我们获取这些结果,这些结果都将是不同搜索部分的文本结果。

View/Hide Original English

We get those results, which will all be the text results of the different bits of search.

我们使用XML(Extensible Markup Language: 可扩展标记语言)格式,基本上将所有这些数据粉碎成一大块相对可读的数据,供分析智能体使用。

View/Hide Original English

We use format as XML to basically smash all of that into a massive lump of reasonably readable data for the analysis agent.

然后我们去运行智能体。

View/Hide Original English

Then we go off and run the agent.

我们运行我相对定期向AI提出的销售问题,即“给我一份在伦敦使用Python的对冲基金列表”。

View/Hide Original English

And we run the kind of question that I'm asking AI relatively regularly for sales, which is find me a list of hedge funds that write Python in London.

如果我运行这个UV run deep research,我们会看到它开始运行。

View/Hide Original English

And if I go and run this uh UV run deep research, we'll see it starting to churn away.

我们可以在终端中看到Logfire的输出。

View/Hide Original English

We can see it in the terminal with logfire.

但我们也可以来到Logfire这里。让我清除它。

View/Hide Original English

But we can also come over here to log fire. Let me clear that.

到这里底部,我们可以看到这个运行正在进行。

View/Hide Original English

Um go to the bottom here and we can see this this run here as it's going on.

它在九秒内运行了计划步骤,你可以看到所有搜索步骤都在并行进行。

View/Hide Original English

It's it's run the plan step in nine seconds and you can see all of the search steps going on in parallel.

一旦它们完成,它就会开始。你可以看到分析智能体刚刚开始。

View/Hide Original English

Once they've finished, it will start. You can see the analysis agent has just started.

我们可以查看单独的搜索。

View/Hide Original English

We can look at the individual searches.

所以,你会对发生了什么有一个很好的了解,它被问到的问题,它决定运行的查询,来自Medium、不同网站的大量数据,结构化数据,然后智能体也注入了大量的上下文,对吧?

View/Hide Original English

So, you get a pretty good idea of what happened, the question it got asked, uh the queries it decided to run, bunch of data from medium, different sites, structured data, and then like the agent also bangs in quite a lot of context, right?

所以这是我们大约10个并行搜索中的每一个。

View/Hide Original English

So this is each individual one of our like 10 parallel searches.

现在分析将带着所有这些输入运行。

View/Hide Original English

And now the analysis is going to go and run with all of that input.

你可以看到到目前为止,我们这次运行花费了8美分。

View/Hide Original English

You can see so far we've sent spent 8 cents on this particular run.

我们将看看它完成时会达到多少。

View/Hide Original English

We'll see what it gets to by the time it finishes.

但显然,问题在于如果我现在杀死它,它就会停止,如果我想再次运行它,就必须从头开始。

View/Hide Original English

But obviously the problem with this is if I kill this now, it's just going to die and I'd have to restart from the beginning if I wanted to to run it again.

所以,当它运行时,让我开始向你介绍持久化执行的示例。

View/Hide Original English

So, while that churns away, let me start introducing you to the durable execution example,

剧透一下,它会非常相似。

View/Hide Original English

spoiler, it's going to be pretty similar.

我昨晚发现Vertex SDK(Google Cloud Vertex AI的软件开发工具包)有一个bug,这意味着你现在不能将它与Temporal一起使用。

View/Hide Original English

I discovered last night that there's a bug with the Vertex SDK. That means that you can't use it with temporal right now.

所以我已经换成了OpenAI的响应,我正在使用Tavily而不是内置搜索。

View/Hide Original English

So, I've swapped out uh I think we should fix that, or at least I'll be winging at uh Deep Mind today to go and fix that. So, I've switched it out to OpenAI responses here and I'm using Tavilli instead of the built-in search.

是的,但除此之外,这都是非常相似的代码。

View/Hide Original English

Yeah, but other than that, this is all pretty similar code.

我可能可以直接从另一个模块导入代码。我只是决定复制它以保持简单。

View/Hide Original English

I could have probably imported the code from the other module. I just decided to duplicate it just to keep things easy.

但你再次看到,我们做同样的事情。我们将我们的智能体封装在Temporal智能体中。

View/Hide Original English

But you see again, we do the same thing. We wrap uh our agents in temporal agent.

这个分析智能体可以花费比默认活动持续时间更长的时间,因为它基本上会构建一个很长的摘要。

View/Hide Original English

This analysis one can take more than I think whatever the default activity duration is because it's a long basically build up a a summary.

所以我给它设置了时间,我想它花费的时间超过了2分钟或默认时间。

View/Hide Original English

And so I give it I think it was taking longer than 2 minutes or whatever the default is.

所以我只是给了它一个小时,这样它就不会失败。

View/Hide Original English

So I just gave it an hour so it's not going to fail.

对我来说,最强大的部分是我的工作流中的所有内容看起来都完全相同。

View/Hide Original English

Um and then the the for me the most powerful bit is everything here inside my workflow looks exactly the same.

我不需要做任何疯狂的事情来实现并行。我只是使用完全相同的任务组。

View/Hide Original English

I don't have to do any crazy stuff to do parallelism. I just use uh task group exactly the same.

我可以使用async.io gather。这都只是你习惯的命令式Python代码。

View/Hide Original English

I could use async.io gather. It's all just imperative Python code as you would be used to.

如果我想定期运行它,我可以在这里暂停七天。Temporal会负责暂停所有内容。

View/Hide Original English

I could have a if I wanted to run this periodically, I could sleep for seven days in here. Temporal would take care of pausing everything.

再次声明,我不是Temporal的销售人员。我并不喜欢他们所做的一切,但它是一种非常强大的编码方式。

View/Hide Original English

Again, I'm not here to be a temporal salesperson. I don't love everything about what they do, but it's a pretty powerful way of thinking about code.

我们不需要做所有的基础设施工作。

View/Hide Original English

We don't have to do all the infra stuff.

然后最终,再次,将我所有的上下文粉碎到最后一个智能体中并运行它。

View/Hide Original English

And then ultimately, again, smash all my context into the last agent and run it.

再次,有一些插件的东西。我必须插入日志,添加一些插件,将智能体添加为插件。

View/Hide Original English

And again, there's a bit of plug-in stuff. I have to plug in log add some plugins, add the agents as plugins.

但再次,我实际启动它的代码只是execute workflow。就这么简单。

View/Hide Original English

But again, my code to actually go and kick it off is just execute workflow. Simple as that.

我在这里问了一个稍微有争议的问题:“对于持久化执行和类型安全,最好使用哪个Python智能体框架?”

View/Hide Original English

And I asked it here slightly more controversial question of what's the best Python agent framework to use for durable execution and type safety.

我们祈祷它在大家面前运行时能给出正确的答案。

View/Hide Original English

And we will pray to God it gives the right answer when we run it in front of everyone.

如果我启动并再次运行它,我们应该会看到它。

View/Hide Original English

If I go and kick that off and run this again, we should see it.

如果我们来到这里,我们应该会在Logfire中看到它正在运行。

View/Hide Original English

If we come over here, we should see it running in Logfire.

你可以看到我们有与启动智能体相关的内容。

View/Hide Original English

You can see we have the stuff related to kicking off the agent.

抱歉,它在这里启动工作流。

View/Hide Original English

It's kicking off the workflow, excuse me, here.

我们看到搜索开始进行。

View/Hide Original English

And we have the searches beginning to happen happen.

但这里强大的地方是,再次想象我们正在运行所有这些搜索的中间,我们即将开始最后一步,然后有什么东西杀死了进程。

View/Hide Original English

But the the powerful bit here is again imagine that we're halfway through running all these searches. We're about to start the final step and something comes along and kills the process.

通常,你必须完全重新启动这个进程,并重新运行你的深度研究。

View/Hide Original English

And by in general, you'd have to go and completely restart this process and run your deep deep research all over again.

使用Temporal,它只会自动重新运行该工作流。

View/Hide Original English

With temporal it will just go and rerun that workflow automatically.

在这种情况下,我正在重新启动它,并且只运行那一个工作流。

View/Hide Original English

In this case, I'm restarting it and just running that one workflow.

但通常情况下,它会自动重新启动,并且在Kubernetes下次启动时,工作流将像以前一样运行,但它会立即获得每个问题的答案。

View/Hide Original English

But in general, it would just automatically go and be restarted and on the the next time that Kubernetes comes up, the workflow will run as it would have done before, but it will get answers to each individual question basically instantly.

所以,如果它不会失败,它似乎正在失败,我们又开始了。

View/Hide Original English

And so if it's not going to fail for me, which it seems to be, there we are. It started again.

你看到一个计划用了24毫秒。

View/Hide Original English

You see a plan took 24 milliseconds.

从大局来看,搜索根本没有花费时间,因为它立即从Temporal获得了结果。

View/Hide Original English

Search all took no time at all in the grand scheme of things because it just got the result back from temporal immediately.

然后是分析,这是我们尚未运行的任务。

View/Hide Original English

And then the analysis that was the the task we needed to that we hadn't run yet.

显然,它需要重新开始,因为那是一个活动,活动显然必须从头开始运行。

View/Hide Original English

Obviously that needs to go and start again because that's an activity and you can't activities obviously have to run again from scratch.

所以一旦它完成,我想它确实需要相当长的时间。

View/Hide Original English

And so once that finishes I think it does take quite a long time.

也许我可以展示之前的输出。

View/Hide Original English

Maybe I can show the previous output.

或者我们没有显示之前的输出吗?它讽刺地在之前失败了吗?

View/Hide Original English

Or did we not get to displaying the previous output? Did it ironically actually fail the time before?

但希望一旦它完成,我们应该能够看到它的分析,我认为这与其他深度研究的结果不相上下。

View/Hide Original English

But hopefully once this finishes, we should be able to see uh its analysis, which you know, I think is on a par with what I see from the other deep research things.

显然,还需要做一些UI工作才能在一个漂亮的深度研究界面中显示它。

View/Hide Original English

Obviously, there will be some there's some UI work to do to display this in a nice deep deep research interface.

我们完成了。它的主要建议是Pydantic AI与Temporal。

View/Hide Original English

There we are. It's completed and it has primary recommendation is pantic AI with temporal.

所以,它做到了我希望它做的事情。

View/Hide Original English

So, it it it did what I hoped it would do.

你看到它在这里给出了一个合理的报告,说明了其他劣质智能体框架的相对权衡,并且它应该在开头有一个带有链接的执行摘要。

View/Hide Original English

And you see it's given a reasonable report here of like the relative trade-offs of the other inferior agent frameworks and it should have done an executive summary at the beginning with with links.

是的,它提到了Pydantic AI、LangGraph(一个用于构建复杂智能体和多智能体系统的库),显然如果你喜欢快照或编写类型不安全的代码,以及Temporal本身,这很有道理。

View/Hide Original English

Yeah. So it said podantic AI langraph obviously if you love snapshotting or writing unsafe code type unsafe code temporal on its own which makes sense.

是的,这就是摘要。

View/Hide Original English

Yeah. So there's a there's the summary.

这是我主要想展示的内容。我将在这里合并持久化执行的内容。

View/Hide Original English

That is the main stuff I had to show. I will merge the the durable execution stuff in here.

Pydantic AI Gateway预告

所以到这里。我只想快速说一下另一件事,虽然我无法弄清楚如何发表评论,但如果你在Pydantic上搜索,你会找到它。

View/Hide Original English

So go here. I just other thing I want to just say quickly while I have I can't work out how to post a comment but like you'll find it on Pantic if you if you if you look for it.

哦,我可以这样做,如果有人想拍下这个二维码。

View/Hide Original English

Um oh I have I can do that if anyone wants to take a picture of that QR code.

我想提的另一件事是,我们即将宣布Pydantic AI Gateway(一个用于购买和管理来自各种模型的AI推理的平台)。

View/Hide Original English

The other thing I just wanted to mention we're about to announce uh Pantic AI gateway.

所以如果有人想提前尝试,请告诉我们。

View/Hide Original English

So if anyone wants to try it early let us know.

但这个平台很快就会上线。

View/Hide Original English

Um but yeah that platform will be landing soon.

你将能够直接使用Pydantic Gateway购买来自任何大型模型或大多数开源模型的推理,以及企业自托管的所有可观测性功能。

View/Hide Original English

You'll be able to use panic gateway directly to buy inference from any of the big models or most of the open source models and self-hosting for enterprise all the observability stuff.

但我会省去完整的介绍,它很快就会到来。

View/Hide Original English

But I I'll I'll save you the full spiel, but that's coming soon.

我想你们中的一些人会觉得它很有趣。

View/Hide Original English

I think some of you will find it interesting.

就是这样。非常感谢观看。

View/Hide Original English

That's it. Thanks so much for watching.

如果你想了解更多关于Pydantic AI、Pydantic AI Gateway或Pydantic Logfire的信息,请扫描这些二维码。

View/Hide Original English

If you want to learn more about Padantic AI, Padantic AI gateway or padantic logfire, please scan these QR codes.

如果你有任何反馈,请与我们联系。非常感谢聆听。

View/Hide Original English

If you have any feedback, uh please come and talk to us. Thanks so much for listening.

📌 文中提及的人物和组织

公司/组织: OpenAI, DeepMind

产品/模型: Claude Sonnet 4.5, Gemini Flash

关键字: ai-agent code durable-execution history llm