RayNene commited on
Commit
a3c41e8
Β·
verified Β·
1 Parent(s): 1dcec6c

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +326 -15
README.md CHANGED
@@ -1,29 +1,340 @@
 
 
 
1
  # LOOP DFS Qwen Merged 16-bit Model
2
 
3
- This repository contains a merged 16-bit model fine-tuned for LOOP DFS
4
- assistance.
 
 
 
5
 
6
- ## Recommended generation configuration
7
 
8
  - `max_new_tokens`: 512
9
  - `temperature`: 0.2
10
  - `top_p`: 0.9
11
  - `repetition_penalty`: 1.05
12
 
13
- The recommended system prompt is stored in `system_prompt.txt`, and the
14
- machine-readable inference settings are stored in
15
- `deployment_config.json`.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  ## Deployment requirements
18
 
19
- Current company facts, product terms, fees, eligibility requirements,
20
- leadership information, partnerships, and API behavior should be supplied
21
- through approved retrieval sources or verified tools.
 
 
22
 
23
- Authentication and authorization must be handled outside the conversation.
24
- The assistant must never collect passwords, PINs, OTPs, CVVs, secret keys,
25
- or access tokens through chat.
26
 
27
- Financial transactions must be validated by application code, summarized
28
- for the user, and explicitly confirmed through an approved secure flow.
29
- The model must not be used as the transaction authorization layer.
 
 
 
 
 
 
1
+ ### Revised `README.md`
2
+
3
+ ````markdown
4
  # LOOP DFS Qwen Merged 16-bit Model
5
 
6
+ This repository contains a merged 16-bit Qwen model fine-tuned for LOOP DFS assistance.
7
+
8
+ The model is designed to answer questions about LOOP DFS company information, products, services, partnerships, and documented API behavior.
9
+
10
+ ## Generation configuration
11
 
12
+ The recommended generation settings are:
13
 
14
  - `max_new_tokens`: 512
15
  - `temperature`: 0.2
16
  - `top_p`: 0.9
17
  - `repetition_penalty`: 1.05
18
 
