基于Nano Banana的设计智能体

创意社区自从谷歌发布新的Nano Banana模型以来就一直在热议。极快的生成速度和出乎意料的高质量输出引发了创新浪潮,创作者们在社交平台上分享着从逼真肖像到抽象艺术作品的各种内容。个人而言,我惊讶于该模型能够同时实现速度和质量——这在历史上是需要二选一的权衡。

这让我思考:虽然直接调用Nano Banana API能产生令人印象深刻的结果,但如果我们通过智能编排来增强它的能力会怎样?如果我们构建的AI代理不仅能按需生成图像,还能理解上下文、迭代反馈、自主制作营销内容,那会怎样?

这就引出了一个令人兴奋的交叉点:谷歌的代理开发工具包(ADK)与Nano Banana强大的图像生成能力相结合,创建真正智能的设计代理。在本文中,我将带您构建一个设计代理,展示精心设计的代理架构如何将原始AI能力转化为专业级的创意工作流程。

1、为什么要构建代理而不是直接调用API?

在深入研究架构之前,值得问一下:为什么不直接调用Nano Banana?答案在于原始AI能力与现实创意工作流程之间的差距。

直接API调用功能强大但有限:

  • 用户需要编写有效的提示词(大多数人不是提示词工程师)
  • 请求之间没有记忆或上下文
  • 没有质量控制或迭代逻辑
  • 无法与现有创意工作流程集成
  • 无法根据品牌指南或风格一致性进行定制

基于代理的方法解锁专业工作流程:

智能提示词工程:设计代理内置提示词重写逻辑,将用户请求转换为针对Nano Banana优化的提示词。用户只需说"创建节日促销",代理就会处理技术性的提示词编写。

自适应需求收集:设计代理可以动态提出相关问题,确保在开始图像生成之前拥有所有相关信息,而不是依赖人类用户记住输入所有相关详细信息。

自定义功能:诸如"深度思考模式"之类的功能——系统智能地迭代多轮,每一轮都改进前一轮(就像设计机构中的情况一样)——无法通过直接API调用实现。代理可以实现复杂的、多步骤的工作流程,自动迭代并提高内容质量。

品牌合规性:代理可以加入特定控制来检查是否符合品牌指南,确保所有生成内容的一致视觉标识。

上下文和长期记忆:与无状态的API调用不同,代理维护会话状态,允许迭代改进并在之前的工作基础上继续。代理还可以配置长期记忆,这对于用户和代理之间的长期交互非常有用。

质量保证:内置审查机制确保输出符合专业标准,然后再呈现给用户。

工作流程集成:代理可以与现有创意工具、资产管理系统和审批流程无缝集成。

结果如何?一个感觉像是在与专业创意人员合作而不是与强大但原始的AI工具合作的系统。

2、什么造就了优秀的设计代理?

构建有效的设计代理不仅仅是将AI模型连接到图像生成API。它是创建能够理解上下文、维护状态并能够根据反馈迭代的系统——就像与熟练的人类设计师合作一样。

有效设计代理的关键特征包括:

上下文感知:跨会话理解用户偏好、品牌指南和项目历史。

迭代改进:接收反馈并改进先前工作的能力,而不仅仅是从头开始生成。

工具集成:无缝访问各种创意工具和资产管理系统。

质量控制:内置审查机制确保输出符合专业标准。

有状态记忆:维护过去创作、用户偏好和正在进行的项目的知识。

3、ADK驱动的设计代理

这个设计代理采用谷歌的ADK框架构建,通过多代理架构体现这些原则。其核心设计用于处理两种不同的操作模式:

3.1 常规模式:交互式设计助手

在常规模式下,代理作为智能设计助手运行。用户可以请求他们想要的内容类型,指定纵横比和文本叠加等参数,并接收专业制作的内容。代理维护对话上下文,并允许根据用户反馈进行迭代改进。

3.2 深度思考模式:自主质量保证

我们可以通过"深度思考模式"进一步提升代理的能力——这是一种复制专业创意团队运作方式的自主工作流程。当用户请求代理"深度思考"时,代理进入复杂的内容生成、审查和 refinement 循环。

4、架构:专业代理的交响乐

ADK的美在于其对可组合多代理系统的支持。我们的设计代理实际上是几个专业子代理的编排,每个代理都有明确的职责:

4.1 主要社交媒体代理

root_agent = LlmAgent(
    name="social_media_agent",
    model="gemini-2.5-flash",
    instruction="""You are a design agent. Your goal is to help users create and iterate on creative designs for the user including but not limited to posters, social media posts, ads, infographics etc.**Deep Think Mode**: If the user says they want you to "deep think" or use any instructions along those lines, then call the deep_think_loop to perform a deeper generation process.
    **Regular Mode**: For normal requests, use the `generate_image` tool to create the first version of the image.""",
    tools=[generate_image, edit_image, list_asset_versions, list_reference_images, load_artifacts_tool],
    sub_agents=[deep_think_loop],
    before_model_callback=process_reference_images_callback
)

