Moses25 commited on
Commit
f0b3110
·
verified ·
1 Parent(s): e3ce47e

Update README.md

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