# L7 Evaluation: Utils.py - A bit of prompt refinement needed in find\_category\_and\_product\_only function

**URL:** <https://community.deeplearning.ai/t/l7-evaluation-utils-py-a-bit-of-prompt-refinement-needed-in-find-category-and-product-only-function/349718>\
**Category:** Building Systems with the ChatGPT API\
**Created:** [June 3, 2023, 5:55pm UTC](https://community.deeplearning.ai/t/l7-evaluation-utils-py-a-bit-of-prompt-refinement-needed-in-find-category-and-product-only-function/349718 "2023-06-03T17:55:48Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajulian](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@ajulian](https://community.deeplearning.ai/u/ajulian)\
**Post date:** [June 3, 2023, 5:55pm UTC](https://community.deeplearning.ai/t/l7-evaluation-utils-py-a-bit-of-prompt-refinement-needed-in-find-category-and-product-only-function/349718/1 "2023-06-03T17:55:49Z")

</div>

**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.

---

<div class="post-metadata">

**Author:** ![mmibrahim](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/mmibrahim/32/13095_2.png) [@mmibrahim](https://community.deeplearning.ai/u/mmibrahim)\
**Post date:** [June 4, 2023, 5:16am UTC](https://community.deeplearning.ai/t/l7-evaluation-utils-py-a-bit-of-prompt-refinement-needed-in-find-category-and-product-only-function/349718/2 "2023-06-04T05:16:10Z")

</div>

Where can I find the code for utils?

---

<div class="post-metadata">

**Author:** ![ajulian](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@ajulian](https://community.deeplearning.ai/u/ajulian)\
**Post date:** [June 4, 2023, 6:44am UTC](https://community.deeplearning.ai/t/l7-evaluation-utils-py-a-bit-of-prompt-refinement-needed-in-find-category-and-product-only-function/349718/3 "2023-06-04T06:44:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hada.garcia](https://avatars.discourse-cdn.com/v4/letter/h/b5e925/32.png) [@hada.garcia](https://community.deeplearning.ai/u/hada.garcia)\
**Post date:** [June 15, 2023, 12:19pm UTC](https://community.deeplearning.ai/t/l7-evaluation-utils-py-a-bit-of-prompt-refinement-needed-in-find-category-and-product-only-function/349718/4 "2023-06-15T12:19:25Z")

</div>

Thanks for the tip! 🙂

---

<div class="post-metadata">

**Author:** ![franfurey](https://avatars.discourse-cdn.com/v4/letter/f/e0b2c6/32.png) [@franfurey](https://community.deeplearning.ai/u/franfurey)\
**Post date:** [June 21, 2023, 12:41am UTC](https://community.deeplearning.ai/t/l7-evaluation-utils-py-a-bit-of-prompt-refinement-needed-in-find-category-and-product-only-function/349718/5 "2023-06-21T00:41:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JustinHsu](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@JustinHsu](https://community.deeplearning.ai/u/JustinHsu)\
**Post date:** [June 26, 2023, 7:21am UTC](https://community.deeplearning.ai/t/l7-evaluation-utils-py-a-bit-of-prompt-refinement-needed-in-find-category-and-product-only-function/349718/6 "2023-06-26T07:21:08Z")

</div>

thanks @ajulian . very helpful for the tips! 🙂

---

<div class="post-metadata">

**Author:** ![anggoro\_yn](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@anggoro\_yn](https://community.deeplearning.ai/u/anggoro_yn)\
**Post date:** [June 27, 2023, 7:04am UTC](https://community.deeplearning.ai/t/l7-evaluation-utils-py-a-bit-of-prompt-refinement-needed-in-find-category-and-product-only-function/349718/7 "2023-06-27T07:04:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Kim\_Snyder](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/kim_snyder/32/42484_2.png) [@Kim\_Snyder](https://community.deeplearning.ai/u/Kim_Snyder)\
**Post date:** [June 29, 2023, 5:16pm UTC](https://community.deeplearning.ai/t/l7-evaluation-utils-py-a-bit-of-prompt-refinement-needed-in-find-category-and-product-only-function/349718/9 "2023-06-29T17:16:19Z")

</div>

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 (  
/Users/name/anaconda3/lib/python3.10/site-packages/utils/ **init**.py)

Thanks in advance for your help.

Kim

---

<div class="post-metadata">

**Author:** ![Kim\_Snyder](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/kim_snyder/32/42484_2.png) [@Kim\_Snyder](https://community.deeplearning.ai/u/Kim_Snyder)\
**Post date:** [June 29, 2023, 8:50pm UTC](https://community.deeplearning.ai/t/l7-evaluation-utils-py-a-bit-of-prompt-refinement-needed-in-find-category-and-product-only-function/349718/10 "2023-06-29T20:50:53Z")

</div>

Postscript…  
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.

---

<div class="post-metadata">

**Author:** ![Rouh\_Bow](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@Rouh\_Bow](https://community.deeplearning.ai/u/Rouh_Bow)\
**Post date:** [August 24, 2023, 8:25pm UTC](https://community.deeplearning.ai/t/l7-evaluation-utils-py-a-bit-of-prompt-refinement-needed-in-find-category-and-product-only-function/349718/11 "2023-08-24T20:25:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![CrozzDev](https://avatars.discourse-cdn.com/v4/letter/c/90db22/32.png) [@CrozzDev](https://community.deeplearning.ai/u/CrozzDev)\
**Post date:** [September 4, 2023, 12:51pm UTC](https://community.deeplearning.ai/t/l7-evaluation-utils-py-a-bit-of-prompt-refinement-needed-in-find-category-and-product-only-function/349718/12 "2023-09-04T12:51:52Z")

</div>

Good to know you solved the issue 🙂 . By the way, it is not recommended to have modules called “utils”, check this link for further information: [Stop naming your python modules “utils”](https://breadcrumbscollector.tech/stop-naming-your-python-modules-utils/)

Hope that helps,  
Crozz

---

<div class="post-metadata">

**Author:** ![deeharwood](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/deeharwood/32/66187_2.png) [@deeharwood](https://community.deeplearning.ai/u/deeharwood)\
**Post date:** [October 6, 2023, 1:11am UTC](https://community.deeplearning.ai/t/l7-evaluation-utils-py-a-bit-of-prompt-refinement-needed-in-find-category-and-product-only-function/349718/13 "2023-10-06T01:11:51Z")

</div>

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?

Thanks.

---

<div class="post-metadata">

**Author:** ![egutierrez](https://avatars.discourse-cdn.com/v4/letter/e/f6c823/32.png) [@egutierrez](https://community.deeplearning.ai/u/egutierrez)\
**Post date:** [March 1, 2024, 8:31pm UTC](https://community.deeplearning.ai/t/l7-evaluation-utils-py-a-bit-of-prompt-refinement-needed-in-find-category-and-product-only-function/349718/14 "2024-03-01T20:31:04Z")

</div>

> [@ajulian](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![egutierrez](https://avatars.discourse-cdn.com/v4/letter/e/f6c823/32.png) [@egutierrez](https://community.deeplearning.ai/u/egutierrez)\
**Post date:** [March 3, 2024, 8:51pm UTC](https://community.deeplearning.ai/t/l7-evaluation-utils-py-a-bit-of-prompt-refinement-needed-in-find-category-and-product-only-function/349718/15 "2024-03-03T20:51:06Z")

</div>

> [@ajulian](#):
>
> **[{‘category’: ‘Smartphones and Accessories’, ‘products’: [‘SmartX ProPhone’]},**

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”**

```auto
[
   {‘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”**

```auto
[
   {'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\>,  
> OR  
> ‘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.