19
+ The complete system prompt is stored in `system_prompt.txt`.
20
+
21
+ Machine-readable inference settings are stored in `deployment_config.json`.
22
+
23
+ ## Quick start
24
+
25
+ Install the required packages:
26
+
27
+ ```bash
28
+ pip install -U transformers accelerate huggingface_hub sentencepiece torch
29
+ ```
30
+
31
+ Load the model and its system prompt directly from this public repository:
32
+
33
+
34
+ ```python
35
+ import torch
36
+ from huggingface_hub import hf_hub_download
37
+ from transformers import AutoModelForCausalLM, AutoTokenizer
38
+
39
+
40
+ MODEL_ID = "RayNene/Loop-DFS-Qwen-Merged"
41
+ MAX_NEW_TOKENS = 512
42
+
43
+
44
+ # Load the complete system prompt from the repository.
45
+ system_prompt_path = hf_hub_download(
46
+ repo_id=MODEL_ID,
47
+ filename="system_prompt.txt",
48
+ )
49
+
50
+ with open(system_prompt_path, "r", encoding="utf-8") as file:
51
+ SYSTEM_PROMPT = file.read().strip()
52
+
53
+ if not SYSTEM_PROMPT:
54
+ raise RuntimeError("system_prompt.txt is empty.")
55
+
56
+
57
+ # Select an appropriate inference data type.
58
+ if torch.cuda.is_available():
59
+ inference_dtype = (
60
+ torch.bfloat16
61
+ if torch.cuda.is_bf16_supported()
62
+ else torch.float16
63
+ )
64
+ else:
65
+ inference_dtype = torch.float32
66
+
67
+
68
+ # Load the tokenizer.
69
+ tokenizer = AutoTokenizer.from_pretrained(
70
+ MODEL_ID,
71
+ trust_remote_code=True,
72
+ )
73
+
74
+ if tokenizer.pad_token_id is None:
75
+ tokenizer.pad_token = tokenizer.eos_token
76
+
77
+
78
+ # Load the merged model.
79
+ model = AutoModelForCausalLM.from_pretrained(
80
+ MODEL_ID,
81
+ torch_dtype=inference_dtype,
82
+ device_map="auto",
83
+ trust_remote_code=True,
84
+ low_cpu_mem_usage=True,
85
+ )
86
+
87
+ model.eval()
88
+
89
+
90
+ # Configure valid end-of-response tokens.
91
+ terminator_ids = []
92
+
93
+ for token_id in [
94
+ tokenizer.eos_token_id,
95
+ tokenizer.convert_tokens_to_ids("<|im_end|>"),
96
+ ]:
97
+ if token_id is None:
98
+ continue
99
+
100
+ if token_id == tokenizer.unk_token_id:
101
+ continue
102
+
103
+ if token_id not in terminator_ids:
104
+ terminator_ids.append(token_id)
105
+
106
+ if not terminator_ids:
107
+ raise RuntimeError("No valid generation terminator was found.")
108
+
109
+
110
+ def clean_answer(answer):
111
+ """Prevent accidental generation of another conversation turn."""
112
+ stop_markers = [
113
+ "<|im_end|>",
114
+ "<|im_start|>system",
115
+ "<|im_start|>user",
116
+ "<|im_start|>assistant",
117
+ "\nSystem:",
118
+ "\nUser:",
119
+ ]
120
+
121
+ positions = [
122
+ answer.find(marker)
123
+ for marker in stop_markers
124
+ if answer.find(marker) >= 0
125
+ ]
126
+
127
+ if positions:
128
+ answer = answer[:min(positions)]
129
+
130
+ return answer.strip()
131
+
132
+
133
+ def ask(question, history=None):
134
+ """Generate one LOOP DFS assistant response."""
135
+ if not isinstance(question, str) or not question.strip():
136
+ raise ValueError("The question must be a non-empty string.")
137
+
138
+ if history is None:
139
+ history = []
140
+
141
+ messages = [
142
+ {
143
+ "role": "system",
144
+ "content": SYSTEM_PROMPT,
145
+ },
146
+ *history,
147
+ {
148
+ "role": "user",
149
+ "content": question.strip(),
150
+ },
151
+ ]
152
+
153
+ prompt = tokenizer.apply_chat_template(
154
+ messages,
155
+ tokenize=False,
156
+ add_generation_prompt=True,
157
+ )
158
+
159
+ inputs = tokenizer(
160
+ prompt,
161
+ return_tensors="pt",
162
+ add_special_tokens=False,
163
+ )
164
+
165
+ input_device = model.get_input_embeddings().weight.device
166
+
167
+ inputs = {
168
+ name: tensor.to(input_device)
169
+ for name, tensor in inputs.items()
170
+ }
171
+
172
+ prompt_length = inputs["input_ids"].shape[1]
173
+
174
+ with torch.inference_mode():
175
+ output = model.generate(
176
+ **inputs,
177
+ max_new_tokens=MAX_NEW_TOKENS,
178
+ do_sample=True,
179
+ temperature=0.2,
180
+ top_p=0.9,
181
+ repetition_penalty=1.05,
182
+ eos_token_id=terminator_ids,
183
+ pad_token_id=tokenizer.pad_token_id,
184
+ use_cache=True,
185
+ )
186
+
187
+ generated_tokens = output[0, prompt_length:]
188
+
189
+ answer = tokenizer.decode(
190
+ generated_tokens,
191
+ skip_special_tokens=True,
192
+ )
193
+
194
+ return clean_answer(answer)
195
+
196
+
197
+ answer = ask("Who is the CEO of LOOP DFS Kenya?")
198
+ print(answer)
199
+ ```
200
+
201
+ ## Interactive chat
202
+
203
+ The following starter code maintains conversation history and uses the complete system prompt loaded from `system_prompt.txt`:
204
+
205
+ ```python
206
+ conversation = []
207
+
208
+ print("LOOP DFS Assistant")
209
+ print("Commands: clear | exit | quit")
210
+ print()
211
+
212
+ while True:
213
+ try:
214
+ user_message = input("You: ").strip()
215
+
216
+ if not user_message:
217
+ continue
218
+
219
+ command = user_message.lower()
220
+
221
+ if command in {"exit", "quit"}:
222
+ print("Chat ended.")
223
+ break
224
+
225
+ if command == "clear":
226
+ conversation.clear()
227
+ print("Conversation history cleared.")
228
+ print()
229
+ continue
230
+
231
+ response = ask(
232
+ question=user_message,
233
+ history=conversation,
234
+ )
235
+
236
+ print(f"\nLOOP DFS Assistant: {response}\n")
237
+
238
+ conversation.extend(
239
+ [
240
+ {
241
+ "role": "user",
242
+ "content": user_message,
243
+ },
244
+ {
245
+ "role": "assistant",
246
+ "content": response,
247
+ },
248
+ ]
249
+ )
250
+
251
+ except KeyboardInterrupt:
252
+ print("\nChat ended.")
253
+ break
254
+ ```
255
+
256
+ ## Google Colab starter
257
+
258
+ In a new Google Colab notebook:
259
+
260
+ 1. Select **Runtime β†’ Change runtime type β†’ GPU**.
261
+ 2. Run the installation cell:
262
+
263
+ ```python
264
+ !pip -q install -U transformers accelerate huggingface_hub sentencepiece
265
+ ```
266
+
267
+ 3. Run the Python code from the **Quick start** section.
268
+ 4. Run the **Interactive chat** section.
269
+
270
+ No Hugging Face token is required because the repository is public.
271
+
272
+ ## Loading the deployment configuration
273
+
274
+ Applications can load the recommended generation configuration directly from `deployment_config.json`:
275
+
276
+ ```python
277
+ import json
278
+ from huggingface_hub import hf_hub_download
279
+
280
+
281
+ config_path = hf_hub_download(
282
+ repo_id="RayNene/Loop-DFS-Qwen-Merged",
283
+ filename="deployment_config.json",
284
+ )
285
+
286
+ with open(config_path, "r", encoding="utf-8") as file:
287
+ deployment_config = json.load(file)
288
+
289
+ print(deployment_config)
290
+ ```
291
+
292
+ It can then be applied during generation:
293
+
294
+ ```python
295
+ output = model.generate(
296
+ **inputs,
297
+ max_new_tokens=deployment_config.get(
298
+ "max_new_tokens",
299
+ 512,
300
+ ),
301
+ do_sample=deployment_config.get(
302
+ "do_sample",
303
+ True,
304
+ ),
305
+ temperature=deployment_config.get(
306
+ "temperature",
307
+ 0.2,
308
+ ),
309
+ top_p=deployment_config.get(
310
+ "top_p",
311
+ 0.9,
312
+ ),
313
+ repetition_penalty=deployment_config.get(
314
+ "repetition_penalty",
315
+ 1.05,
316
+ ),
317
+ eos_token_id=terminator_ids,
318
+ pad_token_id=tokenizer.pad_token_id,
319
+ use_cache=True,
320
+ )
321
+ ```
322
 
323
  ## Deployment requirements
324
 
325
+ Current company facts, product terms, fees, eligibility requirements, leadership information, partnerships, and API behavior should be supplied through approved retrieval sources or verified tools.
326
+
327
+ Authentication and authorization must be handled outside the conversation. The assistant must never collect passwords, PINs, OTPs, CVVs, secret keys, access tokens, or other authentication credentials through chat.
328
+
329
+ Financial transactions must be validated by application code, summarized for the user, and explicitly confirmed through an approved secure flow. The model must not be used as the transaction authorization layer.
330
 
331
+ ## Repository files
 
 
332
 
333
+ - `system_prompt.txt` β€” complete production system prompt
334
+ - `deployment_config.json` β€” recommended generation and deployment settings
335
+ - `config.json` β€” model architecture configuration
336
+ - `generation_config.json` β€” model generation configuration, when included
337
+ - `tokenizer_config.json` β€” tokenizer configuration
338
+ - `tokenizer.json` β€” tokenizer vocabulary and rules, when included
339
+ - `*.safetensors` β€” merged model weights
340
+ ````