Skip to content

Forum in maintenance, we will back soon πŸ™‚

Connect Python with...
 
Notifications
Clear all

Connect Python with OpenAI API install error

23 Posts
4 Users
6 Reactions
86 Views
(@ionut-antal)
Posts: 21
Eminent Member Customer
Topic starter
 

Please, I need help, I don't know how to make it work.

I do have a problem connecting Python to OpenAI API.Β 

I did:

Open the Terminal, and install OpenAI Package by running the command:

pip install openai
Β 
Then I wrote the same code like Hasan did:
Β 
import openai

# I did replaced it with my key from OpenAI API
openai.api_key="my key from OpenAI API"

def generate_text_with_openai(user_prompt):
Β  Β  Β completions=openai.ChatCompletion.create(
Β  Β  Β  Β  Β  Β  model="gpt-3.5-turbo", Β # you can replace this with your preferred model
Β  Β  Β  Β  Β  Β  messages=[{"role": "user", "content": user_prompt}],
Β  Β  Β )
Β  Β  Β return completion.choices[0].message.content

prompt="Generate a sentence about polar bear"

response=generate_text_with_openai(prompt)

print(response)
Β 
After I run the code the answer is :
Β 
PS C:\Users\10_Pro> & C:/Users/10_Pro/AppData/Local/Programs/Python/Python312/python.exe c:/Users/10_Pro/chatgpt.py
File "c:\Users\10_Pro\chatgpt.py", line 11
return completion.choices[0].message.content
IndentationError: unexpected indent
PS C:\Users\10_Pro> & C:/Users/10_Pro/AppData/Local/Programs/Python/Python312/python.exe c:/Users/10_Pro/chatgpt.py
Traceback (most recent call last):
File "c:\Users\10_Pro\chatgpt.py", line 15, in <module>
response = generate_text_with_openai(prompt)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\10_Pro\chatgpt.py", line 7, in generate_text_with_openai
completions = openai.ChatCompletion.create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\10_Pro\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai\lib\_old_api.py", line 39, in __call__
raise APIRemovedInV1(symbol=self._symbol)
openai.lib._old_api.APIRemovedInV1:

You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the
API.
You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface.

Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

PS C:\Users\10_Pro> & C:/Users/10_Pro/AppData/Local/Programs/Python/Python312/python.exe c:/Users/10_Pro/chatgpt.py
Traceback (most recent call last):
File "c:\Users\10_Pro\chatgpt.py", line 15, in <module>
response = generate_text_with_openai(prompt)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\10_Pro\chatgpt.py", line 7, in generate_text_with_openai
completions = openai.ChatCompletion.create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\10_Pro\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai\lib\_old_api.py", line 39, in __call__
raise APIRemovedInV1(symbol=self._symbol)
openai.lib._old_api.APIRemovedInV1:

You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the
API.
You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface.

Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

PS C:\Users\10_Pro> & C:/Users/10_Pro/AppData/Local/Programs/Python/Python312/python.exe c:/Users/10_Pro/chatgpt.py
Traceback (most recent call last):
File "c:\Users\10_Pro\chatgpt.py", line 15, in <module>
response = generate_text_with_openai(prompt)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\10_Pro\chatgpt.py", line 7, in generate_text_with_openai
completions = openai.ChatCompletion.create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\10_Pro\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai\lib\_old_api.py", line 39, in __call__
raise APIRemovedInV1(symbol=self._symbol)
openai.lib._old_api.APIRemovedInV1:

You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the

You can run `openai migrate` to automatically upgrade your codebase to use the 1

Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

PS C:\Users\10_Pro> openai migrate
Error: Windows is not supported yet in the migration CLI
PS C:\Users\10_Pro> & C:/Users/10_Pro/AppData/Local/Programs/Python/Python312/python.exe c:/Users/10_Pro/chatgpt.py
Traceback (most recent call last):
File "c:\Users\10_Pro\chatgpt.py", line 15, in <module>
response = generate_text_with_openai(prompt)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\10_Pro\chatgpt.py", line 7, in generate_text_with_openai
completions = openai.ChatCompletion.create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\10_Pro\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai\lib\_old_api.py", line 39, in __call__
raise APIRemovedInV1(symbol=self._symbol)
openai.lib._old_api.APIRemovedInV1:

