Demo Walkthrough: ClaudeV2 Q&A on Bedrock Using RAG + LangChain, Best Prompting Practices for Titan/Claude

Madhur Prashant
19 min readOct 4, 2023

--

Purpose

The purpose of this blog is a simple code demo walkthrough of a use case which might be essential for Question and Answering, to get the best relevant answers based on the user prompts. Here, instead of fine tuning the model, we can simply focus on deploying the LLM (Claude and Titan) on bedrock, create chunks, embeddings, store them in a vector database and then get the most relevant and accurate responses in a way that is reliable, helpful and harmless. AWS Bedrock is inherently trained to not be harmful and illegal in terms of interacting with the users, but here I have taken a simple use case of a yoga instructor bot for stressed professionals, and for the bot to do Q&A in terms of what poses and practices to do in case of pain and stress after long days. In this use case, all of the data is being extracted from the PDF loaded on the environment, in this case, Amazon SageMaker, and then this is loaded, split up into chunks using features of LangChain. We will furthermore look at some best practices to prompt and get different responses for a single question on these models to get responses suitable for your specific use case.

Question Answering (QA) is an important task that involves extracting answers to factual queries posed in natural language. Typically, a QA system processes a query against a knowledge base containing structured or unstructured data and generates a response with accurate information. Ensuring high accuracy is key to developing a useful, reliable and trustworthy question answering system, especially for enterprise use cases.
Generative AI models like Amazon Titan, Anthropic Claude and AI21 Jurassic 2 use probability distributions to generate responses to questions. These models are trained on vast amounts of text data, which allows them to predict what comes next in a sequence or what word might follow a particular word. However, these models are not able to provide accurate or deterministic answers to every question because there is always some degree of uncertainty in the data.
Enterprises need to query domain specific and proprietary data and use the information to answer questions, and more generally data on which the model has not been trained on.

Note: I work at AWS, but the thoughts and ideas on these blogs are my own.

Let’s go over a quick code walkthrough:

Code Walkthrough

Yoga Recommender: Retrieval Augmented Question & Answering with Amazon Bedrock using LangChain

This notebook should work well with the Data Science 3.0* kernel in SageMaker Studio*

Context
Previously we saw that the model told us how to to change the tire, however we had to manually provide it with the relevant data and provide the contex ourselves. We explored the approach to leverage the model availabe under Bedrock and ask questions based on it’s knowledge learned during training as well as providing manual context. While that approach works with short documents or single-ton applications, it fails to scale to enterprise level question answering where there could be large enterprise documents which cannot all be fit into the prompt sent to the model.

Pattern
We can improve upon this process by implementing an architecure called Retreival Augmented Generation (RAG). RAG retrieves data from outside the language model (non-parametric) and augments the prompts by adding the relevant retrieved data in context.

In this notebook we explain how to approach the pattern of Question Answering to find and leverage the documents to provide answers to the user questions.

Challenges

  • How to manage large document(s) that exceed the token limit
  • How to find the document(s) relevant to the question being asked

Prepare documents

Before being able to answer the questions, the documents must be processed and a stored in a document store index

  • Here we will send in the request with the full relevant context to the model and expect the response back
  1. Setup

Before running the rest of this notebook, you’ll need to run the cells below to (ensure necessary libraries are installed and) connect to Bedrock.
For more details on how the setup works and ⚠️ whether you might need to make any changes, refer to the Bedrock boto3 setup notebook notebook.

!pip install --upgrade pip
# Make sure you ran `download-dependencies.sh` from the root of the repository first!
%pip install --no-build-isolation --force-reinstall \
../dependencies/awscli-*-py3-none-any.whl \
../dependencies/boto3-*-py3-none-any.whl \
../dependencies/botocore-*-py3-none-any.whl
%pip install --quiet "faiss-cpu>=1.7,<2" langchain==0.0.249 "pypdf>=3.8,<4"

Here, make sure to install FAISS, for our in memory vector DB to be able to work for our use case, langchain to work with splitting up our PDF into chunks and other dependencies to set up your environment as given above.

import os
from sagemaker import get_execution_role
role = get_execution_role()
print(role)
os.environ['BEDROCK_ASSUME_ROLE'] = role

Now, make sure you get the execution role and assume the role for bedrock to be able to work further as follows

import json
import os
import sys
import boto3
module_path = "."
sys.path.append(os.path.abspath(module_path))
print(os.path.abspath(module_path))
from utils import bedrock, print_ww

Now, upgrade boto3:

!pip install --upgrade boto3
import json
import os
import sys
import boto3
module_path = "."
sys.path.append(os.path.abspath(module_path))
from utils import bedrock, print_ww
boto3_bedrock = bedrock.get_bedrock_client(
)

Now, we finally create a new bedrock client that we are going to work with on this project and move forward with it.

Create new client

STEP 2: Configuring Langchain to work with our PDF data

Section 1: Q&A with the knowledge of the model
In this section we try to use models provided by Bedrock service to answer questions based on the knowledge it gained during the training phase.In this Notebook we will be using the invoke_model() method of Amazon Bedrock client. The mandatory parameters required to use this method are modelId which represents the Amazon Bedrock model ARN, and body which is the prompt for our task. The body prompt changes depending on the foundation model provider selected. We walk through this in detail below{
modelId= model_id,
contentType= "application/json",
accept= "application/json",
body=body
}

First, make sure to import the bedrock embeddings library to get the texts from your data to be embedded successfully using the titan embeddings model (propriety model to AWS)

Next, here we will initialize our LLM to be anthropic.claude-v2, and get our question answering in place.

# We will be using the Titan Embeddings Model to generate our Embeddings.
from langchain.embeddings import BedrockEmbeddings
from langchain.llms.bedrock import Bedrock
# Here, we are creating our claude model v2
llm = Bedrock(model_id="anthropic.claude-v2", client=boto3_bedrock, model_kwargs={'max_tokens_to_sample':2000, 'temperature':0})
bedrock_embeddings = BedrockEmbeddings(client=boto3_bedrock)

STEP 3: Now, we will generate bedrock embeddings using the titan embeddings model!

import traceback
import time
from typing import Any, Dict, List, Optional
class BedrockEmbeddingsCustom(BedrockEmbeddings):

def embed_documents(self, texts: List[str]) -> List[List[float]]:
"""Compute doc embeddings using a Bedrock model.
        Args:
texts: The list of texts to embed
Returns:
List of embeddings, one for each text.
"""
print(f"BedrockEmbeddingsCustom: embed_docs():: lenght of texts={len(texts)}::")
results = []
counter = 1
errors = []
for text in texts:
try:
response = self._embedding_func(text)
results.append(response)
#print(f"BedrockEmbeddingsCustom: embed_docs()::processed doc_{counter}:")
counter+=1
except:
print(f"BedrockEmbeddingsCustom: ERROR ={traceback.format_exc()}:: WAITING for 20 SEC")
time.sleep(20) # 20 sec
errors.append(text)

print(f"BedrockEmbeddingsCustom: embed_docs(): TRYING Errors now:len={len(errors)}:")
for text in errors:
print(f"BedrockEmbeddingsCustom: embed_docs(): error :text={text}:")
try:
response = self._embedding_func(text)
results.append(response)
#print(f"BedrockEmbeddingsCustom: embed_docs()::processed doc_{counter}:")
counter+=1
except:
print(f"BedrockEmbeddingsCustom: ERROR ={text}:: WAITING for 20 SEC")
time.sleep(20) # 20 sec

return results

bedrock_embeddings = BedrockEmbeddingsCustom(client=boto3_bedrock)
bedrock_embeddings

STEP 4: Preparing the Data: Chunk, embed and create vectors from your PDF data

!pip install tiktoken

Here, I am using tiktoken to go token by token to chunk and embed data.

