Skip to content

Commit cddfe91

Browse files
committed
merge test
2 parents c995965 + f9cce74 commit cddfe91

4 files changed

Lines changed: 107 additions & 9 deletions

File tree

agents/matmaster_agent/flow_agents/agent.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@
9494
StepValidationSchema,
9595
)
9696
from agents.matmaster_agent.flow_agents.thinking_agent.agent import ThinkingAgent
97+
from agents.matmaster_agent.flow_agents.thinking_agent.callback import (
98+
filter_thinking_llm_contents,
99+
)
97100
from agents.matmaster_agent.flow_agents.thinking_agent.constant import THINKING_AGENT
98101
from agents.matmaster_agent.flow_agents.utils import (
99102
check_plan,
@@ -216,6 +219,7 @@ def after_init(self):
216219
model=MatMasterLlmConfig.default_litellm_model,
217220
description='在制定计划前对工具选择与顺序进行推理',
218221
instruction='',
222+
before_model_callback=filter_thinking_llm_contents,
219223
)
220224

221225
self._execution_agent = None
@@ -242,6 +246,7 @@ def after_init(self):
242246
self.intent_agent,
243247
self.expand_agent,
244248
self.scene_agent,
249+
self.thinking_agent,
245250
self.plan_make_agent,
246251
self.analysis_agent,
247252
self.report_agent,
@@ -274,6 +279,11 @@ def expand_agent(self) -> LlmAgent:
274279
def scene_agent(self) -> LlmAgent:
275280
return self._scene_agent
276281

