Gudelines Section - ChatGPT Prompt Engineering for Developers

Hi Community,
I’m trying to run command in colab research google:

Blockquote
text = f"“”
You should express what you want a model to do by
providing instructions that are as clear and
specific as you can possibly make them.
This will guide the model towards the desired output,
and reduce the chances of receiving irrelevant
or incorrect responses. Don’t confuse writing a
clear prompt with writing a short prompt.
In many cases, longer prompts provide more clarity
and context for the model, which can lead to
more detailed and relevant outputs.
“”"
prompt = f"“”
Summarize the text delimited by triple backticks
into a single sentence.
{text}
“”"
response = get_completion(prompt)
print(response)

but get error says the following:

As recommended by instructor I didnt configure private API key and implemented before the command:

import openai
import os
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local dotenv file
openai.api_key = os.getenv(‘OPENAI_API_KEY’) # os.getenv(‘OPENAI_API_KEY’)

could you advise me how I can solve the error?

Hello, @vladizero8.

Are you setting your API KEY in the dotenv file?

Hi @juanjonavarro
Yes, I run this one before:

!pip install python-dotenv

I mean, this module reads environment variables from a file named “.env” in your system.

Are you creating this .env file? Are you setting your OpenAI API_KEY in this file?

1 Like

no, I have not created any .env file
You mean, I need to generate API key as described here OpenAI API and put it in .env file? If it is, what directory should I place .env file then?

I have not tried, but I think it should be in the same directory as the notebook.

1 Like

The simplest fix is just stick your key into your code. they don’t do it in the video so people can’t copy their private key. No .env, no import os, no install dotenv…

The proposed solution by @ai_curious is:

openai.api_key = <paste_here_your_api_key>

Now, if you run this in the notebooks provided by the course, you don’t have to do anything regarding api_key… just run the cells.

3 Likes

+1 @Juan_Olano but just to be completely clear for the newer programmers,we treat it as a Python string literal. So use the single or double quotes, not angle brackets <…>

openai.api_key = “paste_here_your_api_key”

as is demonstrated in the Guidelines for Prompting video just at the 2:00 mark

1 Like

thank you @ai_curious and @Juan_Olano
I started to use built-in notebooks and everything works correctly, but at the same time I tried to use 'openai.api_key = “sk-…” ’ in colab google for curiosity and does not work :frowning:

@vladizero8, it’s very strange. I have just created a new notebook in Collab with only one cell:

!pip install openai
import openai

openai.api_key  = '<put-here-your-api-key>'

def get_completion(prompt, model="gpt-3.5-turbo"):
    messages = [{"role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0, # this is the degree of randomness of the model's output
    )
    return response.choices[0].message["content"]

text = f"""
You should express what you want a model to do by \ 
providing instructions that are as clear and \ 
specific as you can possibly make them. \ 
This will guide the model towards the desired output, \ 
and reduce the chances of receiving irrelevant \ 
or incorrect responses. Don't confuse writing a \ 
clear prompt with writing a short prompt. \ 
In many cases, longer prompts provide more clarity \ 
and context for the model, which can lead to \ 
more detailed and relevant outputs.
"""
prompt = f"""
Summarize the text delimited by triple backticks \ 
into a single sentence.
```{text}```
"""
response = get_completion(prompt)
print(response)

And it works (of course I need to put my API key).

2 Likes

What error are you getting?

thanks @juanjonavarro
I tried to put my api key and after a few iteration it worked. Unfortunately, I did it manually - copy and paste does not work in colab

solved @Juan_Olano thanks for help

Either use the course Jupyter notebook (you do need your own open API key). else use your open API key if you are using your own jupyter notebook. Do not mix and match and I reccomend the use of Course Jupyter notebook

Hi! can you help me please?

i’m usign jupyter and i’m stuck on the first step. I install correctly the !pip install openai, but qhe i try to use the command “Import openai” it gives me this error:


ModuleNotFoundError Traceback (most recent call last)
in
----> 1 import openai

ModuleNotFoundError: No module named ‘openai’

Hello.

Can you put here the output of your first command?

Are you using your own Jupyter installation or using somthing like Collab? Do you know how Jupyter notebooks works?

You need to execute the first “cell” (!pip install) previously to the second one (import openai) and in the same session.

Did you do the pip install of openai first? If not, you need to do that first (it’s a one time thing - once you do it you won’t have to do it every time). You can run the command on the first line of the Jupyter notebook as the instructor shows, or you can do it on the mac terminal (not sure how you do it if you use Windows)

Have you been able to solve this? I am this same problem too?

Hi All,
As stated in the official OpenAI article:

To explore and experiment with the API, all new users get $5 worth of free tokens. These tokens expire after 3 months.
After the quota has passed you can choose to enter billing information to upgrade to a paid plan and continue your use of the API on pay-as-you-go basis. If no billing information is entered you will still have login access, but will be unable to make any further API requests.
Please see the pricing page for the latest information on pay-as-you-go pricing.

Check your API usage in the usage dashboard.