当前位置: 首页 > news >正文

【LLM】Agents

Agents

智能体是一个独立的执行单元,旨在自主行动以实现特定目标。智能体可以执行任务、与用户互动、使用外部工具以及与其他智能体协调。
基于BaseAgent扩展成LlmAgent、WorkflowAgent、CustomAgent

LLM agents

Learn more about LLM Agents…

code_writer_agent = LlmAgent( name="CodeWriterAgent", model=GEMINI_MODEL, # Change 3: Improved instruction instruction="""You are a Python Code Generator. Based *only* on the user's request, write Python code that fulfills the requirement. Output *only* the complete Python code block, enclosed in triple backticks (```python ... ```). Do not add any other text before or after the code block. """, description="Writes initial Python code based on a specification.", output_key="generated_code" # Stores output in state['generated_code'] )

Workflow agents

Learn more about Workflow Agents…

Sequential agents
code_pipeline_agent = SequentialAgent( name="CodePipelineAgent", sub_agents=[code_writer_agent, code_reviewer_agent, code_refactorer_agent], description="Executes a sequence of code writing, reviewing, and refactoring.", # The agents will run in the order provided: Writer -> Reviewer -> Refactorer )
Loop agents
# Part of agent.py --> Follow https://google.github.io/adk-docs/get-started/quickstart/ to learn the setup import asyncio import os from google.adk.agents import LoopAgent, LlmAgent, BaseAgent, SequentialAgent from google.genai import types from google.adk.runners import InMemoryRunner from google.adk.agents.invocation_context import InvocationContext from google.adk.tools.tool_context import ToolContext from typing import AsyncGenerator, Optional from google.adk.events import Event, EventActions # --- Constants --- APP_NAME = "doc_writing_app_v3" # New App Name USER_ID = "dev_user_01" SESSION_ID_BASE = "loop_exit_tool_session" # New Base Session ID GEMINI_MODEL = "gemini-2.0-flash" STATE_INITIAL_TOPIC = "initial_topic" # --- State Keys --- STATE_CURRENT_DOC = "current_document" STATE_CRITICISM = "criticism" # Define the exact phrase the Critic should use to signal completion COMPLETION_PHRASE = "No major issues found." # --- Tool Definition --- def exit_loop(tool_context: ToolContext): """Call this function ONLY when the critique indicates no further changes are needed, signaling the iterative process should end.""" print(f" [Tool Call] exit_loop triggered by {tool_context.agent_name}") tool_context.actions.escalate = True # Return empty dict as tools should typically return JSON-serializable output return {} # --- Agent Definitions --- # STEP 1: Initial Writer Agent (Runs ONCE at the beginning) initial_writer_agent = LlmAgent( name="InitialWriterAgent", model=GEMINI_MODEL, include_contents='none', # MODIFIED Instruction: Ask for a slightly more developed start instruction=f"""You are a Creative Writing Assistant tasked with starting a story. Write the *first draft* of a short story (aim for 2-4 sentences). Base the content *only* on the topic provided below. Try to introduce a specific element (like a character, a setting detail, or a starting action) to make it engaging. Topic: {{initial_topic}} Output *only* the story/document text. Do not add introductions or explanations. """, description="Writes the initial document draft based on the topic, aiming for some initial substance.", output_key=STATE_CURRENT_DOC ) # STEP 2a: Critic Agent (Inside the Refinement Loop) critic_agent_in_loop = LlmAgent( name="CriticAgent", model=GEMINI_MODEL, include_contents='none', # MODIFIED Instruction: More nuanced completion criteria, look for clear improvement paths. instruction=f"""You are a Constructive Critic AI reviewing a short document draft (typically 2-6 sentences). Your goal is balanced feedback. **Document to Review:** ``` {{current_document}} ``` **Task:** Review the document for clarity, engagement, and basic coherence according to the initial topic (if known). IF you identify 1-2 *clear and actionable* ways the document could be improved to better capture the topic or enhance reader engagement (e.g., "Needs a stronger opening sentence", "Clarify the character's goal"): Provide these specific suggestions concisely. Output *only* the critique text. ELSE IF the document is coherent, addresses the topic adequately for its length, and has no glaring errors or obvious omissions: Respond *exactly* with the phrase "{COMPLETION_PHRASE}" and nothing else. It doesn't need to be perfect, just functionally complete for this stage. Avoid suggesting purely subjective stylistic preferences if the core is sound. Do not add explanations. Output only the critique OR the exact completion phrase. """, description="Reviews the current draft, providing critique if clear improvements are needed, otherwise signals completion.", output_key=STATE_CRITICISM ) # STEP 2b: Refiner/Exiter Agent (Inside the Refinement Loop) refiner_agent_in_loop = LlmAgent( name="RefinerAgent", model=GEMINI_MODEL, # Relies solely on state via placeholders include_contents='none', instruction=f"""You are a Creative Writing Assistant refining a document based on feedback OR exiting the process. **Current Document:** ``` {{current_document}} ``` **Critique/Suggestions:** {{criticism}} **Task:** Analyze the 'Critique/Suggestions'. IF the critique is *exactly* "{COMPLETION_PHRASE}": You MUST call the 'exit_loop' function. Do not output any text. ELSE (the critique contains actionable feedback): Carefully apply the suggestions to improve the 'Current Document'. Output *only* the refined document text. Do not add explanations. Either output the refined document OR call the exit_loop function. """, description="Refines the document based on critique, or calls exit_loop if critique indicates completion.", tools=[exit_loop], # Provide the exit_loop tool output_key=STATE_CURRENT_DOC # Overwrites state['current_document'] with the refined version ) # STEP 2: Refinement Loop Agent refinement_loop = LoopAgent( name="RefinementLoop", # Agent order is crucial: Critique first, then Refine/Exit sub_agents=[ critic_agent_in_loop, refiner_agent_in_loop, ], max_iterations=5 # Limit loops ) # STEP 3: Overall Sequential Pipeline # For ADK tools compatibility, the root agent must be named `root_agent` root_agent = SequentialAgent( name="IterativeWritingPipeline", sub_agents=[ initial_writer_agent, # Run first to create initial doc refinement_loop # Then run the critique/refine loop ], description="Writes an initial document and then iteratively refines it with critique using an exit tool." )
Parallel agents

Custom agents

Multi-agent systems

http://www.jsqmd.com/news/1242793/

相关文章:

  • Fusion Children API:构建复杂UI的简单方法
  • 鸿蒙 ArkTS 实战:Plant Watering Calc 从浇水量计算器到绿植养护应用完整解析
  • 2026年7月最新全国耐磨钢球生产厂家采购选型指南:优质耐磨材料服务商FAQ - 安互工业信息
  • 革命性本地语音助手local-talking-llm:完全离线打造你的专属AI语音交互系统
  • 2026 年可自我隐藏的安全 U 盘 Phantomdrive 诞生,多种加密技术保障数据安全!
  • RTEMS多任务调度机制深度解析:为什么选择这个开源RTOS
  • 深入解析McBSP数据收发机制:从三级流水线到时序配置实战
  • Jellium Desktop字幕描边设置:调整描边宽度与颜色的完整指南
  • 打造高效条码扫描器:RSBarcodes_Swift读者模式深度配置指南
  • Linux 环境下 Apache HTTP Server (httpd) 的安装、配置与基础使用指南
  • 【万字文档+源码】基于springboot+vue小区停车位管理系统-可用于毕设-课程设计-练手学习-学习资料分享
  • TI处理器PLL寄存器深度解析:从原理到配置实战
  • 庆阳公务员培训机构TOP5排名:2025年口碑实力榜深度解析
  • SoC时钟与复位管理深度解析:从DPLL配置到PRCM复位序列实战
  • ChatGPT Images 2.0 以假乱真,C2PA 和 SynthID 技术能否守护视觉信任?
  • 骑行戴什么耳机合适?2026最适合骑行戴的运动耳机合集来了,码住
  • 07 LiveCharts图表
  • RTEMS中断处理机制:如何实现高响应性的嵌入式系统
  • rofi-pass 与 wtype/wl-clipboard:Wayland 环境下的最佳配置实践
  • 如何用FlyoverKit打造电影级地图动画:5个实用技巧与示例
  • 深入解析嵌入式SoC ROM引导机制:从原理到调试实践
  • Calculus题目生成原理:MathGenerator如何模拟高等数学问题?
  • 内容创作 Prompt 的版本进化:从一句话到多层约束的迭代历程
  • 2026年7月GitHub热榜深度解析:AI Agent与MCP协议如何重塑开发范式
  • Silk Guardian:终极USB防取证工具,让你的数据安全无忧
  • aws2tf革命性优化:86.5%代码缩减背后的处理系统原理揭秘
  • AMD推出Agent Computer概念,PC与AC未来会走向共存吗?
  • InspectiveC核心功能详解:从对象监视到方法拦截的完整调试方案
  • Docker部署PyPtt应用:简单几步实现PTT机器人容器化
  • 绝啦!7 月和平区黄金回收门店盘点,称重透明,回款速度拉满 - 逸程奢侈品回收中心