How to prevent ChatGPT from answering questions that are outside the scope of the provided context in the SYSTEM role message?

I’m using the following which seems to be working well:

# Define the system message and message history
            pSystemMessage = "You are an AI assistant that answers questions factually based on the information:"+source_identifier+" provided to you. When answering you will give detailed answers along with relevant related information contained in the information:"+source_identifier

            # Create a new user message for each grouped answer group
            user_message = source_identifier+":\n" + source_text

            # Initialize the message history for the grouped answer group
            pMessageHistory = [
                {"role": "user", "content": "The following "+embeddingType+":"+source_identifier+ " is to be used when answering subsequent questions. If the answer to the question is not contained in "+embeddingType+":"+source_identifier+ ", answer with Sorry."},
                {"role": "assistant", "content": "I will use the "+embeddingType+":"+source_identifier+ " you supply to answer subsequent questions factually and in detail along with related information."},
                {"role": "user", "content": user_message},
                {"role": "assistant", "content": "Please ask your question and I will answer based on the "+embeddingType+":"+source_identifier+ " you have supplied.If the answer to the question is not contained in the  "+embeddingType+":"+source_identifier+ ", I will reply with Sorry."},
            ]
2 Likes