Generate text with multiple LoRA adapters

Source https://github.com/NVIDIA/TensorRT-LLM/tree/main/examples/llm-api/llm_multilora.py.

 1### Generate text with multiple LoRA adapters
 2from huggingface_hub import snapshot_download
 3
 4from tensorrt_llm import LLM, BuildConfig
 5from tensorrt_llm.executor import LoRARequest
 6from tensorrt_llm.lora_manager import LoraConfig
 7
 8
 9def main():
10
11    # Download the LoRA adapters from huggingface hub.
12    lora_dir1 = snapshot_download(repo_id="snshrivas10/sft-tiny-chatbot")
13    lora_dir2 = snapshot_download(
14        repo_id="givyboy/TinyLlama-1.1B-Chat-v1.0-mental-health-conversational")
15    lora_dir3 = snapshot_download(repo_id="barissglc/tinyllama-tarot-v1")
16
17    # Currently, we need to pass at least one lora_dir to LLM constructor via build_config.lora_config.
18    # This is necessary because it requires some configuration in the lora_dir to build the engine with LoRA support.
19    build_config = BuildConfig()
20    build_config.lora_config = LoraConfig(lora_dir=[lora_dir1])
21    llm = LLM(model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
22              enable_lora=True,
23              max_lora_rank=64,
24              build_config=build_config)
25
26    # Sample prompts
27    prompts = [
28        "Hello, tell me a story: ",
29        "Hello, tell me a story: ",
30        "I've noticed you seem a bit down lately. Is there anything you'd like to talk about?",
31        "I've noticed you seem a bit down lately. Is there anything you'd like to talk about?",
32        "In this reading, the Justice card represents a situation where",
33        "In this reading, the Justice card represents a situation where",
34    ]
35
36    # At runtime, multiple LoRA adapters can be specified via lora_request; None means no LoRA used.
37    for output in llm.generate(prompts,
38                               lora_request=[
39                                   None,
40                                   LoRARequest("chatbot", 1, lora_dir1), None,
41                                   LoRARequest("mental-health", 2, lora_dir2),
42                                   None,
43                                   LoRARequest("tarot", 3, lora_dir3)
44                               ]):
45        prompt = output.prompt
46        generated_text = output.outputs[0].text
47        print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
48
49    # Got output like
50    # Prompt: 'Hello, tell me a story: ', Generated text: '1. Start with a question: "What\'s your favorite color?" 2. Ask a question that leads to a story: "What\'s your'
51    # Prompt: 'Hello, tell me a story: ', Generated text: '1. A person is walking down the street. 2. A person is sitting on a bench. 3. A person is reading a book.'
52    # Prompt: "I've noticed you seem a bit down lately. Is there anything you'd like to talk about?", Generated text: "\n\nJASON: (smiling) No, I'm just feeling a bit overwhelmed lately. I've been trying to"
53    # Prompt: "I've noticed you seem a bit down lately. Is there anything you'd like to talk about?", Generated text: "\n\nJASON: (sighs) Yeah, I've been struggling with some personal issues. I've been feeling like I'm"
54    # Prompt: 'In this reading, the Justice card represents a situation where', Generated text: 'you are being asked to make a decision that will have a significant impact on your life. The card suggests that you should take the time to consider all the options'
55    # Prompt: 'In this reading, the Justice card represents a situation where', Generated text: 'you are being asked to make a decision that will have a significant impact on your life. It is important to take the time to consider all the options and make'
56
57
58if __name__ == '__main__':
59    main()