Skip to content

Instantly share code, notes, and snippets.

@cpfiffer
Created September 10, 2024 21:11
Show Gist options
  • Save cpfiffer/d35f57b060b35435e5e09495169753e4 to your computer and use it in GitHub Desktop.
Save cpfiffer/d35f57b060b35435e5e09495169753e4 to your computer and use it in GitHub Desktop.
Coin flipping with outlines
import outlines
from transformers import BitsAndBytesConfig
# Load the model
model = outlines.models.transformers(
"microsoft/Phi-3-mini-4k-instruct",
model_kwargs={
'quantization_config':BitsAndBytesConfig(
# Load the model in 4-bit mode
load_in_4bit=True,
# Can use 8-bit for more precision
# load_in_8bit=True,
)
}
)
# Specify temperature in sampler
sampler = outlines.samplers.multinomial(samples=100)
# Define the coinflip choice
coinflip_regex_pattern = r"[H|T]"
generator = outlines.generate.choice(
model,
["H", "T"],
sampler=sampler
)
output = generator("Flip a coin, respond with H or T: ")
print(output)
# Count the occurrences of each outcome
heads_count = output.count("H")
tails_count = output.count("T")
print(f"Heads: {heads_count}, Tails: {tails_count}")
# Heads: 80, Tails: 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment