Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed to explicit map reduce function #177

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ __pycache__/

# Customized ENV files
.env*
.venv/
output.json
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove lines 58-59 if no longer building image from source

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to be a temp measure for re:Invent

# 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,
Expand Down
1 change: 1 addition & 0 deletions cdk/examples/generative_ai_service/sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import streamlit as st
import requests
import json
import time
import boto3

st.set_page_config(
page_title="text generation",
layout="wide",
page_icon=":technologist:"
)

st.header("Generative AI Demo - Text Generation :books:")
st.caption("Using FLAN-T5-XL model from Hugging Face")

def get_parameter(name):
ssm = boto3.client('ssm')
param = ssm.get_parameter(Name=name,WithDecryption=True)
return param['Parameter']['Value']

runtime = boto3.client("runtime.sagemaker")

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

while not all_configs_loaded:
try:
# Retrive SageMaker Endpoint name from Parameter Store
sm_endpoint = get_parameter("txt2txt_sm_endpoint")
all_configs_loaded = True
except:
time.sleep(5)

endpoint_name = st.sidebar.text_input("SageMaker Endpoint Name:",sm_endpoint)

context = st.text_area("Input Context:", conversation, height=300)

query = st.text_area("Input Query:", "Are customers happy?")
st.caption("e.g., write a summary")

if st.button("Generate Response", key=query):
if endpoint_name == "" or query == "":
st.error("Please enter a valid endpoint name and prompt!")
else:
with st.spinner("Wait for it..."):
try:
prompt = f"{context}\n{query}"
response = runtime.invoke_endpoint(
EndpointName=endpoint_name,
Body=prompt,
ContentType="application/x-text",
)
response_body = json.loads(response["Body"].read().decode())
generated_text = response_body["generated_text"]
st.write(generated_text)

except requests.exceptions.ConnectionError as errc:
st.error("Error Connecting:",errc)

except requests.exceptions.HTTPError as errh:
st.error("Http Error:",errh)

except requests.exceptions.Timeout as errt:
st.error("Timeout Error:",errt)

except requests.exceptions.RequestException as err:
st.error("OOps: Something Else",err)

st.success("Done!")
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 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)
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"])

Expand All @@ -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 = """
Expand All @@ -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!")

This file was deleted.