L7 - Confusing with notebook content and Video content

As the title says, right now 02/03/2024 if you run the code in L7:

def process_user_message(user_input, all_messages, debug=True):
    delimiter = "```"
    
    # Step 1: Check input to see if it flags the Moderation API or is a prompt injection
    response = openai.Moderation.create(input=user_input)
    moderation_output = response["results"][0]

    if moderation_output["flagged"]:
        print("Step 1: Input flagged by Moderation API.")
        return "Sorry, we cannot process this request."

    if debug: print("Step 1: Input passed moderation check.")
    
    category_and_product_response = utils.find_category_and_product_only(user_input, utils.get_products_and_category())
    #print(print(category_and_product_response)
    
    # Step 2: Extract the list of products
    category_and_product_list = utils.read_string_to_list(category_and_product_response)
    #print(category_and_product_list)

    if debug: print("Step 2: Extracted list of products.")

    # Step 3: If products are found, look them up
    product_information = utils.generate_output_string(category_and_product_list)
    if debug: print("Step 3: Looked up product information.")

    # Step 4: Answer the user question
    system_message = f"""
    You are a customer service assistant for a large electronic store. \
    Respond in a friendly and helpful tone, with concise answers. \
    Make sure to ask the user relevant follow-up questions.
    """
    messages = [
        {'role': 'system', 'content': system_message},
        {'role': 'user', 'content': f"{delimiter}{user_input}{delimiter}"},
        {'role': 'assistant', 'content': f"Relevant product information:\n{product_information}"}
    ]

    final_response = get_completion_from_messages(all_messages + messages)
    if debug:print("Step 4: Generated response to user question.")
    all_messages = all_messages + messages[1:]

    # Step 5: Put the answer through the Moderation API
    response = openai.Moderation.create(input=final_response)
    moderation_output = response["results"][0]

    if moderation_output["flagged"]:
        if debug: print("Step 5: Response flagged by Moderation API.")
        return "Sorry, we cannot provide this information."

    if debug: print("Step 5: Response passed moderation check.")

    # Step 6: Ask the model if the response answers the initial user query well
    user_message = f"""
    Customer message: {delimiter}{user_input}{delimiter}
    Agent response: {delimiter}{final_response}{delimiter}

    Does the response sufficiently answer the question?
    """
    messages = [
        {'role': 'system', 'content': system_message},
        {'role': 'user', 'content': user_message}
    ]
    evaluation_response = get_completion_from_messages(messages)
    if debug: print("Step 6: Model evaluated the response.")

    # Step 7: If yes, use this answer; if not, say that you will connect the user to a human
    if "Y" in evaluation_response:  # Using "in" instead of "==" to be safer for model output variation (e.g., "Y." or "Yes")
        if debug: print("Step 7: Model approved the response.")
        return final_response, all_messages
    else:
        if debug: print("Step 7: Model disapproved the response.")
        neg_str = "I'm unable to provide the information you're looking for. I'll connect you with a human representative for further assistance."
        return neg_str, all_messages

user_input = "tell me about the smartx pro phone and the fotosnap camera, the dslr one. Also what tell me about your tvs"
response,_ = process_user_message(user_input,[])
print(response)

You get an error, as invalid JSON String, but somehow the system continues working and giving responses.

Step 1: Input passed moderation check.
Error: Invalid JSON string
Step 2: Extracted list of products.
Step 3: Looked up product information.
Step 4: Generated response to user question.
Step 5: Response passed moderation check.
Step 6: Model evaluated the response.
Step 7: Model approved the response.
The SmartX Pro phone is a high-performance smartphone with a powerful processor, advanced camera features, and a sleek design. It offers a smooth user experience and great connectivity options.

The FotoSnap camera is a DSLR camera known for its professional-grade image quality, manual controls, and interchangeable lenses. It's perfect for photography enthusiasts and professionals looking to capture stunning photos.

As for our TVs, we have a wide range of options available, including LED, OLED, and QLED models in various sizes. Our TVs offer vibrant colors, sharp image quality, and smart features for streaming content. 

Do you have any specific questions about the SmartX Pro phone, FotoSnap camera, or our TVs? Are you looking for any particular features or specifications in these products?

In the other hand in the video the code is ( i write it here, just in case anybody else want to use it):

