Skip to content

Commit cde7a9a

Browse files
feat: add content filter for thinking agent
1 parent 374336f commit cde7a9a

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

agents/matmaster_agent/flow_agents/agent.py

Lines changed: 4 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

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

0 commit comments

Comments
 (0)