Skip to content

Commit 8d6c011

Browse files
Merge pull request #56 from STRIDES/zyawstutorials
AWS Bedrock Tutorial and example scripts
2 parents e0312e1 + 8fa2863 commit 8d6c011

27 files changed

+869
-1
lines changed

docs/images/bedrock_agents_1.png

225 KB
Loading

docs/images/bedrock_agents_2.png

223 KB
Loading

docs/images/bedrock_agents_3.png

254 KB
Loading

docs/images/bedrock_agents_4.png

262 KB
Loading

docs/images/bedrock_agents_5.png

249 KB
Loading

docs/images/bedrock_agents_6.png

252 KB
Loading

docs/images/bedrock_agents_7.png

375 KB
Loading
204 KB
Loading
138 KB
Loading
434 KB
Loading
189 KB
Loading
351 KB
Loading
477 KB
Loading
227 KB
Loading
254 KB
Loading
318 KB
Loading
209 KB
Loading
317 KB
Loading
282 KB
Loading
317 KB
Loading
314 KB
Loading

docs/images/bedrock_model_access.png

304 KB
Loading

docs/images/bedrock_page.png

332 KB
Loading

tutorials/notebooks/GenAI/AWS_Bedrock_Intro.ipynb

Lines changed: 630 additions & 0 deletions
Large diffs are not rendered by default.