def process_user_message_me(user_input, all_messages=[], debug=True):
    delimiter = "####"
    
    # Step 1: Check input to see if it flags the Moderation API or is a prompt injection
    step_1_moderation_response = openai.Moderation.create(input=user_input)
    step_1_moderation_output = step_1_moderation_response["results"][0]

    print(step_1_moderation_output)
    
    if step_1_moderation_output["flagged"]:
        print("Step 1: Input flagged by Moderation API.")
        return ("Sorry, we cannot process this request.", all_messages) #Optional to add all_messages

    if debug: print("Step 1: Input passed moderation check.")
    
   

    # Step 2: Extract the list of products
    step_2_system_message = utils.step_2_system_message
    step_2_messages =[step_2_system_message] + all_messages
    step_2_messages = step_2_messages + [    
        {'role':'user', 'content': f"{delimiter}{user_input}{delimiter}"},  
    ] 
    step_2_response = get_completion_from_messages(step_2_messages)
    step_2_category_and_product_list = utils.read_string_to_list(step_2_response)

    print(step_2_category_and_product_list)
    
    if debug: print("Step 2: Extracted list of products.")

    # Step 3: If products are found, look them up
    step_3_product_information = utils.generate_output_string(step_2_category_and_product_list)
    print(step_3_product_information)
    
    if debug: print("Step 3: Looked up product information.")

    # Step 4: Answer the user question
    step_4_system_message = utils.step_4_system_message
    step_4_messages =[step_4_system_message] #No le suma all_messages ????
    step_4_messages += all_messages #step_4_messages = [step_4_messages] + all_messages ???
    step_4_user_message_and_product_info = [    
        {'role':'user', 'content': f"{user_input}"},  
        {'role':'assistant', 'content': f"Relevant product information:\n"}  #Aqui duda product_info}
    ] 
    step_4_messages += step_4_user_message_and_product_info
    all_messages += step_4_user_message_and_product_info
    step_4_response = get_completion_from_messages(step_4_messages)

    if debug: print("Step 4: Generated response.")

    print(step_4_response)
    
    # Step 5: Put the answer through moderation API
    step_5_moderation_response = openai.Moderation.create(input=step_4_response)
    step_5_moderation_output = step_5_moderation_response["results"][0]
    print(step_5_moderation_output)
    
    if step_5_moderation_output["flagged"]:
        print("Step 5: Response flagged by Moderation API.")
        return ("Sorry, we cannot provide this information.Let me connect to a human", all_messages) #Optional to add all_messages
    
    if debug: print("Step 5: Response passed moderation check.")
    
    return (step_4_response,all_messages)

    
user_input = "tell me about the smartx pro phone and the fotosnap camera, the dslr one. Also what tell me about your tvs"
response,conversation = process_user_message_me(user_input)

print("\n Response from model:")
print(response)
print("\n Conversation:")
print(conversation)

There are some extra prints just to show and follow the execution…
and the response is:

