from https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI/blob/main/app.py system_message = "\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, d
(model, tokenizer,
max_length: int = 256, num_beams=1, top_p=0.7, top_k=0, temperature=0.95)
| 8 | import os |
| 9 | |
| 10 | def chat(model, tokenizer, |
| 11 | max_length: int = 256, num_beams=1, top_p=0.7, top_k=0, temperature=0.95): |
| 12 | """from https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI/blob/main/app.py |
| 13 | system_message = "\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information." |
| 14 | input_prompt = f"[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n " |
| 15 | for interaction in chatbot: |
| 16 | input_prompt = input_prompt + str(interaction[0]) + " [/INST] " + str(interaction[1]) + " </s><s> [INST] " |
| 17 | |
| 18 | input_prompt = input_prompt + str(message) + " [/INST] " |
| 19 | """ |
| 20 | prompt = "[INST] <<SYS>>\n\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\n<</SYS>>\n\n What is the capital of China? [/INST] " |
| 21 | inputs = tokenizer([prompt], return_tensors="pt", add_special_tokens=False).to(model.parameters().__next__().device)['input_ids'][0] |
| 22 | seq = torch.cat( |
| 23 | [inputs, torch.tensor([-1]*(max_length-len(inputs)), device=inputs.device)], dim=0 |
| 24 | ) |
| 25 | strategy = BaseStrategy(temperature=temperature, top_p=top_p, top_k=0, end_tokens=[tokenizer.eos_token_id]) |
| 26 | strategy = BeamSearchStrategy(temperature=temperature, top_p=top_p, top_k=0, end_tokens=[tokenizer.eos_token_id], num_beams=num_beams, consider_end=True) |
| 27 | |
| 28 | output = filling_sequence( |
| 29 | model, seq, |
| 30 | batch_size=1, |
| 31 | strategy=strategy |
| 32 | )[0] # drop memory |
| 33 | |
| 34 | # --------------- |
| 35 | # port from inference_glm.py, more general than chat mode |
| 36 | # clip -1s and fill back generated things into seq |
| 37 | output_list = list(output) |
| 38 | |
| 39 | response = tokenizer.decode(output_list[0]) |
| 40 | if get_model_parallel_rank() == 0: |
| 41 | with open(f"{torch.distributed.get_rank()}.txt", 'w') as f: |
| 42 | f.write(response) |
| 43 | |
| 44 | if __name__ == "__main__": |
| 45 | import argparse |
no test coverage detected