这个主要代理作为接口层,处理用户交互并确定是否将请求路由到深度思考循环或通过子代理委托直接处理。

4.2 内容生成代理

content_generation_agent = LlmAgent(
    name="ContentGenAgent",
    instruction="""
    You are a helpful and creative design assistant who helps to create images based on the user's requirements. 
    
    if deep_think_iteration: {deep_think_iteration} is 1, call the generate_image tool, else call the edit_image tool
    Use the below feedback (if any) given by the review agent when you draft your inputs for the edit_image tool to ensure that the content is corrected to meet the user's requirements.
      
    **Important**:
    1. when calling the generate_image or edit_image tools, be very clear and succinct with your instructions
    2. avoid vague instructions. for example "improve contrast" and "reduce font size" are vague. instead be explicit "add a black gradient background to the top of the image behind the text to increase contrast"
    3. use your creativity to figure out how the user requirements and improvement suggestions can be implemented
    Feedback from previous iterations: {content_review}
    """,
    tools=[generate_image, edit_image, load_artifacts_tool]
)

这个代理处理实际的内容创建,根据迭代上下文智能选择生成新内容或编辑现有资产,并提供详细、可操作的指令。

4.3 内容审查代理

也许最具创新性的组件是审查代理,它作为AI质量保证专家运作:

content_review_agent = LlmAgent(
    name="ContentReviewAgent",
    model="gemini-2.5-flash",
    instruction="""You are a marketing content reviewer. Your job is to evaluate generated marketing content and provide constructive feedback.Load the generated image named {last_generated_image} using load_artifacts_tool and evaluate it against the original user request and provide feedback on:
        1. **Adherence to Request**: Does the content match what the user originally asked for?
        2. **Visual Appeal**: Is the composition, colors, and overall design appealing and professional?
        3. **Obvious Issues**: Are there any clear problems like poor text readability, distorted elements, or technical issues?
        4. **Previous Feedback**: If this is a revision, has the previous feedback been properly addressed?
        5. **Typos**: Are there any misspelt words on the image?
        Original user request: {original_prompt}
        Current iteration: {iteration_count}""",
    output_schema=ContentReview,
    output_key="content_review",
    tools=[load_artifacts_tool]
)

这个代理从多个维度评估生成的内容:

  • 符合请求:内容是否符合用户的要求?
  • 视觉吸引力:构图和设计是否专业?
  • 技术质量:是否存在明显的问题或缺陷?
  • 拼写错误检测:识别叠加文本中的拼写错误
  • 反馈整合:之前的反馈是否已正确处理?

4.4 循环控制代理

loop_control_agent = LlmAgent(
    name="LoopControlAgent",
    model="gemini-2.5-flash",
    instruction="""You are responsible for determining whether the deep think content creation process should continue or conclude.**Continue Loop If:**
      - The content doesn't match the user's original request
      - There are significant visual appeal issues
      - Obvious problems or technical issues exist
      - Previous feedback hasn't been properly addressed
      - The content could be significantly improved
      **End Loop If:**
      - The content matches the user's request well
      - Visual appeal is good and professional
      - No obvious issues or problems
      - Previous feedback has been addressed
      - Only minor improvements could be made
      - Maximum iterations have been reached
      Current iteration: {iteration_count}
      Max iterations: 4
      Review feedback: {content_review}""",
    output_schema=LoopDecision,
    output_key="loop_decision",
)

这个代理智能地决定何时内容"足够好"可以呈现给用户,防止无限循环同时确保质量标准。

5、ADK技术说明

5.1 有状态上下文管理

与传统AI交互将每个请求孤立处理不同,我们的代理通过ADK的会话状态管理维护丰富的上下文:

# Store reference images with versioning
callback_context.state["reference_images"][filename] = {
    "version": ref_count,
    "uploaded_version": version
}
# Track asset versions and iteration history
tool_context.state["asset_versions"][asset_name] = version
tool_context.state["deep_think_iteration"] = iteration_count

这允许代理在之前的工作基础上继续,记住用户偏好,并在会话之间保持项目连续性。

5.2 高级回调系统

ADK的回调架构支持复杂的预处理和后处理:

async def process_reference_images_callback(
    callback_context: CallbackContext, llm_request: LlmRequest
) -> Optional[Content]:
    """A before_model_callback to process uploaded reference images.
    
    This function intercepts the request before it goes to the LLM.
    If it finds an image upload, it saves it as a reference artifact.
    """
    if not llm_request.contents:
        return None
        
    latest_user_message = llm_request.contents[-1]
    image_part = None
    
    # Look for uploaded images in the latest user message
    for part in latest_user_message.parts:
        if part.inline_data and part.inline_data.mime_type.startswith("image/"):
            logger.info(f"Found reference image to process: {part.inline_data.mime_type}")
            image_part = part
            break
    
    # Process reference image if found
    if image_part:
        # Generate versioned filename for reference image
        reference_images = callback_context.state.get("reference_images", {})
        ref_count = len(reference_images) + 1
        filename = f"reference_image_v{ref_count}.png"
        
        # Save the image as an artifact
        version = await callback_context.save_artifact(filename=filename, artifact=image_part)
        callback_context.state["reference_images"][filename] = {
            "version": ref_count,
            "uploaded_version": version
        }
        callback_context.state["latest_reference_image"] = filename
    
    return None

