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
peterc / image.rb
Created November 17, 2025 15:41
Produce images with gemini-2.5-flash-image (Nano Banana) with RubyLLM 1.9.1
# Just because the example in the docs doesn't seem to work outside of a Rails app
require 'ruby_llm'
RubyLLM.configure do |config|
config.gemini_api_key = ENV['GEMINI_API_KEY']
end
chat = RubyLLM.chat(model: "gemini-2.5-flash-image")
reply = chat.ask("Cyberpunk city street at night, raining, neon signs")
@peterc
peterc / openai.rb
Last active August 8, 2025 16:49
Calling GPT-5 from a plain Ruby script using the official OpenAI library
require "openai"
client = OpenAI::Client.new # assumes valid key in OPENAI_API_KEY
begin
resp = client.chat.completions.create(
model: :"gpt-5-nano",
reasoning_effort: :high,
verbosity: :low,
messages: [{ role: "user", content: "Tell me a joke" }]
@peterc
peterc / make_palette.py
Created August 2, 2025 16:25
Generate color palettes by generating AI images and extracting the colors
import sys
import requests
import replicate
from Pylette import extract_colors
# YOU NEED A REPLICATE API TOKEN IN 'REPLICATE_API_TOKEN'
prompt = sys.argv[1] if len(sys.argv) > 1 else "sandy beach"
output = replicate.run(
@peterc
peterc / openai-openrouter-multiple-models.rb
Last active July 29, 2025 18:26
How to call an OpenRouter LLM with the official Ruby OpenAI gem
require "openai"
client = OpenAI::Client.new(
api_key: ENV.fetch("OPENROUTER_API_KEY"),
base_url: "https://openrouter.ai/api/v1"
)
models = %w{
google/gemma-2b-it
anthropic/claude-sonnet-4
@peterc
peterc / useful.md
Last active July 7, 2025 09:20
Useful random stuff
@peterc
peterc / count_min.rb
Created May 4, 2025 13:23
A memory efficient count-min style compact approximator in Ruby
require 'zlib'
require 'objspace'
# A variant of https://en.wikipedia.org/wiki/Count%E2%80%93min_sketch
class CompactApproximator
attr_accessor :cells
def initialize(size, hashes, min_elem, join_fn, meet_fn)
@m, @k = size, hashes
@peterc
peterc / zig.txt
Created April 14, 2025 18:18
Basic Zig programming cheat sheet for LLM AI agent
# Zig Programming Cheat Sheet
This guide condenses the key ideas from Chapter 1 of the Zig book, emphasizing Zig's syntax, semantics, workflows, and philosophy. It distills nuanced language details, technical rules, compiler constraints, and examples for direct application by AI or technical users.
---
## 1. Zig Philosophy and Paradigm
- Zig is a **low-level, general-purpose, modern programming language** designed for safety, control, and simplicity.
- Major design goal: **Less is more** (removes confusing/unsafe C/C++ behaviors; adds consistency).
@peterc
peterc / README.md
Created April 14, 2025 17:41
GPT-4.1-mini generated documentation for Ruby's JSON library

JSON Library for Ruby

The JSON library is a comprehensive and robust solution for parsing, generating, and manipulating JSON data in Ruby. It offers full support for JSON encoding and decoding, including extensions for common Ruby types like Date, Time, Complex, Rational, and more. The library also provides advanced features such as extended JSON object creation, custom generators, and parser configurations.

This README provides an overview of usage, advanced options, compatible Ruby types, and important notes for developers who want to integrate JSON handling seamlessly in their Ruby applications.


Features

@peterc
peterc / hanami.txt
Created April 14, 2025 13:02
Hanami plain text docs for LLMs
Hanami is a Ruby framework designed to create software that is well-architected, maintainable and a pleasure to work on.
These guides aim to introduce you to the Hanami framework and demonstrate how its components fit together to produce a coherent app.
To create a Hanami app, you will need Ruby 3.1 or greater. Check your ruby version with `ruby --version`
To install: `gem install hanami`
Hanami provides a hanami new command for generating a new app: `hanami new bookshelf` (where bookshelf is an example app name)
@peterc
peterc / README.md
Last active April 14, 2025 12:13
Bun-powered MCP server for looking up `man` pages

Basic MCP server for man page lookups

bun init -y
bun add @modelcontextprotocol/sdk
bun add zod
bunx @modelcontextprotocol/inspector bun index.ts # if needed

For my claude_desktop_config.json (change if you're someone else):