AIML Docs

OnChunk Element

Element for handling data chunks

OnChunk Element

Coming soon: This feature is not yet available. Please check back soon.

The <onchunk> element handles data chunks:

<llm id="chunked-llm">
  <prompt>Processing chunk: {({chunk}) => chunk.data}</prompt>
</llm>
<onchunk for="chunked-llm">
  <log expr={({chunk}) => `Processing chunk: ${chunk.data}`} />
</onchunk>

Props

PropTypeDefault
for?
string
(the output of the previous element)

Examples

Basic Chunk Processing

<onchunk>
  <log expr={({chunk}) => `Processing chunk: ${chunk.data}`} />
  <script>
    processChunk(chunk);
  </script>
</onchunk>

Stream Processing

<onchunk>
  <if condition={({chunk}) => chunk.type === "data"}>
    <script>
      accumulateData(chunk.data);
      updateProgress(chunk.progress);
    </script>
  </if>
  <elseif condition={({chunk}) => chunk.type === "metadata"}>
    <script>
      updateMetadata(chunk.metadata);
    </script>
  </elseif>
</onchunk>

Real-time Analysis

<onchunk>
  <script>
    const analysis = analyzeChunk(chunk);
    if (analysis.needsAction) {
      triggerAction(analysis.action);
    }
  </script>
  <log expr={({analysis}) => `
    Chunk Analysis:
    Size: ${analysis.size}
    Type: ${analysis.type}
    Action: ${analysis.action}
  `} />
</onchunk>

Usage Notes

  • Handles streaming data
  • Processes chunks sequentially
  • Can analyze in real-time
  • Supports async processing

Common Patterns

  1. Data Accumulation
<onchunk>
  <script>
    const buffer = getBuffer();
    buffer.append(chunk.data);
    
    if (buffer.isComplete()) {
      processCompleteData(buffer.getData());
      buffer.clear();
    }
  </script>
  
  <log expr={({buffer}) => `
    Buffer Status:
    Size: ${buffer.size}
    Complete: ${buffer.isComplete()}
  `} />
</onchunk>
  1. Stream Analysis
<onchunk>
  <datamodel>
    <data 
      id="metrics"
      type="JSON"
      value={{
        chunks: 0,
        totalSize: 0,
        patterns: {}
      }}
    />
  </datamodel>
  
  <script>
    updateMetrics(chunk);
    analyzePatterns(chunk);
    detectAnomalies(chunk);
  </script>
  
  <if condition={({metrics}) => metrics.anomalies.length > 0}>
    <raise event="anomalyDetected" />
  </if>
</onchunk>
  1. Progress Tracking
<onchunk>
  <assign 
    location="state.progress"
    expr={({chunk, state}) => ({
      processed: state.progress.processed + 1,
      total: chunk.total,
      percentage: ((state.progress.processed + 1) / chunk.total) * 100
    })}
  />
  
  <if condition={({state}) => state.progress.percentage % 10 === 0}>
    <log expr={({state}) => `
      Progress Update:
      ${state.progress.percentage}% complete
      (${state.progress.processed}/${state.progress.total})
    `} />
  </if>
</onchunk>

Advanced Usage

Chunk Transformation

<onchunk>
  <script>
    const transformed = transformChunk(chunk, {
      format: state.outputFormat,
      encoding: state.encoding,
      compression: state.compression
    });
    
    processTransformedChunk(transformed);
  </script>
  
  <if condition={({transformed}) => transformed.needsValidation}>
    <script>
      validateChunk(transformed);
    </script>
  </if>
</onchunk>

Parallel Processing

<parallel id="chunkProcessing">
  <onchunk>
    <state id="analysis">
      <script>
        analyzeChunkContent(chunk);
      </script>
    </state>
    
    <state id="storage">
      <script>
        storeChunkData(chunk);
      </script>
    </state>
    
    <state id="monitoring">
      <script>
        updateMetrics(chunk);
      </script>
    </state>
  </onchunk>
</parallel>

Adaptive Processing

<onchunk>
  <script>
    const strategy = selectProcessingStrategy(chunk, {
      size: chunk.size,
      type: chunk.type,
      complexity: analyzeComplexity(chunk),
      systemLoad: getSystemLoad()
    });
    
    applyProcessingStrategy(chunk, strategy);
  </script>
  
  <log expr={({strategy}) => `
    Selected Strategy:
    Type: ${strategy.type}
    Priority: ${strategy.priority}
    Resources: ${strategy.resources}
  `} />
  
  <if condition={({strategy}) => strategy.needsScaling}>
    <raise event="scaleResources" />
  </if>
</onchunk>

On this page