Skip to main content

An easy-to-use LLM controllable generation tool

Project description

FlexyGen

English | 中文版

FlexyGen is an easy-to-use LLM controllable generation tool that controls and adjusts model generation content by injecting triggers and invocations into the model.

Installation

pip install flexygen

Getting Started

Demo Code: examples/emoji.py

0. Import Dependencies

import random

from typing import List
from transformers import AutoTokenizer, AutoModelForCausalLM
from flexygen import Insertable

1. Load HF Tokenizer and Model

tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")

2. Wrap the Model with FlexyGen Interface

The Insertable interface can be used to insert content into the sentence being generated when certain conditions are met.

model = Insertable.wrap(model, tokenizer)

3. Inject a Trigger

Inject a trigger named emoji into the model.

During the model generation process, the trigger is called every time a token is generated, and an invocation is triggered when the trigger returns True.

Here input_ids is the content currently generated by the model.

@model.trigger("emoji")
def emoji_trigger(input_ids) -> bool:
    sentence = tokenizer.batch_decode(input_ids)[0].strip()
    return sentence.endswith((",", ".", "!", "?"))

4. Inject an Invocation

Injects an invocation named emoji.

This invocation is triggered when the trigger of the same name returns True.

The string returned by the call is inserted after the currently generated content (Currently only batch_size=1 supported).

@model.invocation("emoji")
def generate_random_emoji(input_ids) -> List[str]:
    def random_emoji():
        ranges = [
            (0x1F600, 0x1F64F),
            (0x1F300, 0x1F5FF),
            (0x1F680, 0x1F6FF),
            (0x2700, 0x27BF),
        ]
        start, end = random.choice(ranges)
        code_point = random.randint(start, end)
        return chr(code_point)
    bsz = input_ids.size(0)
    return [random_emoji() for _ in range(bsz)]

5. Generate

input_text = tokenizer.apply_chat_template([
    {"role": "user", "content": "Why the sky is blue?"},
], tokenize=False, add_generation_prompt=True)
inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, do_sample=True, max_new_tokens=128)
# Emoji attached after each sentence.
print(tokenizer.batch_decode(outputs)[0])

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flexygen-0.0.3.tar.gz (20.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flexygen-0.0.3-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file flexygen-0.0.3.tar.gz.

File metadata

  • Download URL: flexygen-0.0.3.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.13

File hashes

Hashes for flexygen-0.0.3.tar.gz
Algorithm Hash digest
SHA256 87574f84b8cbfef4edd0164c4505a0af62238af49633ff54857327f85551aa37
MD5 cca44a84e4a6292681fae045abb88aa4
BLAKE2b-256 6dcff5dc5150b26e55c0a235b5adff4625d9aab20ddcb3d683924ed710629ecd

See more details on using hashes here.

File details

Details for the file flexygen-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: flexygen-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.13

File hashes

Hashes for flexygen-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 96eb41f1da4bffefd83555f2a1ffc0584db1f16be653a7f58fe69c98c5ef13c9
MD5 7d751e42e3d703c7f06b1f0a045e4f2e
BLAKE2b-256 1ef2845f35c68ff1250948083ef013ed3a1c32a30c9e2e2d56f4291dc7031abc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page