Skip to content

Instantly share code, notes, and snippets.

View peterc's full-sized avatar
Working from home

Peter Cooper peterc

Working from home
View GitHub Profile
peterc / groq_client.rb
Last active April 19, 2024 17:45
Basic Groq API client for Ruby
require 'http'
require 'json'
class GroqClient
def initialize(api_key: nil, api_url: "", model: "mixtral-8x7b-32768")
@api_key = api_key || ENV['GROQ_API_KEY']
@api_url = api_url
@model = model
peterc / Caddyfile
Last active March 16, 2024 11:52
Caddyfile for running Mastodon – November 2022 edition {
@local {
not path /
log {
output file /var/log/caddy/mastodon.log
peterc / embedding_store.rb
Last active December 28, 2023 06:27
Using SQLite to store OpenAI vector embeddings from Ruby
# Example of using SQLite VSS with OpenAI's text embedding API
# from Ruby.
# Note: Install/bundle the sqlite3, sqlite_vss, and ruby-openai gems first
# OPENAI_API_KEY must also be set in the environment
# Other embeddings can be used, but this is the easiest for a quick demo
# More on the topic at
peterc / email_list_mx_checker.rb
Created March 13, 2023 12:53
Email list MX record checker
# Read stdin, extract email addresses
# Look up MX records for those addresses
# Return each email address and their first MX record
# (or IP address, as a fallback)
hosts = {}
STDIN.each_line do |l|
# Extract the email and the email domain from each line
email = l[/^([^\s]+\@[^\s+\,]+)\b/, 1]
peterc / methods_returning.rb
Last active October 29, 2023 03:10
Object#methods_returning - to work out which method on an object returns what we want
require 'stringio'
require 'timeout'
class Object
def methods_returning(expected, *args, &blk)
old_stdout = $>
$> = do |meth|
Timeout::timeout(1) { dup.public_send(meth, *args, &blk) == expected rescue false } rescue false
peterc / perceptron.rb
Created June 7, 2023 20:30
Basic implementation of a perceptron in Ruby
class Perceptron
def initialize(inputs, bias = 0.0)
@weights = { rand }
@inputs = inputs
@bias = bias
def run(inputs)
z = { |i, w| i * w }.reduce(:+) + @bias
1.0 / (1.0 + Math.exp(-z))
peterc / index.html
Created October 13, 2023 13:57
Basic example of using import maps and TailwindCSS in a buildless fashion
<!DOCTYPE html>
<!-- A totally pointless and overkill use of import maps
and Moment.js, in order to simply use the technology and
see it in action.. -->
<html lang="en">
<meta charset="UTF-8">
<title>Buildless example</title>
<link href="^2/dist/tailwind.min.css" rel="stylesheet">
peterc /
Last active October 9, 2023 17:22
The first e-mail in my original Gmail account on April 25, 2004

First off, welcome. And thanks for agreeing to help us test Gmail. By now you probably know the key ways in which Gmail differs from traditional webmail services. Searching instead of filing. A free gigabyte of storage. Messages displayed in context as conversations.

So what else is new?

Gmail has many other special features that will become apparent as you use your account. You’ll find answers to most of your questions in our searchable help section, which includes a Getting Started guide. You'll find information there on such topics as:

  • How to use address auto-complete
  • Setting up filters for incoming mail
  • Using advanced search options
peterc /
Last active October 4, 2023 21:10
A pretend 404 page

A 404 page.. kinda

The content which was previous linked to by the link you clicked has been removed for quality control reasons. Sorry! On the plus side, you didn't waste your time reading something that wasn't actually very good :-)

peterc / index.html
Created April 28, 2014 16:40
Most popular link from each issue of JavaScript Weekly
<h1>Most popular link from each issue of JavaScript Weekly</h1>
<p>Please note that due to the age of many issues, a lot of these links won't work or will point to outdated resources.</p>
<li><a href=''>Enterprise JavaScript</a> 9.6%</li>
<li><a href=''>Essential JavaScript And jQuery Design Patterns - A New, Free Book</a> 14.6%</li>
<li><a href=''>Implementing an HTML5 Canvas-based Tilt-sensing Snow Globe</a> 13.0%</li>