Moses25 commited on
Commit
745d04a
1 Parent(s): b1f4b95

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +72 -3
README.md CHANGED
@@ -1,3 +1,72 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ ---
4
+ Train your custom long context model with llama-recipes.Based on [Moses25/Mistral-7B-Base-V1](Moses25/Mistral-7B-Base-V1)
5
+ ```shell
6
+ git clone https://github.com/moseshu/llama-recipes
7
+ sh ft.sh
8
+ ```
9
+
10
+ ```python
11
+ from transformers import GenerationConfig, LlamaForCausalLM, LlamaTokenizer,AutoTokenizer,AutoModelForCausalLM,MistralForCausalLM
12
+ import torch
13
+ model = AutoModelForCausalLM.from_pretrained(model_id,torch_dtype=torch.bfloat16,device_map="auto",)
14
+ from transformers import GenerationConfig, LlamaForCausalLM, LlamaTokenizer,AutoTokenizer,AutoModelForCausalLM,MistralForCausalLM
15
+ import torch
16
+
17
+
18
+ model_id=Moses25/Mistral-7B-Instruct-32K-GPTQ-INT8
19
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
20
+
21
+
22
+ mistral_template="{% if messages[0]['role'] == 'system' %}{% set loop_messages = messages[1:] %}{% set system_message = messages[0]['content'] %}{% else %}{% set loop_messages = messages %}{% set system_message = false %}{% endif %}{% for message in loop_messages %}{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}{% endif %}{% if loop.index0 == 0 and system_message != false %}{% set content = '<<SYS>>\\n' + system_message + '\\n<</SYS>>\\n\\n' + message['content'] %}{% else %}{% set content = message['content'] %}{% endif %}{% if message['role'] == 'user' %}{{ bos_token + '[INST] ' + content.strip() + ' [/INST]' }}{% elif message['role'] == 'assistant' %}{{ ' ' + content.strip() + ' ' + eos_token }}{% endif %}{% endfor %}"
23
+
24
+ llama3_template="{% set loop_messages = messages %}{% for message in loop_messages %}{% set content = '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'+ message['content'] | trim + '<|eot_id|>' %}{% if loop.index0 == 0 %}{% set content = bos_token + content %}{% endif %}{{ content }}{% endfor %}{{ '<|start_header_id|>assistant<|end_header_id|>\n\n' }}"
25
+
26
+ def chat_format(conversation:list,tokenizer,chat_type="mistral"):
27
+ system_prompt = "You are a helpful, respectful and honest assistant.Help humman as much as you can."
28
+ ap = [{"role":"system","content":system_prompt}] + conversation
29
+ if chat_type=='mistral':
30
+ id = tokenizer.apply_chat_template(ap,chat_template=mistral_template,tokenize=False)
31
+ elif chat_type=='llama3':
32
+ id = tokenizer.apply_chat_template(ap,chat_template=llama3_template,tokenize=False)
33
+ id = id.rstrip("<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n")
34
+ return id
35
+
36
+ user_chat=[{"role":"user","content":"In a basket, there are 20 oranges, 60 apples, and 40 bananas. If 15 pears were added, and half of the oranges were removed, what would be the new ratio of oranges to apples, bananas, and pears combined within the basket?"}]
37
+ text = chat_format(user_chat,tokenizer,'mistral')
38
+ def predict(content_prompt):
39
+ inputs = tokenizer(content_prompt,return_tensors="pt",add_special_tokens=True)
40
+ input_ids = inputs["input_ids"].to("cuda:0")
41
+ # print(f"input length:{len(input_ids[0])}")
42
+ with torch.no_grad():
43
+ generation_output = model.generate(
44
+ input_ids=input_ids,
45
+ #generation_config=generation_config,
46
+ return_dict_in_generate=True,
47
+ output_scores=True,
48
+ max_new_tokens=2048,
49
+ top_p=0.9,
50
+ num_beams=1,
51
+ do_sample=True,
52
+ repetition_penalty=1.0,
53
+ eos_token_id=tokenizer.eos_token_id,
54
+ pad_token_id=tokenizer.pad_token_id,
55
+ )
56
+ s = generation_output.sequences[0]
57
+ output = tokenizer.decode(s,skip_special_tokens=True)
58
+ output1 = output.split("[/INST]")[-1].strip()
59
+ # print(output1)
60
+ return output1
61
+
62
+ predict(text)
63
+ output:"""Let's break down the steps to find the new ratio of oranges to apples, bananas, and pears combined:
64
+ Calculate the total number of fruits initially in the basket: Oranges: 20 Apples: 60 Bananas: 40 Total Fruits = 20 + 60 + 40 = 120
65
+ Add 15 pears: Total Fruits after adding pears = 120 + 15 = 135
66
+ Remove half of the oranges: Oranges remaining = 20 / 2 = 10
67
+ Calculate the total number of fruits remaining in the basket after removing half of the oranges: Total Remaining Fruits = 10 (oranges) + 60 (apples) + 40 (bananas) + 15 (pears) = 125
68
+ Find the ratio of oranges to apples, bananas, and pears combined: Ratio of Oranges to (Apples, Bananas, Pears) Combined = Oranges / (Apples + Bananas + Pears) = 10 / (60 + 40 + 15) = 10 / 115
69
+ So, the new ratio of oranges to apples, bananas, and pears combined within the basket is 10:115.
70
+ However, I should note that the actual fruit distribution in your basket may vary depending on how you decide to count and categorize the fruits. The example calculation provides a theoretical ratio based on the initial quantities mentioned."""
71
+
72
+ ```