From e32f453a16a35dbcaacafd203912aecb6bfff309 Mon Sep 17 00:00:00 2001 From: Munish Dabra <100611213+awsdabra@users.noreply.github.com> Date: Tue, 15 Aug 2023 04:07:10 +0000 Subject: [PATCH] Added Code Generation use-case module --- .../00_code_generatation_w_bedrock.ipynb | 4 +- .../01_sql_query_generate_w_bedrock.ipynb | 4 +- .../02_code_interpret_w_langchain.ipynb | 5 +- .../03_code_translate_w_langchain.ipynb | 126 ++---------------- 4 files changed, 16 insertions(+), 123 deletions(-) diff --git a/06_CodeGeneration/00_code_generatation_w_bedrock.ipynb b/06_CodeGeneration/00_code_generatation_w_bedrock.ipynb index 7f8cd2b5..d6e45eed 100644 --- a/06_CodeGeneration/00_code_generatation_w_bedrock.ipynb +++ b/06_CodeGeneration/00_code_generatation_w_bedrock.ipynb @@ -113,8 +113,8 @@ "\n", "# ---- ⚠️ Un-comment and edit the below lines as needed for your AWS setup ⚠️ ----\n", "\n", - "os.environ[\"AWS_DEFAULT_REGION\"] = \"us-east-1\" # E.g. \"us-east-1\"\n", - "os.environ[\"AWS_PROFILE\"] = \"fine-tuning-bedrock\"\n", + "# os.environ[\"AWS_DEFAULT_REGION\"] = \"\" # E.g. \"us-east-1\"\n", + "# os.environ[\"AWS_PROFILE\"] = \"\"\n", "# os.environ[\"BEDROCK_ASSUME_ROLE\"] = \"\" # E.g. \"arn:aws:...\"\n", "# os.environ[\"BEDROCK_ENDPOINT_URL\"] = \"\" # E.g. \"https://...\"\n", "\n", diff --git a/06_CodeGeneration/01_sql_query_generate_w_bedrock.ipynb b/06_CodeGeneration/01_sql_query_generate_w_bedrock.ipynb index e3961740..31135b55 100644 --- a/06_CodeGeneration/01_sql_query_generate_w_bedrock.ipynb +++ b/06_CodeGeneration/01_sql_query_generate_w_bedrock.ipynb @@ -97,8 +97,8 @@ "\n", "# ---- ⚠️ Un-comment and edit the below lines as needed for your AWS setup ⚠️ ----\n", "\n", - "os.environ[\"AWS_DEFAULT_REGION\"] = \"us-east-1\" # E.g. \"us-east-1\"\n", - "os.environ[\"AWS_PROFILE\"] = \"fine-tuning-bedrock\"\n", + "# os.environ[\"AWS_DEFAULT_REGION\"] = \"\" # E.g. \"us-east-1\"\n", + "# os.environ[\"AWS_PROFILE\"] = \"\"\n", "# os.environ[\"BEDROCK_ASSUME_ROLE\"] = \"\" # E.g. \"arn:aws:...\"\n", "# os.environ[\"BEDROCK_ENDPOINT_URL\"] = \"\" # E.g. \"https://...\"\n", "\n", diff --git a/06_CodeGeneration/02_code_interpret_w_langchain.ipynb b/06_CodeGeneration/02_code_interpret_w_langchain.ipynb index 1e2d55e9..5421c908 100644 --- a/06_CodeGeneration/02_code_interpret_w_langchain.ipynb +++ b/06_CodeGeneration/02_code_interpret_w_langchain.ipynb @@ -101,12 +101,11 @@ "\n", "# ---- ⚠️ Un-comment and edit the below lines as needed for your AWS setup ⚠️ ----\n", "\n", - "os.environ[\"AWS_DEFAULT_REGION\"] = \"us-east-1\" # E.g. \"us-east-1\"\n", - "os.environ[\"AWS_PROFILE\"] = \"fine-tuning-bedrock\"\n", + "# os.environ[\"AWS_DEFAULT_REGION\"] = \"\" # E.g. \"us-east-1\"\n", + "# os.environ[\"AWS_PROFILE\"] = \"\"\n", "# os.environ[\"BEDROCK_ASSUME_ROLE\"] = \"\" # E.g. \"arn:aws:...\"\n", "# os.environ[\"BEDROCK_ENDPOINT_URL\"] = \"\" # E.g. \"https://...\"\n", "\n", - "\n", "boto3_bedrock = bedrock.get_bedrock_client(\n", " assumed_role=os.environ.get(\"BEDROCK_ASSUME_ROLE\", None),\n", " endpoint_url=os.environ.get(\"BEDROCK_ENDPOINT_URL\", None),\n", diff --git a/06_CodeGeneration/03_code_translate_w_langchain.ipynb b/06_CodeGeneration/03_code_translate_w_langchain.ipynb index 8162b9e6..a6ab6437 100644 --- a/06_CodeGeneration/03_code_translate_w_langchain.ipynb +++ b/06_CodeGeneration/03_code_translate_w_langchain.ipynb @@ -40,13 +40,13 @@ "![](./images/bedrock-code-gen-langchain.png)\n", "\n", "#### Use case\n", - "To demonstrate the generation capability of models in Amazon Bedrock, let's take the use case of email generation.\n", + "To demonstrate how you can use Amazon Bedrock LLMs to port code from one programming language to another.\n", "\n", "#### Persona\n", - "You are Bob a Customer Service Manager at AnyCompany and some of your customers are not happy with the customer service and are providing negative feedbacks on the service provided by customer support engineers. Now, you would like to respond to those customers humbly aplogizing for the poor service and regain trust. You need the help of an LLM to generate a bulk of emails for you which are human friendly and personalized to the customer's sentiment from previous email correspondence.\n", + "Guides you through translating C++ code to Java using Amazon Bedrock and LangChain APIs. It shows techniques for prompting the model to port C++ code over to Java, handling differences in syntax, language constructs, and conventions between the languages.\n", "\n", "#### Implementation\n", - "To fulfill this use case, we will show you how to generate an email with a thank you note based on the customer's previous email. We will use the Amazon Titan Text Large model using the Amazon Bedrock LangChain integration. \n" + "To fulfill this use case, we will show you how to translate a given legacy C++ code to port to Java. We will use the Amazon Bedrock and LangChain integration. \n" ] }, { @@ -65,104 +65,12 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "49e2c0a9-4838-4f2b-bb36-61c0cbcd62af", "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Processing /Users/mundabra/dev/bedrock/amazon-bedrock-workshop/dependencies/awscli-1.29.21-py3-none-any.whl\n", - "Processing /Users/mundabra/dev/bedrock/amazon-bedrock-workshop/dependencies/boto3-1.28.21-py3-none-any.whl\n", - "Processing /Users/mundabra/dev/bedrock/amazon-bedrock-workshop/dependencies/botocore-1.31.21-py3-none-any.whl\n", - "Collecting docutils<0.17,>=0.10\n", - " Using cached docutils-0.16-py2.py3-none-any.whl (548 kB)\n", - "Collecting s3transfer<0.7.0,>=0.6.0\n", - " Using cached s3transfer-0.6.1-py3-none-any.whl (79 kB)\n", - "Collecting PyYAML<6.1,>=3.10\n", - " Using cached PyYAML-6.0.1-cp310-cp310-macosx_10_9_x86_64.whl (189 kB)\n", - "Collecting rsa<4.8,>=3.1.2\n", - " Using cached rsa-4.7.2-py3-none-any.whl (34 kB)\n", - "Collecting colorama<0.4.5,>=0.2.5\n", - " Using cached colorama-0.4.4-py2.py3-none-any.whl (16 kB)\n", - "Collecting urllib3<1.27,>=1.25.4\n", - " Using cached urllib3-1.26.16-py2.py3-none-any.whl (143 kB)\n", - "Collecting jmespath<2.0.0,>=0.7.1\n", - " Using cached jmespath-1.0.1-py3-none-any.whl (20 kB)\n", - "Collecting python-dateutil<3.0.0,>=2.1\n", - " Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)\n", - "Collecting six>=1.5\n", - " Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)\n", - "Collecting pyasn1>=0.1.3\n", - " Using cached pyasn1-0.5.0-py2.py3-none-any.whl (83 kB)\n", - "Installing collected packages: urllib3, six, PyYAML, pyasn1, jmespath, docutils, colorama, rsa, python-dateutil, botocore, s3transfer, boto3, awscli\n", - " Attempting uninstall: urllib3\n", - " Found existing installation: urllib3 1.26.16\n", - " Uninstalling urllib3-1.26.16:\n", - " Successfully uninstalled urllib3-1.26.16\n", - " Attempting uninstall: six\n", - " Found existing installation: six 1.16.0\n", - " Uninstalling six-1.16.0:\n", - " Successfully uninstalled six-1.16.0\n", - " Attempting uninstall: PyYAML\n", - " Found existing installation: PyYAML 6.0.1\n", - " Uninstalling PyYAML-6.0.1:\n", - " Successfully uninstalled PyYAML-6.0.1\n", - " Attempting uninstall: pyasn1\n", - " Found existing installation: pyasn1 0.5.0\n", - " Uninstalling pyasn1-0.5.0:\n", - " Successfully uninstalled pyasn1-0.5.0\n", - " Attempting uninstall: jmespath\n", - " Found existing installation: jmespath 1.0.1\n", - " Uninstalling jmespath-1.0.1:\n", - " Successfully uninstalled jmespath-1.0.1\n", - " Attempting uninstall: docutils\n", - " Found existing installation: docutils 0.16\n", - " Uninstalling docutils-0.16:\n", - " Successfully uninstalled docutils-0.16\n", - " Attempting uninstall: colorama\n", - " Found existing installation: colorama 0.4.4\n", - " Uninstalling colorama-0.4.4:\n", - " Successfully uninstalled colorama-0.4.4\n", - " Attempting uninstall: rsa\n", - " Found existing installation: rsa 4.7.2\n", - " Uninstalling rsa-4.7.2:\n", - " Successfully uninstalled rsa-4.7.2\n", - " Attempting uninstall: python-dateutil\n", - " Found existing installation: python-dateutil 2.8.2\n", - " Uninstalling python-dateutil-2.8.2:\n", - " Successfully uninstalled python-dateutil-2.8.2\n", - " Attempting uninstall: botocore\n", - " Found existing installation: botocore 1.31.21\n", - " Uninstalling botocore-1.31.21:\n", - " Successfully uninstalled botocore-1.31.21\n", - " Attempting uninstall: s3transfer\n", - " Found existing installation: s3transfer 0.6.1\n", - " Uninstalling s3transfer-0.6.1:\n", - " Successfully uninstalled s3transfer-0.6.1\n", - " Attempting uninstall: boto3\n", - " Found existing installation: boto3 1.28.21\n", - " Uninstalling boto3-1.28.21:\n", - " Successfully uninstalled boto3-1.28.21\n", - " Attempting uninstall: awscli\n", - " Found existing installation: awscli 1.29.21\n", - " Uninstalling awscli-1.29.21:\n", - " Successfully uninstalled awscli-1.29.21\n", - "Successfully installed PyYAML-6.0.1 awscli-1.29.21 boto3-1.28.21 botocore-1.31.21 colorama-0.4.4 docutils-0.16 jmespath-1.0.1 pyasn1-0.5.0 python-dateutil-2.8.2 rsa-4.7.2 s3transfer-0.6.1 six-1.16.0 urllib3-1.26.16\n", - "\n", - "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip available: \u001b[0m\u001b[31;49m22.2.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.2.1\u001b[0m\n", - "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython3.10 -m pip install --upgrade pip\u001b[0m\n", - "Note: you may need to restart the kernel to use updated packages.\n", - "\n", - "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip available: \u001b[0m\u001b[31;49m22.2.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.2.1\u001b[0m\n", - "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython3.10 -m pip install --upgrade pip\u001b[0m\n", - "Note: you may need to restart the kernel to use updated packages.\n" - ] - } - ], + "outputs": [], "source": [ "# Make sure you ran `download-dependencies.sh` from the root of the repository first!\n", "%pip install --no-build-isolation --force-reinstall \\\n", @@ -175,24 +83,12 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "558a9372-0789-414a-a1d7-2976056f2015", "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Create new client\n", - " Using region: us-east-1\n", - " Using profile: fine-tuning-bedrock\n", - "boto3 Bedrock client successfully created!\n", - "bedrock(https://bedrock.us-east-1.amazonaws.com)\n" - ] - } - ], + "outputs": [], "source": [ "import json\n", "import os\n", @@ -207,8 +103,8 @@ "\n", "# ---- ⚠️ Un-comment and edit the below lines as needed for your AWS setup ⚠️ ----\n", "\n", - "os.environ[\"AWS_DEFAULT_REGION\"] = \"us-east-1\" # E.g. \"us-east-1\"\n", - "os.environ[\"AWS_PROFILE\"] = \"fine-tuning-bedrock\"\n", + "# os.environ[\"AWS_DEFAULT_REGION\"] = \"\" # E.g. \"us-east-1\"\n", + "# os.environ[\"AWS_PROFILE\"] = \"\"\n", "# os.environ[\"BEDROCK_ASSUME_ROLE\"] = \"\" # E.g. \"arn:aws:...\"\n", "# os.environ[\"BEDROCK_ENDPOINT_URL\"] = \"\" # E.g. \"https://...\"\n", "\n", @@ -275,9 +171,7 @@ "source": [ "## Create a LangChain custom prompt template\n", "\n", - "By creating a template for the prompt we can pass it different input variables to it on every run. This is useful when you have to generate content with different input variables that you may be fetching from a database.\n", - "\n", - "Previously we hardcoded the prompt, it might be the case that you have multiple customers sending similar negative feedback and you now want to use each of those customer's emails and respond to them with an apology but you also want to keep the response a bit personalized. In the following cell we are exploring how you can create a `PromptTemplate` to achieve this pattern." + "By creating a template for the prompt we can pass it different input variables to it on every run. This is useful when you have to generate content with different input variables that you may be fetching from a database." ] }, {