Instructions to use songff/SinglePO with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use songff/SinglePO with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="songff/SinglePO") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("songff/SinglePO") model = AutoModelForCausalLM.from_pretrained("songff/SinglePO") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use songff/SinglePO with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "songff/SinglePO" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "songff/SinglePO", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/songff/SinglePO
- SGLang
How to use songff/SinglePO with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "songff/SinglePO" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "songff/SinglePO", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "songff/SinglePO" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "songff/SinglePO", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use songff/SinglePO with Docker Model Runner:
docker model run hf.co/songff/SinglePO
Create tokenization.py
Browse files- tokenization.py +53 -0
tokenization.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PreTrainedTokenizerFast
|
| 2 |
+
|
| 3 |
+
class SinglePOTokenizer(PreTrainedTokenizerFast):
|
| 4 |
+
def __init__(self, *args, **kwargs):
|
| 5 |
+
super().__init__(*args, **kwargs)
|
| 6 |
+
|
| 7 |
+
def get_context(
|
| 8 |
+
self,
|
| 9 |
+
raw_instruction,
|
| 10 |
+
rule_description,
|
| 11 |
+
):
|
| 12 |
+
prompt = "You are an expert prompt engineer." + " "
|
| 13 |
+
prompt += "Please help me optimize this prompt to get better response:\n\n[The Start of Raw Prompt]\n{}\n[The End of Raw Prompt]".format(raw_instruction)
|
| 14 |
+
prompt += "\n\nYou should optimize this prompt by {}".format(rule_description)
|
| 15 |
+
|
| 16 |
+
context = self.apply_chat_template(
|
| 17 |
+
[
|
| 18 |
+
{
|
| 19 |
+
"role": "user",
|
| 20 |
+
"content": prompt,
|
| 21 |
+
}
|
| 22 |
+
],
|
| 23 |
+
add_generation_prompt=True,
|
| 24 |
+
tokenize=False,
|
| 25 |
+
) + "The Optimized Prompt:\n\n[The Start of Optimized Prompt"
|
| 26 |
+
|
| 27 |
+
return context
|
| 28 |
+
|
| 29 |
+
def parse_output(
|
| 30 |
+
self,
|
| 31 |
+
output_text,
|
| 32 |
+
raw_instruction = "", # recommend to provide, so when some error happened, we can still use the raw instruction
|
| 33 |
+
):
|
| 34 |
+
better_instruction = "The Optimized Prompt:\n\n[The Start of Optimized Prompt" + output_text
|
| 35 |
+
|
| 36 |
+
if "[The Start of Optimized Prompt]" in better_instruction:
|
| 37 |
+
better_instruction = better_instruction[better_instruction.index("[The Start of Optimized Prompt]") + len("[The Start of Optimized Prompt]"):]
|
| 38 |
+
if better_instruction.startswith("\n"):
|
| 39 |
+
better_instruction = better_instruction[1:]
|
| 40 |
+
if "[The End of Optimized Prompt]" in better_instruction:
|
| 41 |
+
better_instruction = better_instruction[:better_instruction.index("[The End of Optimized Prompt]")]
|
| 42 |
+
if better_instruction.endswith("\n"):
|
| 43 |
+
better_instruction = better_instruction[:-1]
|
| 44 |
+
if "The Optimized Prompt:" in better_instruction: # almost error happened
|
| 45 |
+
better_instruction = better_instruction[:better_instruction.index("The Optimized Prompt:")]
|
| 46 |
+
|
| 47 |
+
if better_instruction.strip() == "": # some error may happen in optimization, so use the raw instruction
|
| 48 |
+
better_instruction = raw_instruction
|
| 49 |
+
|
| 50 |
+
if "The Optimized" in better_instruction: # still some error happened
|
| 51 |
+
better_instruction = raw_instruction
|
| 52 |
+
|
| 53 |
+
return better_instruction
|