An LLM-based event summarizer for sliding window compaction. This class is responsible for summarizing a provided list of events into a single compacted event. It is designed to be used as part of a sliding window compaction process. The actual logic for determining *when* to trigger compa
| 27 | |
| 28 | |
| 29 | class LlmEventSummarizer(BaseEventsSummarizer): |
| 30 | """An LLM-based event summarizer for sliding window compaction. |
| 31 | |
| 32 | This class is responsible for summarizing a provided list of events into a |
| 33 | single compacted event. It is designed to be used as part of a sliding window |
| 34 | compaction process. |
| 35 | |
| 36 | The actual logic for determining *when* to trigger compaction and *which* |
| 37 | events form the sliding window (based on parameters like |
| 38 | `compaction_invocation_threshold` and `overlap_size` from |
| 39 | `EventsCompactionConfig`) is handled by an external component, such as an ADK |
| 40 | "Runner". This compactor focuses solely on generating a summary of the events |
| 41 | it receives. |
| 42 | |
| 43 | When `maybe_compact_events` is called with a list of events, this class |
| 44 | formats the events, generates a summary using an LLM, and returns a new |
| 45 | `Event` containing the summary within an `EventCompaction`. |
| 46 | """ |
| 47 | |
| 48 | _DEFAULT_PROMPT_TEMPLATE = ( |
| 49 | 'The following is a conversation history between a user and an AI agent.' |
| 50 | ' It may or may not start from a compacted history. Please identify and' |
| 51 | ' reiterate the user request, summarize the context so far, focusing on' |
| 52 | ' key decisions made and information obtained, as well as any unresolved' |
| 53 | ' questions or tasks. The summary should be concise and capture the' |
| 54 | ' essence of the interaction.\n\n{conversation_history}' |
| 55 | ) |
| 56 | |
| 57 | # Tool call args and responses can be large (e.g. search results). Cap how |
| 58 | # much of each is rendered so compaction does not inflate the very context |
| 59 | # it exists to shrink. |
| 60 | _MAX_TOOL_CONTENT_CHARS = 2000 |
| 61 | |
| 62 | def __init__( |
| 63 | self, |
| 64 | llm: BaseLlm, |
| 65 | prompt_template: Optional[str] = None, |
| 66 | ): |
| 67 | """Initializes the LlmEventSummarizer. |
| 68 | |
| 69 | Args: |
| 70 | llm: The LLM used for summarization. |
| 71 | prompt_template: An optional template string for the summarization |
| 72 | prompt. If not provided, a default template will be used. The template |
| 73 | should contain a '{conversation_history}' placeholder. |
| 74 | """ |
| 75 | self._llm = llm |
| 76 | self._prompt_template = prompt_template or self._DEFAULT_PROMPT_TEMPLATE |
| 77 | |
| 78 | def _format_events_for_prompt(self, events: list[Event]) -> str: |
| 79 | """Formats events into prompt text, including thoughts and tool calls. |
| 80 | |
| 81 | Thoughts carry the agent's analysis of tool responses, and tool calls and |
| 82 | responses carry the evidence retrieved so far, so all three are included. |
| 83 | Thoughts emitted by a compaction event are skipped so a prior summary's |
| 84 | reasoning does not leak into the next summary. |
| 85 | """ |
| 86 | formatted_history = [] |
no outgoing calls