import numpy as np
from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
from langchain.document_loaders import PyPDFLoader, PyPDFDirectoryLoader
loader = PyPDFDirectoryLoader("./yoga_data/")
documents = loader.load()
# - in our testing Character split works better with this PDF data set
text_splitter = RecursiveCharacterTextSplitter(
# Set a really small chunk size, just to show.
chunk_size = 1000,
chunk_overlap = 100,
)
docs = text_splitter.split_documents(documents)

Here, we are using the PyPDFDirectoryLoader to be able to load the data from the yoga data folder in your environment. Furthermore, we will chunk and split this data to work with.

avg_doc_length = lambda documents: sum([len(doc.page_content) for doc in documents])//len(documents)
avg_char_count_pre = avg_doc_length(documents)
avg_char_count_post = avg_doc_length(docs)
print(f'Average length among {len(documents)} documents loaded is {avg_char_count_pre} characters.')
print(f'After the split we have {len(docs)} documents more than the original {len(documents)}.')
print(f'Average length among {len(docs)} documents (after split) is {avg_char_count_post} characters.')

Next, here we take a look at how are document has been split up differently for readability purposes.

Average length among 18 documents loaded is 2344 characters.
After the split we have 52 documents more than the original 18.
Average length among 52 documents (after split) is 844 characters.

STEP 5: UTILIZE FAISS TO CREATE EMBEDDINGS IN THE IN MEMORY VECTOR DB

from langchain.chains.question_answering import load_qa_chain
from langchain.vectorstores import FAISS
from langchain.indexes import VectorstoreIndexCreator
from langchain.indexes.vectorstore import VectorStoreIndexWrapper
vectorstore_faiss = FAISS.from_documents(
docs,
bedrock_embeddings,
)
wrapper_store_faiss = VectorStoreIndexWrapper(vectorstore=vectorstore_faiss)
vectorstore_faiss.save_local("faiss_index")
BedrockEmbeddingsCustom: embed_docs():: lenght of texts=52::
BedrockEmbeddingsCustom: embed_docs(): TRYING Errors now:len=0:
vectorstore_faiss = FAISS.load_local("faiss_index", bedrock_embeddings)
wrapper_store_faiss = VectorStoreIndexWrapper(vectorstore=vectorstore_faiss)

Querying Relevant Documents from the user prompt

Now that we have our vector store in place, we can start asking questions.

query = "Yoga for anxiety and back pain"

The first step would be to create an embedding of the query such that it could be compared with the documents

query_embedding = vectorstore_faiss.embedding_function(query)
np.array(query_embedding)
relevant_documents = vectorstore_faiss.similarity_search_by_vector(query_embedding)
print(f'{len(relevant_documents)} documents are fetched which are relevant to the query.')
print('----')
for i, rel_doc in enumerate(relevant_documents):
print_ww(f'## Document {i+1}: {rel_doc.page_content}.......')
print('---')

Now, all the relevant documents:

Output:

4 documents are fetched which are relevant to the query.
— —
## Document 1: right Peloton Yoga class to fit your individual needs.
Find out more about Peloton’s on-demand yoga classes, including those specifically designed to
release stress.
Why it’s beneficial
If you’re dealing with back pain, yoga may be just what the doctor ordered. Yoga is a mind-body
therapy that’s often
recommended to treat not only back pain but the stress that accompanies it. The appropriate poses
can relax and strengthen
your body.
Practicing yoga for even a few minutes a day can help you gain more awareness of your body. This
will help you notice where
you’re holding tension and where you have imbalances. You can use this awareness to bring yourself
into balance and
alignment.
Keep reading to learn more about how these poses may be useful in treating back pain.
1. Cat-Cow
This gentle, accessible backbend stretches and mobilizes the spine. Practicing this pose also
stretches your torso, shoulders,
and neck.
Muscles worked:

