Earlier, we switched between two adapters, one request at a time. But a real server gets requests for different adapters, all mixed together. Can one batch use different adapters?
Yes, it can. We load both adapters onto one base model. Each job names an adapter, and a message. We build one batch, with the right system message for each. Then adapter names tells generate which adapter to use, for each row. Run it. In one batch, ticket rows get JSON, and reply rows get friendly answers.
app.pypython
from transformers import AutoTokenizer, AutoModelForCausalLMfrom peft import PeftModelNAME = "HuggingFaceTB/SmolLM2-135M-Instruct"tok = AutoTokenizer.from_pretrained(NAME, padding_side="left")model = PeftModel.from_pretrained(AutoModelForCausalLM.from_pretrained(NAME), "ticket-lora", adapter_name="tickets")model.load_adapter("reply-lora", adapter_name="reply")model.eval()SYSTEMS = {"tickets": "Read the customer's message. Reply with JSON: the intent and the book.", "reply": "Write a short, friendly reply to the customer."}jobs = [("tickets", "Is Dune in stock?"), ("reply", "Is Dune in stock?"), ("tickets", "My Circe arrived torn."), ("reply", "My Circe arrived torn.")]chats = [[{"role": "system", "content": SYSTEMS[a]}, {"role": "user", "content": m}] for a, m in jobs]ids = tok.apply_chat_template(chats, add_generation_prompt=True, padding=True, return_dict=True, return_tensors="pt")out = model.generate(**ids, max_new_tokens=30, do_sample=False, adapter_names=[a for a, m in jobs])for (a, m), row in zip(jobs, out): print(f"{a:8}", tok.decode(row[ids["input_ids"].shape[1]:], skip_special_tokens=True)[:76])
Output
tickets {"intent": "stock", "book": "Dune"}
reply Good question! I'm checking our shelves for Dune now, and I'll confirm by em
tickets {"intent": "complaint", "book": "Circe"}
reply I'm so sorry about your Circe. I've passed this to our manager, who will con
This is how companies serve hundreds of fine-tuned models cheaply. One base model sits in memory. Each customer, or task, has its own small adapter. And requests for different adapters share the same batch. VLLM, and other servers, do this automatically.
Let's recap. One base model can hold many adapters. Adapter names mixes them in one batch. And this is how large services run many fine-tuned models cheaply. Next, what fine-tuning really costs.