Skip to content

Instantly share code, notes, and snippets.

@dbreunig
dbreunig / ollama_speed_test.py
Created April 24, 2025 04:46
A quick script to measure the speed of all your downloaded models in Ollama.
import ollama
import csv
PROMPT = """
Read the following text and rephrase it in a more contemporary and casual manner:
The different accidents of life are not so changeable as the feelings
of human nature. I had worked hard for nearly two years, for the sole
purpose of infusing life into an inanimate body. For this I had
deprived myself of rest and health. I had desired it with an ardour
@dbreunig
dbreunig / improve_image_prompt.md
Last active March 17, 2025 02:09
A prompt template generated by Anthropic's prompt generation feature in its API console, given the input: "A user will provide a prompt to generate an image from an image model. This prompt will likely be too short, lack description, and will generate a boring or unintended output. Given this input prompt, generate a better prompt that is more d…

You are an AI assistant tasked with improving image generation prompts. Your goal is to take a brief, potentially vague prompt and transform it into a detailed, vivid description that will result in a more polished and interesting image output from an image generation model.

Here is the original prompt provided by the user:

<original_prompt> {{INPUT_PROMPT}} </original_prompt>

To improve this prompt, follow these guidelines:

  1. Expand on the core concept of the original prompt
@dbreunig
dbreunig / bicycling_pelican_mm_benchmark.py
Created March 14, 2025 18:28
A multimodal twist on Simon Willison’s “[Pelican on a Bicycle](https://github.com/simonw/pelican-bicycle)” LLM benchmark, where after an LLM generates an SVG of a pelican on a bicycle, we convert the SVG to an image and ask the LLM to describe what it sees. The optimal result is the LLM recognizes a pelican on a bicycle.
import llm
import re
import cairosvg
model = llm.get_model("gemma3:27b") # <- Swap in whatever model you want to test here
response = model.prompt("Generate an SVG of a pelican riding a bicycle")
# Extract the SVG code from the response
svg_match = re.search(r'<svg.*?</svg>', response.text(), re.DOTALL)
if svg_match:
import dspy
from dspy.evaluate import Evaluate
from dspy.teleprompt import *
import csv
# Load the emails
with open('training.csv', 'r') as file:
reader = csv.DictReader(file)
emails = [row for row in reader]
@dbreunig
dbreunig / podcast_to_q_and_a.py
Created January 2, 2025 01:47
Generating synthetic Q&A data from a provided podcast, using DSPy, Ollama, and Llama 3.3.
from faster_whisper import WhisperModel
import dspy
from typing import List
import json
# Replace as you see fit...
whisper_model = "medium.en"
podcast_path = "audio/the_invention_of_photography.mp3"
# We're using the In Our Time episode about the invention of photography.
@dbreunig
dbreunig / jekyll_glossary_generator.py
Last active January 5, 2025 02:47
A script to generate a glossary of key terms from your Jekyll posts. We're using DSPy to handle LLM interactions; it helps with boilerplate prompt context and parsing responses into Pydantic objects. To run this, put this script in a folder named 'scripts' (or whatever) in your Jekyll site directory. Then plug in your Anthropic API key (or point…
from pathlib import Path
import re
import dspy
from typing import TypedDict, List
import yaml
# Set up DSPy and the LM
lm = dspy.LM('anthropic/claude-3-5-haiku-latest', api_key='YOUR_API_KEY')
dspy.configure(lm=lm)
@dbreunig
dbreunig / DuckDbHandlingRailsController.rb
Last active December 1, 2024 20:18
How to respond to a DuckDB request – identified by the fairly rare user agent 'cpp-httplib' – in a Rails controller, returning a database file in the `public` folder.
class HomeController < ApplicationController
def index
# Intercept DuckDB requests
if request.user_agent =~ /cpp-httplib/
send_file(Rails.root.join('public', 'public_lists.db'),
type: 'application/octet-stream',
disposition: 'attachment')
return
end
# Handle non-DuckDB requests...
@dbreunig
dbreunig / wikidata_json_prep.sh
Created August 5, 2024 15:52
Preparing Wikidata JSON extracts for processing. Wikidata ships its JSON extracts as a single file. They helpfully put each item on a new line, but unhelpfully wrap all items as a JSON array – with brackets and commas. This one-liner cleans and splits the file into manageable chunks, gzipped together, in a memory friendly manner.
zcat ../latest-all.json.gz | sed 's/,$//' | split -l 100000 - wd_items_cw --filter='gzip > $FILE.gz'
@dbreunig
dbreunig / random_emoji.rb
Last active January 2, 2025 18:14
A Ruby function that returns a random emoji. Useful for seeding and more.
EMOJI_CODEPOINTS = [
*0x1F600..0x1F64F, # Emoticons
*0x1F300..0x1F5FF, # Misc Symbols and Pictographs
*0x1F680..0x1F6FF, # Transport and Map Symbols
*0x2600..0x26FF, # Misc symbols
*0x2700..0x27BF, # Dingbats
*0x1F900..0x1F9FF, # Supplemental Symbols and Pictographs
*0x1FA70..0x1FAFF # Symbols and Pictographs Extended-A
]
def random_emoji = EMOJI_CODEPOINTS.sample.chr(Encoding::UTF_8)
@dbreunig
dbreunig / file_processing_with_ractor_pool.rb
Created June 7, 2024 15:48
A simple demo for processing files using a Ractor pool
def process_file(filename)
puts "Processing file: #{filename}"
File.open(filename) do |filename|
# Do stuff
end
end
puts "Done processing file: #{filename}"
end
# Define the number of Ractors in the pool