| 16 | |
| 17 | |
| 18 | class ToolLLaMA: |
| 19 | def __init__( |
| 20 | self, |
| 21 | model_name_or_path: str, |
| 22 | template:str="tool-llama-single-round", |
| 23 | device: str="cuda", |
| 24 | cpu_offloading: bool=False, |
| 25 | max_sequence_length: int=8192 |
| 26 | ) -> None: |
| 27 | super().__init__() |
| 28 | self.model_name = model_name_or_path |
| 29 | self.template = template |
| 30 | self.max_sequence_length = max_sequence_length |
| 31 | self.tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, use_fast=False, model_max_length=self.max_sequence_length) |
| 32 | self.model = AutoModelForCausalLM.from_pretrained( |
| 33 | model_name_or_path, low_cpu_mem_usage=True |
| 34 | ) |
| 35 | if self.tokenizer.pad_token_id == None: |
| 36 | self.tokenizer.add_special_tokens({"bos_token": "<s>", "eos_token": "</s>", "pad_token": "<pad>"}) |
| 37 | self.model.resize_token_embeddings(len(self.tokenizer)) |
| 38 | self.use_gpu = (True if device == "cuda" else False) |
| 39 | if (device == "cuda" and not cpu_offloading) or device == "mps": |
| 40 | self.model.to(device) |
| 41 | self.chatio = SimpleChatIO() |
| 42 | |
| 43 | def prediction(self, prompt: str, stop: Optional[List[str]] = None) -> str: |
| 44 | with torch.no_grad(): |
| 45 | gen_params = { |
| 46 | "model": "", |
| 47 | "prompt": prompt, |
| 48 | "temperature": 0.5, |
| 49 | "max_new_tokens": 512, |
| 50 | "stop": "</s>", |
| 51 | "stop_token_ids": None, |
| 52 | "echo": False |
| 53 | } |
| 54 | generate_stream_func = generate_stream |
| 55 | output_stream = generate_stream_func(self.model, self.tokenizer, gen_params, "cuda", self.max_sequence_length, force_generate=True) |
| 56 | outputs = self.chatio.return_output(output_stream) |
| 57 | prediction = outputs.strip() |
| 58 | return prediction |
| 59 | |
| 60 | def add_message(self, message): |
| 61 | self.conversation_history.append(message) |
| 62 | |
| 63 | def change_messages(self,messages): |
| 64 | self.conversation_history = messages |
| 65 | |
| 66 | def display_conversation(self, detailed=False): |
| 67 | role_to_color = { |
| 68 | "system": "red", |
| 69 | "user": "green", |
| 70 | "assistant": "blue", |
| 71 | "function": "magenta", |
| 72 | } |
| 73 | print("before_print"+"*"*50) |
| 74 | for message in self.conversation_history: |
| 75 | print_obj = f"{message['role']}: {message['content']} " |
no outgoing calls
no test coverage detected