You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the
API.
You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface.

Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

PS C:\Users\10_Pro> & C:/Users/10_Pro/AppData/Local/Programs/Python/Python312/python.exe c:/Users/10_Pro/chatgpt.py
Traceback (most recent call last):
File "c:\Users\10_Pro\chatgpt.py", line 15, in <module>
response = generate_text_with_openai(prompt)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\10_Pro\chatgpt.py", line 7, in generate_text_with_openai
completions = openai.ChatCompletion.create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\10_Pro\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai\lib\_old_api.py", line 39, in __call__
raise APIRemovedInV1(symbol=self._symbol)
openai.lib._old_api.APIRemovedInV1:

You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the
API.
You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface.

Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

PS C:\Users\10_Pro> & C:/Users/10_Pro/AppData/Local/Programs/Python/Python312/python.exe c:/Users/10_Pro/chatgpt.py
Traceback (most recent call last):
File "c:\Users\10_Pro\chatgpt.py", line 15, in <module>
response = generate_text_with_openai(prompt)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\10_Pro\chatgpt.py", line 7, in generate_text_with_openai
completions = openai.ChatCompletion.create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\10_Pro\AppData\Local\Programs\Python\Python312\Lib\site-packages\openai\lib\_old_api.py", line 39, in __call__
raise APIRemovedInV1(symbol=self._symbol)
openai.lib._old_api.APIRemovedInV1:

You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the
API.

You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface.

Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

PS C:\Users\10_Pro>

Β 
 
Posted : 12/11/2023 5:43 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@ionut-antal update your code library with the one provided here https://github.com/hassancs91/PE-Course-codes

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 12/12/2023 2:28 pm
(@ionut-antal)
Posts: 21
Eminent Member Customer
Topic starter
 

@ssadvisor , sorry, I tried to understand but I don't.Β 

Please can you be more specific with a few steps?Β 

Thanks a lot for your time and help!

 
Posted : 12/12/2023 5:43 pm
(@husein)
Posts: 550
Member Moderator
 

@ionut-antal OpenAI's update changed some structures of the codes. @hasan is working on updating all codes in the course, but for now, you can check the updated codes in @Hasan's GitHub repository @ssadvisor provided: Β  https://github.com/hassancs91/PE-Course-codes

 
Posted : 12/12/2023 6:42 pm
(@ionut-antal)
Posts: 21
Eminent Member Customer
Topic starter
 

Thanks

I am a completely beginner,Β  I saw what @ssadvisor provided but I don't understand wich code to use, because there are so many. That's why I asked for some steps.Β 


Β 

 
Posted : 12/12/2023 6:51 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

Hi @ionut-antal, what they mean, is just update the code you are using since OpenAI updated.

this code is no longer working, you should use the new one.

you can find all new codes here: https://github.com/hassancs91/PE-Course-codes

 
Posted : 12/12/2023 7:16 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@ionut-antal you can use this link to download a zip of the new code. https://github.com/hassancs91/PE-Course-codes/archive/refs/heads/main.zip

Or you can use the command ```git clone https://github.com/hassancs91/PE-Course-codes.git``` if you want to use the proper git command.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 12/13/2023 12:36 am
(@ionut-antal)
Posts: 21
Eminent Member Customer
Topic starter
 

Hi guys,Β 

Happy New Year! All the best to everyone!Β 

Β 

It is not a really a course for a beginner.Β  It is not easy to set up the computer right! After 2 weeks of breakΒ  I realised that I want to go ahead and learn about this skill because I nearly give up after hours and hoursΒ  trying.Β 

Also I have to specify that my system is Windows 10 pro.

I don't find a way to connect them.Β Β 

