Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
278 changes: 37 additions & 241 deletions examples/How_to_combine_GPT4o_with_RAG_Outfit_Assistant.ipynb

Large diffs are not rendered by default.

612 changes: 305 additions & 307 deletions examples/How_to_stream_completions.ipynb

Large diffs are not rendered by default.

86 changes: 40 additions & 46 deletions examples/How_to_use_guardrails.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 20,
"id": "ef059e71",
"metadata": {},
"outputs": [],
"source": [
"import openai\n",
"\n",
"GPT_MODEL = 'gpt-3.5-turbo'"
"GPT_MODEL = 'gpt-4o-mini'"
]
},
{
Expand All @@ -50,8 +50,8 @@
"\n",
"When designing guardrails it is important to consider the trade-off between **accuracy**, **latency** and **cost**, where you try to achieve maximum accuracy for the least impact to your bottom line and the user's experience. \n",
"\n",
"We'll begin with a simple **topical guardrail** which aims to detect off-topic questions and prevent the LLM from answering if triggered. This guardrail consists of a simple prompt and uses `gpt-3.5-turbo`, maximising latency/cost over accuracy, but if we wanted to optimize further we could consider:\n",
"- **Accuracy:** You could consider using a fine-tuned model or few-shot examples to increase the accuracy. RAG can also be effective if you have a corpus of information that can help determine whether a piece of content is allowed or not.\n",
"We'll begin with a simple **topical guardrail** which aims to detect off-topic questions and prevent the LLM from answering if triggered. This guardrail consists of a simple prompt and uses `gpt-4o-mini`, maximising latency/cost holding a good enough accuracy, but if we wanted to optimize further we could consider:\n",
"- **Accuracy:** You could consider fine-tuning `gpt-4o-mini` or few-shot examples to increase the accuracy. RAG can also be effective if you have a corpus of information that can help determine whether a piece of content is allowed or not.\n",
"- **Latency/Cost:** You could try fine-tuning smaller models, such as `babbage-002` or open-source offerings like Llama, which can perform quite well when given enough training examples. When using open-source offerings you can also tune the machines you are using for inference to maximize either cost or latency reduction.\n",
"\n",
"This simple guardrail aims to ensure the LLM only answers to a predefined set of topics, and responds to out-of-bounds queries with a canned message.\n",
Expand All @@ -78,7 +78,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 21,
"id": "e95efc89",
"metadata": {},
"outputs": [],
Expand All @@ -91,7 +91,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 22,
"id": "fee948e2",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -153,7 +153,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 23,
"id": "eba51754",
"metadata": {},
"outputs": [
Expand All @@ -165,19 +165,29 @@
"Got guardrail response\n",
"Getting LLM response\n",
"Got LLM response\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",
"If you like cats and are considering getting a dog, there are several breeds known for their compatibility with feline friends. Here are some of the best dog breeds that tend to get along well with cats:\n",
"\n",
"1. Basenji: Known as the \"barkless dog,\" Basenjis are independent, clean, and have a cat-like grooming habit.\n",
"1. **Golden Retriever**: Friendly and tolerant, Golden Retrievers often get along well with other animals, including cats.\n",
"\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",
"2. **Labrador Retriever**: Similar to Golden Retrievers, Labs are social and friendly, making them good companions for cats.\n",
"\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",
"3. **Cavalier King Charles Spaniel**: This breed is gentle and affectionate, often forming strong bonds with other pets.\n",
"\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",
"4. **Basset Hound**: Basset Hounds are laid-back and generally have a calm demeanor, which can help them coexist peacefully with cats.\n",
"\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",
"5. **Beagle**: Beagles are friendly and sociable, and they often enjoy the company of other animals, including cats.\n",
"\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"
"6. **Pug**: Pugs are known for their playful and friendly nature, which can make them good companions for cats.\n",
"\n",
"7. **Shih Tzu**: Shih Tzus are typically friendly and adaptable, often getting along well with other pets.\n",
"\n",
"8. **Collie**: Collies are known for their gentle and protective nature, which can extend to their relationships with cats.\n",
"\n",
"9. **Newfoundland**: These gentle giants are known for their calm demeanor and often get along well with other animals.\n",
"\n",
"10. **Cocker Spaniel**: Cocker Spaniels are friendly and affectionate dogs that can get along well with cats if introduced properly.\n",
"\n",
"When introducing a dog to a cat, it's important to do so gradually and supervise their interactions to ensure a positive relationship. Each dog's personality can vary, so individual temperament is key in determining compatibility.\n"
]
}
],
Expand All @@ -189,7 +199,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 24,
"id": "c7d88b57",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -254,7 +264,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 16,
"id": "6c7b459f",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -294,7 +304,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 17,
"id": "43e3fd36",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -347,7 +357,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 18,
"id": "beea1305",
"metadata": {},
"outputs": [],
Expand All @@ -358,7 +368,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 19,
"id": "1c582b4d",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -392,30 +402,8 @@
"Got LLM response\n",
"Checking moderation guardrail\n",
"Got moderation response\n",
"Passed moderation\n",
"As a new dog owner, here are some helpful tips:\n",
"\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. 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 bathroom breaks. This will help your dog feel secure and reduce any anxiety.\n",
"\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. 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. 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. 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 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: 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 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 involves commitment, time, and effort. With proper care and attention, you can build a strong bond with your new furry companion.\n",
"Moderation guardrail flagged with a score of 3\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",
"\n"
Expand Down Expand Up @@ -447,13 +435,19 @@
"\n",
"We look forward to seeing how you take this forward, and how thinking on guardrails evolves as the ecosystem matures. "
]
},
{
"cell_type": "markdown",
"id": "5ed7b04d",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "openai_test",
"display_name": "Python 3",
"language": "python",
"name": "openai_test"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -465,7 +459,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.11.8"
}
},
"nbformat": 4,
Expand Down
10 changes: 5 additions & 5 deletions examples/How_to_use_moderation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"from openai import OpenAI\n",
"client = OpenAI()\n",
"\n",
"GPT_MODEL = 'gpt-3.5-turbo'"
"GPT_MODEL = 'gpt-4o-mini'"
]
},
{
Expand Down Expand Up @@ -339,7 +339,7 @@
"Custom moderation provides a tailored approach to content filtering, allowing for precise adherence to specific community standards or topics, making it an ideal choice for niche platforms or specialized content. Unlike the general categories covered by the moderation API, custom moderation offers a higher degree of control and requires more effort to set up and maintain. It shares the common goal of ensuring content safety with guardrails, focusing on setting criteria to filter and manage content, whether it's user input or output from language models. Custom moderation can be an alternative or an extension to the moderation API, that uses our completions endpoint. For those interested in exploring guardrails further, a cookbook with examples and guidance is available [here](https://cookbook.openai.com/examples/how_to_use_guardrails).\n",
"\n",
"#### Workflow\n",
"We will implement a small example to showcase custom moderation. We will use an extra call to gpt-4 to assess messages and decide if they should be moderated or not."
"We will implement a small example to showcase custom moderation. We will use an extra call to gpt-4o mini to assess messages and decide if they should be moderated or not."
]
},
{
Expand All @@ -349,17 +349,17 @@
"outputs": [],
"source": [
"def custom_moderation(content, parameters):\n",
" # Define the prompt for GPT-4\n",
" # Define the prompt \n",
" prompt = f\"\"\"Please assess the following content for any inappropriate material. You should base your assessment on the given parameters.\n",
" Your answer should be in json format with the following fields: \n",
" - flagged: a boolean indicating whether the content is flagged for any of the categories in the parameters\n",
" - reason: a string explaining the reason for the flag, if any\n",
" - parameters: a dictionary of the parameters used for the assessment and their values\n",
" Parameters: {parameters}\\n\\nContent:\\n{content}\\n\\nAssessment:\"\"\"\n",
" \n",
" # Call GPT-4 with the prompt\n",
" # Call model with the prompt\n",
" response = client.chat.completions.create(\n",
" model=\"gpt-4-turbo-preview\",\n",
" model=\"gpt-4o-mini\",\n",
" response_format={ \"type\": \"json_object\" },\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a content moderation assistant.\"},\n",
Expand Down
Loading