|
| 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