L7 Evaluation: Utils.py - A bit of prompt refinement needed in find_category_and_product_only function

Update: I’ve realized that the L7 jupyter displayed in the video is newer and it doesn’t seem to call this function find_category_and_product_only.

When “user_input” mentions two products, the output of “find_category_and_product_only” is correct: two dictionaries, one per category/product pair. Example:
user_input = “tell me about the smartx pro phone and the fotosnap camera, the dslr one”
“find_category_and_product_only” returns
[{‘category’: ‘Smartphones and Accessories’, ‘products’: [‘SmartX ProPhone’]},
{‘category’: ‘Cameras and Camcorders’, ‘products’: [‘FotoSnap DSLR Camera’]}]

But if only one product is mentioned, the output of “find_category_and_product_only” is wrong: two dictionaries, one for the category and one for the product. Example:
user_input = “tell me about the smartx pro phone”
[{‘category’: ‘Smartphones and Accessories’}, {‘products’: [‘SmartX ProPhone’]}]
(the right answer is:
[{‘category’: ‘Smartphones and Accessories’, ‘products’: [‘SmartX ProPhone’]}]

Consequently, in the following step the model is queried with the descriptions of ALL the products in the category, rather than ONE product, with the corresponding waste of tokens.

I have tried refining the system prompt, but no luck till now.

1 Like

Where can I find the code for utils?

1 Like

In L7, L8 or L9: click on File in the jupyter menu, then click Open.
You will see the list of files in the lesson, at list the notebook and utils.py.
Check on the left of the file and some buttons will appear on the top of the jupyter menu, one of them to Download.


Thanks for the tip! :slight_smile:

1 Like

Thanks a lot, i was searching in the documentation a lot haha.

1 Like

thanks @ajulian . very helpful for the tips! :slight_smile:

1 Like

I try to follow along the code. I have also downloaded utils.py.

But the last cell where we chat with the bot, I got got no interaction. After I pressed the button, I don’t get any response.

Do we need to change the code?

1 Like

I am also stuck in the same place in this L7 “Evaluation” lesson – and I think I am almost at a solution, but a bit tripped up on where to put the utils and json docs.

To note: I setup a Jupyter notebook with Anaconda and have been going through the exercises using that. I am able to run this through the course notebook, but wanted to do it in my own.

This is what I’ve done so far…After going around in circles to import initially to import a utils module, I thought I was able to do this correctly – or at least I was able to run “import utils” and install what seemed to be a standard utils module for python. This got me mostly through L7 because it could at least now run the import

The process stumbles on this line that is right at the end of Step 1, not being able to find: utils.find_category_and_product_only

I downloaded the utils.py file from the course itself (hadn’t even thought to do that before reading this thread - thank you!). I have been able to download it and I thought I had placed it in the same location that the former utils.py had been located, but apparently note. Now I get this message which seems to point to issues in the file (?)

FileNotFoundError Traceback (most recent call last)
Cell In[42], line 76
73 return neg_str, all_messages
75 user_input = “tell me about the smartx pro phone and the fotosnap camera, the dslr one. Also what tell me about your tvs”
—> 76 response,_ = process_user_message(user_input,)
77 print(response)

Cell In[42], line 14, in process_user_message(user_input, all_messages, debug)
10 return “Sorry, we cannot process this request.”
12 if debug: print(“Step 1: Input passed moderation check.”)
—> 14 category_and_product_response = utils.find_category_and_product_only(user_input, utils.get_products_and_category())
15 #print(print(category_and_product_response)
16 # Step 2: Extract the list of products
17 category_and_product_list = utils.read_string_to_list(category_and_product_response)

File ~/utils.py:171, in get_products_and_category()
167 def get_products_and_category():
168 “”"
169 Used in L5
170 “”"
→ 171 products = get_products()
172 products_by_category = defaultdict(list)
173 for product_name, product_info in products.items():

File ~/utils.py:181, in get_products()
180 def get_products():
→ 181 with open(products_file, ‘r’) as file:
182 products = json.load(file)
183 return products

FileNotFoundError: [Errno 2] No such file or directory: 'products.json

I am aware there’s a json file - where does that need to get copied to? And I thought that I had copied the utils.py file to the right location (

Thanks in advance for your help.


1 Like

I did figure it out … it’s not so fussy about where the json file goes, or the util. They can both go right into the same location where the Jupyter notebooks files are kept.

1 Like

Thank you for the fix.
I download the utils.py in the same directory of the notebook and changed the name to utils_1.py. There are too many utils in my system. The name change forced the notebook link to the utils_1.py. All the utils in the notebook cells has to be changed to utils_1 accordingly.
I also removed “sys.path.append(‘…/…’)” in the original script. I think the purpose of it was to add the path to the directory of the utils.py.

1 Like

Good to know you solved the issue :slight_smile: . By the way, it is not recommended to have modules called “utils”, check this link for further information: Stop naming your python modules “utils”

Hope that helps,


Hi. I’m a newbie and taking the short courses. I have a local set up and trying to run any of the programs for chatgpt. I’m running into the error on utils. Where do I get the utils?



You have the answer in other post…some time late…but perhaps it helps…

1 Like

Im newbie here but i dont agree, if you read carefully the prompt:

So response should be in your first example: “tell me about the smartx pro phone and the fotosnap camera, the dslr one”

   {‘products’: ‘SmartX ProPhone’},
   {'products’: ‘FotoSnap DSLR Camera’}

In the example of the lesson and video; “tell me about the smartx pro phone and the fotosnap camera, the dslr one. Also what tell me about your tvs”

   {'products': 'SmartX ProPhone'},
   {'products': 'FotoSnap DSLR Camera'},
   {'category': 'Televisions and Home Theater Systems'}

As my understanding:

Output a python list of objects, where each object has the following format:
‘category’: <one of Computers and
Laptops, Smartphones and Accessories, Televisions and Home
Theater Systems, Gaming Consoles and Accessories, Audio
Equipment, Cameras and Camcorders>,
‘products’: <a list of products that must be found in the allowed products below

Where the categories and products must be found in the customer service query

Should then output product or category, not both…

Also have you tried to run the code different times ? I dont know why by different executions give me slighty different results.

1 Like