How to get helper functions on my local system mentioned in AI Python for Beginners course

Hey guys!

I’m trying to use helper functions in python for building LLM prompts with variables in the first course but Im unable to do so.
I found this link How to have helper_functions in python and did find the helper.py file, however I couldn’t get much clarity on how to use them. Can someone provide their valuable inputs if they’ve done it before successfully on their local computer?

TIA

Hi preetika, welcome to the community!

First, you need to create your notebook in a folder. You’ll need to put the helper_functions.py file in the same folder as your notebook. Throughout the courses, you have used functions you imported from helper_functions. It turns out that these were stored in this file and you need to put this helper_functions.py file in the same directory as your notebook. There are several ways to import functions from a file. In your notebook, run the following code to import all the functions in the file helper_functions.py:

import helper_functions

Or you can import only the print_llm_response function from the helper_functions.py file:

from helper_functions import print_llm_response

Hope this helps!

Hi @nadtriana, thanks for your response!

I did download the helper_functions.py file from and pasted it in the same directory as my notebook. Since I am using jupyter notebook for running the scripts on my local PC, I see the following on my local host:

In addition to the above, when I run the following :

import helper_functions

The following error message pops up!

slight_smile:


could you help me resolve this? how can I get the openai module?

Hi TIA,

Try to put helper_function in the folder where you initiate the Jupyter notebook.
Hope it works.

Note that you are using the openai package for the first time! The OpenAI function here allows you to connect to the chatbot in Python. Read the OpenAI documentation if you want to learn more!

Note: If you want to install this on your computer, you would run !pip install openai:

!pip install openai

And the load_dotenv function is used to load environment variables from a .env file into your program. This can be very useful to manage configuration settings like API keys or database connections without having to hardcode them into your script. If you want to install this on your computer:

  1. Install the python-dotenv package:

    !pip install python-dotenv

  2. In the root directory of your project, create a new file named .env. The filename should be exactly .env with no prefix.

  3. Open the .env file in a text editor and add the following line with your OpenAI API key

    OPENAI_API_KEY=‘put-here-your-api-key’

To test it, you can try the example used in the lesson:

from helper_functions import print_llm_response 

print_llm_response("What is the capital of France?")

You have to install the openio package (i.e have it available locally) before you can import it from a program.

Install the packages in your Jupyter notebook:

!pip install openai
!pip install python-dotenv

Create a new file named .env in the root directory of your project. The filename should be exactly .env without any prefix.

Open the .env file in a text editor and add the following line with your OpenAI API key

OPENAI_API_KEY=‘put-here-your-api-key’

Save the .env file. You can try the example used in the lesson in your notebook:

from helper_functions import print_llm_response

print_llm_response(“What is the capital of France?”)

Thanks @nadtriana . I’ve implemented and installed all the necessary files but just saw that I’m getting error code: 429 i.e API quota exceeded. Since this is my first time using the API for my code, I’m not sure how the quota got exceeded.

image

I tried creating a new account to create a new key and see the quota but it seems that chatgpt does not provide any quota to use their API for a new user as well. It wasn’t mentioned anywhere in the course that we need to have a paid chatgpt account to be able to call their APIs.

Unfortunately, if you want to use it on your computer, you need to use your API key, and OpenAI’s API access model usually requires a paid account.

Hello, following the discussion here, I need some clarification. I downloaded the helper_functions.py file from the python AI for Beginners course and put it in my jupyter notebook folder on my local pc.
If I use the instruction in a notebook

from helper_functions import print_llm_response

I get this error:

ImportError Traceback (most recent call last)
Cell In[1], line 1
----> 1 from helper_functions import print_llm_response

File ~\Documents\Python_AI_for_Beginners\Lab\Jupyter_Notebook\helper_functions.py:1
----> 1 from openai import AzureOpenAI, DefaultHttpxClient
3 client = AzureOpenAI(
4 api_key=“abcdefg”,
5 api_version=“2024-02-01”,
6 azure_endpoint = “https://cour-external-playground.openai.azure.com/”,
7 http_client=DefaultHttpxClient(verify=False)
8 )
11 # ### If you want to use your own OpenAI key, uncomment these cells below and comment out the other get_llm_response function cells:
12
13 # from openai import OpenAI
(…)
42 # except TypeError as e:
43 # print(“Error:”, str(e))

ImportError: cannot import name ‘DefaultHttpxClient’ from ‘openai’ (C:\Users\Mahan\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai_init_.py)

Following the discussion (especially your comment nadtriana) I wonder whether I can use helper_functions.sy at all or whether I have to change it using an own API key? But then how? How would I need to adjust helper_functions.sy?

This error is being thrown not because of helper_function.py file.

but because you are running the codes locally the API keys used by the client won’t work and you need to use your own api keys.

Also read the below instructions

so first make sure you use your own api keys and then uncomment the cells where own openai key can be used and also comment out the get_llm_response function cells which is using client api keys.

@Jochen_Keilitz

2 Likes

Thank you very much for the fast response. I understand. I have to modify helper_functions.py using the client definition (that currently is commented)

from openai import OpenAI
openai_api_key = “my API key here”
client = OpenAI(api_key=openai_api_key)

also exchanging

def get_llm_response(prompt): …

using my own api keys. Hope it will work. Thank you very much.

1 Like

Hello Deepti Prasad,

thank you very much for your clarification. Now I understand. I also left a note under the topic.

It is really great to get so much support.

Have a good day

Jochen

| Deepti_Prasad Mentor/Moderator/Tester
December 6 |

  • | - |

This error is being thrown not because of helper_function.py file.

but because you are running the codes locally the API keys used by the client won’t work and you need to use your own api keys.

Also read the below instructions

Jochen_Keilitz:

11 # ### If you want to use your own OpenAI key, uncomment these cells below and comment out the other get_llm_response function cells:

so first make sure you use your own api keys and then uncomment the cells where own openai key can be used and also comment out the get_llm_response function cells which is using client api keys.

@Jochen_Keilitz

great work @Jochen_Keilitz

just a request from next time create new topic even if you find similar topic to your issue.

you can always pinned the post link in your created post and tag any mentor who you want response from.

This approach is advised to avoid any confusion for future learners and also so that your query don’t get mixed with post creator query.

Keep Learning!!!

1 Like