Upload getting_started_with_mistral_7b_instruct.py
Browse files
getting_started_with_mistral_7b_instruct.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Getting-Started-with-Mistral-7b-Instruct.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colaboratory.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1ckGJcooxH_jiohgmb7PIKrFsDxHoUPq2
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
!pip install -q git+https://github.com/huggingface/transformers
|
| 11 |
+
|
| 12 |
+
model_path="/kaggle/input/mistral/pytorch/7b-instruct-v0.1-hf/1"
|
| 13 |
+
|
| 14 |
+
from transformers import AutoTokenizer
|
| 15 |
+
|
| 16 |
+
tokenizer=AutoTokenizer.from_pretrained(model_path)
|
| 17 |
+
|
| 18 |
+
from transformers import AutoModelForCausalLM
|
| 19 |
+
|
| 20 |
+
import torch
|
| 21 |
+
|
| 22 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
+
model_path,
|
| 24 |
+
torch_dtype = torch.bfloat16,
|
| 25 |
+
device_map = "auto",
|
| 26 |
+
trust_remote_code = True
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
messages = [{
|
| 30 |
+
"role":"user",
|
| 31 |
+
"content": "Can you tell us 3 cities to visit in Turkey"
|
| 32 |
+
}]
|
| 33 |
+
|
| 34 |
+
tokenizer.apply_chat_template(messages, tokenize=False)
|
| 35 |
+
|
| 36 |
+
model_inputs = tokenizer.apply_chat_template(messages,
|
| 37 |
+
return_tensors = "pt")
|
| 38 |
+
|
| 39 |
+
model_inputs
|
| 40 |
+
|
| 41 |
+
generated_ids = model.generate(
|
| 42 |
+
model_inputs,
|
| 43 |
+
max_new_tokens = 1000,
|
| 44 |
+
do_sample = True,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
decoded = tokenizer.batch_decode(generated_ids)
|
| 48 |
+
|
| 49 |
+
print(decoded[0])
|
| 50 |
+
|
| 51 |
+
messages = [{
|
| 52 |
+
"role": "user",
|
| 53 |
+
"content": "Act as a gourmet chef. I have a friend coming over who is a vegetarian. \
|
| 54 |
+
I want to impress my friend with a special vegetarian dish. \
|
| 55 |
+
What do you recommend? \
|
| 56 |
+
Give me two options, along with the whole recipe for each"
|
| 57 |
+
}]
|
| 58 |
+
|
| 59 |
+
model_inputs = tokenizer.apply_chat_template(messages,return_tensors = "pt")
|
| 60 |
+
|
| 61 |
+
generated_ids = model.generate(
|
| 62 |
+
model_inputs,
|
| 63 |
+
max_new_tokens = 1000,
|
| 64 |
+
do_sample = True,
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
decoded = tokenizer.batch_decode(generated_ids)
|
| 68 |
+
|
| 69 |
+
print(decoded[0])
|
| 70 |
+
|
| 71 |
+
messages = [{
|
| 72 |
+
"role": "user", "content": "How many helicopters can a human eat in one sitting?"
|
| 73 |
+
}]
|
| 74 |
+
model_inputs = tokenizer.apply_chat_template(messages,return_tensors = "pt")
|
| 75 |
+
generated_ids = model.generate(
|
| 76 |
+
model_inputs,
|
| 77 |
+
max_new_tokens = 1000,
|
| 78 |
+
do_sample = True,
|
| 79 |
+
)
|
| 80 |
+
decoded = tokenizer.batch_decode(generated_ids)
|
| 81 |
+
print(decoded[0])
|
| 82 |
+
|
| 83 |
+
messages = [
|
| 84 |
+
{"role": "user", "content": "What fascinates you about AI?"},
|
| 85 |
+
{"role": "assistant", "content": "I'm fascinated \
|
| 86 |
+
by AI's data analysis and prediction abilities. \
|
| 87 |
+
It has the potential to revolutionize industries and improve problem-solving."},
|
| 88 |
+
{"role": "user", "content": "Should people be afraid of AI?"}
|
| 89 |
+
]
|
| 90 |
+
model_inputs = tokenizer.apply_chat_template(messages,return_tensors = "pt")
|
| 91 |
+
generated_ids = model.generate(
|
| 92 |
+
model_inputs,
|
| 93 |
+
max_new_tokens = 1000,
|
| 94 |
+
do_sample = True,
|
| 95 |
+
)
|
| 96 |
+
decoded = tokenizer.batch_decode(generated_ids)
|
| 97 |
+
print(decoded[0])
|
| 98 |
+
|
| 99 |
+
messages = [
|
| 100 |
+
{"role": "user", "content": "Türkiye'de en fazla ziyaret edilen 3 şehir hangidir?"},
|
| 101 |
+
]
|
| 102 |
+
model_inputs = tokenizer.apply_chat_template(messages,return_tensors = "pt")
|
| 103 |
+
generated_ids = model.generate(
|
| 104 |
+
model_inputs,
|
| 105 |
+
max_new_tokens = 1000,
|
| 106 |
+
do_sample = True,
|
| 107 |
+
)
|
| 108 |
+
decoded = tokenizer.batch_decode(generated_ids)
|
| 109 |
+
print(decoded[0])
|
| 110 |
+
|
| 111 |
+
"""Let's connect [YouTube](http://youtube.com/tirendazacademy) | [Medium](http://tirendazacademy.medium.com) | [X](http://x.com/tirendazacademy) | [Linkedin](https://www.linkedin.com/in/tirendaz-academy)"""
|