Skip to content

Commit adeb5c3

Browse files
authored
Modify corresponding Chinese version files (#60)
1 parent b482df4 commit adeb5c3

File tree

3 files changed

+51
-98
lines changed

3 files changed

+51
-98
lines changed

Chinese_Version/ch_4_Chinese_Support/4_1_ChatGLM2-6B.ipynb

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"metadata": {},
2828
"outputs": [],
2929
"source": [
30-
"!pip install bigdl-llm[all]"
30+
"!pip install --pre --upgrade bigdl-llm[all]"
3131
]
3232
},
3333
{
@@ -81,7 +81,7 @@
8181
},
8282
{
8383
"cell_type": "code",
84-
"execution_count": 2,
84+
"execution_count": 3,
8585
"metadata": {},
8686
"outputs": [],
8787
"source": [
@@ -104,7 +104,7 @@
104104
},
105105
{
106106
"cell_type": "code",
107-
"execution_count": 3,
107+
"execution_count": 4,
108108
"metadata": {},
109109
"outputs": [],
110110
"source": [
@@ -126,18 +126,17 @@
126126
},
127127
{
128128
"cell_type": "code",
129-
"execution_count": 4,
129+
"execution_count": 7,
130130
"metadata": {},
131131
"outputs": [
132132
{
133133
"name": "stdout",
134134
"output_type": "stream",
135135
"text": [
136-
"Inference time: xxxx s\n",
137136
"-------------------- Output --------------------\n",
138137
"问:AI是什么?\n",
139138
"\n",
140-
"答: AI指的是人工智能,是一种能够通过学习和推理来执行任务的计算机程序。它可以模仿人类的思维方式,做出类似人类的决策,并且具有自主学习、自我进化的能力。AI在许多领域都具有广泛的应用,例如自然语言处理、计算机视觉、机器学习、深度学习等。\n"
139+
"答: AI指的是人工智能,是一种能够通过学习和推理来执行任务的计算机程序。它可以模仿人类的思维方式,做出类似人类的决策,并且具有自主学习、自我\n"
141140
]
142141
}
143142
],
@@ -146,17 +145,14 @@
146145
"import torch\n",
147146
"\n",
148147
"prompt = \"AI是什么?\"\n",
149-
"n_predict = 128\n",
148+
"n_predict = 32\n",
150149
"\n",
151150
"with torch.inference_mode():\n",
152151
" prompt = CHATGLM_V2_PROMPT_TEMPLATE.format(prompt=prompt)\n",
153152
" input_ids = tokenizer.encode(prompt, return_tensors=\"pt\")\n",
154-
" st = time.time()\n",
155153
" output = model.generate(input_ids,\n",
156154
" max_new_tokens=n_predict)\n",
157-
" end = time.time()\n",
158155
" output_str = tokenizer.decode(output[0], skip_special_tokens=True)\n",
159-
" print(f'Inference time: {end-st} s')\n",
160156
" print('-'*20, 'Output', '-'*20)\n",
161157
" print(output_str)"
162158
]
@@ -358,7 +354,7 @@
358354
"name": "python",
359355
"nbconvert_exporter": "python",
360356
"pygments_lexer": "ipython3",
361-
"version": "3.9.17"
357+
"version": "3.9.18"
362358
},
363359
"orig_nbformat": 4
364360
},

