SemanticBlocks

org.llm4s.context.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:

  1. '''UserMessage''': Starts a new block expecting an assistant response
  2. '''AssistantMessage''': Completes a user block, or becomes standalone
  3. '''ToolMessage''': Attaches to the current block or becomes standalone
  4. '''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 Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

Group messages into semantic blocks.

Group messages into semantic blocks.

The algorithm processes messages sequentially, maintaining state about the current block being built. User messages start new blocks, assistant messages complete them, and tool messages are attached to existing blocks.

Value parameters

messages

The conversation messages to group

Attributes

Returns

A sequence of semantic blocks, or an error if grouping fails