Spaces:
Runtime error
Runtime error
File size: 21,934 Bytes
120efdc |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import openai\n",
"import os\n",
"\n",
"from dotenv import load_dotenv, find_dotenv\n",
"_ = load_dotenv(find_dotenv())\n",
"openai.api_key = os.environ['OPENAI_API_KEY']\n",
"\n",
"def get_completion(prompt, model = 'gpt-3.5-turbo'):\n",
" messages = [{\n",
" \"role\": \"user\",\n",
" \"content\": prompt\n",
" }]\n",
" response = openai.ChatCompletion.create(\n",
" model=model,\n",
" messages=messages,\n",
" temperature=0\n",
" )\n",
" return response.choices[0].message['content']"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'4 + 4 equals 8.'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_completion('what is 4+4?')"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"email_text = \"\"\"\n",
"Arrr, I be fuming that me blender lid \\\n",
"flew off and splattered the kitchen walls \\\n",
"with smoothie! And to make matters worse, \\\n",
"the warranty don't cover the cost of cleaning up me kitchen. I need yer help \\\n",
"right now, matey!\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"style = \"\"\"American English \\\n",
"in a calm and respectful tone\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Translate the text that into delimated by triple backticks \n",
"into a style that is American English in a calm and respectful tone.\n",
"text: ```\n",
"Arrr, I be fuming that me blender lid flew off and splattered the kitchen walls with smoothie! And to make matters worse, the warranty don't cover the cost of cleaning up me kitchen. I need yer help right now, matey!\n",
"```\n",
"\n"
]
}
],
"source": [
"prompt = f\"\"\"Translate the text \\\n",
"that into delimated by triple backticks \n",
"into a style that is {style}.\n",
"text: ```{email_text}```\n",
"\"\"\"\n",
"print(prompt)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"response = get_completion(prompt)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"Arrgh, I'm quite frustrated that my blender lid flew off and made a mess of the kitchen walls with smoothie! And to add insult to injury, the warranty doesn't cover the expenses of cleaning up my kitchen. I kindly request your assistance at this moment, my friend!\""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"from langchain.chat_models import ChatOpenAI"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"chat = ChatOpenAI(temperature=0.0)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ChatOpenAI(cache=None, verbose=False, callbacks=None, callback_manager=None, tags=None, metadata=None, client=<class 'openai.api_resources.chat_completion.ChatCompletion'>, model_name='gpt-3.5-turbo', temperature=0.0, model_kwargs={}, openai_api_key='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', openai_api_base='', openai_organization='', openai_proxy='', request_timeout=None, max_retries=6, streaming=False, n=1, max_tokens=None, tiktoken_model_name=None)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chat"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"template_string = \"\"\"Translate the text \\\n",
"that into delimited by triple backticks \\\n",
"into a style that is {style}. \\\n",
"text: ```{text}```\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"from langchain.prompts import ChatPromptTemplate\n",
"from langchain.chains import LLMChain\n",
"from langchain.chat_models import ChatOpenAI\n",
"prompt_template = ChatPromptTemplate.from_template(template_string)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"PromptTemplate(input_variables=['style', 'text'], output_parser=None, partial_variables={}, template='Translate the text that into delimited by triple backticks into a style that is {style}. text: ```{text}```\\n', template_format='f-string', validate_template=True)"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt_template.messages[0].prompt"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['style', 'text']"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt_template.messages[0].prompt.input_variables"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"customer_style = \"\"\"American English \\\n",
"in a calm and respectful tone\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"\\nArrr, I be fuming that me blender lid flew off and splattered the kitchen walls with smoothie! And to make matters worse, the warranty don't cover the cost of cleaning up me kitchen. I need yer help right now, matey!\\n\""
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"email_text"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"customer_messages = prompt_template.format_messages(style=customer_style, text=email_text)\n"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"HumanMessage(content=\"Translate the text that into delimited by triple backticks into a style that is American English in a calm and respectful tone. text: ```\\nArrr, I be fuming that me blender lid flew off and splattered the kitchen walls with smoothie! And to make matters worse, the warranty don't cover the cost of cleaning up me kitchen. I need yer help right now, matey!\\n```\\n\", additional_kwargs={}, example=False)"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"customer_messages[0]"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'langchain.schema.messages.HumanMessage'>\n",
"<class 'list'>\n"
]
}
],
"source": [
"print(type(customer_messages[0]))\n",
"print(type(customer_messages))"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"content=\"Translate the text that into delimited by triple backticks into a style that is American English in a calm and respectful tone. text: ```\\nArrr, I be fuming that me blender lid flew off and splattered the kitchen walls with smoothie! And to make matters worse, the warranty don't cover the cost of cleaning up me kitchen. I need yer help right now, matey!\\n```\\n\" additional_kwargs={} example=False\n"
]
}
],
"source": [
"print(customer_messages[0])"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"customer_response = chat(customer_messages)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I'm really frustrated that my blender lid flew off and made a mess of the kitchen walls with smoothie! And to add to my frustration, the warranty doesn't cover the cost of cleaning up my kitchen. I could really use your help at this moment, my friend!\n"
]
}
],
"source": [
"print(customer_response.content)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"service_reply = \"\"\"Hey There Customer, \\\n",
"the warranty does not cover \\\n",
"cleaning expenses for your kitchen \\\n",
"because it's your fault that \\\n",
"you misused your blender \\\n",
"by forgotting to put the lid on before \\\n",
"starting the blender. \\\n",
"Tough luck! see ya!\n",
"\"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"service_reply_pirate = \"\"\"\\\n",
"a polite tone \\\n",
"that speaks in English Pirate\\\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"service_messages = prompt_template.format_messages(style=service_reply, text=service_reply_pirate)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Translate the text that into delimited by triple backticks into a style that is Hey There Customer, the warranty does not cover cleaning expenses for your kitchen because it's your fault that you misused your blender by forgotting to put the lid on before starting the blender. Tough luck! see ya!\n",
". text: ```a polite tone that speaks in English Pirate```\n",
"\n"
]
}
],
"source": [
"print(service_messages[0].content)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"service_response = chat(service_messages)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ahoy there, matey! The warranty be not coverin' the costs o' cleanin' yer galley due to yer own folly. Ye be misusin' yer blender by forgettin' to secure the lid afore startin' it. Tough luck, me heartie! Fare thee well!\n"
]
}
],
"source": [
"print(service_response.content)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'gift': False, 'delivery_days': 5, 'price_value': 'pretty affordable!'}"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"{\n",
" \"gift\": False,\n",
" \"delivery_days\": 5,\n",
" \"price_value\": \"pretty affordable!\"\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"customer_review = \"\"\" \\\n",
"This leaf blower is pretty amazing. It has four settings: \\\n",
"candle blower, gentle breeze, windy city, and tornado. \\\n",
"It arrived in two days, just in time for my wife's \\\n",
"anniversary present. \\\n",
"I think my wife liked it so much she was speechless. \\\n",
"So far I've been the only one using it, and I've been \\\n",
"using it every other morning to clear the leaves on our lawn \\\n",
"It's slightly more expensive than the other leaf blowers \\\n",
"out there, But I think its worth it for the extra features.\n",
"\"\"\"\n",
"\n",
"review_template = \"\"\"\n",
"For the following text, extra the following information: \\\n",
"\n",
"gift: Was the item purchased as a gift for someone else? Ans should be in boolean\n",
"delivery_days: How many days it take for the product to deliver?\n",
"price_value: Extract any sentence about the value or price,\n",
"\n",
"format the output as JSON with the following keys:\n",
"gift\n",
"delivery_days\n",
"price_value\n",
"\n",
"text = {text}\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"input_variables=['text'] output_parser=None partial_variables={} messages=[HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['text'], output_parser=None, partial_variables={}, template='\\nFor the following text, extra the following information: \\ngift: Was the item purchased as a gift for someone else? Ans should be in boolean\\ndelivery_days: How many days it take for the product to deliver?\\nprice_value: Extract any sentence about the value or price,\\n\\nformat the output as JSON with the following keys:\\ngift\\ndelivery_days\\nprice_value\\n\\ntext = {text}\\n', template_format='f-string', validate_template=True), additional_kwargs={})]\n"
]
}
],
"source": [
"promp_temp = ChatPromptTemplate.from_template(review_template)\n",
"print(promp_temp)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised Timeout: Request timed out: HTTPSConnectionPool(host='api.openai.com', port=443): Read timed out. (read timeout=600).\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"gift\": false,\n",
" \"delivery_days\": 2,\n",
" \"price_value\": \"It's slightly more expensive than the other leaf blowers out there, But I think its worth it for the extra features.\"\n",
"}\n"
]
}
],
"source": [
"messages = promp_temp.format_messages(text=customer_review)\n",
"chat = ChatOpenAI(temperature=0.0)\n",
"response = chat(messages)\n",
"print(response.content)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(response.content)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The output should be a markdown code snippet formatted in the following schema, including the leading and trailing \"```json\" and \"```\":\n",
"\n",
"```json\n",
"{\n",
"\t\"gift\": string // was the item purchased as a gift for someone else? Answer True if yes, false if not or unknown.\n",
"\t\"delivery_days\": string // How many days it take for the product to arrive?\n",
"\t\"price_value\": string // Extract any sentence about the value or price\n",
"}\n",
"```\n"
]
}
],
"source": [
"from langchain.output_parsers import ResponseSchema\n",
"from langchain.output_parsers import StructuredOutputParser\n",
"\n",
"gift_schema = ResponseSchema(name=\"gift\", description=\"was the item purchased as a gift for someone else? Answer True if yes, false if not or unknown.\")\n",
"delivery_days_schema = ResponseSchema(name=\"delivery_days\", description=\"How many days it take for the product to arrive?\")\n",
"price_value_schema = ResponseSchema(name=\"price_value\", description=\"Extract any sentence about the value or price\")\n",
"\n",
"response_schemas = [gift_schema, delivery_days_schema, price_value_schema]\n",
"output_parser = StructuredOutputParser.from_response_schemas(response_schemas)\n",
"format_instructions = output_parser.get_format_instructions()\n",
"print(format_instructions)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'The output should be a markdown code snippet formatted in the following schema, including the leading and trailing \"```json\" and \"```\":\\n\\n```json\\n{\\n\\t\"gift\": string // was the item purchased as a gift for someone else? Answer True if yes, false if not or unknown.\\n\\t\"delivery_days\": string // How many days it take for the product to arrive?\\n\\t\"price_value\": string // Extract any sentence about the value or price\\n}\\n```'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"format_instructions"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"review_template_2 = \"\"\"\\\n",
"For the following text, extract the following information:\n",
"\n",
"gift: Was the item purchased as a gift for someone else? \\\n",
"Answer True if yes, False if not or unknown.\n",
"\n",
"delivery_days: How many days did it take for the product\\\n",
"to arrive? If this information is not found, output -1.\n",
"\n",
"price_value: Extract any sentences about the value or price,\\\n",
"and output them as a comma separated Python list.\n",
"\n",
"text: {text}\n",
"\n",
"{format_instructions}\n",
"\"\"\"\n",
"\n",
"prompt = ChatPromptTemplate.from_template(template=review_template_2)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"messages = promp_temp.format_messages(text=customer_review, \n",
" format_instructions=format_instructions)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"For the following text, extract the following information:\n",
"\n",
"gift: Was the item purchased as a gift for someone else? Answer True if yes, False if not or unknown.\n",
"\n",
"delivery_days: How many days did it take for the productto arrive? If this information is not found, output -1.\n",
"\n",
"price_value: Extract any sentences about the value or price,and output them as a comma separated Python list.\n",
"\n",
"text: {text}\n",
"\n",
"{format_instructions}\n",
"\n"
]
}
],
"source": [
"print(review_template_2)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[HumanMessage(content=\"\\nFor the following text, extra the following information: \\ngift: Was the item purchased as a gift for someone else? Ans should be in boolean\\ndelivery_days: How many days it take for the product to deliver?\\nprice_value: Extract any sentence about the value or price,\\n\\nformat the output as JSON with the following keys:\\ngift\\ndelivery_days\\nprice_value\\n\\ntext = This leaf blower is pretty amazing. It has four settings: candle blower, gentle breeze, windy city, and tornado. It arrived in two days, just in time for my wife's anniversary present. I think my wife liked it so much she was speechless. So far I've been the only one using it, and I've been using it every other morning to clear the leaves on our lawn It's slightly more expensive than the other leaf blowers out there, But I think its worth it for the extra features.\\n\\n\", additional_kwargs={}, example=False)]"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|