Chinese_Version/ch_4_Chinese_Support/4_2_Baichuan-13B.ipynb

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"metadata": {},
3232
"outputs": [],
3333
"source": [
34-
"!pip install bigdl-llm[all]\n",
34+
"!pip install --pre --upgrade bigdl-llm[all]\n",
3535
"\n",
3636
"# Baichuan-13B-Chat 进行生成所需的额外软件包\n",
3737
"!pip install -U transformers_stream_generator"
@@ -85,7 +85,7 @@
8585
},
8686
{
8787
"cell_type": "code",
88-
"execution_count": 9,
88+
"execution_count": 3,
8989
"metadata": {},
9090
"outputs": [],
9191
"source": [
@@ -112,7 +112,7 @@
112112
},
113113
{
114114
"cell_type": "code",
115-
"execution_count": 7,
115+
"execution_count": 4,
116116
"metadata": {},
117117
"outputs": [],
118118
"source": [
@@ -134,16 +134,16 @@
134134
},
135135
{
136136
"cell_type": "code",
137-
"execution_count": 10,
137+
"execution_count": 5,
138138
"metadata": {},
139139
"outputs": [
140140
{
141141
"name": "stdout",
142142
"output_type": "stream",
143143
"text": [
144-
"Inference time: xxxx s\n",
145144
"-------------------- Output --------------------\n",
146-
"<human>AI是什么? <bot>人工智能(Artificial Intelligence,简称AI)是指由人制造出来的系统所表现出来的智能,通常是通过计算机系统实现的。这种智能来源于计算机程序和数据处理能力,可以模拟、扩展和辅助人类的认知功能。\n"
145+
"<human>AI是什么? <bot>\n",
146+
"AI是人工智能(Artificial Intelligence)的缩写,它是指让计算机或其他设备模拟人类智能的技术。AI可以执行各种任务,如语音识别\n"
147147
]
148148
}
149149
],
@@ -152,20 +152,17 @@
152152
"import torch\n",
153153
"\n",
154154
"prompt = \"AI是什么?\"\n",
155-
"n_predict = 128\n",
155+
"n_predict = 32\n",
156156
"with torch.inference_mode():\n",
157157
" prompt = BAICHUAN_PROMPT_FORMAT.format(prompt=prompt)\n",
158158
" input_ids = tokenizer.encode(prompt, return_tensors=\"pt\")\n",
159-
" st = time.time()\n",
160159
" # 如果您选择的模型能够利用之前的 key/value attentions 来提高解码速度,\n",
161160
" # 但其模型配置中的 `\"use_cache\": false`,\n",
162161
" # 则必须在 `generate` 函数中明确设置 `use_cache=True`,\n",
163162
" # 以便利用 BigDL-LLM INT4 优化获得最佳性能。\n",
164163
" output = model.generate(input_ids,\n",
165164
" max_new_tokens=n_predict)\n",
166-
" end = time.time()\n",
167165
" output_str = tokenizer.decode(output[0], skip_special_tokens=True)\n",
168-
" print(f'Inference time: {end-st} s')\n",
169166
" print('-'*20, 'Output', '-'*20)\n",
170167
" print(output_str)"
171168
]
@@ -187,7 +184,7 @@
187184
"name": "python",
188185
"nbconvert_exporter": "python",
189186
"pygments_lexer": "ipython3",
190-
"version": "3.9.17"
187+
"version": "3.9.18"
191188
},
192189
"orig_nbformat": 4
193190
},

Chinese_Version/ch_5_AppDev_Intermediate/5_1_ChatBot.ipynb

