from google.adk.agents.parallel_agent import ParallelAgent
from google.adk.agents.llm_agent import LlmAgent
from google.adk.agents.sequential_agent import SequentialAgent
from google.adk.tools import google_search
# --- 常量 ---GEMINI_MODEL ="gemini-2.5-flash"# --- 1. 定义研究子 Agent(并行运行) ---# 研究员 1:可再生能源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],
# 将结果存储在状态中以供合并 Agent 使用 output_key="renewable_energy_result")
# 研究员 2:电动汽车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],
# 将结果存储在状态中以供合并 Agent 使用 output_key="ev_technology_result")
# 研究员 3:碳捕获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],
# 将结果存储在状态中以供合并 Agent 使用 output_key="carbon_capture_result")
# --- 2. 创建并行 Agent(同时运行研究员) ---# 该 Agent 协调研究员的并发执行。# 当所有研究员完成并将结果存储在状态中时,它结束。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. 定义合并 Agent(在并行 Agent 之后运行) ---# 该 Agent 获取并行 Agent 存储在会话状态中的结果# 并将其综合为一个带有归属的单一结构化响应。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:**
## 最近可持续技术进展总结
### 可再生能源研究结果
(Based on RenewableEnergyResearcher's findings)
[Synthesize and elaborate *only* on the renewable energy input summary provided above.]
### 电动汽车研究结果
(Based on EVResearcher's findings)
[Synthesize and elaborate *only* on the EV input summary provided above.]
### 碳捕获研究结果
(Based on CarbonCaptureResearcher's findings)
[Synthesize and elaborate *only* on the carbon capture input summary provided above.]
### 综合结论
[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.",
# 合并不需要工具# 这里不需要 output_key,因为它的直接响应是序列的最终输出)
# --- 4. 创建顺序 Agent(协调整体流程) ---# 这是要运行的主要 Agent。它首先执行并行 Agent# 以填充状态,然后执行合并 Agent 以生成最终输出。sequential_pipeline_agent = SequentialAgent(
name="ResearchAndSynthesisPipeline",
# 先运行并行研究,然后再合并 sub_agents=[parallel_research_agent, merger_agent],
description="Coordinates parallel research and synthesizes the results.")
root_agent = sequential_pipeline_agent
go116 行展开收起expand_more
model, err:=gemini.NewModel(ctx, modelName, &genai.ClientConfig{})
iferr!=nil {
returnfmt.Errorf("failed to create model: %v", err)
}
// --- 1. 定义研究子 Agent(并行运行) ---researcher1, err:=llmagent.New(llmagent.Config{
Name: "RenewableEnergyResearcher",
Model: 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.",
OutputKey: "renewable_energy_result",
})
iferr!=nil {
returnerr }
researcher2, err:=llmagent.New(llmagent.Config{
Name: "EVResearcher",
Model: 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.",
OutputKey: "ev_technology_result",
})
iferr!=nil {
returnerr }
researcher3, err:=llmagent.New(llmagent.Config{
Name: "CarbonCaptureResearcher",
Model: 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.",
OutputKey: "carbon_capture_result",
})
iferr!=nil {
returnerr }
// --- 2. 创建并行 Agent(同时运行研究员) ---parallelResearchAgent, err:=parallelagent.New(parallelagent.Config{
AgentConfig: agent.Config{
Name: "ParallelWebResearchAgent",
Description: "Runs multiple research agents in parallel to gather information.",
SubAgents: []agent.Agent{researcher1, researcher2, researcher3},
},
})
iferr!=nil {
returnfmt.Errorf("failed to create parallel agent: %v", err)
}
// --- 3. 定义合并 Agent(在并行 Agent 之后运行) ---synthesisAgent, err:=llmagent.New(llmagent.Config{
Name: "SynthesisAgent",
Model: model,
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.",
})
iferr!=nil {
returnfmt.Errorf("failed to create synthesis agent: %v", err)
}
// --- 4. 创建顺序 Agent(协调整体流程) ---pipeline, err:=sequentialagent.New(sequentialagent.Config{
AgentConfig: agent.Config{
Name: "ResearchAndSynthesisPipeline",
Description: "Coordinates parallel research and synthesizes the results.",
SubAgents: []agent.Agent{parallelResearchAgent, synthesisAgent},
},
})
iferr!=nil {
returnfmt.Errorf("failed to create sequential agent pipeline: %v", err)
}