import json
TOOLS = [{"name": "live_giveaways_by_type",
"description": "Retrieve live giveaways by type.",
"parameters": {"type": {"type": "str", "description": "game, loot or beta"}}}]
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer
base = "openbmb/MiniCPM5-1B"
tokenizer = AutoTokenizer.from_pretrained(base)
model = AutoModelForCausalLM.from_pretrained(base, torch_dtype="bfloat16", device_map="auto")
model = PeftModel.from_pretrained(model, "HIMANSHUKUMARJHA/minicpm5-1b-toolcall-lora")
messages = [
{
"role": "system",
"content": (
"You are a function calling AI model. You are provided with function "
"signatures within <tools></tools> XML tags. Call one or more functions "
"to assist with the user query. Do not make assumptions about what "
"values to plug into functions.\n<tools>\n"
+ json.dumps(TOOLS, ensure_ascii=False)
+ "\n</tools>\nFor each call, return a JSON object inside "
"<tool_call></tool_call> tags."
),
},
{"role": "user", "content": "Show me live giveaways for beta access."},
]
inputs = tokenizer.apply_chat_template(
messages, add_generation_prompt=True, return_tensors="pt", return_dict=True
).to(model.device)
inputs.pop("token_type_ids", None)
out = model.generate(**inputs, max_new_tokens=256)
text = tokenizer.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=False)
for ctrl in ("<s>", "</s>", "<|im_start|>", "<|im_end|>"):
text = text.replace(ctrl, "")
print(text.strip())