From 9938b454dd48c6715116969e9ae179ddec1eb9ad Mon Sep 17 00:00:00 2001 From: Arvind Soni Date: Sat, 25 Nov 2023 14:11:00 -0800 Subject: [PATCH 1/4] changed order of pages in side nav. changed the llm langchain map reduce to invoke map reduce explicitly for understanding the concept --- .gitignore | 2 + ...ext_generation.py => 1_text_generation.py} | 0 .../{llm_langchain.py => 2_llm_langchain.py} | 47 +++++++++++++++---- .../{vector_search.py => 3_vector_search.py} | 0 ...ge_generation.py => 4_image_generation.py} | 0 5 files changed, 39 insertions(+), 10 deletions(-) rename cdk/examples/generative_ai_service/web-app/pages/{text_generation.py => 1_text_generation.py} (100%) rename cdk/examples/generative_ai_service/web-app/pages/{llm_langchain.py => 2_llm_langchain.py} (83%) rename cdk/examples/generative_ai_service/web-app/pages/{vector_search.py => 3_vector_search.py} (100%) rename cdk/examples/generative_ai_service/web-app/pages/{image_generation.py => 4_image_generation.py} (100%) diff --git a/.gitignore b/.gitignore index da5e69ef..f6fc7b26 100644 --- a/.gitignore +++ b/.gitignore @@ -75,3 +75,5 @@ __pycache__/ # Customized ENV files .env* +.venv/ +output.json diff --git a/cdk/examples/generative_ai_service/web-app/pages/text_generation.py b/cdk/examples/generative_ai_service/web-app/pages/1_text_generation.py similarity index 100% rename from cdk/examples/generative_ai_service/web-app/pages/text_generation.py rename to cdk/examples/generative_ai_service/web-app/pages/1_text_generation.py diff --git a/cdk/examples/generative_ai_service/web-app/pages/llm_langchain.py b/cdk/examples/generative_ai_service/web-app/pages/2_llm_langchain.py similarity index 83% rename from cdk/examples/generative_ai_service/web-app/pages/llm_langchain.py rename to cdk/examples/generative_ai_service/web-app/pages/2_llm_langchain.py index 9e253be8..bc6f5060 100644 --- a/cdk/examples/generative_ai_service/web-app/pages/llm_langchain.py +++ b/cdk/examples/generative_ai_service/web-app/pages/2_llm_langchain.py @@ -1,6 +1,7 @@ import json, boto3 from langchain.prompts import PromptTemplate from langchain.llms.sagemaker_endpoint import LLMContentHandler, SagemakerEndpoint +from langchain import LLMChain from langchain.chains.summarize import load_summarize_chain from langchain.text_splitter import RecursiveCharacterTextSplitter import streamlit as st @@ -46,17 +47,41 @@ def create_chunks(context): return docs -def invoke_map_reduce(context, query, endpoint_name, region_name): +def simple_map_reduce(context, map_query, reduce_query, endpoint_name, region_name): + docs = create_chunks(context) + content_handler = ContentHandler() + llm = SagemakerEndpoint( + endpoint_name = endpoint_name, + region_name= region_name, + content_handler=content_handler + ) + + map_template = """In the following text find all answers to the question which is delimited by ```. {text}. Provide answer as complete sentence.""" + """```""" + map_query + """```\n""" + map_prompt = PromptTemplate(template=map_template, + input_variables=["text"]) + map_chain = LLMChain(llm=llm,prompt=map_prompt) + + output_list =[] + for doc in docs: + output = map_chain.run(doc) + output_list.append(output) + + reduce_template = """In the following comma separate text list find all answers to the question which is delimited by ```. {text}. Provide answer as complete sentence.""" + """```""" + reduce_query + """```\n""" + reduce_prompt = PromptTemplate(template=reduce_template, + input_variables=["text"]) + reduce_chain = LLMChain(llm=llm,prompt=reduce_prompt) + reduce_output = reduce_chain.run(','.join(output_list)) + return {"map_answers":output_list, "reduce_answer":reduce_output} + +def langchain_map_reduce(context, map_query, reduce_query, endpoint_name, region_name): content_handler = ContentHandler() map_template = """{text} Question: {question} - Question's answer:""" + Answer:""" map_prompt_template = PromptTemplate(template=map_template, input_variables=["text","question"]) - combine_template = """{text} - Combine above texts and rewrite into summary. - """ + combine_template = """{text} """ + """ Question: """ + reduce_query + """ Answer:""" combine_prompt_template = PromptTemplate(template=combine_template, input_variables=["text"]) @@ -77,7 +102,7 @@ def invoke_map_reduce(context, query, endpoint_name, region_name): # split pharagraph docs = create_chunks(context) - summary = summary_chain({"input_documents": docs, "question": query, 'token_max': 10000}, return_only_outputs=True) + summary = summary_chain({"input_documents": docs, "question": map_query, 'token_max': 10000}, return_only_outputs=True) return summary.get("output_text","**no response found**") conversation = """ @@ -103,14 +128,16 @@ def invoke_map_reduce(context, query, endpoint_name, region_name): context = st.text_area("Input Context:", conversation, height=700) - query = st.text_area("Input Query:", "What are being introduced in detail?") + map_query = st.text_area("Map Query:", "What is being introduced in this blog text?") + + reduce_query = st.text_area("Reduce Query:", "What is the common topic?") - if st.button("Generate Response", key=query): - if endpoint_name == "" or query == "": + if st.button("Generate Response", key=map_query): + if endpoint_name == "" or map_query == "": st.error("Please enter a valid endpoint name and prompt!") else: with st.spinner("Wait for it..."): - generated_text = invoke_map_reduce(context, query, endpoint_name, os.getenv('region')) + generated_text = simple_map_reduce(context, map_query, reduce_query, endpoint_name, os.getenv('region')) st.info(generated_text) st.success("Done!") diff --git a/cdk/examples/generative_ai_service/web-app/pages/vector_search.py b/cdk/examples/generative_ai_service/web-app/pages/3_vector_search.py similarity index 100% rename from cdk/examples/generative_ai_service/web-app/pages/vector_search.py rename to cdk/examples/generative_ai_service/web-app/pages/3_vector_search.py diff --git a/cdk/examples/generative_ai_service/web-app/pages/image_generation.py b/cdk/examples/generative_ai_service/web-app/pages/4_image_generation.py similarity index 100% rename from cdk/examples/generative_ai_service/web-app/pages/image_generation.py rename to cdk/examples/generative_ai_service/web-app/pages/4_image_generation.py From b1a3462154ed424147f45dea8ac51d0fadd8842e Mon Sep 17 00:00:00 2001 From: Arvind Soni Date: Sat, 25 Nov 2023 17:08:21 -0800 Subject: [PATCH 2/4] changed the example text --- .../web-app/pages/1_text_generation.py | 27 ++----------------- .../web-app/pages/2_llm_langchain.py | 8 +++--- 2 files changed, 6 insertions(+), 29 deletions(-) diff --git a/cdk/examples/generative_ai_service/web-app/pages/1_text_generation.py b/cdk/examples/generative_ai_service/web-app/pages/1_text_generation.py index 4e73c0c6..b5c27b4b 100644 --- a/cdk/examples/generative_ai_service/web-app/pages/1_text_generation.py +++ b/cdk/examples/generative_ai_service/web-app/pages/1_text_generation.py @@ -20,30 +20,7 @@ def get_parameter(name): runtime = boto3.client("runtime.sagemaker") -conversation = """Customer: Hi there, I'm having a problem with my iPhone. -Agent: Hi! I'm sorry to hear that. What's happening? -Customer: The phone is not charging properly, and the battery seems to be draining very quickly. I've tried different charging cables and power adapters, but the issue persists. -Agent: Hmm, that's not good. Let's try some troubleshooting steps. Can you go to Settings, then Battery, and see if there are any apps that are using up a lot of battery life? -Customer: Yes, there are some apps that are using up a lot of battery. -Agent: Okay, try force quitting those apps by swiping up from the bottom of the screen and then swiping up on the app to close it. -Customer: I did that, but the issue is still there. -Agent: Alright, let's try resetting your iPhone's settings to their default values. This won't delete any of your data. Go to Settings, then General, then Reset, and then choose Reset All Settings. -Customer: Okay, I did that. What's next? -Agent: Now, let's try restarting your iPhone. Press and hold the power button until you see the "slide to power off" option. Slide to power off, wait a few seconds, and then turn your iPhone back on. -Customer: Alright, I restarted it, but it's still not charging properly. -Agent: I see. It looks like we need to run a diagnostic test on your iPhone. Please visit the nearest Apple Store or authorized service provider to get your iPhone checked out. -Customer: Do I need to make an appointment? -Agent: Yes, it's always best to make an appointment beforehand so you don't have to wait in line. You can make an appointment online or by calling the Apple Store or authorized service provider. -Customer: Okay, will I have to pay for the repairs? -Agent: That depends on whether your iPhone is covered under warranty or not. If it is, you won't have to pay anything. However, if it's not covered under warranty, you will have to pay for the repairs. -Customer: How long will it take to get my iPhone back? -Agent: It depends on the severity of the issue, but it usually takes 1-2 business days. -Customer: Can I track the repair status online? -Agent: Yes, you can track the repair status online or by calling the Apple Store or authorized service provider. -Customer: Alright, thanks for your help. -Agent: No problem, happy to help. Is there anything else I can assist you with? -Customer: No, that's all for now. -Agent: Alright, have a great day and good luck with your iPhone!""" +conversation = """Customers were very excited about the wireless charging feature, but the launch has not lived up to their expectations. The phones are not reliably charging and that is frustrating since it is such a fundamental aspect of any electronic device.""" with st.spinner("Retrieving configurations..."): all_configs_loaded = False @@ -60,7 +37,7 @@ def get_parameter(name): context = st.text_area("Input Context:", conversation, height=300) - query = st.text_area("Input Query:", "What steps were suggested to the customer to fix the issue?") + query = st.text_area("Input Query:", "Are customers happy?") st.caption("e.g., write a summary") if st.button("Generate Response", key=query): diff --git a/cdk/examples/generative_ai_service/web-app/pages/2_llm_langchain.py b/cdk/examples/generative_ai_service/web-app/pages/2_llm_langchain.py index bc6f5060..08a2ac1f 100644 --- a/cdk/examples/generative_ai_service/web-app/pages/2_llm_langchain.py +++ b/cdk/examples/generative_ai_service/web-app/pages/2_llm_langchain.py @@ -1,7 +1,7 @@ import json, boto3 from langchain.prompts import PromptTemplate from langchain.llms.sagemaker_endpoint import LLMContentHandler, SagemakerEndpoint -from langchain import LLMChain +from langchain import LLMChain from langchain.chains.summarize import load_summarize_chain from langchain.text_splitter import RecursiveCharacterTextSplitter import streamlit as st @@ -55,17 +55,17 @@ def simple_map_reduce(context, map_query, reduce_query, endpoint_name, region_na region_name= region_name, content_handler=content_handler ) - + map_template = """In the following text find all answers to the question which is delimited by ```. {text}. Provide answer as complete sentence.""" + """```""" + map_query + """```\n""" map_prompt = PromptTemplate(template=map_template, input_variables=["text"]) map_chain = LLMChain(llm=llm,prompt=map_prompt) - + output_list =[] for doc in docs: output = map_chain.run(doc) output_list.append(output) - + reduce_template = """In the following comma separate text list find all answers to the question which is delimited by ```. {text}. Provide answer as complete sentence.""" + """```""" + reduce_query + """```\n""" reduce_prompt = PromptTemplate(template=reduce_template, input_variables=["text"]) From 2e9d69cc14dbd8c320479db2947b626fbf008938 Mon Sep 17 00:00:00 2001 From: Arvind Soni Date: Sat, 25 Nov 2023 19:17:17 -0800 Subject: [PATCH 3/4] changed to use pre-built image from public ECR since the image several GBs and not rworkshop setting is not reliable to build and upload since large image --- .../generative_ai_service/lib/gen_ai_service_stack.py | 6 +++++- cdk/examples/generative_ai_service/sample.env | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cdk/examples/generative_ai_service/lib/gen_ai_service_stack.py b/cdk/examples/generative_ai_service/lib/gen_ai_service_stack.py index 13307205..e206388f 100644 --- a/cdk/examples/generative_ai_service/lib/gen_ai_service_stack.py +++ b/cdk/examples/generative_ai_service/lib/gen_ai_service_stack.py @@ -56,7 +56,11 @@ def __init__( fargate_task_image = ApplicationLoadBalancedTaskImageOptions( container_name=self.stack_props.container_name, # build container image from local folder - image=ContainerImage.from_asset("web-app", platform=Platform.LINUX_AMD64), + # image=ContainerImage.from_asset("web-app", platform=Platform.LINUX_AMD64), + # load pre-built image from public repository + image=ContainerImage.from_registry( + self.stack_props.container_image + ), environment={'region': self.stack_props.aws_region}, container_port=self.stack_props.container_port, execution_role=self.ecs_task_execution_role, diff --git a/cdk/examples/generative_ai_service/sample.env b/cdk/examples/generative_ai_service/sample.env index 7647a0db..6222dfeb 100644 --- a/cdk/examples/generative_ai_service/sample.env +++ b/cdk/examples/generative_ai_service/sample.env @@ -19,6 +19,7 @@ az_count="3" # Gen AI Service Props container_name="web-container" +container_image="public.ecr.aws/g4t0l7p7/sc-genai:latest" container_port="80" task_cpu="2048" task_memory="8192" From 1fe42b81592d88793c819ab8747828694ba9c48d Mon Sep 17 00:00:00 2001 From: arvindsoni80 Date: Sun, 26 Nov 2023 16:29:10 -0800 Subject: [PATCH 4/4] Update cdk/examples/generative_ai_service/web-app/pages/2_llm_langchain.py Co-authored-by: Bryant Biggs --- .../generative_ai_service/web-app/pages/2_llm_langchain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cdk/examples/generative_ai_service/web-app/pages/2_llm_langchain.py b/cdk/examples/generative_ai_service/web-app/pages/2_llm_langchain.py index 08a2ac1f..9551517a 100644 --- a/cdk/examples/generative_ai_service/web-app/pages/2_llm_langchain.py +++ b/cdk/examples/generative_ai_service/web-app/pages/2_llm_langchain.py @@ -66,7 +66,7 @@ def simple_map_reduce(context, map_query, reduce_query, endpoint_name, region_na output = map_chain.run(doc) output_list.append(output) - reduce_template = """In the following comma separate text list find all answers to the question which is delimited by ```. {text}. Provide answer as complete sentence.""" + """```""" + reduce_query + """```\n""" + reduce_template = """In the following comma separated text list, find all answers to the question which is delimited by ```. {text}. Provide answer as complete sentence.""" + """```""" + reduce_query + """```\n""" reduce_prompt = PromptTemplate(template=reduce_template, input_variables=["text"]) reduce_chain = LLMChain(llm=llm,prompt=reduce_prompt)