Skip to content

Commit a9dbcba

Browse files
committed
Merge branch 'main' of https://github.com/beform88/MatMaster into retry/tool-level
2 parents d8bf9d7 + 08d8891 commit a9dbcba

11 files changed

Lines changed: 114 additions & 51 deletions

File tree

agents/matmaster_agent/core_agents/comp_agents/recommend_summary_agent/agent.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
from agents.matmaster_agent.core_agents.comp_agents.recommend_summary_agent.recommend_params_agent.schema import (
3838
create_tool_args_schema,
3939
)
40+
from agents.matmaster_agent.core_agents.comp_agents.recommend_summary_agent.subagent_summary_agent.callback import (
41+
filter_summary_llm_contents,
42+
)
4043
from agents.matmaster_agent.core_agents.comp_agents.recommend_summary_agent.subagent_summary_agent.prompt import (
4144
get_subagent_summary_prompt,
4245
)
@@ -120,6 +123,7 @@ def _after_init(self):
120123
description=self.description,
121124
global_instruction=GLOBAL_INSTRUCTION,
122125
instruction=self.instruction,
126+
before_model_callback=filter_summary_llm_contents,
123127
)
124128
else:
125129
self._summary_agent = DisallowTransferAndContentLimitLlmAgent(
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
MATMASTER_FLOW,
11+
MATMASTER_FLOW_PLANS,
12+
MATMASTER_GENERATE_NPS,
13+
UNIVERSAL_CONTEXT_FILTER_KEYWORDS,
14+
)
15+
from agents.matmaster_agent.flow_agents.plan_info_agent.constant import PLAN_INFO_AGENT
16+
from agents.matmaster_agent.flow_agents.plan_make_agent.constant import PLAN_MAKE_AGENT
17+
from agents.matmaster_agent.logger import PrefixFilter
18+
from agents.matmaster_agent.utils.context_utils import is_content_has_keywords
19+
20+
logger = logging.getLogger(__name__)
21+
logger.addFilter(PrefixFilter(MATMASTER_AGENT_NAME))
22+
logger.setLevel(logging.INFO)
23+
24+
25+
async def filter_summary_llm_contents(
26+
callback_context: CallbackContext, llm_request: LlmRequest
27+
) -> Optional[LlmResponse]:
28+
contents = []
29+
for content in llm_request.contents[::-1]:
30+
if is_content_has_keywords(
31+
content,
32+
UNIVERSAL_CONTEXT_FILTER_KEYWORDS
33+
+ [PLAN_MAKE_AGENT, PLAN_INFO_AGENT]
34+
+ [MATMASTER_FLOW, MATMASTER_FLOW_PLANS, MATMASTER_GENERATE_NPS],
35+
):
36+
continue
37+
else:
38+
contents.insert(0, content)
39+
40+
logger.info(
41+
f'{callback_context.session.id} {callback_context.agent_name} contents = {contents}'
42+
)
43+
44+
if not contents:
45+
contents = [
46+
Content(
47+
role=ModelRole,
48+
parts=[Part(text='Default Text')],
49+
)
50+
]
51+
52+
llm_request.contents = contents

agents/matmaster_agent/flow_agents/agent.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232
ChatAgentGlobalInstruction,
3333
ChatAgentInstruction,
3434
)
35-
from agents.matmaster_agent.flow_agents.constant import MATMASTER_FLOW
35+
from agents.matmaster_agent.flow_agents.constant import (
36+
MATMASTER_FLOW,
37+
MATMASTER_FLOW_PLANS,
38+
MATMASTER_GENERATE_NPS,
39+
)
3640
from agents.matmaster_agent.flow_agents.execution_agent.agent import (
3741
MatMasterSupervisorAgent,
3842
)
@@ -59,6 +63,7 @@
5963
from agents.matmaster_agent.flow_agents.plan_info_agent.callback import (
6064
filter_plan_info_llm_contents,
6165
)
66+
from agents.matmaster_agent.flow_agents.plan_info_agent.constant import PLAN_INFO_AGENT
6267
from agents.matmaster_agent.flow_agents.plan_info_agent.prompt import (
6368
PLAN_INFO_INSTRUCTION,
6469
)
@@ -67,6 +72,7 @@
6772
from agents.matmaster_agent.flow_agents.plan_make_agent.callback import (
6873
filter_plan_make_llm_contents,
6974
)
75+
from agents.matmaster_agent.flow_agents.plan_make_agent.constant import PLAN_MAKE_AGENT
7076
from agents.matmaster_agent.flow_agents.plan_make_agent.prompt import (
7177
get_plan_make_instruction,
7278
)
@@ -180,7 +186,7 @@ def after_init(self):
180186
)
181187

