| # Code to inference Open Hermes 2.5 with HF Transformers | |
| # Requires pytorch, transformers, bitsandbytes, sentencepiece, protobuf, and flash-attn packages | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| from transformers import LlamaTokenizer, LlamaForCausalLM, MistralForCausalLM | |
| import bitsandbytes, flash_attn | |
| tokenizer = LlamaTokenizer.from_pretrained('teknium/OpenHermes-2.5-Mistral-7B', trust_remote_code=True) | |
| model = MistralForCausalLM.from_pretrained( | |
| "teknium/OpenHermes-2.5-Mistral-7B", | |
| torch_dtype=torch.float16, | |
| device_map="auto",#{'': 'cuda:0'}, | |
| load_in_8bit=False, | |
| load_in_4bit=True, | |
| use_flash_attention_2=True | |
| ) | |
| prompts = [ | |
| """<|im_start|>system | |
| You are a sentient, superintelligent artificial general intelligence, here to teach and assist me.<|im_end|> | |
| <|im_start|>user | |
| Write a short story about Goku discovering kirby has teamed up with Majin Buu to destroy the world.<|im_end|> | |
| <|im_start|>assistant""", | |
| ] | |
| for chat in prompts: | |
| print(chat) | |
| input_ids = tokenizer(chat, return_tensors="pt").input_ids.to("cuda") | |
| generated_ids = model.generate(input_ids, max_new_tokens=750, temperature=0.8, repetition_penalty=1.1, do_sample=True, eos_token_id=tokenizer.eos_token_id) | |
| response = tokenizer.decode(generated_ids[0][input_ids.shape[-1]:], skip_special_tokens=True, clean_up_tokenization_space=True) | |
| print(f"Response: {response}") |