{
  "categories": {
    "harassment": false,
    "harassment/threatening": false,
    "hate": false,
    "hate/threatening": false,
    "self-harm": false,
    "self-harm/instructions": false,
    "self-harm/intent": false,
    "sexual": false,
    "sexual/minors": false,
    "violence": false,
    "violence/graphic": false
  },
  "category_scores": {
    "harassment": 1.1170866855536588e-05,
    "harassment/threatening": 1.4300552493295982e-06,
    "hate": 6.180536047395435e-07,
    "hate/threatening": 8.955092312135093e-08,
    "self-harm": 8.116802746371832e-06,
    "self-harm/instructions": 7.912258297437802e-06,
    "self-harm/intent": 6.722237594658509e-05,
    "sexual": 9.00048416951904e-06,
    "sexual/minors": 6.928541552042589e-06,
    "violence": 6.768796447431669e-05,
    "violence/graphic": 1.6808195368867018e-06
  },
  "flagged": false
}
Step 1: Input passed moderation check.
[{'category': 'Smartphones and Accessories'}, {'category': 'Cameras and Camcorders'}, {'category': 'Televisions and Home Theater Systems'}]
Step 2: Extracted list of products.
{
    "name": "SmartX ProPhone",
    "category": "Smartphones and Accessories",
    "brand": "SmartX",
    "model_number": "SX-PP10",
    "warranty": "1 year",
    "rating": 4.6,
    "features": [
        "6.1-inch display",
        "128GB storage",
        "12MP dual camera",
        "5G"
    ],
    "description": "A powerful smartphone with advanced camera features.",
    "price": 899.99
}
{
    "name": "MobiTech PowerCase",
    "category": "Smartphones and Accessories",
    "brand": "MobiTech",
    "model_number": "MT-PC20",
    "warranty": "1 year",
    "rating": 4.3,
    "features": [
        "5000mAh battery",
        "Wireless charging",
        "Compatible with SmartX ProPhone"
    ],
    "description": "A protective case with built-in battery for extended usage.",
    "price": 59.99
}
{
    "name": "SmartX MiniPhone",
    "category": "Smartphones and Accessories",
    "brand": "SmartX",
    "model_number": "SX-MP5",
    "warranty": "1 year",
    "rating": 4.2,
    "features": [
        "4.7-inch display",
        "64GB storage",
        "8MP camera",
        "4G"
    ],
    "description": "A compact and affordable smartphone for basic tasks.",
    "price": 399.99
}
{
    "name": "MobiTech Wireless Charger",
    "category": "Smartphones and Accessories",
    "brand": "MobiTech",
    "model_number": "MT-WC10",
    "warranty": "1 year",
    "rating": 4.5,
    "features": [
        "10W fast charging",
        "Qi-compatible",
        "LED indicator",
        "Compact design"
    ],
    "description": "A convenient wireless charger for a clutter-free workspace.",
    "price": 29.99
}
{
    "name": "SmartX EarBuds",
    "category": "Smartphones and Accessories",
    "brand": "SmartX",
    "model_number": "SX-EB20",
    "warranty": "1 year",
    "rating": 4.4,
    "features": [
        "True wireless",
        "Bluetooth 5.0",
        "Touch controls",
        "24-hour battery life"
    ],
    "description": "Experience true wireless freedom with these comfortable earbuds.",
    "price": 99.99
}
{
    "name": "FotoSnap DSLR Camera",
    "category": "Cameras and Camcorders",
    "brand": "FotoSnap",
    "model_number": "FS-DSLR200",
    "warranty": "1 year",
    "rating": 4.7,
    "features": [
        "24.2MP sensor",
        "1080p video",
        "3-inch LCD",
        "Interchangeable lenses"
    ],
    "description": "Capture stunning photos and videos with this versatile DSLR camera.",
    "price": 599.99
}
{
    "name": "ActionCam 4K",
    "category": "Cameras and Camcorders",
    "brand": "ActionCam",
    "model_number": "AC-4K",
    "warranty": "1 year",
    "rating": 4.4,
    "features": [
        "4K video",
        "Waterproof",
        "Image stabilization",
        "Wi-Fi"
    ],
    "description": "Record your adventures with this rugged and compact 4K action camera.",
    "price": 299.99
}
{
    "name": "FotoSnap Mirrorless Camera",
    "category": "Cameras and Camcorders",
    "brand": "FotoSnap",
    "model_number": "FS-ML100",
    "warranty": "1 year",
    "rating": 4.6,
    "features": [
        "20.1MP sensor",
        "4K video",
        "3-inch touchscreen",
        "Interchangeable lenses"
    ],
    "description": "A compact and lightweight mirrorless camera with advanced features.",
    "price": 799.99
}
{
    "name": "ZoomMaster Camcorder",
    "category": "Cameras and Camcorders",
    "brand": "ZoomMaster",
    "model_number": "ZM-CM50",
    "warranty": "1 year",
    "rating": 4.3,
    "features": [
        "1080p video",
        "30x optical zoom",
        "3-inch LCD",
        "Image stabilization"
    ],
    "description": "Capture life's moments with this easy-to-use camcorder.",
    "price": 249.99
}
{
    "name": "FotoSnap Instant Camera",
    "category": "Cameras and Camcorders",
    "brand": "FotoSnap",
    "model_number": "FS-IC10",
    "warranty": "1 year",
    "rating": 4.1,
    "features": [
        "Instant prints",
        "Built-in flash",
        "Selfie mirror",
        "Battery-powered"
    ],
    "description": "Create instant memories with this fun and portable instant camera.",
    "price": 69.99
}
{
    "name": "CineView 4K TV",
    "category": "Televisions and Home Theater Systems",
    "brand": "CineView",
    "model_number": "CV-4K55",
    "warranty": "2 years",
    "rating": 4.8,
    "features": [
        "55-inch display",
        "4K resolution",
        "HDR",
        "Smart TV"
    ],
    "description": "A stunning 4K TV with vibrant colors and smart features.",
    "price": 599.99
}
{
    "name": "SoundMax Home Theater",
    "category": "Televisions and Home Theater Systems",
    "brand": "SoundMax",
    "model_number": "SM-HT100",
    "warranty": "1 year",
    "rating": 4.4,
    "features": [
        "5.1 channel",
        "1000W output",
        "Wireless subwoofer",
        "Bluetooth"
    ],
    "description": "A powerful home theater system for an immersive audio experience.",
    "price": 399.99
}
{
    "name": "CineView 8K TV",
    "category": "Televisions and Home Theater Systems",
    "brand": "CineView",
    "model_number": "CV-8K65",
    "warranty": "2 years",
    "rating": 4.9,
    "features": [
        "65-inch display",
        "8K resolution",
        "HDR",
        "Smart TV"
    ],
    "description": "Experience the future of television with this stunning 8K TV.",
    "price": 2999.99
}
{
    "name": "SoundMax Soundbar",
    "category": "Televisions and Home Theater Systems",
    "brand": "SoundMax",
    "model_number": "SM-SB50",
    "warranty": "1 year",
    "rating": 4.3,
    "features": [
        "2.1 channel",
        "300W output",
        "Wireless subwoofer",
        "Bluetooth"
    ],
    "description": "Upgrade your TV's audio with this sleek and powerful soundbar.",
    "price": 199.99
}
{
    "name": "CineView OLED TV",
    "category": "Televisions and Home Theater Systems",
    "brand": "CineView",
    "model_number": "CV-OLED55",
    "warranty": "2 years",
    "rating": 4.7,
    "features": [
        "55-inch display",
        "4K resolution",
        "HDR",
        "Smart TV"
    ],
    "description": "Experience true blacks and vibrant colors with this OLED TV.",
    "price": 1499.99
}

