Skip to content

Commit

Permalink
Merge pull request aws-samples#40 from zack-anthropic/human_assistant…
Browse files Browse the repository at this point in the history
…_formatting

Human-Assistant Formatting Updates + Version Updates
  • Loading branch information
mani-aiml authored Sep 20, 2023
2 parents d202472 + 7ea62b7 commit 286312e
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 55 deletions.
6 changes: 3 additions & 3 deletions 01_Generation/00_generate_w_bedrock.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
"source": [
"## Generate text\n",
"\n",
"Following on the use case explained above, let's prepare an input for the Amazon Bedrock service to generate an email"
"Following on the use case explained above, let's prepare an input for the Amazon Bedrock service to generate an email. Note that this prompt would need to be modified with [Human:/Assistant: formatting for Claude.](https://docs.anthropic.com/claude/docs/human-and-assistant-formatting)"
]
},
{
Expand Down Expand Up @@ -181,8 +181,8 @@
"- `amazon.titan-tg1-large`\n",
"- `ai21.j2-grande-instruct`\n",
"- `ai21.j2-jumbo-instruct`\n",
"- `anthropic.claude-instant-v1`\n",
"- `anthropic.claude-v1`"
"- `anthropic.claude-instant-v2`\n",
"- `anthropic.claude-v2`"
]
},
{
Expand Down
14 changes: 9 additions & 5 deletions 01_Generation/01_zero_shot_generation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@
"- amazon.titan-tg1-large\n",
"- ai21.j2-grande-instruct\n",
"- ai21.j2-jumbo-instruct\n",
"- anthropic.claude-instant-v1\n",
"- anthropic.claude-v1\n",
"- anthropic.claude-instant-v2\n",
"- anthropic.claude-v2\n",
"\n",
"Note that different models support different `model_kwargs`."
]
Expand All @@ -152,7 +152,7 @@
"}\n",
"\n",
"textgen_llm = Bedrock(\n",
" model_id=\"anthropic.claude-v1\",\n",
" model_id=\"anthropic.claude-v2\",\n",
" client=boto3_bedrock,\n",
" model_kwargs=inference_modifier,\n",
")"
Expand Down Expand Up @@ -198,9 +198,13 @@
},
"outputs": [],
"source": [
"response = textgen_llm(\"\"\"Write an email from Bob, Customer Service Manager, \n",
"response = textgen_llm(\"\"\"\n",
"\n",
"Human: Write an email from Bob, Customer Service Manager, \n",
"to the customer \"John Doe\" that provided negative feedback on the service \n",
"provided by our customer support engineer.\\n\\nHuman:\"\"\")\n",
"provided by our customer support engineer.\n",
"\n",
"Assistant:\"\"\")\n",
"\n",
"print_ww(response)"
]
Expand Down
17 changes: 11 additions & 6 deletions 01_Generation/02_contextual_generation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@
"- amazon.titan-tg1-large\n",
"- ai21.j2-grande-instruct\n",
"- ai21.j2-jumbo-instruct\n",
"- anthropic.claude-instant-v1\n",
"- anthropic.claude-v1\n",
"- anthropic.claude-instant-v2\n",
"- anthropic.claude-v2\n",
"\n",
"Note that different models support different `model_kwargs`."
]
Expand All @@ -158,7 +158,7 @@
" \"stop_sequences\": [\"\\n\\nHuman\"]\n",
" }\n",
"\n",
"textgen_llm = Bedrock(model_id = \"anthropic.claude-v1\",\n",
"textgen_llm = Bedrock(model_id = \"anthropic.claude-v2\",\n",
" client = boto3_bedrock, \n",
" model_kwargs = inference_modifier \n",
" )\n"
Expand Down Expand Up @@ -190,9 +190,14 @@
"# Create a prompt template that has multiple input variables\n",
"multi_var_prompt = PromptTemplate(\n",
" input_variables=[\"customerServiceManager\", \"customerName\", \"feedbackFromCustomer\"], \n",
" template=\"\"\"Create an apology email from the Service Manager {customerServiceManager} to {customerName}. \n",
" in response to the following feedback that was received from the customer: {feedbackFromCustomer}.\n",
" \"\"\"\n",
" template=\"\"\"\n",
"\n",
"Human: Create an apology email from the Service Manager {customerServiceManager} to {customerName} in response to the following feedback that was received from the customer: \n",
"<customer_feedback>\n",
"{feedbackFromCustomer}\n",
"</customer_feedback>\n",
"\n",
"Assistant:\"\"\"\n",
")\n",
"\n",
"# Pass in values to the input variables\n",
Expand Down
10 changes: 6 additions & 4 deletions 02_Summarization/01.small-text-summarization-claude.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
" \"top_p\":0.5,\n",
" \"stop_sequences\":[]\n",
" },\n",
" modelId=\"anthropic.claude-v1\", \n",
" modelId=\"anthropic.claude-v2\", \n",
" accept=accept, \n",
" contentType=contentType)\n",
"\n",
Expand All @@ -161,8 +161,9 @@
"outputs": [],
"source": [
"prompt = \"\"\"\n",
"Please provide a summary of the following text.\n",
"\n",
"Human: Please provide a summary of the following text.\n",
"<text>\n",
"AWS took all of that feedback from customers, and today we are excited to announce Amazon Bedrock, \\\n",
"a new service that makes FMs from AI21 Labs, Anthropic, Stability AI, and Amazon accessible via an API. \\\n",
"Bedrock is the easiest way for customers to build and scale generative AI-based applications using FMs, \\\n",
Expand All @@ -173,8 +174,9 @@
"customize FMs with their own data, and easily integrate and deploy them into their applications using the AWS \\\n",
"tools and capabilities they are familiar with, without having to manage any infrastructure (including integrations \\\n",
"with Amazon SageMaker ML features like Experiments to test different models and Pipelines to manage their FMs at scale).\n",
"</text>\n",
"\n",
"\"\"\""
"Assistant:\"\"\""
]
},
{
Expand Down Expand Up @@ -224,7 +226,7 @@
},
"outputs": [],
"source": [
"modelId = 'anthropic.claude-v1' # change this to use a different version from the model provider\n",
"modelId = 'anthropic.claude-v2' # change this to use a different version from the model provider\n",
"accept = 'application/json'\n",
"contentType = 'application/json'\n",
"\n",
Expand Down
16 changes: 10 additions & 6 deletions 03_QuestionAnswering/01_qa_w_rag_claude.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
"- `ai21.j2-grande-instruct`\n",
"- `ai21.j2-jumbo-instruct`\n",
"- `anthropic.claude-instant-v1`\n",
"- `anthropic.claude-v1`\n",
"- `anthropic.claude-v2`"
"\n",
"Similarly for Embeddings:\n",
"\n",
Expand All @@ -195,7 +195,7 @@
"from langchain.llms.bedrock import Bedrock\n",
"\n",
"# - create the Anthropic Model\n",
"llm = Bedrock(model_id=\"anthropic.claude-v1\", client=boto3_bedrock, model_kwargs={'max_tokens_to_sample':200})\n",
"llm = Bedrock(model_id=\"anthropic.claude-v2\", client=boto3_bedrock, model_kwargs={'max_tokens_to_sample':200})\n",
"bedrock_embeddings = BedrockEmbeddings(model_id=\"amazon.titan-embed-g1-text-02\", client=boto3_bedrock)"
]
},
Expand Down Expand Up @@ -412,7 +412,7 @@
"### Quick way\n",
"You have the possibility to use the wrapper provided by LangChain which wraps around the Vector Store and takes input the LLM.\n",
"This wrapper performs the following steps behind the scences:\n",
"- Takes input the question\n",
"- Take the question as input\n",
"- Create question embedding\n",
"- Fetch relevant documents\n",
"- Stuff the documents and the question into a prompt\n",
Expand Down Expand Up @@ -468,7 +468,7 @@
"### Customisable option\n",
"In the above scenario you explored the quick and easy way to get a context-aware answer to your question. Now let's have a look at a more customizable option with the helpf of [RetrievalQA](https://python.langchain.com/en/latest/modules/chains/index_examples/vector_db_qa.html) where you can customize how the documents fetched should be added to prompt using `chain_type` parameter. Also, if you want to control how many relevant documents should be retrieved then change the `k` parameter in the cell below to see different outputs. In many scenarios you might want to know which were the source documents that the LLM used to generate the answer, you can get those documents in the output using `return_source_documents` which returns the documents that are added to the context of the LLM prompt. `RetrievalQA` also allows you to provide a custom [prompt template](https://python.langchain.com/en/latest/modules/prompts/prompt_templates/getting_started.html) which can be specific to the model.\n",
"\n",
"Note: In this example we are using Anthropic Claude as the LLM under Amazon Bedrock, this particular model performs best if the inputs are provided under `Human:` and the model is requested to generate an output after `Assistant:`. In the cell below you see an example of how to control the prompt such that the LLM stays grounded and doesn't answer outside the context."
"Note: In this example we are using Anthropic Claude as the LLM under Amazon Bedrock. This particular model [performs best](https://docs.anthropic.com/claude/docs/human-and-assistant-formatting) if the inputs are provided under `Human:` and the model is requested to generate an output after `Assistant:`. In the cell below you see an example of how to control the prompt such that the LLM stays grounded and doesn't answer outside the context."
]
},
{
Expand All @@ -482,11 +482,15 @@
"from langchain.chains import RetrievalQA\n",
"from langchain.prompts import PromptTemplate\n",
"\n",
"prompt_template = \"\"\"Human: Use the following pieces of context to provide a concise answer to the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.\n",
"prompt_template = \"\"\"\n",
"\n",
"Human: Use the following pieces of context to provide a concise answer to the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.\n",
"<context>\n",
"{context}\n",
"</context\n",
"\n",
"Question: {question}\n",
"\n",
"Assistant:\"\"\"\n",
"\n",
"PROMPT = PromptTemplate(\n",
Expand Down Expand Up @@ -523,7 +527,7 @@
"metadata": {},
"source": [
"## Conclusion\n",
"Congratulations on completing this moduel on retrieval augmented generation! This is an important technique that combines the power of large language models with the precision of retrieval methods. By augmenting generation with relevant retrieved examples, the responses we recieved become more coherent, consistent and grounded. You should feel proud of learning this innovative approach. I'm sure the knowledge you've gained will be very useful for building creative and engaging language generation systems. Well done!\n",
"Congratulations on completing this module on retrieval augmented generation! This is an important technique that combines the power of large language models with the precision of retrieval methods. By augmenting generation with relevant retrieved examples, the responses we recieved become more coherent, consistent and grounded. You should feel proud of learning this innovative approach. I'm sure the knowledge you've gained will be very useful for building creative and engaging language generation systems. Well done!\n",
"\n",
"In the above implementation of RAG based Question Answering we have explored the following concepts and how to implement them using Amazon Bedrock and it's LangChain integration.\n",
"\n",
Expand Down
32 changes: 18 additions & 14 deletions 04_Chatbot/00_Chatbot_Claude.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@
"from langchain.memory import ConversationBufferMemory\n",
"\n",
"cl_llm = Bedrock(\n",
" model_id=\"anthropic.claude-v1\",\n",
" model_id=\"anthropic.claude-v2\",\n",
" client=boto3_bedrock,\n",
" model_kwargs={\"max_tokens_to_sample\": 1000},\n",
")\n",
Expand All @@ -346,7 +346,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"What happens here? We said \"Hi there!\" and the model spat out a several conversations. This is due to the fact that the default prompt used by Langchain ConversationChain is not well designed for Claude. An [effective Claude prompt](https://docs.anthropic.com/claude/docs/introduction-to-prompt-design) should end with `\\n\\nHuman\\n\\nAassistant:`. Let's fix this.\n",
"What happens here? We said \"Hi there!\" and the model spat out a several conversations. This is due to the fact that the default prompt used by Langchain ConversationChain is not well designed for Claude. An [effective Claude prompt](https://docs.anthropic.com/claude/docs/introduction-to-prompt-design) should contain `\\n\\nHuman:` at the beginning and also contain `\\n\\nAssistant:` in the prompt sometime after the `\\n\\nHuman:` (optionally followed by other text that you want to [put in Claude's mouth](https://docs.anthropic.com/claude/docs/human-and-assistant-formatting#use-human-and-assistant-to-put-words-in-claudes-mouth)). Let's fix this.\n",
"\n",
"To learn more about how to write prompts for Claude, check [Anthropic documentation](https://docs.anthropic.com/claude/docs/introduction-to-prompt-design)."
]
Expand Down Expand Up @@ -390,16 +390,21 @@
")\n",
"\n",
"# langchain prompts do not always work with all the models. This prompt is tuned for Claude\n",
"claude_prompt = PromptTemplate.from_template(\"\"\"The following is a friendly conversation between a human and an AI.\n",
"claude_prompt = PromptTemplate.from_template(\"\"\"\n",
"\n",
"Human: The following is a friendly conversation between a human and an AI.\n",
"The AI is talkative and provides lots of specific details from its context. If the AI does not know\n",
"the answer to a question, it truthfully says it does not know.\n",
"\n",
"Current conversation:\n",
"<conversation_history>\n",
"{history}\n",
"</conversation_history>\n",
"\n",
"\n",
"Human: {input}\n",
"\n",
"Here is the human's next reply:\n",
"<human_reply>\n",
"{input}\n",
"</human_reply>\n",
"\n",
"Assistant:\n",
"\"\"\")\n",
Expand Down Expand Up @@ -556,7 +561,7 @@
"source": [
"### Interactive session using ipywidgets\n",
"\n",
"The following utility class allows us to interact with Claude in a more natural way. We write out question in an input box, and get Claude answer. We can then continue our conversation."
"The following utility class allows us to interact with Claude in a more natural way. We write out the question in an input box, and get Claude's answer. We can then continue our conversation."
]
},
{
Expand Down Expand Up @@ -591,7 +596,7 @@
" else: \n",
" prompt = self.name.value\n",
" if 'q' == prompt or 'quit' == prompt or 'Q' == prompt:\n",
" print(\"Thank you , that was a nice chat !!\")\n",
" print(\"Thank you , that was a nice chat!!\")\n",
" return\n",
" elif len(prompt) > 0:\n",
" with self.out:\n",
Expand Down Expand Up @@ -719,8 +724,8 @@
"# store previous interactions using ConversationalBufferMemory and add custom prompts to the chat.\n",
"memory = ConversationBufferMemory()\n",
"memory.chat_memory.add_user_message(\"You will be acting as a career coach. Your goal is to give career advice to users\")\n",
"memory.chat_memory.add_ai_message(\"I am career coach and give career advice\")\n",
"cl_llm = Bedrock(model_id=\"anthropic.claude-v1\",client=boto3_bedrock)\n",
"memory.chat_memory.add_ai_message(\"I am a career coach and give career advice\")\n",
"cl_llm = Bedrock(model_id=\"anthropic.claude-v2\",client=boto3_bedrock)\n",
"conversation = ConversationChain(\n",
" llm=cl_llm, verbose=True, memory=memory\n",
")\n",
Expand Down Expand Up @@ -809,7 +814,7 @@
"metadata": {},
"source": [
"## Chatbot with Context \n",
"In this use case we will ask the Chatbot to answer question from some external corpus it has likely never seen before. To do this we apply a pattern called RAG (Retrieval Augmented Generation): the idea is to index the corpus in chunks, then lookup which sections of the corpus might be relevant to provide an answer by using semantic similarity between the chunks and the question. Finally the most relevant chunks are aggregated and passed as context to the ConversationChain, similar to providing an history.\n",
"In this use case we will ask the Chatbot to answer question from some external corpus it has likely never seen before. To do this we apply a pattern called RAG (Retrieval Augmented Generation): the idea is to index the corpus in chunks, then look up which sections of the corpus might be relevant to provide an answer by using semantic similarity between the chunks and the question. Finally the most relevant chunks are aggregated and passed as context to the ConversationChain, similar to providing a history.\n",
"\n",
"We will take a csv file and use **Titan Embeddings Model** to create vectors for each line of the csv. This vector is then stored in FAISS, an open source library providing an in-memory vector datastore. When the chatbot is asked a question, we query FAISS with the question and retrieve the text which is semantically closest. This will be our answer. "
]
Expand Down Expand Up @@ -1173,8 +1178,8 @@
"\n",
"Assistant: Question:\"\"\")\n",
"\n",
"# recreate the Claude LLM with more tokens to sample - this provide longer responses but introduces some latency\n",
"cl_llm = Bedrock(model_id=\"anthropic.claude-v1\", client=boto3_bedrock, model_kwargs={\"max_tokens_to_sample\": 500})\n",
"# recreate the Claude LLM with more tokens to sample - this provides longer responses but introduces some latency\n",
"cl_llm = Bedrock(model_id=\"anthropic.claude-v2\", client=boto3_bedrock, model_kwargs={\"max_tokens_to_sample\": 500})\n",
"memory_chain = ConversationBufferMemory(memory_key=\"chat_history\", return_messages=True)\n",
"qa = ConversationalRetrievalChain.from_llm(\n",
" llm=cl_llm, \n",
Expand All @@ -1192,7 +1197,6 @@
"qa.combine_docs_chain.llm_chain.prompt = PromptTemplate.from_template(\"\"\"\n",
"{context}\n",
"\n",
"\n",
"Human: Use at maximum 3 sentences to answer the question inside the <q></q> XML tags. \n",
"\n",
"<q>{question}</q>\n",
Expand Down
2 changes: 2 additions & 0 deletions 06_CodeGeneration/00_code_generatation_w_bedrock.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@
"# Analyzing sales\n",
"\n",
"prompt_data = \"\"\"\n",
"\n",
"Human: You have a CSV, sales.csv, with columns:\n",
"- date (YYYY-MM-DD)\n",
"- product_id\n",
Expand All @@ -227,6 +228,7 @@
"- Visualize monthly sales using a bar chart\n",
"\n",
"Ensure the code is syntactically correct, bug-free, optimized, not span multiple lines unnessarily, and prefer to use standard libraries. Return only python code without any surrounding text, explanation or context.\n",
"\n",
"Assistant:\n",
"\"\"\""
]
Expand Down
9 changes: 6 additions & 3 deletions 06_CodeGeneration/01_sql_query_generate_w_bedrock.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,14 @@
"source": [
"# create the prompt to generate SQL query\n",
"prompt_data = \"\"\"\n",
"Command: Human: AnyCompany has a database with a table named sales_data containing sales records. The table has following columns:\n",
"\n",
"Human: AnyCompany has a database with a table named sales_data containing sales records. The table has following columns:\n",
"- date (YYYY-MM-DD)\n",
"- product_id\n",
"- price\n",
"- units_sold\n",
"\n",
"Can you generate SQL queries for below: \n",
"Can you generate SQL queries for the below: \n",
"- Identify the top 5 best selling products by total sales for the year 2023\n",
"- Calculate the monthly average sales for the year 2023\n",
"\n",
Expand Down Expand Up @@ -227,7 +228,9 @@
"outputs": [],
"source": [
"# create the prompt\n",
"prompt_sql_data = \"\"\"Command: You're provided with a database schema representing any hospital's patient management system.\n",
"prompt_sql_data = \"\"\"\n",
"\n",
"Human: You're provided with a database schema representing any hospital's patient management system.\n",
"The system holds records about patients, their prescriptions, doctors, and the medications prescribed.\n",
"\n",
"Here's the schema:\n",
Expand Down
Loading

0 comments on commit 286312e

Please sign in to comment.