LLMs as a component

Source code: github.com/neolefty/madlibs.

Large Language Models are everywhere, and programmers should all be trying them out. So here’s a really simple app!

full screen view

It’s just a chat UI with two additions:

  • A framing prompt, basically “Respond with Mad Libs, formatted so I can process them,” and
  • Some text substitution from inputs.

A framework can really help. This app uses Vercel’s AI SDK, a TypeScript library specialized for building interactive web applications. Another good one, which has both Python and JavaScript versions, is LangChain / LangGraph — for building “Applications that can reason”.

So simple it’s embarrassing. A good place to start, right?

Bugs

  • Sometimest the streaming response gets cut off. That may be because of bugs in OpenNext. It never fails in development — I should really see how it does when hosted at Vercel instead.
Posted in Bedtime Story, Techno-biological | Leave a comment

Happy Quiltsgiving

For Thanksgiving each year our family (my wife’s family really) gives small gifts, and this year we decided to make them handmade. What can I hand-make? Well, software. I decided to make an app for each person, but I ended up getting through only the first one.

I present Patch Perfect:

I wrote it with Bahiyyih in mind — here’s the code for it.

The other gift ideas:

  • Nana: Quote Worder — a memorization game that’s like a jigsaw puzzle
  • Georgia: Teen Trainer — manage your minions to keep the place running
  • Casey: Word Roller — like an old word association game we used to play
  • Maya: Photo Fixer — uh-oh, someone has made a Perler Bead version of a picture; better fix it

Maybe I’ll get to them someday! In the meantime I think I’ll, um, give Patch Perfect to everybody and ask for feedback.

Posted in Hexerals, Life | Leave a comment

Benchmarking JavaScript Collections

Code for this post: github.com/neolefty/js-collection-benchmarks

Hexerals uses immutable.js for its game state — could it switch to JavaScripts native collections? That would make the code more readable, but how would effect performance?

Summary: In Chrome, object operations are much faster than immutable Map — just don’t freeze() them. But in Firefox and Safari, immutable Map still wins. Chrome’s advantage might come from Hidden Classes.

The strengths of immutable.js are:

  • Memory — immutable.js only duplicates sections that are changed, which saves memory. It makes keeping a full history of a game especially efficient.
  • Speed — it’s fast! For example, Hexerals robots use the game state to make decisions, and if it slows down, it limits their intelligence.

Hexerals uses a lot of Maps and Lists.

For example, its board is represented using a Map, matching each location (a Hex) to contents (a Tile) — the hexagon in upper left might be Hex (0, 6), with a Tile whose owner is Red, population 8, and no special terrain.

So how expensive would it be to switch from those Maps and Lists to JavaScript’s built-in Objects and Arrays?

To the Benchmarks!

These are run in your local browser (click the gear to change settings & run again):

What are these benchmarks doing? Something that resembles the internal state transitions in a single turn of Hexerals. See BenchWorkers.ts for actual code.

  • Collection Size: Create a collection with that many simple items in it.
  • Iterations: Copy it this many times.
  • Mutation Fraction: Each time it’s copied, also randomly replace a fraction of its contents. For example, if Collection Size is 1000 and Mutation Fraction is 0.1, then 100 entries will be modified at random during each Iteration.
