|
22 | 22 | "metadata": {}, |
23 | 23 | "outputs": [], |
24 | 24 | "source": [ |
25 | | - "!pip install bigdl-llm[all]" |
| 25 | + "!pip install --pre --upgrade bigdl-llm[all]" |
26 | 26 | ] |
27 | 27 | }, |
28 | 28 | { |
|
79 | 79 | "source": [ |
80 | 80 | "from bigdl.llm.transformers import AutoModelForCausalLM\n", |
81 | 81 | "\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", |
83 | 83 | " load_in_4bit=True)" |
84 | 84 | ] |
85 | 85 | }, |
|
116 | 116 | }, |
117 | 117 | { |
118 | 118 | "cell_type": "code", |
119 | | - "execution_count": null, |
| 119 | + "execution_count": 12, |
120 | 120 | "metadata": {}, |
121 | 121 | "outputs": [], |
122 | 122 | "source": [ |
|
229 | 229 | }, |
230 | 230 | { |
231 | 231 | "cell_type": "code", |
232 | | - "execution_count": null, |
| 232 | + "execution_count": 13, |
233 | 233 | "metadata": {}, |
234 | 234 | "outputs": [], |
235 | 235 | "source": [ |
|
264 | 264 | }, |
265 | 265 | { |
266 | 266 | "cell_type": "code", |
267 | | - "execution_count": null, |
| 267 | + "execution_count": 14, |
268 | 268 | "metadata": {}, |
269 | 269 | "outputs": [], |
270 | 270 | "source": [ |
271 | 271 | "def chat(model, tokenizer, input_str, chat_history):\n", |
272 | 272 | " # 通过聊天记录将对话上下文格式化为 prompt\n", |
| 273 | + " print(f\"Input: {input_str.strip()}\")\n", |
273 | 274 | " prompt = format_prompt(input_str, chat_history)\n", |
274 | 275 | " input_ids = tokenizer.encode(prompt, return_tensors=\"pt\")\n", |
275 | 276 | "\n", |
276 | 277 | " # 预测接下来的 token,同时施加停止的标准\n", |
277 | 278 | " output_ids = model.generate(input_ids,\n", |
278 | | - " max_new_tokens=128)\n", |
| 279 | + " max_new_tokens=32)\n", |
279 | 280 | "\n", |
280 | 281 | " output_str = tokenizer.decode(output_ids[0][len(input_ids[0]):], # 在生成的 token 中跳过 prompt\n", |
281 | 282 | " skip_special_tokens=True)\n", |
|
299 | 300 | }, |
300 | 301 | { |
301 | 302 | "cell_type": "code", |
302 | | - "execution_count": 9, |
| 303 | + "execution_count": 17, |
303 | 304 | "metadata": {}, |
304 | 305 | "outputs": [ |
305 | 306 | { |
306 | 307 | "name": "stdout", |
307 | 308 | "output_type": "stream", |
308 | 309 | "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", |
346 | 315 | "Chat with Llama 2 (7B) stopped.\n" |
347 | 316 | ] |
348 | 317 | } |
|
356 | 325 | " with torch.inference_mode():\n", |
357 | 326 | " user_input = input(\"Input:\")\n", |
358 | 327 | " if user_input == \"stop\": # 当用户输入 \"stop\" 时停止对话\n", |
| 328 | + " print(\"Input: stop\")\n", |
359 | 329 | " print(\"Chat with Llama 2 (7B) stopped.\")\n", |
360 | 330 | " break\n", |
361 | 331 | " chat(model=model_in_4bit,\n", |
|
376 | 346 | }, |
377 | 347 | { |
378 | 348 | "cell_type": "code", |
379 | | - "execution_count": null, |
| 349 | + "execution_count": 18, |
380 | 350 | "metadata": {}, |
381 | 351 | "outputs": [], |
382 | 352 | "source": [ |
|
386 | 356 | " # 通过聊天记录将对话上下文格式化为 prompt\n", |
387 | 357 | " prompt = format_prompt(input_str, chat_history)\n", |
388 | 358 | " input_ids = tokenizer([prompt], return_tensors='pt')\n", |
| 359 | + " print(f\"\\nInput: {input_str.strip()}\")\n", |
389 | 360 | "\n", |
390 | 361 | " streamer = TextIteratorStreamer(tokenizer,\n", |
391 | 362 | " skip_prompt=True, # 在生成的 token 中跳过 prompt\n", |
|
429 | 400 | }, |
430 | 401 | { |
431 | 402 | "cell_type": "code", |
432 | | - "execution_count": null, |
| 403 | + "execution_count": 20, |
433 | 404 | "metadata": {}, |
434 | 405 | "outputs": [ |
435 | 406 | { |
436 | 407 | "name": "stdout", |
437 | 408 | "output_type": "stream", |
438 | 409 | "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" |
468 | 427 | ] |
469 | 428 | } |
470 | 429 | ], |
|
475 | 434 | " with torch.inference_mode():\n", |
476 | 435 | " user_input = input(\"Input:\")\n", |
477 | 436 | " if user_input == \"stop\": # 当用户输入 \"stop\" 时停止对话\n", |
| 437 | + " print(\"\\nInput: stop\")\n", |
478 | 438 | " print(\"Stream Chat with Llama 2 (7B) stopped.\")\n", |
479 | 439 | " break\n", |
480 | 440 | " stream_chat(model=model_in_4bit,\n", |
|
510 | 470 | "name": "python", |
511 | 471 | "nbconvert_exporter": "python", |
512 | 472 | "pygments_lexer": "ipython3", |
513 | | - "version": "3.9.17" |
| 473 | + "version": "3.9.18" |
514 | 474 | } |
515 | 475 | }, |
516 | 476 | "nbformat": 4, |
|
0 commit comments