Moses25 commited on
Commit
8d46c88
1 Parent(s): b9c1273

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +117 -3
README.md CHANGED
@@ -1,3 +1,117 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ ---
4
+ ---
5
+ license: apache-2.0
6
+ ---
7
+ ### This model is trained from Mistral-7B-Instruct-V0.2 with 90% chinese dataset and 10% english dataset
8
+ github [Web-UI](https://github.com/moseshu/llama2-chat/tree/main/webui)
9
+
10
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/62f4c7172f63f904a0c61ba3/NGBrnC1eaeTcDE_A3niyn.png)
11
+
12
+ ```python
13
+ from transformers import GenerationConfig, LlamaForCausalLM, LlamaTokenizer,AutoTokenizer,AutoModelForCausalLM,MistralForCausalLM
14
+ import torch
15
+ model = AutoModelForCausalLM.from_pretrained(model_id,torch_dtype=torch.bfloat16,device_map="auto",)
16
+ from transformers import GenerationConfig, LlamaForCausalLM, LlamaTokenizer,AutoTokenizer,AutoModelForCausalLM,MistralForCausalLM
17
+ import torch
18
+
19
+
20
+ model_id=Moses25/Meta-LlaMA-3-8B-Instruct-16k
21
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
22
+
23
+
24
+ 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 %}"
25
+
26
+ 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' }}"
27
+
28
+ def chat_format(conversation:list,tokenizer,chat_type="mistral"):
29
+ system_prompt = "You are a helpful, respectful and honest assistant.Help humman as much as you can."
30
+ ap = [{"role":"system","content":system_prompt}] + conversation
31
+ if chat_type=='mistral':
32
+ id = tokenizer.apply_chat_template(ap,chat_template=mistral_template,tokenize=False)
33
+ elif chat_type=='llama3':
34
+ id = tokenizer.apply_chat_template(ap,chat_template=llama3_template,tokenize=False)
35
+ id = id.rstrip("<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n")
36
+ return id
37
+
38
+ 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?"}]
39
+ text = chat_format(user_chat,tokenizer,'llama3')
40
+ def predict(content_prompt):
41
+ inputs = tokenizer(content_prompt,return_tensors="pt",add_special_tokens=True)
42
+ input_ids = inputs["input_ids"].to("cuda:0")
43
+ # print(f"input length:{len(input_ids[0])}")
44
+ with torch.no_grad():
45
+ generation_output = model.generate(
46
+ input_ids=input_ids,
47
+ #generation_config=generation_config,
48
+ return_dict_in_generate=True,
49
+ output_scores=True,
50
+ max_new_tokens=2048,
51
+ top_p=0.9,
52
+ num_beams=1,
53
+ do_sample=True,
54
+ repetition_penalty=1.0,
55
+ eos_token_id=tokenizer.eos_token_id,
56
+ pad_token_id=tokenizer.pad_token_id,
57
+ )
58
+ s = generation_output.sequences[0]
59
+ output = tokenizer.decode(s,skip_special_tokens=True)
60
+ output1 = output.split("<|eot_id|>")[-1].strip()
61
+ # print(output1)
62
+ return output1
63
+
64
+ predict(text)
65
+ output:"""Let's break down the steps to find the new ratio of oranges to apples, bananas, and pears combined:
66
+ Calculate the total number of fruits initially in the basket: Oranges: 20 Apples: 60 Bananas: 40 Total Fruits = 20 + 60 + 40 = 120
67
+ Add 15 pears: Total Fruits after adding pears = 120 + 15 = 135
68
+ Remove half of the oranges: Oranges remaining = 20 / 2 = 10
69
+ 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
70
+ 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
71
+ So, the new ratio of oranges to apples, bananas, and pears combined within the basket is 10:115.
72
+ 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."""
73
+ ```
74
+
75
+
76
+ ## vLLM server
77
+ ```shell
78
+ #llama3-chat-template.jinja file is chat-template above 'llama3-template'
79
+ model_path=Meta-LlaMA-3-8B-Instruct-16k
80
+ python -m vllm.entrypoints.openai.api_server --model=$model_path \
81
+ --trust-remote-code --host 0.0.0.0 --port 7777 \
82
+ --gpu-memory-utilization 0.8 \
83
+ --max-model-len 8192 --chat-template llama3-chat-template.jinja \
84
+ --tensor-parallel-size 1 --served-model-name chatbot
85
+ ```
86
+
87
+ ```python
88
+ from openai import OpenAI
89
+ # Set OpenAI's API key and API base to use vLLM's API server.
90
+ openai_api_key = "EMPTY"
91
+ openai_api_base = "http://localhost:7777/v1"
92
+
93
+ client = OpenAI(
94
+ api_key=openai_api_key,
95
+ base_url=openai_api_base,
96
+ )
97
+ call_args = {
98
+ 'temperature': 0.7,
99
+ 'top_p': 0.9,
100
+ 'top_k': 40,
101
+ 'max_tokens': 2048, # output-len
102
+ 'presence_penalty': 1.0,
103
+ 'frequency_penalty': 0.0,
104
+ "repetition_penalty":1.0,
105
+ "stop":["<|eot_id|>","<|end_of_text|>"],
106
+ }
107
+ chat_response = client.chat.completions.create(
108
+ model="chatbot",
109
+ messages=[
110
+ {"role": "user", "content": "你好"},
111
+ ],
112
+ extra_body=call_args
113
+ )
114
+ print("Chat response:", chat_response)
115
+
116
+
117
+ ```