Demo1 – Groq LLM streaming

A Dyalog APL demo that sends a prompt to the Groq chat completions API and consumes the reply as a Server-Sent Events (SSE) stream using HttpCommand’s SSE support. The model is asked to answer in HTML, which is then displayed.

Prerequisites

Configure the API key

The demo reads the key from the Auth field in GroqStream. Use either option:

  1. HeaderSubstitution (default). Auth:'%APIKeys\groq%' is a placeholder that HttpCommand expands when the request is sent. The key is held in an environment variable, and its name is up to you: put whatever name you choose between the % characters in Auth, and set an environment variable of that name to your key. For example, with Auth:'%MY_GROQ_KEY%':

    export MY_GROQ_KEY=gsk_your-key-here
    dyalog

    Set the variable before starting Dyalog so the interpreter inherits it. See the HttpCommand documentation on header substitution for details.

  2. Paste the key directly. Edit Demo1.apln and replace the placeholder with your key: Auth:'gsk_your-key-here'. Don’t commit a real key.

Running

Start Dyalog APL any way you like (the terminal interpreter, RIDE, or the Windows IDE). The session’s current directory must be the demo1 folder, so change to it with the ]CD user command, giving the path to the folder on your machine:

]CD /path/to/demo1

Then load and run the demo:

⎕FIX'file://Demo1.apln'
result ← Demo1.Run 'Write a short poem about APL'

Progress dots (one per SSE event) print while the response streams in, followed by a newline when it is complete. Then the HTML reply is shown:

Results

Run returns the HttpCommand result namespace. Besides the usual HttpCommand fields it contains:

Name Contents
result.result Complete response as a UTF-8 string (HTML)
result.tokens The raw token fragments, in order
result.buffer Every SSE event received

To get the stream without the display step, call Demo1.GroqStream directly:

result ← Demo1.GroqStream 'Explain ⍳ in one paragraph'

Customising

Edit GroqStream in Demo1.apln:

Setting Default Purpose
model openai/gpt-oss-20b Any model available on groq.com
max_completion_tokens 2048 Upper bound on response length
messages[0].content “reply ONLY in valid HTML” The system prompt

Troubleshooting