tutorials/notebooks/GenAI/Pubmed_chatbot.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@
218218
"outputs": [],
219219
"source": [
220220
"#download the metadata file\n",
221-
"!aws s3 cp oa_comm/txt/metadata/txt/oa_comm.filelist.txt . --sse"
221+
"!aws s3 cp s3://pmc-oa-opendata/oa_comm/txt/metadata/txt/oa_comm.filelist.txt . --sse"
222222
]
223223
},
224224
{
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
from langchain.retrievers import AmazonKendraRetriever
2+
from langchain.chains import ConversationalRetrievalChain
3+
from langchain.prompts import PromptTemplate
4+
from langchain.llms import SagemakerEndpoint
5+
from langchain.llms.sagemaker_endpoint import LLMContentHandler
6+
import sys
7+
import json
8+
import os
9+
10+
class bcolors:
11+
HEADER = '\033[95m'
12+
OKBLUE = '\033[94m'
13+
OKCYAN = '\033[96m'
14+
OKGREEN = '\033[92m'
15+
WARNING = '\033[93m'
16+
FAIL = '\033[91m'
17+
ENDC = '\033[0m'
18+
BOLD = '\033[1m'
19+
UNDERLINE = '\033[4m'
20+
21+
MAX_HISTORY_LENGTH = 1
22+
23+
def build_chain():
24+
region = os.environ["AWS_REGION"]
25+
kendra_index_id = os.environ["KENDRA_INDEX_ID"]
26+
endpoint_name = os.environ["LLAMA_2_ENDPOINT"]
27+
28+
class ContentHandler(LLMContentHandler):
29+
content_type = "application/json"
30+
accepts = "application/json"
31+
32+
def transform_input(self, prompt: str, model_kwargs: dict) -> bytes:
33+
input_str = json.dumps({"inputs":
34+
[[
35+
#{"role": "system", "content": ""},
36+
{"role": "user", "content": prompt},
37+
]],
38+
**model_kwargs
39+
})
40+
#print(input_str)
41+
42+
return input_str.encode('utf-8')
43+
44+
def transform_output(self, output: bytes) -> str:
45+
response_json = json.loads(output.read().decode("utf-8"))
46+
47+
return response_json[0]['generation']['content']
48+
49+
content_handler = ContentHandler()
50+
51+
llm=SagemakerEndpoint(
52+
endpoint_name=endpoint_name,
53+
region_name=region,
54+
model_kwargs={"parameters": {"max_new_tokens": 1000, "top_p": 0.9,"temperature":0.6}},
55+
endpoint_kwargs={"CustomAttributes":"accept_eula=true"},
56+
content_handler=content_handler,
57+
)
58+
59+
retriever = AmazonKendraRetriever(index_id=kendra_index_id,region_name=region)
60+
61+
prompt_template = """
62+
Ignore everything before.
63+
64+
Instruction:
65+
I want you to act as a research paper summarizer. I will provide you with a research paper on a specific topic in English, and you will create a summary. The summary should be concise and should accurately and objectively communicate the takeaway of the paper. You should not include any personal opinions or interpretations in your summary, but rather focus on objectively presenting the information from the paper. Your summary should be written in your own words and ensure that your summary is clear, concise, and accurately reflects the content of the original paper.
66+
67+
First, provide a concise summary. Then provides the sources.
68+
69+
{question} Answer "don't know" if not present in the document.
70+
{context}
71+
Solution:"""
72+
PROMPT = PromptTemplate(
73+
template=prompt_template, input_variables=["context", "question"],
74+
)
75+
76+
condense_qa_template = """
77+
Chat History:
78+
{chat_history}
79+
Here is a new question for you: {question}
80+
Standalone question:"""
81+
standalone_question_prompt = PromptTemplate.from_template(condense_qa_template)
82+
83+
qa = ConversationalRetrievalChain.from_llm(
84+
llm=llm,
85+
retriever=retriever,
86+
condense_question_prompt=standalone_question_prompt,
87+
return_source_documents=True,
88+
combine_docs_chain_kwargs={"prompt":PROMPT},
89+
)
90+
return qa
91+
92+
def run_chain(chain, prompt: str, history=[]):
93+
print(prompt)
94+
return chain({"question": prompt, "chat_history": history})
95+
96+
if __name__ == "__main__":
97+
chat_history = []
98+
qa = build_chain()
99+
print(bcolors.OKBLUE + "Hello! How can I help you?" + bcolors.ENDC)
100+
print(bcolors.OKCYAN + "Ask a question, start a New search: or CTRL-D to exit." + bcolors.ENDC)
101+
print(">", end=" ", flush=True)
102+
for query in sys.stdin:
103+
if (query.strip().lower().startswith("new search:")):
104+
query = query.strip().lower().replace("new search:","")
105+
chat_history = []
106+
elif (len(chat_history) == MAX_HISTORY_LENGTH):
107+
chat_history.pop(0)
108+
result = run_chain(qa, query, chat_history)
109+
chat_history.append((query, result["answer"]))
110+
print(bcolors.OKGREEN + result['answer'] + bcolors.ENDC)
111+
if 'source_documents' in result:
112+
print(bcolors.OKGREEN + 'Sources:')
113+
for d in result['source_documents']:
114+
print(d.metadata['source'])
115+
print(bcolors.ENDC)
116+
print(bcolors.OKCYAN + "Ask a question, start a New search: or CTRL-D to exit." + bcolors.ENDC)
117+
print(">", end=" ", flush=True)
118+
print(bcolors.OKBLUE + "Bye" + bcolors.ENDC)
119+
120+
121+
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
from langchain.retrievers import PubMedRetriever
2+
from langchain.chains import ConversationalRetrievalChain
3+
from langchain.prompts import PromptTemplate
4+
#from langchain import SagemakerEndpoint
5+
from langchain.llms.sagemaker_endpoint import LLMContentHandler
6+
import sys
7+
import json
8+
import os
9+
from langchain.llms import SagemakerEndpoint
10+
11+
12+
class bcolors:
13+
HEADER = '\033[95m'
14+
OKBLUE = '\033[94m'
15+
OKCYAN = '\033[96m'
16+
OKGREEN = '\033[92m'
17+
WARNING = '\033[93m'
18+
FAIL = '\033[91m'
19+
ENDC = '\033[0m'
20+
BOLD = '\033[1m'
21+
UNDERLINE = '\033[4m'
22+
23+
MAX_HISTORY_LENGTH = 1
24+
25+
def build_chain():
26+
region = os.environ["AWS_REGION"]
27+
#kendra_index_id = os.environ["KENDRA_INDEX_ID"]
28+
endpoint_name = os.environ["LLAMA_2_ENDPOINT"]
29+
30+
class ContentHandler(LLMContentHandler):
31+
content_type = "application/json"
32+
accepts = "application/json"
33+
34+
def transform_input(self, prompt: str, model_kwargs: dict) -> bytes:
35+
input_str = json.dumps({"inputs":
36+
[[
37+
#{"role": "system", "content": ""},
38+
{"role": "user", "content": prompt},
39+
]],
40+
**model_kwargs
41+
})
42+
#print(input_str)
43+
44+
return input_str.encode('utf-8')
45+
46+
def transform_output(self, output: bytes) -> str:
47+
response_json = json.loads(output.read().decode("utf-8"))
48+
49+
return response_json[0]['generation']['content']
50+
51+
content_handler = ContentHandler()
52+
53+
llm=SagemakerEndpoint(
54+
endpoint_name=endpoint_name,
55+
region_name=region,
56+
model_kwargs={"parameters": {"max_new_tokens": 1000, "top_p": 0.9,"temperature":0.6}},
57+
endpoint_kwargs={"CustomAttributes":"accept_eula=true"},
58+
content_handler=content_handler,
59+
)
60+
61+
#retriever = AmazonKendraRetriever(index_id=kendra_index_id,region_name=region)
62+
retriever= PubMedRetriever()
63+
64+
prompt_template = """
65+
Ignore everything before.
66+
67+
Instruction:
68+
I want you to act as a research paper summarizer. I will provide you with a research paper on a specific topic in English, and you will create a summary. The summary should be concise and should accurately and objectively communicate the takeaway of the paper. You should not include any personal opinions or interpretations in your summary, but rather focus on objectively presenting the information from the paper. Your summary should be written in your own words and ensure that your summary is clear, concise, and accurately reflects the content of the original paper.
69+
70+
First, provide a concise summary then provide the sources.
71+
72+
{question} Answer "don't know" if not present in the document.
73+
{context}
74+
Solution:"""
75+
PROMPT = PromptTemplate(
76+
template=prompt_template, input_variables=["context", "question"],
77+
)
78+
79+
condense_qa_template = """
80+
Chat History:
81+
{chat_history}
82+
Here is a new question for you: {question}
83+
Standalone question:"""
84+
standalone_question_prompt = PromptTemplate.from_template(condense_qa_template)
85+
86+
qa = ConversationalRetrievalChain.from_llm(
87+
llm=llm,
88+
retriever=retriever,
89+
condense_question_prompt=standalone_question_prompt,
90+
return_source_documents=True,
91+
combine_docs_chain_kwargs={"prompt":PROMPT},
92+
)
93+
return qa
94+
95+
def run_chain(chain, prompt: str, history=[]):
96+
print(prompt)
97+
return chain({"question": prompt, "chat_history": history})
98+
99+
if __name__ == "__main__":
100+
chat_history = []
101+
qa = build_chain()
102+
print(bcolors.OKBLUE + "Hello! How can I help you?" + bcolors.ENDC)
103+
print(bcolors.OKCYAN + "Ask a question, start a New search: or CTRL-D to exit." + bcolors.ENDC)
104+
print(">", end=" ", flush=True)
105+
for query in sys.stdin:
106+
if (query.strip().lower().startswith("new search:")):
107+
query = query.strip().lower().replace("new search:","")
108+
chat_history = []
109+
elif (len(chat_history) == MAX_HISTORY_LENGTH):
110+
chat_history.pop(0)
111+
result = run_chain(qa, query, chat_history)
112+
chat_history.append((query, result["answer"]))
113+
print(bcolors.OKGREEN + result['answer'] + bcolors.ENDC)
114+
print(bcolors.ENDC)
115+
print(bcolors.OKCYAN + "Ask a question, start a New search: or CTRL-D to exit." + bcolors.ENDC)
116+
print(">", end=" ", flush=True)
117+
print(bcolors.OKBLUE + "Bye" + bcolors.ENDC)

0 commit comments

Comments
 (0)