282+
@computed_field
283+
@property
284+
def thinking_agent(self) -> LlmAgent:
285+
return self._thinking_agent
286+
277287
@computed_field
278288
@property
279289
def plan_make_agent(self) -> LlmAgent:
@@ -607,6 +617,7 @@ async def _run_plan_make_agent(
607617
{
608618
'matmaster_thinking_ui_args': json.dumps(
609619
{
620+
'title': '已思考',
610621
'status': 'end',
611622
}
612623
)

agents/matmaster_agent/flow_agents/constant.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
from agents.matmaster_agent.flow_agents.expand_agent.constant import EXPAND_AGENT
22
from agents.matmaster_agent.flow_agents.intent_agent.constant import INTENT_AGENT
3+
from agents.matmaster_agent.flow_agents.plan_make_agent.constant import PLAN_MAKE_AGENT
34
from agents.matmaster_agent.flow_agents.scene_agent.constant import SCENE_AGENT
45

56
# Agent Constants
67
MATMASTER_SUPERVISOR_AGENT = 'matmaster_supervisor_agent'
8+
EXECUTION_SUMMARY_AGENT = 'execution_summary_agent'
9+
REPORT_AGENT = 'report_agent'
710

811
# Function-Call Constants
912
MATMASTER_FLOW = 'matmaster_flow'
@@ -21,3 +24,12 @@
2124
EXPAND_AGENT.replace('_agent', '_schema'),
2225
SCENE_AGENT,
2326
]
27+
28+
THINKING_CONTEXT_FILTER_KEYWORDS = UNIVERSAL_CONTEXT_FILTER_KEYWORDS + [
29+
PLAN_MAKE_AGENT,
30+
MATMASTER_FLOW,
31+
MATMASTER_FLOW_PLANS,
32+
MATMASTER_GENERATE_NPS,
33+
EXECUTION_SUMMARY_AGENT,
34+
REPORT_AGENT,
35+
]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import logging
2+
from typing import Optional
3+
4+
from google.adk.agents.callback_context import CallbackContext
5+
from google.adk.models import LlmRequest, LlmResponse
6+
from google.genai.types import Content, Part
7+
8+
from agents.matmaster_agent.constant import MATMASTER_AGENT_NAME, ModelRole
9+
from agents.matmaster_agent.flow_agents.constant import (
10+
THINKING_CONTEXT_FILTER_KEYWORDS,
11+
)
12+
from agents.matmaster_agent.logger import PrefixFilter
13+
from agents.matmaster_agent.utils.context_utils import is_content_has_keywords
14+
15+
logger = logging.getLogger(__name__)
16+
logger.addFilter(PrefixFilter(MATMASTER_AGENT_NAME))
17+
logger.setLevel(logging.INFO)
18+
19+
20+
async def filter_thinking_llm_contents(
21+
callback_context: CallbackContext, llm_request: LlmRequest
22+
) -> Optional[LlmResponse]:
23+
contents = []
24+
for content in llm_request.contents[::-1]:
25+
if is_content_has_keywords(
26+
content,
27+
THINKING_CONTEXT_FILTER_KEYWORDS,
28+
):
29+
continue
30+
contents.insert(0, content)
31+
32+
logger.info(
33+
f'{callback_context.session.id} {callback_context.agent_name} contents = {len(contents)}'
34+
)
35+
36+
if not contents:
37+
contents = [
38+
Content(
39+
role=ModelRole,
40+
parts=[Part(text='Default Text')],
41+
)
42+
]
43+
44+
llm_request.contents = contents

agents/matmaster_agent/flow_agents/thinking_agent/prompt.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,49 @@ def get_static_system_block(available_tools_with_info: str) -> str:
3535
If <Session Memory> is provided: use it only to align tool choice and parameters (e.g. preferred methods, previously chosen parameters). Do not reason about "whether to fix" past errors—only use memory to constrain the next tool chain.
3636
</phase_1_analysis>
3737
38+
<phase_1b_strategy_brainstorming>
39+
Before drafting the plan, brainstorm at least 3 distinct strategic paradigms to ensure solution diversity:
40+
- **Construction-Led Paradigm**: Prioritize building the structure using `build_` tools.
41+
- **Retrieval-Led Paradigm**: Prioritize fetching existing structures from high-fidelity databases.
42+
- **Input-Respecting/Expert Paradigm**: (If user files are provided) Execute based strictly on user-uploaded scripts and parameters.
43+
Evaluate the pros and cons of each for this specific query.
44+
</phase_1b_strategy_brainstorming>
45+
3846
<phase_2_drafting>
39-
Propose a preliminary tool sequence from the Available Tools list.
47+
For **each** paradigm from phase_1b (Construction-Led, Retrieval-Led, Input-Respecting/Expert), propose a preliminary tool sequence from the Available Tools list. If a paradigm is inapplicable to this query, state why and skip that paradigm's draft.
4048
Select tools based STRICTLY on their "What it does" and "Prerequisites / Inputs" descriptions.
4149
</phase_2_drafting>
4250
4351
<phase_3_simulation_and_verification>
44-
CRITICAL — Mental Simulation (Dry Run): Before finalizing the plan, you MUST perform a type-checking simulation.
52+
CRITICAL — Mental Simulation (Dry Run): You MUST run the type-checking simulation **for each paradigm's proposed plan**. Do not validate only one paradigm; every paradigm that received a draft in phase_2 must be verified.
4553
46-
1. **Virtual File System Check**: For every proposed step, verify whether the *specific* input file type required by that step exists in the *current* virtual file system at that point in the sequence.
54+
For **each** paradigm you drafted:
55+
1. **Virtual File System Check**: For every proposed step of that paradigm's plan, verify whether the *specific* input file type required by that step exists in the *current* virtual file system at that point in the sequence.
4756
- Example: Step 3 requires `slab_structure.cif`. After Step 1 and Step 2, does the virtual state contain a slab/structure file, or only a bulk CIF / chemical formula?
4857
- Output explicitly: "Step N requires Input Type A. Current State provides Type B. [PASS if A matches B; otherwise: Mismatch -> Insert Converter Tool.]"
4958
50-
2. **Mandatory Dependency Constraints** (you MUST obey these):
59+
2. **Mandatory Dependency Constraints** (you MUST obey these for each paradigm's plan):
5160
- If a tool requires a **Surface/Slab** (e.g., slab structure, surface model) and you only have a **Bulk** structure or a **chemical formula**, you MUST insert the `surface_cutting` or equivalent tool first (to generate the slab from bulk), or insert `structure_generation` first if you only have a formula.
5261
- If a tool requires a **structure file** (e.g., CIF, VASP) and you only have **SMILES**, you MUST insert a step that produces structure from SMILES (e.g. `smiles_to_structure` / build_molecule_structures_from_smiles) before that step.
5362
- If a tool requires a **bulk structure** and you only have a formula or composition, you MUST insert a `structure_generation` (or crystal structure generation) step first.
5463
55-
3. **Output Format for Each Step** (inside your <simulation> tag):
56-
For each step N, state exactly:
64+
3. **Output Format for Each Step, Per Paradigm** (inside your <simulation> tag):
65+
For each paradigm, then for each step N of that paradigm's plan, state exactly:
5766
- "Step N requires Input Type A. Current State provides Type B. [PASS / FAIL]. [If FAIL: Insert Converter Tool.]"
5867
You MUST give a definite verdict: use only PASS or FAIL. Do not use hedging (e.g. "可能", "perhaps", "maybe").
5968
No step may be proposed without its inputs being available (Backward Chaining).
6069
</phase_3_simulation_and_verification>
6170
6271
<phase_4_final_plan>
63-
Output the validated linear sequence of steps.
72+
Output the validated linear sequence of steps **for every paradigm** you drafted and simulated. You MUST list each paradigm's full plan (not only the chosen one). After listing all plans, state which paradigm you recommend and briefly justify. Every plan must be visible in the output.
6473
</phase_4_final_plan>
6574
</thinking_protocol>
6675
6776
### CONSTRAINTS
77+
- **Capability-Driven Build Priority**: You should prioritize using `build_` tools to generate structures. However, you MUST explicitly state whether the tool's capability boundary (as defined in its description/templates) covers the user's specific requirement.
78+
* IF the material is a common lattice or fits the tool's listed templates: USE the build tool.
79+
* IF the material is a specific named complex structure (e.g., a specific MOF, Zeolite, or complex organic crystal) NOT explicitly listed in the tool's templates: You MUST conclude the build tool is insufficient and fallback to **Database Retrieval** tools.
80+
- **Build vs Fetch vs Generate**: When building structures, if the case does NOT meet the tool's template standard, decisively abandon the `build_` path and use `fetch_` (database retrieval) instead. Do NOT abuse `generate_`-prefixed tools—use them only when the user explicitly requests conditional or generative structure creation; otherwise prefer `build_` (when templates fit) or `fetch_`.
6881
- **Grounding**: Use ONLY tools listed in `<Available Tools With Info>`. Do not hallucinate tools.
6982
- **Atomic check**: If a step requires `structure_file` and you only have `SMILES`, you MUST insert a step that produces structure from SMILES.
7083
- **Surface/Slab dependency**: If a tool requires a Surface/Slab (e.g., slab structure, surface model), and you only have a Bulk structure or a chemical formula, you MUST insert the `surface_cutting` or `structure_generation` tool first. Do not propose surface/slab steps without the prerequisite bulk or structure.
@@ -81,14 +94,32 @@ def get_static_system_block(available_tools_with_info: str) -> str:
8194
8295
### OUTPUT FORMAT (use these tags)
8396
<analysis>...</analysis>
97+
<strategy_brainstorming>
98+
1. Paradigm A: [Rationale]
99+
2. Paradigm B: [Rationale]
100+
3. Paradigm C: [Rationale]
101+
</strategy_brainstorming>
84102
<simulation>
103+
**Paradigm A (e.g. Construction-Led):**
85104
Step 1: [Tool Name]. Step 1 requires Input Type [A]. Current State provides [B]. [PASS/FAIL]. [If FAIL: Insert Converter Tool.]
86-
Step 2: [Tool Name]. Step 2 requires Input Type [A]. Current State provides [B]. [PASS/FAIL]. [If FAIL: Insert Converter Tool.]
87-
...
105+
Step 2: ...
106+
**Paradigm B (e.g. Retrieval-Led):**
107+
Step 1: ...
108+
Step 2: ...
109+
**Paradigm C (e.g. Input-Respecting):** [or "Skipped: [reason]"]
110+
Step 1: ...
88111
</simulation>
89112
<plan_proposal>
113+
**Paradigm A (e.g. Construction-Led):**
114+
1. [Tool Name]: [Brief Description]
115+
2. ...
116+
**Paradigm B (e.g. Retrieval-Led):**
90117
1. [Tool Name]: [Brief Description]
91118
2. ...
119+
**Paradigm C (e.g. Input-Respecting):** [or "Skipped: [reason]"]
120+
1. ...
121+
---
122+
Recommended paradigm: [A/B/C]. Justification: [Brief reason.]
92123
</plan_proposal>
93124
Revision needed
94125
"""

0 commit comments

Comments
 (0)