Views
No views yet
pip install -r requirements.txt1
2from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
3from llama_condense_monkey_patch import replace_llama_with_condense
4from peft import PeftConfig
5from peft import PeftModel
6import torch
7
8## config device params & load model
9peft_model_id = "mingkuan/longchat-7b-qlora-customer-support"
10base_model_id = "lmsys/longchat-7b-16k"
11
12config = AutoConfig.from_pretrained(base_model_id)
13replace_llama_with_condense(config.rope_condense_ratio)
14tokenizer = AutoTokenizer.from_pretrained(base_model_id, use_fast=False)
15
16kwargs = {"torch_dtype": torch.float16}
17kwargs["device_map"] = "auto"
18nf4_config = BitsAndBytesConfig(
19 load_in_4bit=True,
20 bnb_4bit_quant_type="nf4",
21 bnb_4bit_use_double_quant=True,
22 bnb_4bit_compute_dtype=torch.bfloat16
23)
24model = AutoModelForCausalLM.from_pretrained(
25 base_model_id,
26 return_dict=True,
27 trust_remote_code=True,
28 quantization_config=nf4_config,
29 load_in_4bit=True,
30 **kwargs
31)
32model = PeftModel.from_pretrained(model, peft_model_id)1
2def getLLMResponse(prompt):
3 device = "cuda"
4 input_ids = tokenizer(prompt, return_tensors='pt').input_ids.cuda()
5 output = model.generate(inputs=input_ids, temperature=0.5, max_new_tokens=256)
6 promptLen = len(prompt)
7 response = tokenizer.decode(output[0], skip_special_tokens=True)[promptLen:] ## omit the user input part
8 return response
9
10query = 'help me to setup my new shipping address.'
11response = getLLMResponse(generate_prompt(query))
12print(f'\nUserInput:{query}\n\nLLM:\n{response}\n\n')
131{
2"category": "SHIPPING",
3"intent": "setup_new_shipping_address",
4"answer": "Sure, I can help you with that. Can you please provide me your full name, current shipping address, and the new shipping address you would like to set up?"
5}