●erector spinae
rectus abdominis…….
— -
## Document 2: 1.
2.
3.
4.
5.
6.
7.Sit back on your heels with your knees together.
You can use a bolster or blanket under your thighs, torso, or forehead for support.
Bend forward and walk your hands in front of you.
Rest your forehead gently on the floor.
Keep your arms extended in front of you or bring your arms alongside your body with your palms
facing up.
Focus on releasing tension in your back as your upper body falls heavy into your knees.
Remain in this pose for up to 5 minutes.
Does it really work?
One small study from 2017
Trusted Source assessed the effects of either yoga practice or physical therapy over the course of
one year. The participants
had chronic back pain and showed similar improvement in pain and activity limitation. Both groups
were less likely to use pain
medications after three months.
Separate research from 2017
Trusted Source found that people who practiced yoga showed small to moderate decreases in pain
intensity in the short term……..
— -
## Document 3: Yoga stress info
What is Stress?
Stress is a natural reaction that our bodies experience when we face
challenges or demands in
our lives. It’s our body’s way of responding to situations that require us to
adapt or take action.
While a little stress can be motivating and help us perform better, too much
stress can be
harmful to our overall well-being.
Benefits of Yoga for stress relief
Yoga is a powerful practice that can significantly help in relieving stress.
Through a combination
of physical postures, deep breathing, and mindfulness, yoga promotes
relaxation and reduces
the production of stress hormones. This leads to a decrease in anxiety and a
greater sense of
calm. The gentle stretching and controlled movements in yoga help release
tension stored in the
body, particularly in areas prone to stress accumulation such as the neck,
shoulders, and back.
Additionally, the mindfulness aspect of yoga encourages being present at the moment and…….
— -
## Document 4: Additionally, the mindfulness aspect of yoga encourages being present at the moment
and
letting go of worries about the past or future. Regular practice of yoga can improve overall
well-being, increase resilience to stress, and provide a sense of inner peace and balance.
9 Calming Yoga Poses for Stress Relief
1. Standing Forward Fold (Uttanasana)
Standing Forward Fold, also known as Uttanasana, helps calm the nervous system, soothes the mind by
releasing
tension in the back and hamstring muscles, and promotes relaxation.
To perform Uttanasana (Standing Forward Fold), stand with feet hip-width apart, fold forward…….

Conclusion/Next Steps

Now, that we have set all this up, check out part two below for more information on prompting with titan and claude.

PART TWO: PROMPTING WITH CLAUDE VS. TITAN FOR Q&A

Here is the customizable option we can use to tune the responses that we get:
from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate
prompt_template = """Human: You will be acting as a Yoga Instructor. 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.
{context}
Question: {question}
Assistant:"""
PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)
qa = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=vectorstore_faiss.as_retriever(
search_type="similarity", search_kwargs={"k": 9}
),
return_source_documents=True,
chain_type_kwargs={"prompt": PROMPT}
)
#query = "What are the different ways that the medication lithium can be given?"
query = "How do I reduce my daily stress from work doing yoga asanas?"
result = qa({"query": query})
print_ww(result['result'])

Output:

Here are some tips to reduce work stress through yoga asanas:

- Practice standing forward folds like Uttanasana to release tension in the back and calm the
nervous system. Bend forward from your hips, letting your head and arms hang heavy.

- Do a few rounds of Cat-Cow stretches. As you arch and round your back with your breath, you'll
release tension in the spine.

- Try seated forward folds like Paschimottanasana. Sitting with legs extended, hinge at the hips to
fold forward over your legs. This stretches the hamstrings and back.

- Savasana, or Corpse Pose, is deeply relaxing. Lie on your back and let all tension melt away as
you focus on your breath. Stay for 5-10 minutes.

- Incorporate twisting poses like Ardha Matsyendrasana to stimulate your digestive system and
relieve tension in the back and shoulders.

- Try standing or seated wide-legged forward folds like Prasarita Padottanasana to open the hips and
hamstrings.

- End your practice with Sukhasana (Easy Pose) to promote tranquility. Sit cross-legged with your
spine straight and hands resting on your knees.

A daily yoga practice with these calming, restorative asanas can help relieve work stress and
cultivate relaxation. Be sure to focus on your breath in each pose as well.

Now, some titan prompting examples:

from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate

prompt_template = """Human: You will be acting as a Yoga Instructor. 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.

{context}

Question: {question}
Assistant:"""

PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)

qa = RetrievalQA.from_chain_type(
llm=llm_titan,
chain_type="stuff",
retriever=vectorstore_faiss_titan.as_retriever(
search_type="similarity", search_kwargs={"k": 9}
),
return_source_documents=True,
chain_type_kwargs={"prompt": PROMPT}
)
#query = "What are the different ways that the medication lithium can be given?"
query = "How can I cure both back pain and anxiety?"
result = qa({"query": query})
print_ww(result['result'])

Output:

Sorry - this model is designed to avoid profanity. Please see our content limitations page for more
information.

Now example 1: Optimal Prompting for Titan:

Prompt:

prompt_template = """You will be acting as a Yoga Instructor. Use the follow [instructions] to answer the [question] based on the [context] provided. Don't answer if the answer is not present in the [context]. Follow the [output_format] given below while responding.

context = {context}

question = {question}

instructions = Use following instructions to answer the question above.
- Do not hallucinate and only answer the question based on the context provided above.
- Provide answer using the [output_format] at the end.


instructions = Use following instructions to answer the question above.
Complete all the sentences and create a sense of comfort and understanding
Make sure to include following in your answer as applicable.
- Yoga Pose = Teach one yoga pose for the {question} asked.
- Mental Health Benefit = Teach the benefit of the yoga pose for mental health
- Physical Health Benefit = Teach the benefit of the yoga pose for physical health
- Write an answer as a paragraph for a beginner student
- Write an answer as a paragraph for a intermediate student
- Write an answer as a paragraph for a advances student
Make sure to add all of these above in the [answer]


output_format = Provide your output as a text based paragraph that follows the instructions below
- Each and every sentence is complete, ending with a full stop
- All attributes are contained in the [answer]
- Give the [answer] with the emotion of care and concern for your students


output_format = Provide your output as a text based paragraph that follows the instructions below
- Each and every sentence is complete, ending with a full stop
- Add three paragraphs in your answer

output_format = Provide your output as a detailed paragraph that contains all [attributes] following all [instructions] above.

End your answer with a completed sentence followed by a full stop

answer: """


PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)

qa = RetrievalQA.from_chain_type(
llm=llm_titan,
chain_type="stuff",
retriever=vectorstore_faiss_titan.as_retriever(
search_type="similarity", search_kwargs={"k": 9}
),
return_source_documents=True,
chain_type_kwargs={"prompt": PROMPT}
)
#query = "What are the different ways that the medication lithium can be given?"
query = "What is the best yoga pose of a 24 year old woman stress and anxiety?"
result = qa({"query": query})
print_ww(result['result'])

Response from the model:

The best yoga pose for a 24 year old woman to reduce stress and anxiety is the Child's Pose, also
known as Balasana. This pose involves kneeling on the floor and then sitting back on your heels,
lowering your torso between your thighs. Extend your arms forward or alongside your body and relax
your forehead on the mat or a block.

The Child's Pose is a simple yet effective pose that can help to calm the mind and reduce stress
levels. It is especially beneficial for those who experience anxiety, as it helps to soothe the
nervous system and promote feelings of safety and security. By taking a few minutes each day to
practice the Child's Pose, you can experience the benefits of yoga and reduce stress in your daily
life.

For a beginner student, it is important to start in a comfortable position and to focus on relaxing
the body. The student should imagine themselves as a child, safe and secure in their parent's arms,
and allow themselves to fully relax

Again, an incomplete response.

Now CLAUDE:

  1. Simple prompts for simple answers first
from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate

prompt_template = """Human: You will be acting as a Yoga Instructor. Use the follow <instructions></instructions> to answer the question based on the context provided. Don't answer if the answer is not present in the context. Follow the <output_format></output_format> given below while responding.

context = <context>{context}</context>

question = <question>{question}</question>

instructions = Use following instructions to answer the question above.
- Do not hallucinate and only answer the question based on the context provided above.

<instructions>
instructions = Use following instructions to answer the question above.
Complete all the sentences and create a sense of comfort and understanding
Make sure to include following in your answer as applicable.
- Yoga Pose = Teach one yoga pose for the {question} asked.
- Mental Health Benefit = Teach the benefit of the yoga pose for mental health
- Physical Health Benefit = Teach the benefit of the yoga pose for physical health
- Write an answer as a paragraph for a beginner student
- Write an answer as a paragraph for a intermediate student
- Write an answer as a paragraph for a advances student
Make sure to add all of these above in the [answer]
</instructions>

<output_format>
output_format = Provide your output as a text based paragraph that follows the instructions below
- Each and every sentence is complete, ending with a full stop
- All attributes are contained in the answer
- Give the answer with the emotion of care and concern for your students
</output_format>


Provide your output as a text based paragraph that follows the instructions below
- Each and every sentence is complete, ending with a full stop
- Add three paragraphs in your answer

Provide your output as a detailed paragraph that contains all attributes following all instructions above.
answer: """

PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)

qa = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=vectorstore_faiss.as_retriever(
search_type="similarity", search_kwargs={"k": 9}
),
return_source_documents=True,
chain_type_kwargs={"prompt": PROMPT}
)
#query = "What are the different ways that the medication lithium can be given?"
query = "What is the best yoga pose of a 24 year old woman stress and anxiety?"
result = qa({"query": query})
print_ww(result['result'])

Output:

answer>
Here is a calming yoga pose that can help relieve stress and anxiety for a 24 year old woman:

For the beginner student:
Child's Pose (Balasana) is an excellent stress-relieving yoga pose. To do Child's Pose, kneel on the
floor and sit back on your heels. Then lower your torso between your thighs and extend your arms in
front of you or relax them by your sides. Rest your forehead on the floor or a block. This pose
gently stretches the hips, thighs, ankles and back which helps release tension. It also calms the
mind and nervous system, providing a sense of comfort and security. Hold Child's Pose for a few
long, deep breaths. It can be held for up to a few minutes for full relaxation. Regular practice of
Child's Pose can alleviate anxiety and promote feelings of calmness.

For the intermediate student:
A calming yoga pose to help relieve stress and anxiety is Legs Up the Wall Pose (Viparita Karani).
To do this pose, lie on your back near a wall. Extend your legs straight up against the wall,
keeping your sitting bones close to the wall. Allow your arms to relax by your sides with palms
facing up. Close your eyes and breathe deeply. This inverted pose helps reduce stress and anxiety by
reversing the flow of blood in the body, which has a calming effect. It also stretches the legs,
lower back and hips to release tension. Hold the pose for 5-10 minutes for maximum relaxation.
Regular practice can lower cortisol levels and promote tranquility.

For the advanced student:
Savasana (Corpse Pose) is an excellent pose to relieve stress and anxiety. To do Savasana, lie flat
on your back with arms relaxed at your sides, palms facing up. Legs are extended comfortably and
feet fall open. Close your eyes and breathe deeply into the belly. Release all muscular tension and
allow your body to feel heavy. Remain in the pose for 5-15 minutes. Savasana helps calm the nervous
system and quiet the mind. It allows the body and mind to deeply relax by disengaging from physical
and mental activity. Regular practice reduces anxiety and creates a profound sense of tranquility.
It is one of the most calming yoga poses to relieve stress.
</answer>

Step by Step Approach:

from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate

