<!-- Sema documentation — ai-console
     Sema is a new AI-native, neurosymbolic programming language most models
     have not been trained on. Full corpus: https://sema.49.12.246.95.sslip.io/llms-full.txt
     Install language support: https://sema.49.12.246.95.sslip.io/install-skill/ -->

# ai-console

> An interactive AI console — simulate, semantic ops, and streaming in a REPL-shaped app.

> An interactive AI console — simulate, semantic ops, and streaming in a REPL-shaped app.

Run it from `sema/`:

```bash
sema check examples/ai-console
SEMA_STRICT=1 sema run examples/ai-console
sema assure examples/ai-console --grade silver
```

## Source

### `src/main.sema`

```sema
"""
AI console — an end-to-end tour of Sema's native AI capabilities.

Exercises, in one deterministic program (no external models required):
- prompt templates + composition debugging (§5.14)
- native multimodal messages: text + image + audio + file (§5.49)
- LLM token streaming and batched/distributed generation (§5.25, §5.50)
- reduced-precision numeric widths and operator overloading (§3.1)

Run it, then inspect `.sema/journal.jsonl` to see every template render (with its
roles + token estimate + composition warnings) and model call recorded.
"""

import math

# ---- a typed vector with overloaded operators (§3.1) ---------------------

struct Vec3:
    x: f64
    y: f64
    z: f64

operator +(a: Vec3, b: Vec3) -> Vec3 !{}:
    return Vec3(x=a.x + b.x, y=a.y + b.y, z=a.z + b.z)

operator *(a: Vec3, k: f64) -> Vec3 !{}:
    return Vec3(x=a.x * k, y=a.y * k, z=a.z * k)

def magnitude(v: Vec3) -> f64 !{}:
    return math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z)

# ---- a prompt template (§5.14) -------------------------------------------

template assistant_system(domain: str) -> Prompt[str]:
    role system:
        text f"You are a precise assistant for {domain}."
        text "Cite evidence and refuse unsupported claims."
    role developer:
        text "Prefer concise answers."

def main() -> None !{model.invoke, fs.read, observe.record}:
    # 1) Operators + reduced-precision widths.
    a = Vec3(x=1.0, y=2.0, z=2.0)
    b = Vec3(x=0.0, y=0.0, z=1.0)
    c = (a + b) * 2.0
    log.info("vectors", sum_scaled=[c.x, c.y, c.z], mag=magnitude(a))
    log.info("widths", f16=f16(0.1), bf16=bf16(0.1), f8=f8(1000.0), i8=i8(200), u8=u8(300))

    # 2) Prompt template + composition debugging.
    sys = assistant_system("logistics")
    log.info("prompt", roles=sys.roles, tokens=sys.tokens, valid=sys.valid)

    # 3) Multimodal message: a text model still "sees"/"hears" via the framework.
    msg = message("user", [
        "Given the attached context, answer the question.",
        image("diagram.png"),
        audio("question.wav"),
        attachment("facts.txt"),
    ])
    composed = compose([sys, msg])
    print("=== composed multimodal prompt ===")
    print(composed.debug)

    # 4) Streaming generation (prints tokens live) + batching/distribution.
    print("=== streaming reply ===")
    streamed = generate_stream("Summarize the logistics plan", 24)
    log.info("streamed", chunks=len(streamed))

    replies = generate_batch([
        "classify: urgent shipment delay",
        "classify: routine restock",
        "classify: customs hold",
    ], 16)
    log.info("batch", n=len(replies))

    # 5) String manipulation.
    report = "shipment DELAYED at customs".title()
    log.info("string", report=report, has_delay=report.lower().contains("delayed"))
```

## Reflected API

# `main`

AI console — an end-to-end tour of Sema's native AI capabilities.

Exercises, in one deterministic program (no external models required):
- prompt templates + composition debugging (§5.14)
- native multimodal messages: text + image + audio + file (§5.49)
- LLM token streaming and batched/distributed generation (§5.25, §5.50)
- reduced-precision numeric widths and operator overloading (§3.1)

Run it, then inspect `.sema/journal.jsonl` to see every template render (with its
roles + token estimate + composition warnings) and model call recorded.

# `struct Vec3`

**Fields**

| field | type | descriptor |
|---|---|---|
| `x` | `f64` |  |
| `y` | `f64` |  |
| `z` | `f64` |  |

# `def magnitude`

```sema
def magnitude(v: Vec3) -> f64 !{}
```

**Parameters**

| name | type |
|---|---|
| `v` | `Vec3` |

**Returns** `f64`

**Effects** `!{}`

# `def main`

```sema
def main() -> None !{model.invoke, fs.read, observe.record}
```

**Returns** `None`

**Effects** `!{model.invoke, fs.read, observe.record}`
