👉 开篇:从一顿饭说起
作为一名开发人员,阅读技术文档或书籍时,常常会被大量专业术语和复杂逻辑吓到。但在阅读了这份关于"并行化模式"的章节后,我发现了一些有趣且实用的思路,即使没有深厚的技术背景,也能从中获得启发。
以下是我的阅读心得,希望能用通俗易懂的方式分享给大家。
📌 一、核心概念:什么是并行化?
在技术领域,"并行化"听起来很高大上,但其实它的核心思想非常贴近我们的生活。
举个例子:假设你要做一顿饭,如果按顺序操作——先洗菜、再切菜、最后炒菜——整个过程会很长。但如果你能同时进行(比如一边煮饭一边切菜),总时间就会大大缩短。
这就是并行化的本质:让多个独立的任务同时进行,而不是一个接一个地等待。
💡 关键洞察:文档中提到,在计算机系统中,并行化尤其适用于需要等待外部资源的任务(比如调用多个API或查询数据库)。通过同时发起请求,系统可以避免"干等"的时间浪费。
这让我联想到平时用手机时,多个APP可以同时下载更新,而不是排队一个一个来——这正是并行化带来的效率提升。
📌 二、为什么并行化重要?效率是硬道理
作为小白,我最直接的感受是:并行化能让复杂任务变得更"快"和"流畅"。
文档中强调,对于需要处理大量独立子任务的工作(例如同时调研多个信息来源、生成内容的不同部分),并行化能显著减少延迟。
举个例子:
🔄 顺序处理:先搜索资料A → 总结A → 再搜索资料B → 总结B → 最后合成结果(耗时较长)。
⚡ 并行处理:同时搜索A和B → 同时总结A和B → 最后合成结果(时间大幅缩短)。
这种优化在技术场景中很常见,比如:
- 旅行规划Agent同时查询机票、酒店和天气
- 内容生成Agent并行撰写标题、正文和关键词
这让我意识到,效率提升不仅依赖于硬件性能,更取决于任务的组织方式。
📌 三、技术如何实现?框架是关键助手
虽然具体实现需要专业框架(如LangChain的RunnableParallel或Google ADK的多Agent系统),但文档用代码示例解释了基本逻辑:
🔄 三步实现并行化
-
任务拆分:先将大任务分解为独立的小任务(例如研究一个公司时,同时收集其财务、产品、市场信息)。
-
并发执行:利用框架机制让这些小任务同时运行。
-
结果合成:所有任务完成后,再统一处理结果。
💡 小白视角:对我这样的非技术人员来说,重点在于理解这种"分而治之"的思路:识别工作中能并行的部分,用工具管理流程,而不是手动控制每一步。
这不仅能用于编程,也能启发我们优化日常工作的流程——比如写报告时,可以同时收集数据、整理案例和设计排版,而不是按顺序操作。
📌 四、小白能学到什么?思维比技术更重要
通过这次阅读,我最大的收获不是技术细节,而是一种优化效率的思维方式:
✅ 三大核心收获
-
识别独立性:分析任务中的子任务是否互不依赖。如果是,就可以尝试并行处理。
-
利用工具简化流程:就像文档中提到的框架(如LangChain)提供了并行化的"脚手架",我们在生活中也可以借助日历工具、协作软件等管理并发任务。
-
权衡利弊:并行化虽好,但文档也提到它会增加系统复杂度和调试难度。这提醒我们:不是所有任务都适合并行,需要根据实际情况权衡。
⚠️ 重要提醒:文档中强调的"批判性思考"也让我受益——阅读技术内容时,多问"为什么这样设计""如何应用到我的场景",而不仅仅是死记概念。
📌 五、总结:从技术到生活的启示
这份关于并行化的章节,虽然面向开发者,但其核心思想——通过并发执行独立任务提升整体效率——具有普适性。
作为技术小白,我可能无法直接写代码实现并行化,但学到了:
💡 三大思维启示
-
效率优化本质:减少等待时间,让资源利用更充分。
-
模块化思维:将大问题拆解为小模块,独立处理再整合。
-
工具的重要性:选择合适的工具(无论是编程框架还是日常APP)能事半功倍。
如果你也是技术新手,下次遇到复杂任务时,不妨问问自己:"哪些步骤可以同时进行?"——这就是并行化思维带来的最大价值。
🌟 金句:通过这次阅读,我深刻体会到技术概念的背后,往往隐藏着通用的解决问题逻辑。正如文档所说,并行化不仅是编程模式,更是一种"优化性能的哲学"。
🤔 互动时间
Q1: 并行化思维在日常工作中如何应用? A: 其实很简单!比如写报告时,可以同时收集数据、整理案例和设计排版,而不是按顺序操作。关键在于识别哪些任务可以同时进行。
Q2: 是不是所有任务都适合并行化? A: 当然不是!文档特别提到,并行化会增加系统复杂度。只有那些相互独立的子任务才适合并行处理,依赖性强的任务还是需要按顺序来。
🎯 写在最后
技术概念往往蕴含着普适的智慧,并行化就是这样一个例子。它不仅是一种编程模式,更是一种优化效率的生活哲学。
下次面对复杂任务时,不妨问问自己:哪些步骤可以同时进行?
📢 互动提问
你觉得在你的领域,哪些工作可以尝试并行化处理?欢迎在评论区分享你的想法和经验!
关注我们,获取更多技术小白也能懂的实用思维模式!
Chapter 3: Parallelization
Parallelization Pattern Overview
In the previous chapters, we've explored Prompt Chaining for sequential workflows and Routing for dynamic decision-making and transitions between different paths. While these patterns are essential, many complex agentic tasks involve multiple sub-tasks that can be executed simultaneously rather than one after another. This is where the Parallelization pattern becomes crucial.
在之前的章节中,我们探讨了用于顺序工作流的 Prompt Chaining 和用于动态决策以及不同路径间转换的 Routing 模式。虽然这些模式至关重要,但许多复杂的智能体任务涉及多个可以同时执行而非一个接一个执行的子任务。这就是 Parallelization (并行化) 模式变得至关重要的地方。
Parallelization involves executing multiple components, such as LLM calls, tool usages, or even entire sub-agents, concurrently (see Fig.1). Instead of waiting for one step to complete before starting the next, parallel execution allows independent tasks to run at the same time, significantly reducing the overall execution time for tasks that can be broken down into independent parts.
Parallelization (并行化) 涉及同时执行多个组件,如 LLM (大语言模型) 调用、工具使用,甚至是整个子智能体(见图1)。与等待一个步骤完成后再开始下一个步骤不同,并行执行允许独立任务同时运行,显著减少了可以分解为独立部分的任务的整体执行时间。
Consider an agent designed to research a topic and summarize its findings. A sequential approach might:
考虑一个设计用于研究主题并总结其发现的智能体。顺序方法可能会:
-
Search for Source A.
-
Summarize Source A.
-
Search for Source B.
-
Summarize Source B.
-
Synthesize a final answer from summaries A and B.
-
搜索来源A。
-
总结来源A。
-
搜索来源B。
-
总结来源B。
-
从总结A和B综合最终答案。
A parallel approach could instead:
并行方法则可以:
-
Search for Source A and Search for Source B simultaneously.
-
Once both searches are complete, Summarize Source A and Summarize Source B simultaneously.
-
Synthesize a final answer from summaries A and B (this step is typically sequential, waiting for the parallel steps to finish).
-
同时搜索来源A和搜索来源B。
-
两个搜索完成后,同时总结来源A和总结来源B。
-
从总结A和B综合最终答案(此步骤通常是顺序的,等待并行步骤完成)。
The core idea is to identify parts of the workflow that do not depend on the output of other parts and execute them in parallel. This is particularly effective when dealing with external services (like APIs or databases) that have latency, as you can issue multiple requests concurrently.
核心思想是识别工作流中不依赖于其他部分输出的部分,并并行执行它们。这在处理具有延迟的外部服务(如API或数据库)时特别有效,因为您可以同时发出多个请求。
Implementing parallelization often requires frameworks that support asynchronous execution or multi-threading/multi-processing. Modern agentic frameworks are designed with asynchronous operations in mind, allowing you to easily define steps that can run in parallel.
实现并行化通常需要支持异步执行或多线程/多处理的框架。现代智能体框架在设计时就考虑了异步操作,允许您轻松定义可以并行运行的步骤。
Fig.1. Example of parallelization with sub-agents
图1. 使用子智能体进行并行化的示例
Frameworks like LangChain, LangGraph, and Google ADK provide mechanisms for parallel execution. In LangChain Expression Language (LCEL), you can achieve parallel execution by combining runnable objects using operators like | (for sequential) and by structuring your chains or graphs to have branches that execute concurrently. LangGraph, with its graph structure, allows you to define multiple nodes that can be executed from a single state transition, effectively enabling parallel branches in the workflow. Google ADK provides robust, native mechanisms to facilitate and manage the parallel execution of agents, significantly enhancing the efficiency and scalability of complex, multi-agent systems. This inherent capability within the ADK framework allows developers to design and implement solutions where multiple agents can operate concurrently, rather than sequentially.
像 LangChain、LangGraph 和 Google ADK 这样的框架提供了并行执行的机制。在 LangChain Expression Language (LCEL) 中,您可以通过使用 | (用于顺序) 等操作符组合可运行对象,或通过构建链或图使其具有并发执行的分支来实现并行执行。LangGraph 凭借其图结构,允许您定义可以从单个状态转换执行的多个节点,有效地在工作流中启用并行分支。Google ADK 提供了强大、原生的机制来促进和管理智能体的并行执行,显著增强了复杂、多智能体系统的效率和可扩展性。ADK框架内的这种固有能力使开发人员能够设计和实现多个智能体可以并发而非顺序运行的解决方案。
The Parallelization pattern is vital for improving the efficiency and responsiveness of agentic systems, especially when dealing with tasks that involve multiple independent lookups, computations, or interactions with external services. It's a key technique for optimizing the performance of complex agent workflows.
Parallelization (并行化) 模式对于提高智能体系统的效率和响应能力至关重要,特别是在处理涉及多个独立查找、计算或与外部服务交互的任务时。它是优化复杂智能体工作流性能的关键技术。
Practical Applications & Use Cases
Parallelization is a powerful pattern for optimizing agent performance across various applications:
Parallelization (并行化) 是优化各种应用程序中智能体性能的强大模式:
1. Information Gathering and Research:
Collecting information from multiple sources simultaneously is a classic use case.
1. 信息收集与研究 (Information Gathering and Research):
同时从多个来源收集信息是一个经典用例。
-
Use Case: An agent researching a company.
- Parallel Tasks: Search news articles, pull stock data, check social media mentions, and query a company database, all at the same time.
- Benefit: Gathers a comprehensive view much faster than sequential lookups.
-
用例 (Use Case): 一个研究公司的智能体。
- 并行任务 (Parallel Tasks): 同时搜索新闻文章、获取股票数据、检查社交媒体提及和查询公司数据库。
- 优势 (Benefit): 比顺序查找更快地收集全面视图。
2. Data Processing and Analysis:
Applying different analysis techniques or processing different data segments concurrently.
2. 数据处理与分析 (Data Processing and Analysis):
同时应用不同的分析技术或处理不同的数据段。
-
Use Case: An agent analyzing customer feedback.
- Parallel Tasks: Run sentiment analysis, extract keywords, categorize feedback, and identify urgent issues simultaneously across a batch of feedback entries.
- Benefit: Provides a multi-faceted analysis quickly.
-
用例 (Use Case): 一个分析客户反馈的智能体。
- 并行任务 (Parallel Tasks): 同时对一批反馈条目运行情感分析、提取关键词、分类反馈和识别紧急问题。
- 优势 (Benefit): 快速提供多方面分析。
3. Multi-API or Tool Interaction:
Calling multiple independent APIs or tools to gather different types of information or perform different actions.
3. 多API或工具交互 (Multi-API or Tool Interaction):
调用多个独立的API或工具以收集不同类型的信息或执行不同的操作。
-
Use Case: A travel planning agent.
- Parallel Tasks: Check flight prices, search for hotel availability, look up local events, and find restaurant recommendations concurrently.
- Benefit: Presents a complete travel plan faster.
-
用例 (Use Case): 一个旅行规划智能体。
- 并行任务 (Parallel Tasks): 同时检查航班价格、搜索酒店可用性、查找当地活动和寻找餐厅推荐。
- 优势 (Benefit): 更快地呈现完整的旅行计划。
4. Content Generation with Multiple Components:
Generating different parts of a complex piece of content in parallel.
4. 多组件内容生成 (Content Generation with Multiple Components):
并行生成复杂内容的不同部分。
-
Use Case: An agent creating a marketing email.
- Parallel Tasks: Generate a subject line, draft the email body, find a relevant image, and create a call-to-action button text simultaneously.
- Benefit: Assembles the final email more efficiently.
-
用例 (Use Case): 一个创建营销邮件的智能体。
- 并行任务 (Parallel Tasks): 同时生成主题行、起草邮件正文、查找相关图像和创建行动号召按钮文本。
- 优势 (Benefit): 更高效地组装最终邮件。
5. Validation and Verification:
Performing multiple independent checks or validations concurrently.
5. 验证与确认 (Validation and Verification):
同时执行多个独立的检查或验证。
-
Use Case: An agent verifying user input.
- Parallel Tasks: Check email format, validate phone number, verify address against a database, and check for profanity simultaneously.
- Benefit: Provides faster feedback on input validity.
-
用例 (Use Case): 一个验证用户输入的智能体。
- 并行任务 (Parallel Tasks): 同时检查电子邮件格式、验证电话号码、根据数据库验证地址和检查不当内容。
- 优势 (Benefit): 更快地提供输入有效性反馈。
6. Multi-Modal Processing:
Processing different modalities (text, image, audio) of the same input concurrently.
6. 多模态处理 (Multi-Modal Processing):
同时处理同一输入的不同模态(文本、图像、音频)。
-
Use Case: An agent analyzing a social media post with text and an image.
- Parallel Tasks: Analyze the text for sentiment and keywords and analyze the image for objects and scene description simultaneously.
- Benefit: Integrates insights from different modalities more quickly.
-
用例 (Use Case): 一个分析带有文本和图像的社交媒体帖子的智能体。
- 并行任务 (Parallel Tasks): 同时分析文本的情感和关键词并分析图像的对象和场景描述。
- 优势 (Benefit): 更快地整合来自不同模态的洞察。
7. A/B Testing or Multiple Options Generation:
Generating multiple variations of a response or output in parallel to select the best one.
7. A/B测试或多选项生成 (A/B Testing or Multiple Options Generation):
并行生成响应或输出的多个变体以选择最佳的一个。
-
Use Case: An agent generating different creative text options.
- Parallel Tasks: Generate three different headlines for an article simultaneously using slightly different prompts or models.
- Benefit: Allows for quick comparison and selection of the best option.
-
用例 (Use Case): 一个生成不同创意文本选项的智能体。
- 并行任务 (Parallel Tasks): 同时使用略有不同的提示或模型为一篇文章生成三个不同的标题。
- 优势 (Benefit): 允许快速比较和选择最佳选项。
Parallelization is a fundamental optimization technique in agentic design, allowing developers to build more performant and responsive applications by leveraging concurrent execution for independent tasks.
Parallelization (并行化) 是智能体设计中的基本优化技术,允许开发人员通过利用独立任务的并发执行来构建更高性能和响应能力的应用程序。
Hands-On Code Example (LangChain)
Parallel execution within the LangChain framework is facilitated by the LangChain Expression Language (LCEL). The primary method involves structuring multiple runnable components within a dictionary or list construct. When this collection is passed as input to a subsequent component in the chain, the LCEL runtime executes the contained runnables concurrently.
在 LangChain 框架内的并行执行由 LangChain Expression Language (LCEL) 促进。主要方法涉及在字典或列表构造中构建多个可运行组件。当此集合作为输入传递给链中的后续组件时,LCEL 运行时并发执行包含的可运行对象。
In the context of LangGraph, this principle is applied to the graph's topology. Parallel workflows are defined by architecting the graph such that multiple nodes, lacking direct sequential dependencies, can be initiated from a single common node. These parallel pathways execute independently before their results can be aggregated at a subsequent convergence point in the graph.
在 LangGraph 的上下文中,此原则应用于图的拓扑结构。并行工作流通过设计图来定义,使得多个缺乏直接顺序依赖关系的节点可以从单个公共节点启动。这些并行路径在它们的结果可以在图中的后续汇聚点聚合之前独立执行。
The following implementation demonstrates a parallel processing workflow constructed with the LangChain framework. This workflow is designed to execute two independent operations concurrently in response to a single user query. These parallel processes are instantiated as distinct chains or functions, and their respective outputs are subsequently aggregated into a unified result.
以下实现演示了使用 LangChain 框架构建的并行处理工作流。此工作流旨在响应单个用户查询并发执行两个独立操作。这些并行过程实例化为不同的链或函数,它们各自的输出随后聚合成统一的结果。
The prerequisites for this implementation include the installation of the requisite Python packages, such as langchain, langchain-community, and a model provider library like langchain-openai. Furthermore, a valid API key for the chosen language model must be configured in the local environment for authentication.
此实现的先决条件包括安装必需的Python包,如 langchain、langchain-community 和模型提供程序库如 langchain-openai。此外,必须在本地环境中配置所选语言模型的有效API密钥以进行身份验证。
import os
import asyncio
from typing import Optional
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import Runnable, RunnableParallel, RunnablePassthrough
# --- Configuration ---
# Ensure your API key environment variable is set (e.g., OPENAI_API_KEY)
try:
llm: Optional[ChatOpenAI] = ChatOpenAI(model="gpt-4o-mini", temperature=0.7)
except Exception as e:
print(f"Error initializing language model: {e}")
llm = None
# --- Define Independent Chains ---
# These three chains represent distinct tasks that can be executed in parallel.
summarize_chain: Runnable = (
ChatPromptTemplate.from_messages([
("system", "Summarize the following topic concisely:"),
("user", "{topic}")
])
| llm
| StrOutputParser()
)
questions_chain: Runnable = (
ChatPromptTemplate.from_messages([
("system", "Generate three interesting questions about the following topic:"),
("user", "{topic}")
])
| llm
| StrOutputParser()
)
terms_chain: Runnable = (
ChatPromptTemplate.from_messages([
("system", "Identify 5-10 key terms from the following topic, separated by commas:"),
("user", "{topic}")
])
| llm
| StrOutputParser()
)
# --- Build the Parallel + Synthesis Chain ---
# 1. Define the block of tasks to run in parallel. The results of these,
# along with the original topic, will be fed into the next step.
map_chain = RunnableParallel(
{
"summary": summarize_chain,
"questions": questions_chain,
"key_terms": terms_chain,
"topic": RunnablePassthrough(), # Pass the original topic through
}
)
# 2. Define the final synthesis prompt which will combine the parallel results.
synthesis_prompt = ChatPromptTemplate.from_messages([
("system", """Based on the following information:
Summary: {summary}
Related Questions: {questions}
Key Terms: {key_terms}
Synthesize a comprehensive answer"""),
("user", "Original topic: {topic}")
])
# 3. Construct the full chain by piping the parallel results directly
# into the synthesis prompt, followed by the LLM and output parser.
full_parallel_chain = map_chain | synthesis_prompt | llm | StrOutputParser()
# --- Run the Chain ---
async def run_parallel_example(topic: str) -> None:
"""
Asynchronously invokes the parallel processing chain with a specific topic
and prints the synthesized result.
Args:
topic: The input topic to be processed by the LangChain chains.
"""
if not llm:
print("LLM not initialized. Cannot run example.")
return
print(f"\n--- Running Parallel LangChain Example for Topic: '{topic}' ---")
try:
# The input to `ainvoke` is the single 'topic' string,
# then passed to each runnable in the `map_chain`.
response = await full_parallel_chain.ainvoke(topic)
print("\n--- Final Response ---")
print(response)
except Exception as e:
print(f"\nAn error occurred during chain execution: {e}")
if __name__ == "__main__":
test_topic = "The history of space exploration"
# In Python 3.7+, asyncio.run is the standard way to run an async function.
asyncio.run(run_parallel_example(test_topic))
The provided Python code implements a LangChain application designed for processing a given topic efficiently by leveraging parallel execution. Note that asyncio provides concurrency, not parallelism. It achieves this on a single thread by using an event loop that intelligently switches between tasks when one is idle (e.g., waiting for a network request). This creates the effect of multiple tasks progressing at once, but the code itself is still being executed by only one thread, constrained by Python's Global Interpreter Lock (GIL).
提供的Python代码实现了一个LangChain应用程序,旨在通过利用并行执行来高效处理给定主题。请注意,asyncio 提供并发性,而不是并行性。它通过使用事件循环在单个线程上实现这一点,该循环在一个任务空闲(例如,等待网络请求)时智能地切换任务。这创建了多个任务同时进行的效果,但代码本身仍然只由一个线程执行,受到Python的全局解释器锁(GIL)的限制。
The code begins by importing essential modules from langchain_openai and langchain_core, including components for language models, prompts, output parsing, and runnable structures. The code attempts to initialize a ChatOpenAI instance, specifically using the "gpt-4o-mini" model, with a specified temperature for controlling creativity. A try-except block is used for robustness during the language model initialization. Three independent LangChain "chains" are then defined, each designed to perform a distinct task on the input topic. The first chain is for summarizing the topic concisely, using a system message and a user message containing the topic placeholder. The second chain is configured to generate three interesting questions related to the topic. The third chain is set up to identify between 5 and 10 key terms from the input topic, requesting them to be comma-separated. Each of these independent chains consists of a ChatPromptTemplate tailored to its specific task, followed by the initialized language model and a StrOutputParser to format the output as a string.
代码首先从langchain_openai和langchain_core导入基本模块,包括语言模型、提示、输出解析和可运行结构的组件。代码尝试初始化一个ChatOpenAI实例,特别是使用"gpt-4o-mini"模型,并指定温度以控制创造性。在语言模型初始化期间使用try-except块以确保健壮性。然后定义了三个独立的LangChain"链",每个链设计用于对输入主题执行不同的任务。第一个链用于简要总结主题,使用系统消息和包含主题占位符的用户消息。第二个链配置为生成与主题相关的三个有趣问题。第三个链设置为从输入主题中识别5到10个关键术语,要求它们用逗号分隔。这些独立链中的每一个都包含一个针对其特定任务定制的ChatPromptTemplate,后跟初始化的语言模型和StrOutputParser以将输出格式化为字符串。
A RunnableParallel block is then constructed to bundle these three chains, allowing them to execute simultaneously. This parallel runnable also includes a RunnablePassthrough to ensure the original input topic is available for subsequent steps. A separate ChatPromptTemplate is defined for the final synthesis step, taking the summary, questions, key terms, and the original topic as input to generate a comprehensive answer. The full end-to-end processing chain, named full_parallel_chain, is created by sequencing the map_chain (the parallel block) into the synthesis prompt, followed by the language model and the output parser. An asynchronous function run_parallel_example is provided to demonstrate how to invoke this full_parallel_chain. This function takes the topic as input and uses invoke to run the asynchronous chain. Finally, the standard Python if name == "main": block shows how to execute the run_parallel_example with a sample topic, in this case, "The history of space exploration", using asyncio.run to manage the asynchronous execution.
然后构建一个RunnableParallel块来捆绑这三个链,允许它们同时执行。这个并行可运行对象还包括一个RunnablePassthrough以确保原始输入主题可用于后续步骤。为最终综合步骤定义了一个单独的ChatPromptTemplate,将总结、问题、关键术语和原始主题作为输入,以生成全面的答案。名为full_parallel_chain的完整端到端处理链通过将map_chain(并行块)排序到综合提示中,然后是语言模型和输出解析器来创建。提供了一个异步函数run_parallel_example来演示如何调用此full_parallel_chain。此函数将主题作为输入,并使用invoke运行异步链。最后,标准的Python if name == "main": 块展示了如何使用示例主题执行run_parallel_example,在本例中是"The history of space exploration",使用asyncio.run管理异步执行。
In essence, this code sets up a workflow where multiple LLM calls (for summarizing, questions, and terms) happen at the same time for a given topic, and their results are then combined by a final LLM call. This showcases the core idea of parallelization in an agentic workflow using LangChain.
本质上,此代码设置了一个工作流,其中多个LLM调用(用于总结、问题和术语)同时针对给定主题发生,然后它们的结果由最终的LLM调用组合。这展示了使用LangChain在智能体工作流中并行化的核心思想。
Hands-On Code Example (Google ADK)
Okay, let's now turn our attention to a concrete example illustrating these concepts within the Google ADK framework. We'll examine how the ADK primitives, such as ParallelAgent and SequentialAgent, can be applied to build an agent flow that leverages concurrent execution for improved efficiency.
现在,让我们转向一个在Google ADK框架内说明这些概念的具体示例。我们将研究如何应用ADK原语,如ParallelAgent和SequentialAgent,以构建利用并发执行提高效率的智能体流。
from google.adk.agents import LlmAgent, ParallelAgent, SequentialAgent
from google.adk.tools import google_search
GEMINI_MODEL="gemini-2.0-flash"
# --- 1. Define Researcher Sub-Agents (to run in parallel) ---
# Researcher 1: Renewable Energy
researcher_agent_1 = LlmAgent(
name="RenewableEnergyResearcher",
model=GEMINI_MODEL,
instruction="""You are an AI Research Assistant specializing in energy. Research the latest advancements in 'renewable energy sources'. Use the Google Search tool provided. Summarize your key findings concisely (1-2 sentences). Output *only* the summary. """,
description="Researches renewable energy sources.",
tools=[google_search],
# Store result in state for the merger agent
output_key="renewable_energy_result"
)
# Researcher 2: Electric Vehicles
researcher_agent_2 = LlmAgent(
name="EVResearcher",
model=GEMINI_MODEL,
instruction="""You are an AI Research Assistant specializing in transportation. Research the latest developments in 'electric vehicle technology'. Use the Google Search tool provided. Summarize your key findings concisely (1-2 sentences). Output *only* the summary. """,
description="Researches electric vehicle technology.",
tools=[google_search],
# Store result in state for the merger agent
output_key="ev_technology_result"
)
# Researcher 3: Carbon Capture
researcher_agent_3 = LlmAgent(
name="CarbonCaptureResearcher",
model=GEMINI_MODEL,
instruction="""You are an AI Research Assistant specializing in climate solutions. Research the current state of 'carbon capture methods'. Use the Google Search tool provided. Summarize your key findings concisely (1-2 sentences). Output *only* the summary. """,
description="Researches carbon capture methods.",
tools=[google_search],
# Store result in state for the merger agent
output_key="carbon_capture_result"
)
# --- 2. Create the ParallelAgent (Runs researchers concurrently) ---
# This agent orchestrates the concurrent execution of the researchers.
# It finishes once all researchers have completed and stored their results in state.
parallel_research_agent = ParallelAgent(
name="ParallelWebResearchAgent",
sub_agents=[researcher_agent_1, researcher_agent_2, researcher_agent_3],
description="Runs multiple research agents in parallel to gather information."
)
# --- 3. Define the Merger Agent (Runs *after* the parallel agents) ---
# This agent takes the results stored in the session state by the parallel agents
# and synthesizes them into a single, structured response with attributions.
merger_agent = LlmAgent(
name="SynthesisAgent",
model=GEMINI_MODEL, # Or potentially a more powerful model if needed for synthesis
instruction="""You are an AI Assistant responsible for combining research findings into a structured report. Your primary task is to synthesize the following research summaries, clearly attributing findings to their source areas. Structure your response using headings for each topic. Ensure the report is coherent and integrates the key points smoothly.
**Crucially: Your entire response MUST be grounded *exclusively* on the information provided in the 'Input Summaries' below. Do NOT add any external knowledge, facts, or details not present in these specific summaries.**
**Input Summaries:**
* **Renewable Energy:**
{renewable_energy_result}
* **Electric Vehicles:**
{ev_technology_result}
* **Carbon Capture:**
{carbon_capture_result}
**Output Format:**
## Summary of Recent Sustainable Technology Advancements
### Renewable Energy Findings (Based on RenewableEnergyResearcher's findings)
[Synthesize and elaborate *only* on the renewable energy input summary provided above.]
### Electric Vehicle Findings (Based on EVResearcher's findings)
[Synthesize and elaborate *only* on the EV input summary provided above.]
### Carbon Capture Findings (Based on CarbonCaptureResearcher's findings)
[Synthesize and elaborate *only* on the carbon capture input summary provided above.]
### Overall Conclusion
[Provide a brief (1-2 sentence) concluding statement that connects *only* the findings presented above.]
Output *only* the structured report following this format. Do not include introductory or concluding phrases outside this structure, and strictly adhere to using only the provided input summary content.""",
description="Combines research findings from parallel agents into a structured, cited report, strictly grounded on provided inputs.",
# No tools needed for merging
# No output_key needed here, as its direct response is the final output of the sequence
)
# --- 4. Create the SequentialAgent (Orchestrates the overall flow) ---
# This is the main agent that will be run. It first executes the ParallelAgent
# to populate the state, and then executes the MergerAgent to produce the final output.
sequential_pipeline_agent = SequentialAgent(
name="ResearchAndSynthesisPipeline",
# Run parallel research first, then merge
sub_agents=[parallel_research_agent, merger_agent],
description="Coordinates parallel research and synthesizes the results."
)
root_agent = sequential_pipeline_agent
This Google ADK example demonstrates how to use ParallelAgent and SequentialAgent to build a parallel research system. The system first defines three specialized research agents that research renewable energy, electric vehicles, and carbon capture technology respectively. These agents run in parallel using ParallelAgent, with each agent independently using the Google search tool to gather information and store results in shared state.
这个Google ADK示例展示了如何使用ParallelAgent和SequentialAgent构建一个并行研究系统。系统首先定义了三个专门的研究智能体,分别研究可再生能源、电动汽车和碳捕获技术。这些智能体使用ParallelAgent并行运行,每个智能体独立使用Google搜索工具收集信息并将结果存储在共享状态中。
Once all parallel research is complete, the SequentialAgent coordinator starts the MergerAgent, which receives the results of all research agents and synthesizes them into a structured, clearly attributed report. This pattern is particularly suitable for tasks that require collecting information from multiple sources and then synthesizing it, significantly improving overall efficiency.
一旦所有并行研究完成,SequentialAgent协调器启动MergerAgent,它接收所有研究智能体的结果,并将它们综合成一个结构化的、带有明确归属的报告。这种模式特别适合需要从多个来源收集信息然后进行综合的任务,显著提高了整体效率。
Key concepts include:
- ParallelAgent: Allows multiple sub-agents to run simultaneously
- SequentialAgent: Ensures steps are executed in a specific order
- State sharing: Parallel agents store results in shared state for subsequent steps to use
- Task specialization: Each agent focuses on a specific domain, improving result quality
关键概念包括:
- ParallelAgent: 允许多个子智能体同时运行
- SequentialAgent: 确保步骤按特定顺序执行
- 状态共享: 并行智能体将结果存储在共享状态中供后续步骤使用
- 任务专门化: 每个智能体专注于特定领域,提高结果质量
这种并行化方法可以大大减少完成复杂研究任务所需的时间,特别是当涉及多个独立信息源时。
This code defines a multi-agent system used to research and synthesize information on sustainable technology advancements. It sets up three LlmAgent instances to act as specialized researchers. ResearcherAgent_1 focuses on renewable energy sources, ResearcherAgent_2 researches electric vehicle technology, and ResearcherAgent_3 investigates carbon capture methods. Each researcher agent is configured to use a GEMINI_MODEL and the google_search tool. They are instructed to summarize their findings concisely (1-2 sentences) and store these summaries in the session state using output_key.
这段代码定义了一个多智能体系统,用于研究和综合可持续技术进展的信息。它设置了三个LlmAgent实例作为专门的研究人员。ResearcherAgent_1专注于可再生能源,ResearcherAgent_2研究电动汽车技术,ResearcherAgent_3调查碳捕获方法。每个研究智能体配置为使用GEMINI_MODEL和google_search工具。它们被指示简要总结其发现(1-2句话),并使用output_key将这些摘要存储在会话状态中。
A ParallelAgent named ParallelWebResearchAgent is then created to run these three researcher agents concurrently. This allows the research to be conducted in parallel, potentially saving time. The ParallelAgent completes its execution once all its sub-agents (the researchers) have finished and populated the state.
然后创建一个名为ParallelWebResearchAgent的ParallelAgent来并发运行这三个研究智能体。这使得研究可以并行进行,可能节省时间。一旦其所有子智能体(研究人员)完成并填充状态,ParallelAgent就完成执行。
Next, a MergerAgent (also an LlmAgent) is defined to synthesize the research results. This agent takes the summaries stored in the session state by the parallel researchers as input. Its instruction emphasizes that the output must be strictly based only on the provided input summaries, prohibiting the addition of external knowledge. The MergerAgent is designed to structure the combined findings into a report with headings for each topic and a brief overall conclusion.
接下来,定义一个MergerAgent(也是一个LlmAgent)来综合研究结果。该智能体将以并行研究人员存储在会话状态中的摘要作为输入。其指令强调输出必须严格仅基于提供的输入摘要,禁止添加外部知识。MergerAgent设计用于将综合发现构建成带有每个主题标题和简要整体结论的报告。
Finally, a SequentialAgent named ResearchAndSynthesisPipeline is created to orchestrate the entire workflow. As the primary controller, this main agent first executes the ParallelAgent to perform the research. Once the ParallelAgent is complete, the SequentialAgent then executes the MergerAgent to synthesize the collected information. The sequential_pipeline_agent is set as the root_agent, representing the entry point for running this multi-agent system. The overall process is designed to efficiently gather information from multiple sources in parallel and then combine it into a single, structured report.
最后,创建一个名为ResearchAndSynthesisPipeline的SequentialAgent来编排整个工作流。作为主要控制器,这个主智能体首先执行ParallelAgent来执行研究。一旦ParallelAgent完成,SequentialAgent然后执行MergerAgent来综合收集的信息。sequential_pipeline_agent设置为root_agent,代表运行这个多智能体系统的入口点。整个过程设计为高效地并行从多个来源收集信息,然后将其组合成单一、结构化的报告。
At a Glance
一览
What: Many agentic workflows involve multiple sub-tasks that must be completed to achieve a final goal. A purely sequential execution, where each task waits for the previous one to finish, is often inefficient and slow. This latency becomes a significant bottleneck when tasks depend on external I/O operations, such as calling different APIs or querying multiple databases. Without a mechanism for concurrent execution, the total processing time is the sum of all individual task durations, hindering the system's overall performance and responsiveness.
内容: 许多智能体工作流涉及多个必须完成才能实现最终目标的子任务。纯顺序执行,即每个任务等待前一个任务完成,通常是低效且缓慢的。当任务依赖于外部I/O操作(如调用不同API或查询多个数据库)时,这种延迟成为显著的瓶颈。没有并发执行机制,总处理时间是所有单个任务持续时间的总和,阻碍了系统的整体性能和响应能力。
Why: The Parallelization pattern provides a standardized solution by enabling the simultaneous execution of independent tasks. It works by identifying components of a workflow, like tool usages or LLM calls, that do not rely on each other's immediate outputs. Agentic frameworks like LangChain and the Google ADK provide built-in constructs to define and manage these concurrent operations. For instance, a main process can invoke several sub-tasks that run in parallel and wait for all of them to complete before proceeding to the next step. By running these independent tasks at the same time rather than one after another, this pattern drastically reduces the total execution time.
原因: Parallelization (并行化) 模式通过启用独立任务的同时执行提供标准化解决方案。它通过识别工作流的组件(如工具使用或LLM调用)来工作,这些组件不依赖于彼此的直接输出。像LangChain和Google ADK这样的智能体框架提供了内置构造来定义和管理这些并发操作。例如,主进程可以调用几个并行运行的子任务,并等待它们全部完成后再进行下一步。通过同时运行这些独立任务而不是一个接一个地运行,这种模式大大减少了总执行时间。
Rule of thumb: Use this pattern when a workflow contains multiple independent operations that can run simultaneously, such as fetching data from several APIs, processing different chunks of data, or generating multiple pieces of content for later synthesis.
经验法则: 当工作流包含可以同时运行的多个独立操作时,使用此模式,例如从多个API获取数据、处理不同的数据块或生成多个内容片段以供后续综合。
Visual summary
视觉摘要
Fig.2: Parallelization design pattern
图2: 并行化设计模式
Key Takeaways
关键要点
Here are the key takeaways:
以下是关键要点:
- Parallelization is a pattern for executing independent tasks concurrently to improve efficiency.
- Parallelization (并行化) 是一种并发执行独立任务以提高效率的模式。
- It is particularly useful when tasks involve waiting for external resources, such as API calls.
- 当任务涉及等待外部资源(如API调用)时,它特别有用。
- The adoption of a concurrent or parallel architecture introduces substantial complexity and cost, impacting key development phases such as design, debugging, and system logging.
- 并发或并行架构的采用引入了显著的复杂性和成本,影响设计、调试和系统记录等关键开发阶段。
- Frameworks like LangChain and Google ADK provide built-in support for defining and managing parallel execution.
- 像LangChain和Google ADK这样的框架提供内置支持来定义和管理并行执行。
- In LangChain Expression Language (LCEL), RunnableParallel is a key construct for running multiple runnables side-by-side.
- 在LangChain Expression Language (LCEL) 中,RunnableParallel是并行运行多个可运行对象的关键构造。
- Google ADK can facilitate parallel execution through LLM-Driven Delegation, where a Coordinator agent's LLM identifies independent sub-tasks and triggers their concurrent handling by specialized sub-agents.
- Google ADK可以通过LLM-Driven Delegation促进并行执行,其中协调器智能体的LLM识别独立子任务并触发专门子智能体的并发处理。
- Parallelization helps reduce overall latency and makes agentic systems more responsive for complex tasks.
- Parallelization (并行化) 有助于减少整体延迟,使智能体系统在复杂任务中更具响应性。
Conclusion
结论
The parallelization pattern is a method for optimizing computational workflows by concurrently executing independent sub-tasks. This approach reduces overall latency, particularly in complex operations that involve multiple model inferences or calls to external services.
并行化模式是一种通过并发执行独立子任务来优化计算工作流的方法。这种方法减少了整体延迟,特别是在涉及多个模型推理或调用外部服务的复杂操作中。
Frameworks provide distinct mechanisms for implementing this pattern. In LangChain, constructs like RunnableParallel are used to explicitly define and execute multiple processing chains simultaneously. In contrast, frameworks like the Google Agent Developer Kit (ADK) can achieve parallelization through multi-agent delegation, where a primary coordinator model assigns different sub-tasks to specialized agents that can operate concurrently.
框架提供了实现此模式的不同机制。在LangChain中,使用RunnableParallel等构造来明确定义和同时执行多个处理链。相比之下,像**Google Agent Developer Kit (ADK)**这样的框架可以通过多智能体委托实现并行化,其中主协调器模型将不同的子任务分配给可以并发操作的专门智能体。
By integrating parallel processing with sequential (chaining) and conditional (routing) control flows, it becomes possible to construct sophisticated, high-performance computational systems capable of efficiently managing diverse and complex tasks.
通过将并行处理与顺序(链接)和条件(路由)控制流集成,可以构建复杂的、高性能的计算系统,能够高效管理多样化和复杂的任务。
References
参考文献
Here are some resources for further reading on the Parallelization pattern and related concepts:
以下是关于Parallelization (并行化) 模式及相关概念的进一步阅读资源:
- LangChain Expression Language (LCEL) Documentation (Parallelism): python.langchain.com/docs/concep…
- Google Agent Developer Kit (ADK) Documentation (Multi-Agent Systems): google.github.io/adk-docs/ag…
- Python asynci
oDocumentation: docs.python.org/3/library/a…