这些回调透明地处理复杂的工作流程,如自动参考图像处理和模式检测。

6、双模式操作:速度与质量的权衡

ADK解锁的关键能力之一,是开发者能够实现自定义行为,例如在此设计代理中实现两种不同的操作模式,让用户控制速度-质量权衡:

简单模式:直接快速交互,用户可以直接调用生成和编辑工具。非常适合快速迭代、头脑风暴或时间紧迫的情况。用户以最少的处理开销立即获得结果。

深度思考模式:自主质量保证工作流程,需要更多时间但提供明显更高质量的结果。激活后,代理进入复杂的生成、审查和 refinement 循环,可能需要几分钟但产生专业级内容。

# Deep think mode is detected through the agent's instruction processing
instruction="""**Deep Think Mode**: If the user says they want you to "deep think" 
or use any instructions along those lines, then call the deep_think_loop to perform 
a deeper generation process.**Regular Mode**: For normal requests, use the `generate_image` tool to create 
the first version of the image."""

这种设计理念认识到不同的创意场景有不同的要求。对于较简单的请求,用户可能更喜欢简单模式的速度,而更复杂的请求可能值得深度思考模式的彻底性。

7、智能工具集成

代理通过ADK的工具框架无缝集成多个工具:

  • 图像生成:使用Nano Banana根据提示词创建新内容
  • 图像编辑:对现有资产的迭代改进
  • 资产管理:版本化存储和检索
  • 参考处理:风格和构图指导

8、深度思考循环:自主创意迭代

我们系统的瑰宝是深度思考循环——一个编排自主创意迭代的LoopAgent:

deep_think_loop = LoopAgent(
    name="DeepThinkLoop",
    sub_agents=[
        DeepThinkPreparationAgent(name="DeepThinkPreparationAgent"),
        prompt_capture_agent,
        content_generation_agent,
        content_review_agent,
        loop_control_agent,
        LoopTerminationAgent(name="LoopTerminationAgent"),
    ],
    max_iterations=5,
)

这个循环创建一个反馈系统,复制专业创意工作流程:

  1. 准备:上下文收集和需求分析
  2. 生成:创建或改进内容
  3. 审查:质量评估和反馈生成
  4. 控制:关于继续的决策
  5. 终止:清理和最终呈现

9、为什么这个架构重要

这种多代理方法展示了相对于单体AI系统的几个关键优势:

专业 expertise:每个代理在其特定领域表现出色,带来更好的整体结果。

可维护的复杂性:复杂工作流程分解为可管理、可测试的组件。

灵活的编排:不同的工作流程可以根据需要重用和重新组合代理。

质量保证:内置审查流程确保专业级输出。

可扩展的开发:团队可以独立开发不同的代理。

10、在Google Cloud上用ADK构建未来

随着AI代理变得越来越复杂,拥有强大的开发框架变得至关重要。谷歌的ADK为构建这些复杂的、有状态的多代理系统提供了基础:

模型无关:通过标准化接口使用Gemini、Claude或任何其他模型。

云原生:为Google Cloud基础设施构建,无缝扩展和部署。

丰富的工具生态系统:大量预构建工具和集成库。

企业就绪:内置安全、监控和治理功能。

11、前方的路

设计代理只是复杂AI编排可能性的开始。通过将ADK的多代理能力与Gemini等高级模型相结合,我们看到AI系统不只是帮助完成任务——它们思考、迭代并产生专业质量水平的工作。

从简单的AI工具到复杂的、有状态的AI代理代表了我们处理自动化方式的根本转变。就像网络从静态页面发展到动态应用程序一样,AI正在从一次性响应发展到持久的、上下文感知的助手。

对于希望构建下一代AI驱动应用程序的开发者和组织来说,理解并拥抱这些多代理模式不仅是一种优势——它正在变得必要。未来属于能够深入思考、有效协作并产生满足人类创造力最高标准的结果的AI系统。

问题不在于AI代理是否会改变创意工作流程——而是当变革到来时,你是否准备好构建和部署它们。

想探索这个设计代理背后的代码吗?完整实现可在此GitHub仓库中获取*,展示了使用Google ADK构建复杂AI代理的实际模式。*


原文链接: Building a Design Agent with Nano Banana and Google ADK

汇智网翻译整理,资料显示出处