182188
self._plan_make_agent = PlanMakeAgent(
183-
name='plan_make_agent',
189+
name=PLAN_MAKE_AGENT,
184190
model=MatMasterLlmConfig.tool_schema_model,
185191
description='根据用户的问题依据现有工具执行计划,如果没有工具可用,告知用户,不要自己制造工具或幻想',
186192
state_key=MULTI_PLANS,
@@ -197,7 +203,7 @@ def after_init(self):
197203
)
198204

199205
self._plan_info_agent = DisallowTransferAndContentLimitSchemaAgent(
200-
name='plan_info_agent',
206+
name=PLAN_INFO_AGENT,
201207
model=MatMasterLlmConfig.tool_schema_model,
202208
global_instruction=GLOBAL_INSTRUCTION,
203209
description='根据 materials_plan 返回的计划进行总结',
@@ -470,7 +476,7 @@ async def _run_plan_make_agent(
470476
for matmaster_flow_plans_event in context_function_event(
471477
ctx,
472478
self.name,
473-
'matmaster_flow_plans',
479+
MATMASTER_FLOW_PLANS,
474480
None,
475481
ModelRole,
476482
{
@@ -584,13 +590,8 @@ async def _run_plan_execute_and_summary_agent(
584590
# Collect report Markdown
585591
report_markdown = ''
586592
async for report_event in self.report_agent.run_async(ctx):
587-
current_text = is_text(report_event)
588-
if not current_text:
589-
continue
590-
591-
report_markdown = current_text
592-
if not report_event.partial:
593-
break
593+
if (cur_text := is_text(report_event)) and not report_event.partial:
594+
report_markdown += cur_text
594595

595596
# matmaster_report_md.md
596597
upload_result = await upload_report_md_to_oss(
@@ -756,7 +757,7 @@ async def _run_async_impl(
756757
for generate_nps_event in context_function_event(
757758
ctx,
758759
self.name,
759-
'matmaster_generate_nps',
760+
MATMASTER_GENERATE_NPS,
760761
{},
761762
ModelRole,
762763
{'session_id': ctx.session.id, 'invocation_id': ctx.invocation_id},
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,21 @@
1+
from agents.matmaster_agent.flow_agents.expand_agent.constant import EXPAND_AGENT
2+
from agents.matmaster_agent.flow_agents.intent_agent.constant import INTENT_AGENT
3+
from agents.matmaster_agent.flow_agents.plan_confirm_agent.constant import (
4+
PLAN_CONFIRM_AGENT,
5+
)
6+
from agents.matmaster_agent.flow_agents.scene_agent.constant import SCENE_AGENT
7+
8+
# Agent Constants
19
MATMASTER_SUPERVISOR_AGENT = 'matmaster_supervisor_agent'
10+
11+
# Function-Call Constants
212
MATMASTER_FLOW = 'matmaster_flow'
13+
MATMASTER_FLOW_PLANS = 'matmaster_flow_plans'
14+
MATMASTER_GENERATE_NPS = 'matmaster_generate_nps'
15+
16+
UNIVERSAL_CONTEXT_FILTER_KEYWORDS = [
17+
INTENT_AGENT,
18+
EXPAND_AGENT.replace('_agent', '_schema'),
19+
SCENE_AGENT,
20+
PLAN_CONFIRM_AGENT,
21+
]

agents/matmaster_agent/flow_agents/plan_info_agent/callback.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66
from google.genai.types import Content, Part
77

88
from agents.matmaster_agent.constant import MATMASTER_AGENT_NAME, ModelRole
9-
from agents.matmaster_agent.flow_agents.constant import MATMASTER_FLOW
10-
from agents.matmaster_agent.flow_agents.expand_agent.constant import EXPAND_AGENT
11-
from agents.matmaster_agent.flow_agents.intent_agent.constant import INTENT_AGENT
12-
from agents.matmaster_agent.flow_agents.plan_confirm_agent.constant import (
13-
PLAN_CONFIRM_AGENT,
9+
from agents.matmaster_agent.flow_agents.constant import (
10+
MATMASTER_FLOW,
11+
UNIVERSAL_CONTEXT_FILTER_KEYWORDS,
1412
)
15-
from agents.matmaster_agent.flow_agents.scene_agent.constant import SCENE_AGENT
16-
from agents.matmaster_agent.flow_agents.utils import is_content_has_keywords
1713
from agents.matmaster_agent.logger import PrefixFilter
14+
from agents.matmaster_agent.utils.context_utils import is_content_has_keywords
1815

1916
logger = logging.getLogger(__name__)
2017
logger.addFilter(PrefixFilter(MATMASTER_AGENT_NAME))
@@ -28,13 +25,7 @@ async def filter_plan_info_llm_contents(
2825
for content in llm_request.contents[::-1]:
2926
if is_content_has_keywords(
3027
content,
31-
[
32-
PLAN_CONFIRM_AGENT,
33-
SCENE_AGENT,
34-
INTENT_AGENT,
35-
EXPAND_AGENT.replace('_agent', '_schema'),
36-
MATMASTER_FLOW,
37-
],
28+
UNIVERSAL_CONTEXT_FILTER_KEYWORDS + [MATMASTER_FLOW],
3829
):
3930
continue
4031
else:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PLAN_INFO_AGENT = 'plan_info_agent'

agents/matmaster_agent/flow_agents/plan_make_agent/callback.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66
from google.genai.types import Content, Part
77

88
from agents.matmaster_agent.constant import MATMASTER_AGENT_NAME, ModelRole
9-
from agents.matmaster_agent.flow_agents.expand_agent.constant import EXPAND_AGENT
10-
from agents.matmaster_agent.flow_agents.intent_agent.constant import INTENT_AGENT
11-
from agents.matmaster_agent.flow_agents.plan_confirm_agent.constant import (
12-
PLAN_CONFIRM_AGENT,
9+
from agents.matmaster_agent.flow_agents.constant import (
10+
UNIVERSAL_CONTEXT_FILTER_KEYWORDS,
1311
)
14-
from agents.matmaster_agent.flow_agents.scene_agent.constant import SCENE_AGENT
15-
from agents.matmaster_agent.flow_agents.utils import is_content_has_keywords
1612
from agents.matmaster_agent.logger import PrefixFilter
13+
from agents.matmaster_agent.utils.context_utils import is_content_has_keywords
1714

1815
logger = logging.getLogger(__name__)
1916
logger.addFilter(PrefixFilter(MATMASTER_AGENT_NAME))
@@ -27,12 +24,7 @@ async def filter_plan_make_llm_contents(
2724
for content in llm_request.contents[::-1]:
2825
if is_content_has_keywords(
2926
content,
30-
[
31-
PLAN_CONFIRM_AGENT,
32-
SCENE_AGENT,
33-
INTENT_AGENT,
34-
EXPAND_AGENT.replace('_agent', '_schema'),
35-
],
27+
UNIVERSAL_CONTEXT_FILTER_KEYWORDS,
3628
):
3729
continue
3830
else:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PLAN_MAKE_AGENT = 'plan_make_agent'

agents/matmaster_agent/flow_agents/utils.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from typing import List
33

44
from google.adk.agents import InvocationContext
5-
from google.genai.types import Content
65

76
from agents.matmaster_agent.flow_agents.model import PlanStepStatusEnum
87
from agents.matmaster_agent.flow_agents.scene_agent.model import SceneEnum
@@ -130,13 +129,3 @@ def has_self_check(current_tool_name: str) -> bool:
130129
if not tool:
131130
return False
132131
return tool.get('self_check', False)
133-
134-
135-
def is_content_has_keywords(content: Content, keywords: List[str]) -> bool:
136-
tokens = [f'[{k}]' if k.endswith('agent') else f'`{k}`' for k in keywords]
137-
138-
return any(
139-
isinstance((text := getattr(part, 'text', None)), str)
140-
and any(token in text for token in tokens)
141-
for part in content.parts
142-
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import List
2+
3+
from google.genai.types import Content
4+
5+
6+
def is_content_has_keywords(content: Content, keywords: List[str]) -> bool:
7+
tokens = [f'[{k}]' if k.endswith('agent') else f'`{k}`' for k in keywords]
8+
9+
return any(
10+
isinstance((text := getattr(part, 'text', None)), str)
11+
and any(token in text for token in tokens)
12+
for part in content.parts
13+
)

0 commit comments

Comments
 (0)