Moses25 commited on
Commit
262558a
·
verified ·
1 Parent(s): f184156

Update README.md

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