Views
No views yet
1# pip install -U accelerate bitsandbytes
2
3import transformers
4
5tokenizer = transformers.AutoTokenizer.from_pretrained("Sera-Network/sera-llama-3.1-8b-0.1")
6
7# 4-bit quantization to run on smaller gpus
8model = transformers.AutoModelForCausalLM.from_pretrained("Sera-Network/sera-llama-3.1-8b-0.1", device_map="auto", quantization_config=transformers.BitsAndBytesConfig(load_in_4bit=True))
9
10# Warm up the model
11input_ids = tokenizer.apply_chat_template([{"role": "user", "content": "What's the capital of France?"}], add_generation_prompt=True, return_tensors='pt').to('cuda')
12output = model.generate(input_ids, do_sample=False, max_new_tokens=128)
13preds = output[:, input_ids.shape[1]:]
14text = tokenizer.decode(preds[0], skip_special_tokens=True)
15
16# Create a helper function to generate text based on user input
17def generate(user_input: str):
18 input_ids = tokenizer.apply_chat_template([{"role": "user", "content": user_input}], add_generation_prompt=True, return_tensors='pt').to('cuda')
19 output = model.generate(input_ids, do_sample=False, max_new_tokens=128)
20 preds = output[:, input_ids.shape[1]:]
21 text = tokenizer.decode(preds[0], skip_special_tokens=True)
22 return text1generate("What's the capital of Swizerland and Germany?")
2# The capital of Switzerland is Bern. The capital of Germany is Berlin.
3
4generate("Set up a host for the domain symbiont.me")
5# [{"name": "add_host", "parameters": {"hostname": "symbiont.me"}}]
6
7generate("Send an email to my friend Andrej wishing him a happy birthday.")
8# [{"name": "send_email", "parameters": {"subject": "Happy Birthday", "body": "Dear Andrej, happy birthday! Best regards, [Your Name]"}}]
9
10generate("Schedule a call with my manager tomorrow 7 am to discuss my promotion.")
11# [{"name": "schedule_call", "parameters": {"date": "2024-07-27", "time": "07:00:00", "topic": "promotion"}}]