my 404 page will mock you
I believe I have found the killer application for LLMs: automatically generated 404 pages that insult the user for their poor navigational abilities. When a lost visitor tries to access a location that does not exist on this website, the server will give them a harsh reality-check. Have a look at the non-existent pages below to get a taste:
- /recipes/delicious-pancakes.html
- /secrets/passwords.txt
- /videos/how-to-tie-a-shoelace.mp4
- /papers/P-equals-NP-proof.pdf
How does it work? When a user requests a page that does not exist, a language model is prompted for an insulting response based on the URI of the requested resource. I use Ollama to self-host a quantized version of Mistral 7B. This allows me to generate responses to prompts by calling a local HTTP API.
The actual HTTP response is assembled by a Bash script that is served via CGI on NGINX using fcgiwrap. In short, CGI executes a program for each request and responds with whatever the program writes to standard output. Here is the script:
#!/usr/bin/env bash
set -o pipefail
echo "content-type: text/html"
echo "status: 404 not found"
echo "cache-control: max-age=3600"
echo ""
location="$REQUEST_URI"
model="mistral-nemo:latest" #"mistral:latest"
system="You are a web server that is an expert at writing creative and profane insults."
prompt="A visitor tried to access '$location' which does not exist, so they ended up on this 404 page. Respond with an insulting personal letter to the visitor. Guess what they expected to find at '$location' and explain how foolish it was to think that you as a server would respond to such a request. Use markdown to format your message, starting with a 404 heading and ending with a signature."
payload=$(cat << EOF
{
"model": "$model",
"system": "$system",
"prompt": "$prompt",
"stream": false
}
EOF
)
result=$(curl -X POST "127.0.0.1:11434/api/generate" -d "$payload" | jq -r ".response" | sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/g' | pandoc -f markdown -t html || echo "Normally I would insult you, but I have better things to do.")
cat << EOF
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>404 not found - fumlig.se</title>
<link href="/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<main>
<section>
$result
</section>
<footer>
<hr />
<nav>
<ul>
<li><a href="/">go back home</a></li>
<li><a href="/insulting-404/">what is this?</a></li>
</ul>
</nav>
</footer>
<script src="/oneko.js"></script>
</main>
</body>
</html>
EOF
A drawback is that it is a rather expensive response to compute. I have configured caching and rate limiting in NGINX try to prevent people from frying my GPU, but I also ask that you are polite and do not exploit this to spike my energy bill :)
2024-02-25
Stockholm
comments
commentEpicality
Erik Lundin wrote on :Now this is epic!
replyFun response
Oskar Lundin wrote on :I liked this one!
reply