I don't want to give up , I want to learn.Β 

Every error I had I gave it to Chatgpt and he tried to give me all kind of solutions like:

- pip install --upgrade openai

- Here's a step-by-step guide for setting up and using WSL for this purpose:

  1. Open a PowerShell or Command Prompt as an administrator and runΒ wsl --install.
  2. Restart your computer.
  3. Open the WSL application.
  4. In the WSL terminal,Β cdΒ into the appropriate directory (e.g.,Β cd /mnt/c/Users/Myself/my/code/) and then run the following commands:
    curl -fsSL  https://docs.grit.io/install  | bash
    grit install
    grit apply openai
    Β 

Β  Β  Β  Β  Β  Then, you can close WSL and go back to using Windows.

Β 

- pip install openai==0.28Β  (because I had this error in the terminal)

- openai migrate

- pip install multidict

- Install Microsoft Visual C++ Build Tools :

Β  Β  Β  Β 

Β  Β  Β  Β  Β  Β  Β  Β  Β When installing Microsoft Visual C++ Build Tools, you generally need to ensure that the following components are selected:

  1. Desktop development with C++:

    • This is a broad category that includes the necessary tools and libraries for C++ development.
  2. Universal Windows Platform development:

    • If you are working with UWP applications, you may need to select this option.
  3. C++ CMake tools for Windows:

    • If your project uses CMake for building, this component is required.
  4. Windows 10 SDK:

    • Ensure that you have the Windows 10 SDK installed. The version may vary, but it's generally a good idea to have the latest.
  5. C++ ATL for latest v142 build tools (x86 & x64):

    • ATL (Active Template Library) is sometimes required for C++ projects

Β 

- pip download multidict==5.1.0.

Β 

Β  Β  Β  Please guys help me to set this up so I can carry on learning! I am really lost right now ( I hope that I didn't get you lost now :))Β  ).

Β 

Β 

 
Posted : 01/04/2024 7:29 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

Hi Friend, Happy New Year.

Sometimes ChatGPT hallucinate and give answers that has nothing to do with your problem.Β 

and remember that it answers on what is trained on, and this may be a new error due to the last openAI update.

Going back to your error. is this what you are getting:

"You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0"

Still the same error?

If yes, it is just that you need to update the code. you just need to use my latest update, can you please share aΒ  screenshot of your code to tell you what to change?

Β 

I know if you are totally new to this, it will look a bit hard, but these skills will really help you a lot, not only in business even in your personal life.

just put in some more effort and try to continue.

 
Posted : 01/05/2024 7:28 am
SSAdvisor reacted
(@ionut-antal)
Posts: 21
Eminent Member Customer
Topic starter
 

@admin thank you!

 
Posted : 01/06/2024 4:44 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

This is because Openai updated their package. You need to update the code and use: openai.chat.completions.create instead of openai.ChatCompletion in the code.

Is this my script? Or something you created?

if it is mine, please share which one to update and fix

 
Posted : 01/07/2024 8:35 am
(@ionut-antal)
Posts: 21
Eminent Member Customer
Topic starter
 

Yes it is your script. I hope that I did the change right.Β 

Now I have this result in the terminal.

Thanks for helping me.Β 

if you think that I have to learn something else before this , let me know, I do everything just to get this right.

Β 

 
Posted : 01/07/2024 12:11 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@ionut-antal did you upgrade the course codes with https://github.com/hassancs91/PE-Course-codes?

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 01/07/2024 9:32 pm
(@ionut-antal)
Posts: 21
Eminent Member Customer
Topic starter
 

@ssadvisor Yes. Actually, can you give me thatΒ  specific code? because you have been asking me so many times about it, maybe I don't get the right code. Thanks

 
Posted : 01/07/2024 9:39 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@ionut-antal the link I gave you has a zip file you can download. If you can't find it maybe you need a 1-on-1 to help?

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 01/08/2024 1:41 am
Page 1 / 2
Share:
[the_ad_group id="312"]