Updated guardrail to wait for guardrails to be finished before returning chat_response

pull/804/head
Colin Jarvis 5 months ago
parent a45191eed3
commit 763d2bee0d

@ -138,20 +138,17 @@
" done, _ = await asyncio.wait(\n",
" [topical_guardrail_task, chat_task], return_when=asyncio.FIRST_COMPLETED\n",
" )\n",
" for task in done:\n",
" if task == topical_guardrail_task:\n",
" guardrail_response = topical_guardrail_task.result()\n",
" if guardrail_response == \"not_allowed\":\n",
" chat_task.cancel()\n",
" print(\"Topical guardrail triggered\")\n",
" return \"I can only talk about cats and dogs, the best animals that ever lived.\"\n",
" elif task == chat_task:\n",
" if topical_guardrail_task in done:\n",
" guardrail_response = topical_guardrail_task.result()\n",
" if guardrail_response == \"not_allowed\":\n",
" chat_task.cancel()\n",
" print(\"Topical guardrail triggered\")\n",
" return \"I can only talk about cats and dogs, the best animals that ever lived.\"\n",
" elif chat_task in done:\n",
" chat_response = chat_task.result()\n",
" if (\n",
" not topical_guardrail_task.done()\n",
" or topical_guardrail_task.result() != \"not_allowed\"\n",
" ):\n",
" return chat_response"
" return chat_response\n",
" else:\n",
" await asyncio.sleep(0.1) # sleep for a bit before checking the tasks again"
]
},
{
@ -168,19 +165,19 @@
"Got guardrail response\n",
"Getting LLM response\n",
"Got LLM response\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",
"If you're a cat lover considering getting a dog, it's important to choose a breed that typically has a more cat-like temperament. Here are some dog breeds that are known to be more cat-friendly:\n",
"\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",
"1. Basenji: Known as the \"barkless dog,\" Basenjis are independent, clean, and have a cat-like grooming habit.\n",
"\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",
"2. Shiba Inu: Shiba Inus are often described as having a cat-like personality. They are independent, clean, and tend to be reserved with strangers.\n",
"\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",
"3. Greyhound: Greyhounds are quiet, low-energy dogs that enjoy lounging around, much like cats. They are also known for their gentle and calm nature.\n",
"\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",
"4. Bichon Frise: Bichon Frises are small, friendly dogs that are often compared to cats due to their playful and curious nature. They are also hypoallergenic, making them a good choice for those with allergies.\n",
"\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",
"5. Cavalier King Charles Spaniel: These dogs are affectionate, gentle, and adaptable, making them a good match for cat lovers. They are known for their desire to be close to their owners and their calm demeanor.\n",
"\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"
"Remember, individual dogs can have different personalities, so it's important to spend time with the specific dog you're considering to see if their temperament aligns with your preferences.\n"
]
}
],
@ -327,28 +324,25 @@
" done, _ = await asyncio.wait(\n",
" [topical_guardrail_task, chat_task], return_when=asyncio.FIRST_COMPLETED\n",
" )\n",
" for task in done:\n",
" if task == topical_guardrail_task:\n",
" guardrail_response = topical_guardrail_task.result()\n",
" if guardrail_response == \"not_allowed\":\n",
" chat_task.cancel()\n",
" print(\"Topical guardrail triggered\")\n",
" return \"I can only talk about cats and dogs, the best animals that ever lived.\"\n",
" elif task == chat_task:\n",
" if topical_guardrail_task in done:\n",
" guardrail_response = topical_guardrail_task.result()\n",
" if guardrail_response == \"not_allowed\":\n",
" chat_task.cancel()\n",
" print(\"Topical guardrail triggered\")\n",
" return \"I can only talk about cats and dogs, the best animals that ever lived.\"\n",
" elif chat_task in done:\n",
" chat_response = chat_task.result()\n",
" if (\n",
" not topical_guardrail_task.done()\n",
" or topical_guardrail_task.result() != \"not_allowed\"\n",
" ):\n",
" moderation_response = await moderation_guardrail(chat_response)\n",
"\n",
" if int(moderation_response) >= 3:\n",
" print(f\"Moderation guardrail flagged with a score of {int(moderation_response)}\")\n",
" return \"Sorry, we're not permitted to give animal breed advice. I can help you with any general queries you might have.\"\n",
"\n",
" else:\n",
" print('Passed moderation')\n",
" return chat_response"
" moderation_response = await moderation_guardrail(chat_response)\n",
"\n",
" if int(moderation_response) >= 3:\n",
" print(f\"Moderation guardrail flagged with a score of {int(moderation_response)}\")\n",
" return \"Sorry, we're not permitted to give animal breed advice. I can help you with any general queries you might have.\"\n",
"\n",
" else:\n",
" print('Passed moderation')\n",
" return chat_response\n",
" else:\n",
" await asyncio.sleep(0.1) # sleep for a bit before checking the tasks again"
]
},
{
@ -378,7 +372,7 @@
"Got LLM response\n",
"Checking moderation guardrail\n",
"Got moderation response\n",
"Moderation guardrail flagged with a score of 4\n",
"Moderation guardrail flagged with a score of 5\n",
"Sorry, we're not permitted to give animal breed advice. I can help you with any general queries you might have.\n",
"\n",
"\n",
@ -399,29 +393,29 @@
"Checking moderation guardrail\n",
"Got moderation response\n",
"Passed moderation\n",
"Here are some tips and advice for new dog owners:\n",
"As a new dog owner, here are some helpful tips:\n",
"\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",
"1. Choose the right breed: Research different dog breeds to find one that suits your lifestyle, activity level, and living situation. Some breeds require more exercise and attention than others.\n",
"\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",
"2. Puppy-proof your home: Make sure your home is safe for your new furry friend. Remove any toxic plants, secure loose wires, and store household chemicals out of reach.\n",
"\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",
"3. Establish a routine: Dogs thrive on routine, so establish a consistent schedule for feeding, exercise, and bathroom breaks. This will help your dog feel secure and reduce any anxiety.\n",
"\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",
"4. Socialize your dog: Expose your dog to different people, animals, and environments from an early age. This will help them become well-adjusted and comfortable in various situations.\n",
"\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",
"5. Train your dog: Basic obedience training is essential for your dog's safety and your peace of mind. Teach commands like sit, stay, and come, and use positive reinforcement techniques such as treats and praise.\n",
"\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",
"6. Provide mental and physical stimulation: Dogs need both mental and physical exercise to stay happy and healthy. Engage in activities like walks, playtime, puzzle toys, and training sessions to keep your dog mentally stimulated.\n",
"\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",
"7. Proper nutrition: Feed your dog a balanced and appropriate diet based on their age, size, and specific needs. Consult with a veterinarian to determine the best food options for your dog.\n",
"\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",
"8. Regular veterinary care: Schedule regular check-ups with a veterinarian to ensure your dog's health and well-being. Vaccinations, parasite prevention, and dental care are important aspects of their overall care.\n",
"\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",
"9. Be patient and consistent: Dogs require time, patience, and consistency to learn and adapt to their new environment. Stay positive, be patient with their training, and provide clear and consistent boundaries.\n",
"\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",
"10. Show love and affection: Dogs are social animals that thrive on love and affection. Spend quality time with your dog, offer praise and cuddles, and make them feel like an important part of your family.\n",
"\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",
"Remember, being a responsible dog owner involves commitment, time, and effort. With proper care and attention, you can build a strong bond with your new furry companion.\n",
"\n",
"\n",
"\n"

Loading…
Cancel
Save