This is likely my lack of experience with python and jupyter but I’m not sure how to move forward.
I am running python 3.10
I have installed openai
My first cell imports openai and sets the API
import openai
open.api_key = "sk-keyhere"
The second cell defines get_completion, as copied from the course:
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"]
However, when I attempt to then run the prompt, I get an error referring back to this definition highlighting openai-ChatCompletion.create
AttributeError: module 'openai' has no attribute 'ChatCompletion'
So if I am understanding this correctly:
The prompt calls get_completion which refers to the openai module and then fails to find ChatCompletion. Now, I know it must be there, as it works on the course notebook without issue.
On principle, I generated a new API key and used that, in case there was something wrong with my copy. But it’s the same issue.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[7], line 18
1 text = f"""
2 You should express what you want a model to do by \
3 providing instructions that are as clear and \
(...)
11 more detailed and relevant outputs.
12 """
13 prompt = f"""
14 Summarize the text delimited by triple backticks \
15 into a single sentence.
16 ```{text}```
17 """
---> 18 response = get_completion(prompt)
19 print(response)
Cell In[6], line 3, in get_completion(prompt, model)
1 def get_completion(prompt, model="gpt-3.5-turbo"):
2 messages = [{"role": "user", "content": prompt}]
----> 3 response = openai.ChatCompletion.create(
4 model=model,
5 messages=messages,
6 temperature=0, # this is the degree of randomness of the model's output
7 )
8 return response.choices[0].message["content"]
AttributeError: module 'openai' has no attribute 'ChatCompletion'
OK intentionally entering the wrong API gets me the same error. I hadn’t thought that was the cause but I’m now going through the posts here that discuss entering the API key.
The end of the course says
You can either set it as the OPENAI_API_KEY environment variable before using the library:
!export OPENAI_API_KEY='sk-...'
Or, set openai.api_key to its value:
import openai
openai.api_key = "sk-..."
I know the import has worked, as if I remove that, I get an immediate error because it doesn’t know the module.
Now that I am looking at other threads, I can see people talking about setting the API in a .env file, which is what I presume the !export call is doing, but I can’t see where. And I am now wondering if my lack of knowledge of how to set up the environment (this is a new install of Jupyter) is causing me to miss something obvious to everyone else.
I am really sorry, but I don’t quite get this I’m not calling the _environ object and that’s not in the course.
I can’t make sense of why openai.api_key = "sk-..."
isn’t working for me and every thread I see on this seems to be using some other way to import the key where there’s clearly some additional steps needed (you must have to put the key somewhere)