Skip to content

ai-console

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

Run it from sema/:

Terminal window
sema check examples/ai-console
SEMA_STRICT=1 sema run examples/ai-console
sema assure examples/ai-console --grade silver
"""
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 with a checked file attachment (§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/runs/<run-id>/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."
test "vector operators and magnitude preserve numeric semantics":
a = Vec3(x=1.0, y=2.0, z=2.0)
b = Vec3(x=0.0, y=0.0, z=1.0)
result = (a + b) * 2.0
ensure [result.x, result.y, result.z] == [2.0, 4.0, 6.0]
ensure magnitude(a) == 3.0
test "prompt composition exposes roles, attachment evidence, and clean diagnostics":
system_prompt = assistant_system("logistics")
user_prompt = message("user", ["Answer from the attachment.", attachment("facts.txt")])
composed = compose([system_prompt, user_prompt])
ensure composed.roles == ["system", "developer", "user"]
ensure composed.valid and len(composed.warnings) == 0
ensure composed.text.contains("the port closes at 18:00")
ensure composed.text.contains("customs clearance takes 2 days")
test "explicit deterministic generation preserves batch cardinality and stream bounds":
chunks = generate_stream("Summarize the logistics plan", 24)
replies = generate_batch(["classify: urgent", "classify: routine", "classify: hold"], 16)
ensure 0 < len(chunks) <= 24
ensure len(replies) == 3
ensure all(len(reply) > 0 for reply in replies)
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) Checked multimodal attachment. Real image/audio inference is exercised
# by examples/sdk-multimodal with its model-specific fixtures.
msg = message("user", [
"Given the attached context, answer the question.",
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"))

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 with a checked file attachment (§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/runs/<run-id>/journal.jsonl to see every template render (with its roles + token estimate + composition warnings) and model call recorded.

Fields

field type descriptor
x f64
y f64
z f64
def magnitude(v: Vec3) -> f64 !{}

Parameters

name type
v Vec3

Returns f64

Effects !{}

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

Returns None

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