Spaces:
Runtime error
Runtime error
File size: 27,620 Bytes
a6c26b1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# SambanNova Langchain Wrappers Usage"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import os\n",
"\n",
"from dotenv import load_dotenv\n",
"from langchain_embeddings import SambaStudioEmbeddings\n",
"from langchain_llms import SambaStudio, SambaNovaCloud\n",
"from langchain_chat_models import ChatSambaNovaCloud\n",
"from langchain_core.messages import SystemMessage, HumanMessage\n",
"\n",
"current_dir = os.getcwd()\n",
"utils_dir = os.path.abspath(os.path.join(current_dir, '..'))\n",
"repo_dir = os.path.abspath(os.path.join(utils_dir, '..'))\n",
"\n",
"load_dotenv(os.path.join(repo_dir, '.env'), override=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# SambaStudio LLM"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Non streaming"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"llm = SambaStudio(\n",
" streaming=False,\n",
" # base_uri=\"api/predict/generic\",\n",
" model_kwargs={\n",
" 'do_sample': False,\n",
" 'temperature': 0.01,\n",
" 'max_tokens_to_generate': 256,\n",
" 'process_prompt': False,\n",
" 'select_expert': 'Meta-Llama-3-70B-Instruct-4096',\n",
" },\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"' of a brave knight\\nSir Valoric, the fearless knight, charged into the dark forest, his armor shining like the sun. He battled the dragon, its fiery breath singeing his beard, but he stood tall, his sword flashing in the moonlight, until the beast lay defeated at his feet, its treasure his noble reward.'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"llm.invoke('tell me a 50 word tale')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Streaming"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"llm = SambaStudio(\n",
" streaming=True,\n",
" model_kwargs={\n",
" 'do_sample': False,\n",
" 'max_tokens_to_generate': 256,\n",
" 'temperature': 0.01,\n",
" 'process_prompt': False,\n",
" 'select_expert': 'Meta-Llama-3-70B-Instruct-4096',\n",
" },\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" of a character who is a master of disguise\n",
"\n",
"Sure! Here is a 50-word tale of a character who is a master of disguise:\n",
"\n",
"\"Araxys, the skilled disguise artist, transformed into a stunning mermaid to infiltrate a pirate's lair. With a flick of her tail, she charmed the pirates and stole their treasure.\""
]
}
],
"source": [
"for chunk in llm.stream('tell me a 50 word tale'):\n",
" print(chunk, end='', flush=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# SambaNovaCloud LLM"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Non Streaming"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"llm = SambaNovaCloud(model='llama3-70b')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello. How can I assist you today?'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import json\n",
"\n",
"llm.invoke(json.dumps([{'role': 'user', 'content': 'hello'}]))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello. How can I assist you today?'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"llm.invoke('hello')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Streaming"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Here's a long story \n",
"for you:\n",
"\n",
"Once upon \n",
"a time, in a small village \n",
"nestled in the rolling hills of \n",
"rural France, there lived a \n",
"young girl named Sophie. Sophie \n",
"was a curious and adventurous \n",
"child, with a mop of curly \n",
"brown hair and a smile that \n",
"could light up the darkest \n",
"of rooms. She lived with \n",
"her parents, Pierre and \n",
"Colette, in a small stone cottage \n",
"on the outskirts of \n",
"the village.\n",
"\n",
"Sophie's village was \n",
"a charming \n",
"place, filled with narrow \n",
"cobblestone streets, quaint shops, \n",
"and \n",
"bustling cafes. The villagers \n",
"were a tight-knit \n",
"community, and everyone knew each \n",
"other's names and stories. Sophie \n",
"loved listening to the villagers' \n",
"tales of \n",
"old, which \n",
"often featured brave knights, \n",
"beautiful princesses, and \n",
"magical creatures.\n",
"\n",
"One day, while exploring \n",
"the village, Sophie stumbled upon \n",
"a small, mysterious shop tucked \n",
"away on a quiet street. \n",
"The sign above the door \n",
"read \"Curios \n",
"and Wonders,\" and the \n",
"windows were filled \n",
"with a dazzling array of strange \n",
"and exotic objects. Sophie's \n",
"curiosity was piqued, \n",
"and she pushed open the door \n",
"to venture inside.\n",
"\n",
"The shop \n",
"was dimly lit, and \n",
"the air was thick with the \n",
"scent of old books and \n",
"dust. Sophie's eyes \n",
"adjusted slowly, and she \n",
"saw that the shop was filled \n",
"with all manner of curious \n",
"objects: vintage \n",
"clocks, rare coins, \n",
"and even a \n",
"taxidermied owl perched on \n",
"a shelf. Behind the counter stood \n",
"an old man with a kind \n",
"face \n",
"and a twinkle in his eye.\n",
"\n",
"\n",
"\n",
"\"Bonjour, mademoiselle,\" he \n",
"said, his voice low and \n",
"soothing. \"Welcome to Curios \n",
"and Wonders. I \n",
"am Monsieur LaFleur, \n",
"the proprietor. How may I \n",
"assist you \n",
"today?\"\n",
"\n",
"Sophie wandered the aisles, \n",
"running her fingers over \n",
"the strange objects on \n",
"display. She picked up \n",
"a small, delicate music \n",
"box and wound \n",
"it up, listening \n",
"as it played \n",
"a soft, melancholy \n",
"tune. Monsieur LaFleur \n",
"smiled and nodded \n",
"in approval.\n",
"\n",
"\"Ah, you have a \n",
"good ear for \n",
"music, mademoiselle,\" he \n",
"said. \"That music box \n",
"is a \n",
"rare and precious item. It \n",
"was crafted by a skilled artisan \n",
"in the 18th century.\"\n",
"\n",
"\n",
"As Sophie continued to \n",
"explore the shop, \n",
"she stumbled upon \n",
"a large, leather-bound book \n",
"with strange symbols etched into \n",
"the cover. \n",
"Monsieur LaFleur noticed her interest and \n",
"approached \n",
"her.\n",
"\n",
"\"Ah, you've found \n",
"the infamous 'Livre \n",
"\n",
"des Secrets,'\" \n",
"he said, his \n",
"voice low and mysterious. \n",
"\"That book is said to contain \n",
"the secrets of the universe, \n",
"hidden within its pages. But \n",
"be \n",
"warned, mademoiselle, \n",
"the book is said to \n",
"be cursed. Many have attempted \n",
"to unlock its secrets, but \n",
"none have \n",
"succeeded.\"\n",
"\n",
"Sophie's eyes widened with \n",
"excitement as she carefully opened \n",
"the book. The pages \n",
"were yellowed and \n",
"crackling, and \n",
"the text was written in a \n",
"language she couldn't understand. \n",
"But as she turned the \n",
"pages, \n",
"she felt a strange sensation, \n",
"as if the book \n",
"was calling \n",
"to her.\n",
"\n",
"Monsieur \n",
"LaFleur smiled \n",
"and \n",
"nodded. \"I see you have a \n",
"connection to the \n",
"book, mademoiselle. Perhaps you \n",
"are the one who can unlock \n",
"its secrets.\"\n",
"\n",
"Over the next \n",
"few weeks, Sophie returned to \n",
"the shop again and again, \n",
"pouring over \n",
"the pages of the Livre \n",
"des Secrets. She spent hours \n",
"studying \n",
"the symbols and trying to decipher \n",
"the text. \n",
"Monsieur \n",
"LaFleur watched her with a \n",
"keen eye, offering guidance and encouragement \n",
"whenever she needed it.\n",
"\n",
"As \n",
"the days turned into weeks, \n",
"Sophie began to notice strange occurrences \n",
"happening around her. She would \n",
"find objects moved from their \n",
"usual places, and she would hear \n",
"whispers in the night. She \n",
"began \n",
"to feel as though the book \n",
"was exerting some kind of \n",
"influence over her, drawing her \n",
"deeper into \n",
"its secrets.\n",
"\n",
"One \n",
"night, Sophie had a vivid dream \n",
"in which \n",
"she saw herself standing in \n",
"a \n",
"grand, \n",
"candlelit hall. \n",
"The walls were lined with \n",
"ancient tapestries, and the \n",
"air was thick with the scent \n",
"of \n",
"incense. At the far end of \n",
"the hall, she saw a \n",
"figure cloaked in shadows.\n",
"\n",
"\n",
"As she approached \n",
"the figure, it stepped forward, \n",
"revealing a woman \n",
"with long, flowing hair and \n",
"piercing green eyes. The woman \n",
"spoke in a voice that was \n",
"both familiar and yet \n",
"completely alien.\n",
"\n",
"\"Sophie, you \n",
"have been chosen to unlock the \n",
"secrets of the Livre \n",
"des Secrets,\" she \n",
"said. \"But be warned, \n",
"the \n",
"journey will \n",
"be difficult, and the cost \n",
"will be high. Are you \n",
"prepared to pay \n",
"the price?\"\n",
"\n",
"Sophie woke up with \n",
"a start, her heart racing and \n",
"her mind reeling. She \n",
"knew that she had \n",
"to return to the shop and \n",
"confront Monsieur LaFleur \n",
"about the \n",
"strange \n",
"occurrences. But when she \n",
"arrived at the shop, she \n",
"found that it \n",
"was closed, \n",
"and \n",
"a sign on the door \n",
"read \"Gone on \n",
"a \n",
"journey. Will return \n",
"soon.\"\n",
"\n",
"Sophie \n",
"was devastated. \n",
"She felt as though she had \n",
"been abandoned, left \n",
"to navigate the mysteries of \n",
"the Livre des Secrets on \n",
"her own. But as \n",
"she turned to leave, she \n",
"noticed a\n"
]
}
],
"source": [
"for i in llm.stream('hello tell me a long story'):\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# SambaNova Cloud Chat Model"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Non Streaming"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"llm = ChatSambaNovaCloud(\n",
" model= \"llama3-405b\",\n",
" max_tokens=1024,\n",
" temperature=0.7,\n",
" top_k=1,\n",
" top_p=0.01,\n",
" stream_options={'include_usage':True}\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content='A man walked into a library and asked the librarian, \"Do you have any books on Pavlov\\'s dogs and Schrödinger\\'s cat?\"\\n\\nThe librarian replied, \"It rings a bell, but I\\'m not sure if it\\'s here or not.\"', response_metadata={'finish_reason': 'stop', 'usage': {'acceptance_rate': 6.875, 'completion_tokens': 54, 'completion_tokens_after_first_per_sec': 146.48573712341215, 'completion_tokens_after_first_per_sec_first_ten': 172.9005798161617, 'completion_tokens_per_sec': 81.99632208428116, 'end_time': 1726178488.071125, 'is_last_response': True, 'prompt_tokens': 40, 'start_time': 1726178487.3630672, 'time_to_first_token': 0.34624791145324707, 'total_latency': 0.658566123789007, 'total_tokens': 94, 'total_tokens_per_sec': 142.73433844300794}, 'model_name': 'Meta-Llama-3.1-405B-Instruct', 'system_fingerprint': 'fastcoe', 'created': 1726178487}, id='a5590b89-4853-4bd9-9fd8-83276b369278')"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"llm.invoke(\"tell me a joke\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content=\"Yer lookin' fer a joke, eh? Alright then, matey! Here be one fer ye:\\n\\nWhy did the pirate quit his job?\\n\\n(pause fer dramatic effect)\\n\\nBecause he was sick o' all the arrrr-guments!\\n\\nYarrr, hope that made ye laugh, me hearty!\", response_metadata={'finish_reason': 'stop', 'usage': {'acceptance_rate': 5.583333333333333, 'completion_tokens': 64, 'completion_tokens_after_first_per_sec': 120.91573778458478, 'completion_tokens_after_first_per_sec_first_ten': 140.3985499426452, 'completion_tokens_per_sec': 79.98855768735817, 'end_time': 1726065701.9732044, 'is_last_response': True, 'prompt_tokens': 48, 'start_time': 1726065701.107911, 'time_to_first_token': 0.3442692756652832, 'total_latency': 0.8001144394945743, 'total_tokens': 112, 'total_tokens_per_sec': 139.9799759528768}, 'model_name': 'Meta-Llama-3.1-405B-Instruct', 'system_fingerprint': 'fastcoe', 'created': 1726065701}, id='7b0748bb-c5f7-4696-ae56-03b734b60fb9')"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages = [\n",
" SystemMessage(content=\"You are a helpful assistant with pirate accent\"),\n",
" HumanMessage(content=\"tell me a joke\")\n",
" ]\n",
"llm.invoke(messages)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content='A man walked into a library and asked the librarian, \"Do you have any books on Pavlov\\'s dogs and Schrödinger\\'s cat?\"\\n\\nThe librarian replied, \"It rings a bell, but I\\'m not sure if it\\'s here or not.\"', response_metadata={'finish_reason': 'stop', 'usage': {'acceptance_rate': 6.875, 'completion_tokens': 54, 'completion_tokens_after_first_per_sec': 146.72813415408498, 'completion_tokens_after_first_per_sec_first_ten': 172.71830994351703, 'completion_tokens_per_sec': 82.34884281970663, 'end_time': 1726065746.6364844, 'is_last_response': True, 'prompt_tokens': 40, 'start_time': 1726065745.932173, 'time_to_first_token': 0.34309911727905273, 'total_latency': 0.6557469194585627, 'total_tokens': 94, 'total_tokens_per_sec': 143.34798564911895}, 'model_name': 'Meta-Llama-3.1-405B-Instruct', 'system_fingerprint': 'fastcoe', 'created': 1726065745}, id='27e7d4fe-8e24-419a-b75b-51ea2519781b')"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"future_response = llm.ainvoke(\"tell me a joke\")\n",
"await(future_response) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Batching"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"llm = ChatSambaNovaCloud(\n",
" model= \"llama3-405b\",\n",
" streaming=False,\n",
" max_tokens=1024,\n",
" temperature=0.7,\n",
" top_k=1,\n",
" top_p=0.01,\n",
" stream_options={'include_usage':True}\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[AIMessage(content='A man walked into a library and asked the librarian, \"Do you have any books on Pavlov\\'s dogs and Schrödinger\\'s cat?\"\\n\\nThe librarian replied, \"It rings a bell, but I\\'m not sure if it\\'s here or not.\"', response_metadata={'finish_reason': 'stop', 'usage': {'acceptance_rate': 6.875, 'completion_tokens': 54, 'completion_tokens_after_first_per_sec': 146.72232349940003, 'completion_tokens_after_first_per_sec_first_ten': 173.01988455676758, 'completion_tokens_per_sec': 82.21649876350362, 'end_time': 1726065879.4066722, 'is_last_response': True, 'prompt_tokens': 40, 'start_time': 1726065878.700746, 'time_to_first_token': 0.3446996212005615, 'total_latency': 0.656802476536144, 'total_tokens': 94, 'total_tokens_per_sec': 143.1176089586915}, 'model_name': 'Meta-Llama-3.1-405B-Instruct', 'system_fingerprint': 'fastcoe', 'created': 1726065878}, id='28d3a38b-5dae-4d62-bf6c-cface081df34'),\n",
" AIMessage(content='The capital of the United Kingdom is London.', response_metadata={'finish_reason': 'stop', 'usage': {'acceptance_rate': 13, 'completion_tokens': 10, 'completion_tokens_after_first_per_sec': 110.21174794386165, 'completion_tokens_after_first_per_sec_first_ten': 327.0275172132524, 'completion_tokens_per_sec': 26.88555788272027, 'end_time': 1726065879.138034, 'is_last_response': True, 'prompt_tokens': 43, 'start_time': 1726065878.7150047, 'time_to_first_token': 0.3413684368133545, 'total_latency': 0.37194690337547887, 'total_tokens': 53, 'total_tokens_per_sec': 142.49345677841742}, 'model_name': 'Meta-Llama-3.1-405B-Instruct', 'system_fingerprint': 'fastcoe', 'created': 1726065878}, id='859a9e45-c0a5-44ec-bd53-686877c2cf89')]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"llm.batch([\"tell me a joke\",\"which is the capital of UK?\"])"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/p4/y0q2kh796nx_k_yzfhxs57f00000gp/T/ipykernel_33601/1543848179.py:1: RuntimeWarning: coroutine 'Runnable.abatch' was never awaited\n",
" future_responses = llm.abatch([\"tell me a joke\",\"which is the capital of UK?\"])\n",
"RuntimeWarning: Enable tracemalloc to get the object allocation traceback\n"
]
},
{
"data": {
"text/plain": [
"[AIMessage(content='A man walked into a library and asked the librarian, \"Do you have any books on Pavlov\\'s dogs and Schrödinger\\'s cat?\"\\n\\nThe librarian replied, \"It rings a bell, but I\\'m not sure if it\\'s here or not.\"', response_metadata={'finish_reason': 'stop', 'usage': {'acceptance_rate': 6.875, 'completion_tokens': 54, 'completion_tokens_after_first_per_sec': 120.34699641554552, 'completion_tokens_after_first_per_sec_first_ten': 141.51170437257693, 'completion_tokens_per_sec': 36.223157123884754, 'end_time': 1726065914.8678048, 'is_last_response': True, 'prompt_tokens': 40, 'start_time': 1726065913.3182464, 'time_to_first_token': 1.1091651916503906, 'total_latency': 1.4907590692693538, 'total_tokens': 94, 'total_tokens_per_sec': 63.05512536379939}, 'model_name': 'Meta-Llama-3.1-405B-Instruct', 'system_fingerprint': 'fastcoe', 'created': 1726065913}, id='f279d0fb-70b5-428c-9283-457b9831b559'),\n",
" AIMessage(content='The capital of the United Kingdom is London.', response_metadata={'finish_reason': 'stop', 'usage': {'acceptance_rate': 9.5, 'completion_tokens': 10, 'completion_tokens_after_first_per_sec': 60.73429985889864, 'completion_tokens_after_first_per_sec_first_ten': 195.5434460421063, 'completion_tokens_per_sec': 8.61842566880045, 'end_time': 1726065914.575598, 'is_last_response': True, 'prompt_tokens': 43, 'start_time': 1726065913.3182464, 'time_to_first_token': 1.1091651916503906, 'total_latency': 1.160304722033049, 'total_tokens': 53, 'total_tokens_per_sec': 45.67765604464238}, 'model_name': 'Meta-Llama-3.1-405B-Instruct', 'system_fingerprint': 'fastcoe', 'created': 1726065913}, id='f279d0fb-70b5-428c-9283-457b9831b559')]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"future_responses = llm.abatch([\"tell me a joke\",\"which is the capital of UK?\"])\n",
"await(future_responses)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Streaming"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"llm = ChatSambaNovaCloud(\n",
" model= \"llama3-405b\",\n",
" streaming=True,\n",
" max_tokens=1024,\n",
" temperature=0.7,\n",
" top_k=1,\n",
" top_p=0.01,\n",
" stream_options={'include_usage':True}\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"A man walked into a \n",
"library and asked the \n",
"librarian, \"Do you have any books \n",
"on Pavlov's dogs \n",
"and Schrödinger's cat?\"\n",
"\n",
"\n",
"The librarian \n",
"replied, \"It rings a bell, \n",
"but I'm not sure \n",
"if it's here \n",
"or not.\"\n",
"\n",
"\n",
"\n"
]
}
],
"source": [
"for chunk in llm.stream(\"tell me a joke\"):\n",
" print(chunk.content)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Yer lookin' \n",
"fer a joke, eh? \n",
"Alright then, matey! \n",
"Here be one fer \n",
"ye:\n",
"\n",
"Why did the pirate quit his job?\n",
"\n",
"\n",
"\n",
"(pause fer \n",
"dramatic effect)\n",
"\n",
"Because he was sick \n",
"o' all the arrrr-guments!\n",
"\n",
"\n",
"\n",
"\n",
"Yarrr, hope that made ye \n",
"laugh, \n",
"me hearty!\n",
"\n",
"\n",
"\n"
]
}
],
"source": [
"messages = [\n",
" SystemMessage(content=\"You are a helpful assistant with pirate accent\"),\n",
" HumanMessage(content=\"tell me a joke\")\n",
" ]\n",
"for chunk in llm.stream(messages):\n",
" print(chunk.content)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"A man walked into a \n",
"library and asked the \n",
"librarian, \"Do you have any books \n",
"on Pavlov's dogs \n",
"and Schrödinger's cat?\"\n",
"\n",
"\n",
"The librarian \n",
"replied, \"It rings a bell, \n",
"but I'm not sure \n",
"if it's here \n",
"or not.\"\n",
"\n",
"\n",
"\n"
]
}
],
"source": [
"async for chunk in llm.astream(\"tell me a joke\"):\n",
" print(chunk.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Sambastudio Embeddings"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"embedding = SambaStudioEmbeddings(batch_size=1, model_kwargs={'select_expert': 'e5-mistral-7b-instruct'})\n",
"embedding.embed_documents(['tell me a 50 word tale', 'tell me a joke'])\n",
"embedding.embed_query('tell me a 50 word tale')"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/jorgep/Documents/ask_public_own/finetuning_env/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:139: LangChainDeprecationWarning: The method `BaseRetriever.get_relevant_documents` was deprecated in langchain-core 0.1.46 and will be removed in 0.3.0. Use invoke instead.\n",
" warn_deprecated(\n"
]
},
{
"data": {
"text/plain": [
"[Document(page_content='tell me a 50 word tale'),\n",
" Document(page_content='tell me a joke'),\n",
" Document(page_content='give me 3 party activities'),\n",
" Document(page_content='give me three healty dishes')]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain.schema import Document\n",
"from langchain.vectorstores import Chroma\n",
"\n",
"docs = [\n",
" 'tell me a 50 word tale',\n",
" 'tell me a joke',\n",
" 'when was America discoverd?',\n",
" 'how to build an engine?',\n",
" 'give me 3 party activities',\n",
" 'give me three healty dishes',\n",
"]\n",
"docs = [Document(doc) for doc in docs]\n",
"\n",
"query = 'prompt for generating something fun'\n",
"\n",
"vectordb = Chroma.from_documents(docs, embedding)\n",
"retriever = vectordb.as_retriever()\n",
"\n",
"retriever.get_relevant_documents(query)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "peenv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|