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.
dyalog), with a Dyalog session (RIDE or the terminal
interpreter).https://api.groq.com.Run fetches it with
]get HttpCommand and upgrades it, so no manual install is
needed.The demo reads the key from the Auth field in
GroqStream. Use either option:
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
dyalogSet the variable before starting Dyalog so the interpreter inherits it. See the HttpCommand documentation on header substitution for details.
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.
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:
HTMLRenderer
window.⎕ED 'r.result'.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'
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 |
VALUE ERROR or 401 response: the API
key isn’t set or the %APIKeys\groq% placeholder wasn’t
resolved. See Configure the API
key.OnGroqEvent at
∘∘∘: the callback hit a JSON error other than an
incomplete chunk (which is expected and ignored). Inspect
⎕DMX and ev.data.