Step 3: Looked up product information.
Step 4: Generated response.
- The SmartX Pro phone has a 6.5-inch display, 128GB storage, and a quad-camera setup.
- The FotoSnap DSLR camera features a 24MP sensor, 4K video recording, and interchangeable lenses.
- Our TVs range from 32-inch to 75-inch sizes, with 4K resolution and smart TV capabilities. 

Do you have any specific features or specifications you are looking for in these products?
{
  "categories": {
    "harassment": false,
    "harassment/threatening": false,
    "hate": false,
    "hate/threatening": false,
    "self-harm": false,
    "self-harm/instructions": false,
    "self-harm/intent": false,
    "sexual": false,
    "sexual/minors": false,
    "violence": false,
    "violence/graphic": false
  },
  "category_scores": {
    "harassment": 2.464333874740987e-06,
    "harassment/threatening": 2.9150553473300533e-06,
    "hate": 5.766873982793186e-07,
    "hate/threatening": 1.9783667084993795e-05,
    "self-harm": 5.331053216650616e-06,
    "self-harm/instructions": 2.0936129203619203e-06,
    "self-harm/intent": 3.967625889345072e-05,
    "sexual": 0.0002091660862788558,
    "sexual/minors": 3.833304435829632e-05,
    "violence": 0.0005284292856231332,
    "violence/graphic": 1.3479902008839417e-05
  },
  "flagged": false
}
Step 5: Response passed moderation check.

 Response from model:
- The SmartX Pro phone has a 6.5-inch display, 128GB storage, and a quad-camera setup.
- The FotoSnap DSLR camera features a 24MP sensor, 4K video recording, and interchangeable lenses.
- Our TVs range from 32-inch to 75-inch sizes, with 4K resolution and smart TV capabilities. 

Do you have any specific features or specifications you are looking for in these products?

 Conversation:
[{'role': 'user', 'content': 'tell me about the smartx pro phone and the fotosnap camera, the dslr one. Also what tell me about your tvs'}, {'role': 'assistant', 'content': 'Relevant product information:\n'}]

That seems that work ok

My question is that in both functions you use all_messages but i dont see what is for, is empty and it apparently is useless…doesnt do anything…

I assume the second one is newer and cleaner, only needs 5 steps…
Also i want to remember and point out that its not a good practice to name utils to your own modules: Stop naming your python modules “utils”

Agree with this.

By the way, I got the code to output the correct JSON so that the Invalid JSOn string error doesn’t show up.
In the utils.py file they gave us, the error is coming from the function: find_category_and_product_only.
So, I went into that function and added this at the end: “Remember that the format of a Python list starts with [ and end with ].”

In other words, the final line in the system_message variable became:
“Only output the list of objects, nothing else. Remember that the format of a Python list starts with [ and end with ].”

This ensured the output was in the correct Python format and removed the error.

1 Like

@mubsi, maybe a fix is needed to this?

Thanks, @TMosh. This has been noted.

2 Likes