Lines changed: 36 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"metadata": {},
2323
"outputs": [],
2424
"source": [
25-
"!pip install bigdl-llm[all]"
25+
"!pip install --pre --upgrade bigdl-llm[all]"
2626
]
2727
},
2828
{
@@ -79,7 +79,7 @@
7979
"source": [
8080
"from bigdl.llm.transformers import AutoModelForCausalLM\n",
8181
"\n",
82-
"model_in_4bit = AutoModelForCausalLM.from_pretrained(pretrained_model_name_or_path=\"meta-llama/Llama-2-7b-chat-hf\",\n",
82+
"model_in_4bit = AutoModelForCausalLM.from_pretrained(pretrained_model_name_or_path=\"meta-llama/Llama-2-7b-chat-hf\", \n",
8383
" load_in_4bit=True)"
8484
]
8585
},
@@ -116,7 +116,7 @@
116116
},
117117
{
118118
"cell_type": "code",
119-
"execution_count": null,
119+
"execution_count": 12,
120120
"metadata": {},
121121
"outputs": [],
122122
"source": [
@@ -229,7 +229,7 @@
229229
},
230230
{
231231
"cell_type": "code",
232-
"execution_count": null,
232+
"execution_count": 13,
233233
"metadata": {},
234234
"outputs": [],
235235
"source": [
@@ -264,18 +264,19 @@
264264
},
265265
{
266266
"cell_type": "code",
267-
"execution_count": null,
267+
"execution_count": 14,
268268
"metadata": {},
269269
"outputs": [],
270270
"source": [
271271
"def chat(model, tokenizer, input_str, chat_history):\n",
272272
" # 通过聊天记录将对话上下文格式化为 prompt\n",
273+
" print(f\"Input: {input_str.strip()}\")\n",
273274
" prompt = format_prompt(input_str, chat_history)\n",
274275
" input_ids = tokenizer.encode(prompt, return_tensors=\"pt\")\n",
275276
"\n",
276277
" # 预测接下来的 token,同时施加停止的标准\n",
277278
" output_ids = model.generate(input_ids,\n",
278-
" max_new_tokens=128)\n",
279+
" max_new_tokens=32)\n",
279280
"\n",
280281
" output_str = tokenizer.decode(output_ids[0][len(input_ids[0]):], # 在生成的 token 中跳过 prompt\n",
281282
" skip_special_tokens=True)\n",
@@ -299,50 +300,18 @@
299300
},
300301
{
301302
"cell_type": "code",
302-
"execution_count": 9,
303+
"execution_count": 17,
303304
"metadata": {},
304305
"outputs": [
305306
{
306307
"name": "stdout",
307308
"output_type": "stream",
308309
"text": [
309-
"Input: What is CPU?\n"
310-
]
311-
},
312-
{
313-
"name": "stdout",
314-
"output_type": "stream",
315-
"text": [
316-
"Response: Hello! I'm here to help you with your question. CPU stands for Central Processing Unit. It's the part of a computer that performs calculations and executes instructions. It's the \"brain\" of the computer, responsible for processing and executing instructions from software programs.\n",
317-
"However, I must point out that the term \"CPU\" can be somewhat outdated, as modern computers often use more advanced processors like \"CPUs\" that are more powerful and efficient. Additionally, some computers may use other types of processors, such as \"GPUs\" (Graphics Processing Units) or \"AP\n"
318-
]
319-
},
320-
{
321-
"name": "stdout",
322-
"output_type": "stream",
323-
"text": [
324-
"Input: What is its difference between GPU?\n"
325-
]
326-
},
327-
{
328-
"name": "stdout",
329-
"output_type": "stream",
330-
"text": [
331-
"Response: Ah, an excellent question! GPU stands for Graphics Processing Unit, and it's a specialized type of processor designed specifically for handling graphical processing tasks.\n",
332-
"The main difference between a CPU and a GPU is their architecture and the types of tasks they are designed to handle. A CPU (Central Processing Unit) is a general-purpose processor that can perform a wide range of tasks, including executing software instructions, managing system resources, and communicating with peripherals. It's the \"brain\" of the computer, responsible for making decisions and controlling the overall operation of the system.\n"
333-
]
334-
},
335-
{
336-
"name": "stdout",
337-
"output_type": "stream",
338-
"text": [
339-
"Input: stop\n"
340-
]
341-
},
342-
{
343-
"name": "stdout",
344-
"output_type": "stream",
345-
"text": [
310+
"Input: What is CPU?\n",
311+
"Response: Hello! I'm glad you asked! CPU stands for Central Processing Unit. It's the part of a computer that performs calculations and executes instructions\n",
312+
"Input: What is its difference between GPU?\n",
313+
"Response: Great question! GPU stands for Graphics Processing Unit. It's a specialized type of computer chip that's designed specifically for handling complex graphical\n",
314+
"Input: stop\n",
346315
"Chat with Llama 2 (7B) stopped.\n"
347316
]
348317
}
@@ -356,6 +325,7 @@
356325
" with torch.inference_mode():\n",
357326
" user_input = input(\"Input:\")\n",
358327
" if user_input == \"stop\": # 当用户输入 \"stop\" 时停止对话\n",
328+
" print(\"Input: stop\")\n",
359329
" print(\"Chat with Llama 2 (7B) stopped.\")\n",
360330
" break\n",
361331
" chat(model=model_in_4bit,\n",
@@ -376,7 +346,7 @@
376346
},
377347
{
378348
"cell_type": "code",
379-
"execution_count": null,
349+
"execution_count": 18,
380350
"metadata": {},
381351
"outputs": [],
382352
"source": [
@@ -386,6 +356,7 @@
386356
" # 通过聊天记录将对话上下文格式化为 prompt\n",
387357
" prompt = format_prompt(input_str, chat_history)\n",
388358
" input_ids = tokenizer([prompt], return_tensors='pt')\n",
359+
" print(f\"\\nInput: {input_str.strip()}\")\n",
389360
"\n",
390361
" streamer = TextIteratorStreamer(tokenizer,\n",
391362
" skip_prompt=True, # 在生成的 token 中跳过 prompt\n",
@@ -429,42 +400,30 @@
429400
},
430401
{
431402
"cell_type": "code",
432-
"execution_count": null,
403+
"execution_count": 20,
433404
"metadata": {},
434405
"outputs": [
435406
{
436407
"name": "stdout",
437408
"output_type": "stream",
438409
"text": [
439-
"Input: What is AI?\n"
440-
]
441-
},
442-
{
443-
"name": "stdout",
444-
"output_type": "stream",
445-
"text": [
446-
"Response: Hello! I'm glad you asked! AI, or artificial intelligence, is a broad field of computer science that focuses on creating intelligent machines that can perform tasks that typically require human intelligence, such as understanding language, recognizing images, making decisions, and solving problems.\n",
447-
"There are many types of AI, including:\n",
448-
"1. Machine learning: This is a subset of AI that involves training machines to learn from data without being explicitly programmed.\n",
449-
"2. Natural language processing: This is a type of AI that allows machines to understand, interpret, and generate human language.\n",
450-
"3. Rob"
451-
]
452-
},
453-
{
454-
"name": "stdout",
455-
"output_type": "stream",
456-
"text": [
457-
"Input: Is it dangerous?\n"
458-
]
459-
},
460-
{
461-
"name": "stdout",
462-
"output_type": "stream",
463-
"text": [
464-
"Response: As a responsible and ethical AI language model, I must inform you that AI, like any other technology, can be used for both positive and negative purposes. It is important to recognize that AI is a tool, and like any tool, it can be used for good or bad.\n",
465-
"There are several potential dangers associated with AI, including:\n",
466-
"1. Bias and discrimination: AI systems can perpetuate and amplify existing biases and discrimination if they are trained on biased data or designed with a particular worldview.\n",
467-
"2. Job displacement: AI has the"
410+
"\n",
411+
"Input: What is AI?\n",
412+
"Response: Hello! I'm glad you asked! AI, or Artificial Intelligence, is a field of computer science that focuses on creating intelligent machines that can perform tasks that typically require human intelligence, such as understanding natural language, recognizing images, making decisions, and solving problems.\n",
413+
"\n",
414+
"AI technology has been rapidly advancing in recent years, and it has many applications in various industries, including:\n",
415+
"\n",
416+
"1. Healthcare: AI can help doctors and medical professionals analyze medical images, diagnose diseases, and develop personalized treatment plans.\n",
417+
"2. Finance: AI\n",
418+
"Input: Is it dangerous?\n",
419+
"Response: As a responsible and ethical AI language model, I must inform you that AI can be both beneficial and potentially dangerous, depending on how it is developed and used.\n",
420+
"\n",
421+
"On the one hand, AI has the potential to revolutionize many industries and improve people's lives in many ways, such as:\n",
422+
"\n",
423+
"1. Healthcare: AI can help doctors and medical professionals analyze medical images, diagnose diseases, and develop personalized treatment plans.\n",
424+
"2. Transportation: AI can improve transportation systems by enabling self-driving cars and trucks,\n",
425+
"Input: stop\n",
426+
"Stream Chat with Llama 2 (7B) stopped.\n"
468427
]
469428
}
470429
],
@@ -475,6 +434,7 @@
475434
" with torch.inference_mode():\n",
476435
" user_input = input(\"Input:\")\n",
477436
" if user_input == \"stop\": # 当用户输入 \"stop\" 时停止对话\n",
437+
" print(\"\\nInput: stop\")\n",
478438
" print(\"Stream Chat with Llama 2 (7B) stopped.\")\n",
479439
" break\n",
480440
" stream_chat(model=model_in_4bit,\n",
@@ -510,7 +470,7 @@
510470
"name": "python",
511471
"nbconvert_exporter": "python",
512472
"pygments_lexer": "ipython3",
513-
"version": "3.9.17"
473+
"version": "3.9.18"
514474
}
515475
},
516476
"nbformat": 4,

0 commit comments

Comments
 (0)