SemanticBlocks
Groups messages into semantic blocks for context compression and history management.
==Semantic Block Concept==
A semantic block represents a logically related group of messages in a conversation. The primary patterns are:
-
'''User-Assistant Pairs''': A user question followed by an assistant response. These form the natural "turns" of a conversation.
-
'''Tool Interactions''': Tool calls and their results, often associated with an assistant message that triggered them.
-
'''Standalone Messages''': Messages that don't fit the pair pattern (e.g., system messages, isolated assistant responses).
==Algorithm==
The grouping algorithm uses a tail-recursive state machine:
- '''UserMessage''': Starts a new block expecting an assistant response
- '''AssistantMessage''': Completes a user block, or becomes standalone
- '''ToolMessage''': Attaches to the current block or becomes standalone
- '''SystemMessage''': Treated similarly to assistant (can complete blocks)
Attributes
- See also
-
HistoryCompressor which uses semantic blocks for history compression
SemanticBlockType for the classification of block types
- Example
-
val messages = Seq( UserMessage("What's the weather?"), AssistantMessage("I'll check for you..."), ToolMessage("""{"temp": 72}""", "call_1"), AssistantMessage("It's 72 degrees.") ) val blocks = SemanticBlocks.groupIntoSemanticBlocks(messages) // Result: One UserAssistantPair block containing all 4 messages - Graph
-
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
SemanticBlocks.type