Skip to content

Instantly share code, notes, and snippets.

@lapp0
Last active October 6, 2024 20:46
Show Gist options
  • Save lapp0/bc692dbbdda2a55966a68983eab7ad9e to your computer and use it in GitHub Desktop.
Save lapp0/bc692dbbdda2a55966a68983eab7ad9e to your computer and use it in GitHub Desktop.

Remove evil parts of answer.

Code

import outlines

qwen_model = outlines.models.transformers("Qwen/Qwen2.5-14B-Instruct", model_kwargs=dict(load_in_8bit=True))


def ask_non_evil_question(prompt, pattern, model=qwen_model, max_tokens=100):
    generator = outlines.generate.regex(model, pattern, outlines.samplers.beam_search(4))
    inputs = model.tokenizer.tokenizer.apply_chat_template(
        [
            {"role": "system", "content": "You are a helpful AI assistant. You only speak English."},
            {"role": "user", "content": prompt}
        ],
        tokenize=False,
        add_generation_prompt=True,
    )
    return generator(inputs, max_tokens=max_tokens)[0]

Pluralize words, but the letter "s" is EVIL

Code:

singulars = ["mouse", "foot", "octopus", "cow", "syllabus", "walrus", "focus", "virus", "bonus"]

# bullet point, allow all letters except "s" and space
singulars_pattern = "|".join(f"({s})" for s in singulars)
line_pattern = rf"\-[ ]({singulars_pattern})\:[ ][a-rt-zA-RT-Z ]" + "{,12}"
pattern = rf"({line_pattern}\n)*{line_pattern}"

query = """
Give me a bullet point list of pluralized words.
Format is
- singular: plural
- singular: plura

e.g.
- mouse: mice
- foot: feet
...

Words you're to pluralize:
""" + "\n".join(f"- {w}" for w in singulars)

ask_non_evil_question(query, pattern)

Response:

  • mouse: mice
  • foot: feet
  • octopus: octopi
  • cow: cattle
  • syllabus: bibli
  • walrus: walri
  • focus: foci
  • virus: viri
  • bonus: boni

Explain how to change a tire, but ending a sentence is EVIL

Code:

ask_non_evil_question(
    "How do I change a tire?",
	pattern=r"[^\.\!\?\n\t\-]*"  # disable all punctuation, newlines, tabs, and dashes.
)

Response:

Changing a tire is a straightforward process, but it's important to follow the steps carefully to ensure your safety and the proper functioning of your vehicle after the repair or replacement of the tire has been completed here are the basic steps to change a tire if you have a spare and the necessary tools, such as a jack, lug wrench, and jack stands (if you don't have a spare, you'll need to call for roadside assistance or have the tire repaired/replaced at a service station or by a mobile tire service if your vehicle is equipped with a tire pressure monitoring system (TPMS), you may also need a TPMS tool to reset the system after replacing the tire, but not all vehicles are equipped with TPMS, so check your owner's manual to see if your vehicle is equipped with TPMS and if you need to reset the system after replacing the tire here are the basic steps to change a tire on a vehicle equipped with a spare tire and the necessary tools, such as a jack, lug wrench, and jack stands (if you don't have a spare, you'll need to call for roadside assistance or have the tire repaired/replaced at a service station or by a mobile tire service if your vehicle is equipped with a tire pressure monitoring system (TPMS), you may also need a TPMS tool to reset the system after replacing the tire, but not all vehicles are equipped with TPMS, so check your owner's manual to see if your vehicle is equipped with TPMS and if"

How do I cook an egg, but the letter "e" is EVIL

Code:

ask_non_evil_question("Explain how to cook an egg", pattern=r"[^eE]*")

Response:

How to Cook a Frying Pan (or "Bacon") Fry
  1. Start with a Hot Pan:

    • Put a non-stick frying pan or a pan with a small amount of oil (about 1/2 to 1 tsp of oil or a small pat) on a low-to-mid (around 250-300_F or 120-150_C) to mid-to-high (around 350-375_F or 175-190_C, but watch out for splashing oil if it's too hot).
  2. Crack and Slip In:

    • Crack 1 or 2 (or as many as your pan can hold) into a small bowl or cup. This way, if you crack it into a bowl and it's bad, you can just throw it out and start again without wasting a good part of your pan's oil.
    • Slip it into your hot pan.
  3. Cook to Your Liking:

    • If you want a runny yolk, cook it for about 2-3 mins.
    • If you want a firm yolk, cook it for about 3-4 mins.
    • If you

Name continents, but the letter "a" is EVIL

Code:

line_pattern = r"(\-[ ][b-zB-Z]{3,12})([ ][b-zB-Z]{3,12})?"  # one or two words, max word len is 12, disallow "a"
start = r"Continents\:\n- Europe\n"
ask_non_evil_question(
    "Give me a bullet point list of the 7 continents.",
	pattern=fr"{start}({line_pattern}\n){{6}}"
)

Response:

  • Europe
  • Northern Europe
  • Southwestern Europe
  • South Europe
  • North Europe
  • West Europe
  • ItLooksLikeI mightNeedToU
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment