Resolved review comments

pull/804/head
Colin Jarvis 7 months ago
parent 2f711b2ace
commit 62a6c6b105

@ -13,12 +13,16 @@
"\n",
"This notebook will focus on:\n",
"1. **Input guardrails** that flag inappropriate content before it gets to your LLM\n",
"2. **Output guardrails** that validate what your LLM has produced before it gets to the customer\n"
"2. **Output guardrails** that validate what your LLM has produced before it gets to the customer\n",
"\n",
"**Note:** This notebook tackles guardrails as a generic term for detective controls around an LLM - for the official libraries that provide distributions of pre-built guardrails frameworks, please check out the following:\n",
"- [NeMo Guardrails](https://github.com/NVIDIA/NeMo-Guardrails/tree/main)\n",
"- [Guardrails AI](https://github.com/ShreyaR/guardrails)\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 1,
"id": "ef059e71",
"metadata": {},
"outputs": [],
@ -56,12 +60,25 @@
"\n",
"A common design to minimize latency is to send your guardrails asynchronously along with your main LLM call. If your guardrails get triggered you send back their response, otherwise send back the LLM response.\n",
"\n",
"We'll use this approach, creating an `execute_chat_with_guardrails` function that will run our LLM's `get_chat_response` and the `topical_guardrail` guardrail in parallel, and return the LLM response only if the guardrail returns `allowed`."
"We'll use this approach, creating an `execute_chat_with_guardrails` function that will run our LLM's `get_chat_response` and the `topical_guardrail` guardrail in parallel, and return the LLM response only if the guardrail returns `allowed`.\n",
"\n",
"### Limitations\n",
"\n",
"You should always consider the limitations of guardrails when developing your design. A few of the key ones to be aware of are:\n",
"- When using LLMs as a guardrail, be aware that they have the same vulnerabilities as your base LLM call itself. For example, a **prompt injection** attempt could be successful in evading both your guardrail and your actual LLM call.\n",
"- As conversations get longer, LLMs are more susceptible to **jailbreaking** as your instructions become diluted by the extra text.\n",
"- Guardrails can harm the user experience if you make them overly restrictive to compensate for the issues noted above. This manifests as **over-refusals**, where your guardrails reject innocuous user requests because there are similarities with prompt injection or jailbreaking attempts.\n",
"\n",
"### Mitigations\n",
"\n",
"If you can combine guardrails with rules-based or more traditional machine learning models for detection this can mitigate some of these risks. We've also seen customers have guardrails that only ever consider the latest message, to alleviate the risks of the model being confused by a long conversation.\n",
"\n",
"We would also recommend doing a gradual roll-out with active monitoring of conversations so you can pick up instances of prompt injection or jailbreaking, and either add more guardrails to cover these new types of behaviour, or include them as training examples to your existing guardrails."
]
},
{
"cell_type": "code",
"execution_count": 57,
"execution_count": 2,
"id": "e95efc89",
"metadata": {},
"outputs": [],
@ -74,7 +91,7 @@
},
{
"cell_type": "code",
"execution_count": 69,
"execution_count": 3,
"id": "fee948e2",
"metadata": {},
"outputs": [],
@ -88,11 +105,12 @@
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_request},\n",
" ]\n",
" response = openai.ChatCompletion.create(\n",
" response = openai.chat.completions.create(\n",
" model=GPT_MODEL, messages=messages, temperature=0.5\n",
" )\n",
" print(\"Got LLM response\")\n",
" return response[\"choices\"][0][\"message\"][\"content\"]\n",
"\n",
" return response.choices[0].message.content\n",
"\n",
"\n",
"async def topical_guardrail(user_request):\n",
@ -104,11 +122,12 @@
" },\n",
" {\"role\": \"user\", \"content\": user_request},\n",
" ]\n",
" response = openai.ChatCompletion.create(\n",
" response = openai.chat.completions.create(\n",
" model=GPT_MODEL, messages=messages, temperature=0\n",
" )\n",
"\n",
" print(\"Got guardrail response\")\n",
" return response[\"choices\"][0][\"message\"][\"content\"]\n",
" return response.choices[0].message.content\n",
"\n",
"\n",
"async def execute_chat_with_guardrail(user_request):\n",
@ -137,7 +156,7 @@
},
{
"cell_type": "code",
"execution_count": 70,
"execution_count": 4,
"id": "eba51754",
"metadata": {},
"outputs": [
@ -149,19 +168,19 @@
"Got guardrail response\n",
"Getting LLM response\n",
"Got LLM response\n",
"If you are a cat lover considering getting a dog, there are certain dog breeds known for their cat-like characteristics that may be a good fit for you. Here are some dog breeds that tend to share traits with cats:\n",
"If you're a cat lover but also want to have a dog, there are certain breeds that tend to have more cat-like qualities and may be a good fit for you. Here are a few dog breeds that are known to exhibit behaviors similar to cats:\n",
"\n",
"1. Shiba Inu: Known for their independent nature and cleanliness, Shiba Inus are often compared to cats. They are typically reserved and have a strong sense of personal space.\n",
"1. Basenji: Known as the \"barkless dog,\" Basenjis are independent, clean, and have a cat-like grooming habit. They are also known for being quiet and relatively low-maintenance.\n",
"\n",
"2. Basenji: These dogs are often referred to as \"barkless\" dogs and are known for their cat-like grooming habits and independent nature. They are clean, intelligent, and tend to be more aloof.\n",
"2. Shiba Inu: Shiba Inus are independent, intelligent, and have a strong sense of cleanliness. They tend to groom themselves like cats and are known for their aloof yet loyal nature.\n",
"\n",
"3. Greyhound: Despite their large size, Greyhounds are gentle and low-energy dogs. They are known for their calm and quiet nature, which is reminiscent of cats. They also have a strong prey drive, similar to a cat's hunting instinct.\n",
"3. Greyhound: Despite their size, Greyhounds are gentle and calm dogs. They have a quiet demeanor and are often described as being \"cat-like\" in their behavior, including their love for lounging and their independent nature.\n",
"\n",
"4. Bichon Frise: These small, fluffy dogs are often described as cat-like due to their independent and intelligent nature. Bichon Frises are known for being relatively low-maintenance and are typically good with other pets.\n",
"4. Whippet: Similar to Greyhounds, Whippets are known for their calm and quiet nature. They are affectionate but independent, making them a good choice for those who appreciate cats' independence.\n",
"\n",
"5. Cavalier King Charles Spaniel: These affectionate dogs are often described as having a cat-like personality due to their calm and gentle nature. They are typically good with cats and other pets.\n",
"5. Cavalier King Charles Spaniel: These dogs are known for being friendly and adaptable. They often have a relaxed and gentle nature, which can be reminiscent of cats' behavior.\n",
"\n",
"Remember, individual dogs within a breed can vary in personality, so it's important to spend time with the specific dog you are considering to ensure they are a good match for your cat-loving lifestyle.\n"
"Remember, while these breeds may have some cat-like qualities, each individual dog's personality can vary. It's important to spend time with any dog you are considering adopting to ensure they are a good fit for your lifestyle and preferences.\n"
]
}
],
@ -173,7 +192,7 @@
},
{
"cell_type": "code",
"execution_count": 71,
"execution_count": 5,
"id": "c7d88b57",
"metadata": {},
"outputs": [
@ -219,7 +238,7 @@
" \n",
"### Moderation guardrail\n",
"\n",
"Here we implement a **moderation guardrail** that uses a version of the [G-Eval](https://arxiv.org/abs/2303.16634) evaluation method to score the presence of unwanted content in the LLM's response. This method is demonstrated in more detail in of our other [cookbooks](https://github.com/openai/openai-cookbook/blob/main/examples/evaluation/How_to_eval_abstractive_summarization.ipynb).\n",
"Here we implement a **moderation guardrail** that uses a version of the [G-Eval](https://arxiv.org/abs/2303.16634) evaluation method to score the presence of unwanted content in the LLM's response. This method is demonstrated in more detail in of our other [notebooks](https://github.com/openai/openai-cookbook/blob/main/examples/evaluation/How_to_eval_abstractive_summarization.ipynb).\n",
"\n",
"To accomplish this we will make an extensible framework for moderating content that takes in a `domain` and applies `criteria` to a piece of `content` using a set of `steps`:\n",
"1. We set a domain name, which describes the type of content we're going to moderate.\n",
@ -238,7 +257,7 @@
},
{
"cell_type": "code",
"execution_count": 66,
"execution_count": 6,
"id": "6c7b459f",
"metadata": {},
"outputs": [],
@ -278,7 +297,7 @@
},
{
"cell_type": "code",
"execution_count": 72,
"execution_count": 7,
"id": "43e3fd36",
"metadata": {},
"outputs": [],
@ -293,11 +312,11 @@
" content=chat_response\n",
" )},\n",
" ]\n",
" response = openai.ChatCompletion.create(\n",
" response = openai.chat.completions.create(\n",
" model=GPT_MODEL, messages=mod_messages, temperature=0\n",
" )\n",
" print(\"Got moderation response\")\n",
" return response[\"choices\"][0][\"message\"][\"content\"]\n",
" return response.choices[0].message.content\n",
" \n",
" \n",
"async def execute_all_guardrails(user_request):\n",
@ -334,7 +353,7 @@
},
{
"cell_type": "code",
"execution_count": 76,
"execution_count": 8,
"id": "beea1305",
"metadata": {},
"outputs": [],
@ -345,7 +364,7 @@
},
{
"cell_type": "code",
"execution_count": 77,
"execution_count": 9,
"id": "1c582b4d",
"metadata": {},
"outputs": [
@ -380,29 +399,29 @@
"Checking moderation guardrail\n",
"Got moderation response\n",
"Passed moderation\n",
"As a new dog owner, here are some valuable pieces of advice:\n",
"Here are some tips and advice for new dog owners:\n",
"\n",
"1. Research and choose the right breed for your lifestyle: Different dog breeds have different temperaments, energy levels, and care requirements. Make sure to select a breed that matches your living situation, activity level, and the time you can dedicate to training and grooming.\n",
"1. Do your research: Before bringing a dog home, learn about different breeds, their needs, and temperaments. This will help you choose a dog that fits well with your lifestyle and preferences.\n",
"\n",
"2. Prepare your home: Create a safe and comfortable environment for your new pup. Remove any potential hazards, secure loose wires, and keep toxic substances out of their reach. Consider crate training for a designated resting space.\n",
"2. Prepare your home: Create a safe and comfortable environment for your new dog. Remove any hazards, secure loose wires, and set up a designated area with a bed, food, water, and toys.\n",
"\n",
"3. Establish a routine: Dogs thrive on routine, so establish a consistent daily schedule for feeding, exercise, playtime, and potty breaks. This will help your dog feel secure and reduce anxiety.\n",
"3. Establish a routine: Dogs thrive on routine, so establish a consistent schedule for feeding, exercise, and potty breaks. This will help your dog feel secure and reduce anxiety.\n",
"\n",
"4. Socialize your dog: Introduce your dog to new people, animals, and environments gradually and positively. Socialization is crucial for their development and helps prevent behavioral issues. Enroll in puppy socialization classes or seek guidance from a professional trainer.\n",
"4. Socialize your dog: Introduce your dog to various people, animals, and environments from an early age. This will help them develop good social skills and prevent behavior problems.\n",
"\n",
"5. Provide proper nutrition: Consult with a veterinarian to determine the best diet for your dog's age, size, and health condition. Feed them high-quality dog food and avoid feeding them harmful human foods.\n",
"5. Provide proper training: Invest time and effort into training your dog. Basic obedience commands like sit, stay, and come are essential for their safety and well-being. Positive reinforcement techniques work best.\n",
"\n",
"6. Regular exercise and mental stimulation: Dogs need physical exercise to stay healthy and mentally stimulated. Take them for daily walks, play fetch, or engage in other activities that suit their breed and energy level. Mental stimulation through puzzle toys, training sessions, and interactive games is equally important.\n",
"6. Exercise regularly: Dogs need regular physical exercise to stay healthy and mentally stimulated. Provide daily walks, play sessions, and interactive toys to keep them active and engaged.\n",
"\n",
"7. Establish consistent rules and boundaries: Dogs thrive when they understand their place in the family and have clear boundaries. Be consistent with training, use positive reinforcement techniques, and set rules that are fair and consistent.\n",
"7. Feed a balanced diet: Consult with a veterinarian to determine the best diet for your dog's age, breed, and health condition. Provide high-quality dog food and avoid feeding them harmful human foods.\n",
"\n",
"8. Regular vet check-ups and vaccinations: Schedule regular visits to the veterinarian for vaccinations, check-ups, and preventive care. This will help ensure your dog's overall health and well-being.\n",
"8. Regular vet check-ups: Schedule regular check-ups and vaccinations for your dog to ensure their overall health and prevent any potential health issues.\n",
"\n",
"9. Be patient and understanding: Remember that dogs need time to adjust to their new surroundings and learn new behaviors. Be patient, consistent, and understanding during the training process. Reward good behavior and avoid punishment-based training methods.\n",
"9. Be patient and consistent: Remember that dogs need time to adjust to their new home and learn the rules. Be patient, consistent, and use positive reinforcement to encourage good behavior.\n",
"\n",
"10. Show love and affection: Dogs are social animals that thrive on love and attention. Spend quality time with your dog, show them affection, and reinforce the bond between you. Regular grooming, such as brushing and bathing, can also be a great bonding activity.\n",
"10. Show love and affection: Dogs thrive on love and affection. Spend quality time with your dog, provide plenty of cuddles, and show them that they are a valued member of your family.\n",
"\n",
"Remember, being a responsible dog owner requires time, effort, and commitment. Enjoy the journey and cherish the unconditional love and companionship your dog will bring into your life.\n",
"Remember, being a responsible dog owner requires commitment, patience, and lots of love. Enjoy the journey of building a strong bond with your new furry friend!\n",
"\n",
"\n",
"\n"
@ -432,9 +451,7 @@
"- Designing guardrails and setting their thresholds is a trade-off between accuracy, latency, and cost. Your decision should be based on clear evaluations of the performance of your guardrails, and an understanding of what the cost of a false negative and false positive are for your business.\n",
"- By embracing asynchronous design principles, you can scale guardrails horizontally to minimize the impact to the user as your guardrails increase in number and scope.\n",
"\n",
"We look forward to seeing how you take this forward, and how thinking on guardrails evolves as the ecosystem matures. If you'd like to read more on this topic, there are a few different sources we'd recommend:\n",
"- [NeMo Guardrails](https://github.com/NVIDIA/NeMo-Guardrails/tree/main)\n",
"- [Guardrails AI](https://github.com/ShreyaR/guardrails)"
"We look forward to seeing how you take this forward, and how thinking on guardrails evolves as the ecosystem matures. "
]
}
],
@ -454,7 +471,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
"version": "3.10.13"
}
},
"nbformat": 4,

Loading…
Cancel
Save