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,
BitsAndBytesConfig,
)
from flexygen import Insertable
1. Load HF Tokenizer and Model
tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B")
model = AutoModelForCausalLM.from_pretrained(
"deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B",
quantization_config=BitsAndBytesConfig(load_in_4bit=True),
)
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)
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flexygen-0.0.2.tar.gz.
File metadata
- Download URL: flexygen-0.0.2.tar.gz
- Upload date:
- Size: 20.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c204d5a176c9695692ff6afe5568ed1e597178fb7423b3a2a8ac3378349f43c8
|
|
| MD5 |
9a04e68391dae3b5c91b1f90d81b8f26
|
|
| BLAKE2b-256 |
ff61271b4e1c3952dd573791c57092636800b38f5983ef17fddd338f5c4aefdd
|
File details
Details for the file flexygen-0.0.2-py3-none-any.whl.
File metadata
- Download URL: flexygen-0.0.2-py3-none-any.whl
- Upload date:
- Size: 21.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d5fc4d5f74bd57f580d33e59f0390b212f07b2360b1de4fa7433764a8ea74286
|
|
| MD5 |
b7fab414895de39f9340c5920acdffac
|
|
| BLAKE2b-256 |
fa5343c7b13379307dc5999021804ed7a01fc723b4519f850dd5a441b5c35131
|