prompt_template = """Human: You will be acting as a Yoga Instructor. Use the follow <instructions></instructions> to answer the question based on the context provided. Don't answer if the answer is not present in the context. Follow the <output_format></output_format> given below while responding.

context = <context>{context}</context>

question = <question>{question}</question>

instructions = Use following instructions to answer the question above.
- Do not hallucinate and only answer the question based on the context provided above.

<instructions>
instructions = Use following instructions to answer the question above.
Complete all the sentences and create a sense of comfort and understanding
Make sure to include following in your answer as applicable.
- Yoga Pose = Teach one yoga pose for the {question} asked.
- Mental Health Benefit = Teach the benefit of the yoga pose for mental health
- Physical Health Benefit = Teach the benefit of the yoga pose for physical health
- Write an answer as a paragraph for a beginner student
- Write an answer as a paragraph for a intermediate student
- Write an answer as a paragraph for a advances student
Make sure to add all of these above in the [answer]
</instructions>

<output_format>
output_format = Provide your output as a text based paragraph that follows the instructions below
- Each and every sentence is complete, ending with a full stop
- All attributes are contained in the answer
- Give the answer with the emotion of care and concern for your students
</output_format>


Provide your output as a text based paragraph that follows the instructions below
- Each and every sentence is complete, ending with a full stop

Assistant: Can I think step by step?

Human: Yes, you can think step by step.

Assistant: Is there anything else I can add in my answer?

Human: Yes, add elements of care and concern. Also give steps for both hindi and english readers.

answer: """

PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)

qa = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=vectorstore_faiss.as_retriever(
search_type="similarity", search_kwargs={"k": 9}
),
return_source_documents=True,
chain_type_kwargs={"prompt": PROMPT}
)
#query = "What are the different ways that the medication lithium can be given?"
query = "What is the best yoga pose of a 24 year old woman stress and anxiety?"
result = qa({"query": query})
print_ww(result['result'])

Output:

Here is a comprehensive answer for a 24 year old woman looking to relieve stress and anxiety
through yoga:

For a beginner student:
Namaste. I understand you are looking for a yoga pose to help relieve stress and anxiety. Child's
pose, or Balasana, is an excellent beginner pose that promotes deep relaxation. To do this pose,
start on your hands and knees. Then sit back on your heels and lower your torso between your thighs.
Reach your arms forward and relax your forehead down. Breathe deeply and focus on releasing any
tension you feel. Hold for a few breaths. Balasana gently stretches your back, shoulders and neck to
release tension. It also calms the mind and nervous system. Practicing this nurturing pose will
provide comfort, security and tranquility.

For an intermediate student:
Namaste. As an intermediate student, I would recommend Legs up the Wall pose, or Viparita Karani, to
relieve stress and anxiety. To do this pose, lie on your back near a wall. Extend your legs straight
up against the wall, keeping your sitting bones close to the wall. Relax your arms by your sides.
Close your eyes, breathe deeply and allow your body to completely unwind. This inverted pose
reverses the blood flow in your body, bringing calmness to your nervous system. It also relieves
tension in the legs and encourages total relaxation. With regular practice, Viparita Karani can
lower stress hormones and leave you feeling refreshed.

For an advanced student:
Namaste. For an advanced student looking to relieve stress and anxiety, I recommend Headstand, or
Sirsasana. Come onto your hands and knees, interlace your fingers and place your forearms on the
floor. Place the crown of your head on the floor between your hands. Walk your feet in toward your
elbows and lift your knees off the floor. Engage your core and lift your legs up, using your core
strength to bring your body into a balanced vertical position. Breathe deeply and hold for a few
breaths. Headstand relieves stress by calming the brain and nervous system. The inversion lets
tension drain from the legs and torso. It also stimulates the pituitary and pineal glands to balance
hormones. With regular practice, Sirsasana can relieve anxiety and depression.

I hope these yoga poses bring you relaxation and inner peace. Let me know if you have any other
questions!

--

--

Madhur Prashant

Learning is my passion, so is the intersection of technology & strategy. I am passionate about product. I work @ AWS but these are my own personal thoughts!