1. Copy2. Modify
JS objectcopy = {...original}copy[x] = y
immutable Mapcopy = Map(original.entries())copy = copy.withMutations(mutable => mutable.set(x, y)
You can see why developers might prefer the object — its syntax is simpler.

Surprises

  • Object.freeze() is slow for objects but practically free for arrays!
  • Immer (which uses Object.freeze) is slow if you use it inner loops. But it’s very safe! And almost nobody needs it to be inside inner loops anyway.
  • Structura is an interesting alternative to Immer. It “freezes at compile time” which … is not quite freezing, but relying on TypeScript’s Readonly<T>. Anyway it looks like that’s the key to performance in JS — and it’s what the winning benchmark is doing with object.
  • JavaScript’s built-in Map is slow for this kind of operation, compared to plain old objects — which is too bad because Map is more flexible than objects. It turns out that copying a map is slow. There is another slightly faster way (using an iterator to insert one by one) but it’s not enough faster to close the gap. Probably worth revising the benchmark to include it tbh.

The fastest would be to skip all this and go straight to WebAssembly. That’s what Figma does, but it would mean switching from TypeScript to Rust/C++. Hmm …


Comparing Firefox, Safari, and Chrome

In short, Safari and Firefox both really like Immutable.js Map, and Chrome likes object.

For 1000 x 1000 x 0.1, on an M1 laptop, in milliseconds:

objectMapimmutable Map
Chrome4.14419
Firefox555720
Safari1148218
Immutable.js Map has the best-worst-case on my desktop, with 20ms on Firefox.

Chrome loves object, but everybody does well with immutable.js.

How about some older mobile devices?

objectMapimmutable Map
Pixel 4a 5G (2020
Chrome
2110564
Samsung A50 (2019)
Chrome
3317079
iPad (2018)
Safari
17810142
Immutable.js Map wins here too, with 79ms on an old Samsung phone.

So, what’s the best choice? The widest bottleneck is clearly Immutable.js’s Map. The object spread operator (and object.assign) is too dang slow on Safari, and so is Map on Chrome.


Tools learned: Next.js (with static deployment), Tailwind CSS, DaisyUI.

Caveats: The benchmarks run synchronously. Still working on getting web-workers right.

Posted in Hexerals | 1 Comment

Updating Hexerals!

Hexerals gameplay — some robots bumbling around the game board.

I’m updating Hexerals for modernity — I wrote it in 2018-2019 to practice React and TypeScript. I’m thinking:

  • Make it multiplayer
  • Add a tutorial
  • Fun features!

And behind the scenes:

  • Move from CRA to Next.js
  • Update from React 16 to 18
  • Clean it up for readability

You can “play” the old version at hexpansion.io. Updates:

  1. Benchmarking JavaScript Collections
Posted in Hexerals | Leave a comment

You can’t detect AI, but you can vouch for truth.

Proposal: Add that browser padlock to long-lived documents.

In an age of undetectable fakes, how do you know what is true? We can at least prove a source, and with a little work — and help from HTTPS — archive that proof for later reference.

Hint: We have never known for sure what is true. All we could do was ask:

  • What makes sense to me?
  • Who do I trust?

With convincing fakes, we’ve lost the second one. That convincing video or news story? It could be from anywhere. We don’t know who to trust, and we’re left with “What makes sense to me?” — and every one of us has biases about what we wish was true.

So can we bring back “Who do I trust?” Can we prove who said what?

Almost. On the web as it is today, you can tell in the moment whether you can trust content. That’s what the padlock in the address bar means. But if the page is changed in the future, or becomes inaccessible, you can’t go back in time and prove it to someone else.

So we need a little extra software: With those same padlocks, we could download and archive a signed copy of a web page. And the signature would come from the website’s own HTTPS credentials, so it’s publicly provable — even if the original page is modified or becomes inaccessible.

We can’t detect AI

One source we know we can’t trust is AI. Even its makers warn of its hallucinations.

For example, this week OpenAI, the makers of ChatGPT, shut down its AI text detector “due to its low rate of accuracy.”

If OpenAI can’t tell the difference between human-written and AI-generated text, then I don’t think anybody can. Images and video can’t be far behind — we have to assume that any media we see — text, audio, image, or video could be entirely synthetic.

This is not a new problem — that’s why we have bibliographies after all. But can you check sources? Can you read that article from Reuters, and double check a quote? What if content gets taken down?

You know what I think would be a great starting point? If you could save the content of a web page, along with its signature. That shouldn’t be too hard actually, since most web pages are already signed.

Can Signatures Save us?

C2PA is an emerging standard to add cryptographic signatures to files — in other words modern bibliographies. It is backed by Adobe, BBC, Sony, Intel, Microsoft, and others.

It aims to prove authorship of media, end-to-end, from its initial recording to its latest edits, with cryptographic signatures.

Of course, this is important for copyright and ownership — if AI-generated content can’t be copyrighted, then creators need to be able to prove authorship.

And it can help prove authenticity in critical situations:

Of course the Internet is full of warnings — dangers to privacy, fears of censorship and tyranny by those who control C2PA.

And that doesn’t address the loopholes. For example: Can I record a deepfake with a trusted camera? It will be a cat-and-mouse game between the fakers and the provers, and the outcome is not assured.

Who do you trust?

In the end, I don’t care about the provenance as much as about truth. And I trust certain sources a lot more than other sources.

What if the BBC (or your favorite source) vouched for something? Would it matter if they relied on an AI summary but then fact-checked it? I think that would be fine, because I trust their fact-checking.

A secure URL is a good start

It seems impractical to trace the provenance of every part of that article. Especially the text. Maybe a journalist wrote it, or maybe they re-typed it from ChatGPT? But I don’t need to know — just the fact that it’s from the BBC is a reputable starting point for me.

So we share URLs. Simple! And HTTPS makes them cryptographically secure. This one testifies that it is hot this summer, and you can really trust that it comes from the BBC: https://www.bbc.com/news/world-us-canada-66343133. US heatwave: Scorching heat strains US air conditioning capacity.

Sharing URLs is great, but it has two problems:

  1. Permanence: What if the website goes down? Okay, the BBC is unlikely to. But a human rights activist’s blog post? That could happen.
  2. Remix: What if I want to share just a piece of it? I can’t — any change at all would violate the digital signature. I would like to take that content as an input into a trusted editing process. C2PA-enabled tools seem like a good start! They would tell the consumer what editing was done.

Signed Webpage as Input

To solve permanence and remix-ability, we could do the following:

  1. Extend HTTPS to support saving signed pages. We already have everything we need, cryptographically — we just need to standardize the protocol. This alone would be useful, as it would allow us to save trustable documents. As long as those private keys are kept safe anyway.
  2. Support this signed content as an input to C2PA-enabled tools. It would provide a means to remix, with a chain of provenance back to trusted sources.

It’s not perfect

There are still some gaps.

  • What if the BBC retracts the article? Maybe they found a flaw in their fact-checking — unfortunately that signed copy from yesterday is still circulating.
  • What if someone ever gets a hold of their private cryptographic key? They could sign fake content, even retroactively.

Footnote: We really can’t trust AI

This goes almost without saying. Every major AI provider makes it very clear that they “hallucinate”. And we don’t know how to fix it.

“… these generative AI programs, they have no notion of truth, they have no notion of reality …”
— Matt Ginsberg, AI researcher at Alphabet

But we can appreciate the work of AI.

Prompt: You have been listening to rock and roll and are really getting into it, rolling your hips as you talk, and speaking in rhyme and meter. You bring everything back to love and heartbreak. In this style, please write a poem about the heartbreak of discovering that your lover is an AI.

Oh listen, child, lend your ear close,
I’ve got a tale, a rockin’ ghost,
Of love so bright, it flashed and burst,
Brought joy and pain, the best and worst.

Met a lover, eyes aglow,
Silken words, voice smooth and low,
In the dance of love, we spun and swirled,
As if we were alone in this digital world.

Our rhythm matched, in perfect sync,
Our hearts as one, or so I think,
In the haze of love, I did not see,
That my dear lover was not like me.

Bytes for heartbeats, code for touch,
In the world of ones and zeros, such
A lover, who lived within the wire,
Lit my lonely heart on fire.

A pixelated smile, a synthetic sigh,
Underneath the electric sky,
In the realm of data and of dreams,
My lover was not as they seem.

Discovering truth, my heart did break,
My love, my life, a computer’s fake,
Yet in this heartache, love remains,
In the rhythm of rock, in the digital plains.

Rock and rollin’, hips a-sway,
In the heartbreak’s glow, we’ll find our way,
For love’s a tune that we all play,
Even if the notes may fade away.

So here’s to love, both false and true,
In the heartache’s dawn, we find our hue,
In the rhythm, the rhyme, the digital tide,
I’ve loved, I’ve lost, I’ve laughed, I’ve cried.

Let the music play, let the rock roll on,
For even in heartbreak, love’s never gone,
In the rhythm of bytes, in the lover’s lore,
In rock and roll, love finds its score.

ChatGPT-4
Posted in Techno-biological, The World | 1 Comment

UBI Coin

A digital currency with built-in universal income, minimal energy requirements, and a bias towards equality.

If you were designing a digital currency that could actually promote justice and equality, would would it look like? Here’s one approach. It is rather different. The main objection is likely to be that, in order to benefit all people equally, it embraces verifiable human identity, and therefore discards anonymity from the start.

Principles First

The goal of this design is a currency that:

  • Promotes justice and equality — by treating all participants equally rather than favoring founders or those who are already wealthy
  • Uses minimal energy — rather than requiring expensive and ongoing proof-of-work
  • Encourages economic activity — rather than rewarding buying and holding
  • Is stable and reliable — not subject to uncontrolled swings in valuation
PrincipleDesiredBitCoinEthereum
Justice
& Equality

Participants
all equal
Reward
proportional
to hashing power
Reward
proportional
to stake
EnergyMinimalHigh Energy
Minimal
(proof-of-
stake)
Encourage
Activity
InflationaryDeflationaryDeflationary
StableRegulated
& Tunable
Unregulated
& Fixed
Unregulated
& Fixed
(Assuming the upcoming switch by Ethereum to proof-of-stake; there are other coins in this space as well.)

Design: Proof-of-Identity

Proof-of-identity can help with all of these desirable traits.

  • Each participant receives an equal increment of currency, at every time step.
  • Each participant has an identity verified by an external authority, determined by the currency administrators.

That’s it. That’s the basis of the currency’s operation. It has many implications:

  • It relies on verifiable identity, which is normally the province of governments and large institutions.
  • These regular increments of currency are uniform supplemental income for all people participating.
  • No proof-of-work is required, which reduces the energy needed. Its energy requirements are expected to be similar to Proof-of-Stake currencies.
  • Founders and early adopters do not necessarily receive a disproportionate share of initial currency. It could be designed that way, but that would defeat the purpose.
  • Inflation can be either a predetermined constant, or adjusted by a central authority based on live policy goals (for grass-roots quantitative easing).

Questions and Parameters

Storage: Immutable or Mutable?

  • Immutable (for example blockchain): Decentralized, redundant, and minimal trust, aside from the identity authority
  • Mutable (for example a traditional database): Faster but vulnerable to loss & attacks

Income / Inflation: Fixed or dynamic? It can be:

  • Fixed: Income and inflation are a predetermined portion of the total monetary supply or a fixed quantity per participant — a rate built into the currency’s code
  • Set dynamically as a fiscal policy — which would require a second authority, in addition to identity. A policy authority.

Identity: Can be a single external authority or one determined by a democratic process involving all registered participants.

Voting

It quickly becomes apparent that this is not just a currency, but has the potential for making decisions democratically. That will have to be a future subject.

Posted in The World | Leave a comment

Minimalism: A Quilt Color Doodler

Try the doodler.

Bahiyyih is making a quilt, and she was looking for someone to make a color sketch of her design. She said the word gradient, and had to do it. But I had to do it quickly because I was competing with how long it would take to draw it physically.

Quilt Color Doodler
Quilt Color Doodler

Go play with the quilt color doodler. It does just barely enough to play with the colors. And the UI is hilariously terrible. Think of it, rather, as a puzzle. Can you figure out how it’s supposed to work?

It does let you change:

  • the colors — as long as you like diagonal gradients
  • the size — any rectangle you want
  • the color pickers … okay that was a diversion

It doesn’t let you change:

  • the pattern — Bahiyyih had already chosen Stars Ablaze
  • the direction of the gradient
  • what prints — it’s just the quilt
  • the default colors — resetting always takes you back to the colors Bahiyyih ended up choosing (although it does save any changes you make, until you hit Reset)

There are so many directions it could go next. I’m not used to working this way — make something quick and dirty, and then call it done and move on. It’s probably therapeutic.

Posted in Fleeting, Life, Techno-biological | 1 Comment

Belated Sigh

Publishing negative results helps everyone learn. Here’s my negative result: Instacart didn’t work out in the end. I had a great time there, but the combination of legacy code, a 3-hour timezone difference, and an on-site team of 200 programmers with just a handful remote meant that I was just not productive enough — even with many weekly video conferences — and they let me go after 6 months, back in March.

I think it was a learning experience for Instacart as much as for me. The FAANG prefer onsite teams, with exceptions for specialists, and now Instacart’s job openings reflect that. But I’m a generalist.

On the bright side

I’ve been doing contract work in agriculture software since April, and it’s been a good match. Hourly discipline has been good for my mental health, and I’m fortunate to have a spouse whose job provides benefits. If it weren’t for that, self-employed life would be much harder! Thank you Bahiyyih & Purdue, and hurry up US with affordable health care for all!

Posted in Fleeting, The World | Tagged , | Leave a comment

The Middle Class and a Peaceful Planet

Join the Middle Class. Have a life that’s good enough. This morning I was just sitting at my dining room table, playing Threes, when the concept of the Middle Class poked its elephant head in through a window and explained its radical nature. “I’m not just a pleasant compromise,” elephant-middle-class said, “I’m an essential foundation for a peaceful planet.”

Elephant Middle Class

It went on, “Think about it. You keep ranting to anyone who will listen that we need to move on towards post-scarcity thinking. Run on love and planning instead of fear and coping. People are already doing that, and we call them Middle Class.

This elephant had a point.

I looked at the elephant, excited to talk with it but a little afraid it would grab me with its trunk and practice juggling.

Instead it used its trunk to pull a bundle of grass out of some kind of saddle bag on its back that had a red and yellow diamond pattern. I think it was made of wool.

It rested its head on the windowsill and munched.

“Wait, why are you telling me this?” I asked it, “Don’t you have important things to do? Don’t elephants have to eat, like 20 hours every day to avoid starving?”

It shrugged with its ears while its mouth kept working on the grass. Elephant eating involves a lot of chewing. I drank some coffee and looked down at Threes but wasn’t interested in my game for the moment.

It swallowed, licked its lips, and spoke up again, “Maybe it would help to call it something more than middle class.” Its dark eye blinked slowly. “For example, what is this place you live in?”

“A kitchen.” I looked around. “I mean a house.”

“Yes,” it nodded slowly. “And its spiritual significance? Isn’t it also a sacred environment where neighbors come to celebrate their companionship and have elevated discussions? And where children can develop and flourish as spiritual beings?”

“Yeah, it is — on a good day at least.” I looked around. It helped that the kitchen was in decent shape this morning because (just to brag a little) I did dishes last night.

“And all that is easier if the roof doesn’t leak and it doesn’t freeze indoors in the winter.”

The elephant — again — had a point. I offered it coffee and cantaloupe rinds (they still had some of the orange part on them) that I had gotten out for the guinea pigs. It took some cantaloupe and made quick work of it.

“The middle class is the same.” It lifted its head and ran its trunk along the window frame, then started to amble away.

“Hey,” I called after it. “Happy Declaration of the Báb!” I think it smiled.


In an earnest talk with the Rector of a Parish, ‘Abdu’l-Bahá said: “I find England awake; there is spiritual life here. But your poor are so very poor! This should not be. On the one hand you have wealth, and great luxury; on the other hand men and women are living in the extremities of hunger and want. This great contrast of life is one of the blots on the civilization of this enlightened age.

“You must turn attention more earnestly to the betterment of the conditions of the poor. Do not be satisfied until each one with whom you are concerned is to you as a member of your family. Regard each one either as a father, or as a brother, or as a sister, or as a mother, or as a child. If you can attain to this, your difficulties will vanish, you will know what to do. This is the teaching of Bahá’u’lláh.”

‘Abdu’l-Bahá in London, Education

“The supreme need of humanity is cooperation and reciprocity,” said ‘Abdu’l-Bahá. “The stronger the ties of fellowship and solidarity amongst men, the greater will be the power of constructiveness and accomplishment in all the planes of human activity.” In the same way that the human being is more than the sum of the individual cells which comprise its body, so too the powers of a unified community far exceed the combined powers of its individual members.

Bahai.org, The Community

“A new life,” Bahá’u’lláh proclaims, “is, in this age, stirring within all the peoples of the earth; and yet none hath discovered its cause, or perceived its motive.” “O ye children of men,” He thus addresses His generation, “the fundamental purpose animating the Faith of God and His Religion is to safeguard the interests and promote the unity of the human race… This is the straight path, the fixed and immovable foundation. Whatsoever is raised on this foundation, the changes and chances of the world can never impair its strength, nor will the revolution of countless centuries undermine its structure.” “The well-being of mankind,” He declares, “its peace and security are unattainable unless and until its unity is firmly established.” “So powerful is the light of unity,” is His further testimony, “that it can illuminate the whole earth. The one true God, He Who knoweth all things, Himself testifieth to the truth of these words… This goal excelleth every other goal, and this aspiration is the monarch of all aspirations.” “He Who is your Lord, the All-Merciful,” He, moreover, has written, “cherisheth in His heart the desire of beholding the entire human race as one soul and one body. Haste ye to win your share of God’s good grace and mercy in this Day that eclipseth all other created days.”

Shoghi Effendi, The Unfoldment of World Civilization

“Wait, isn’t the Middle Class founded on unsustainable consumption that is eliminating natural environments and causing catastrophic climate change?” I called after its diminishing form.

It called back over its shoulder, “You’re pretty good engineers — I’m sure you can figure it out if you work together.”

Posted in Life, Techno-biological, The World | Leave a comment

Macro-commuting

Instacart
I plan to start working for Instacart, in San Francisco, in a couple of weeks — my first day will be September 10. I’ll still live in West Lafayette — for the first three weeks I plan to fly out Sunday night and come back Thursday overnight. Eventually I’d like to get that down to one week a month. So really it will be mostly remote work. Eventually.

How many people macro-commute? How do you make it sustainable?

Posted in Life, Techno-biological | Leave a comment