How to install OpenAI - ChatGPT's python package in Pycharm?

This article was originally created by Daxia (AhcaoZhu) , please declare for reprinting.
Link: https://blog.csdn.net/Ahcao2008

Alt

Summary

  • The full text introduces the installation process and experience of the very ChatGPTpopular API development kit.openai
  • It can help you avoid detours.
  • It is a practical experience. It is part of a series set. Follow-up release, please pay attention. [Original: AhcaoZhu Hero]

background

  • If you want to carry out secondary development (python) based on ChatGPT, you must introduce its application API. The python package (package) of the API released by OpenAI is called openapi.
  • The syntax is as follows:
	import openai
  • Most of the module installation, in fact, the process is interlinked. Please see appendix PyCharm中如何安装第三方库?. 1
  • There is nothing special about the installation of openai, but it is more complicated and depends on more libraries. The experience introduced in this article allows beginners to avoid some detours.
  • OpenAI's official website introduces an example (see appendix): Quick start - example of API access: naming pets . 2
    - For many people, the above is not directly accessible. The author will introduce and analyze this example in the following blog post.

Install

1. Preparation before installation

  • Before the installation starts, we enter import openai, without any surprise, the following information that the module cannot be found will appear:

insert image description here

  • At this point, you can use any of the methods described in Appendix 1 at the end of the article 一种to install:
  • Click directly on the small yellow light to install
  • In the bottom toolbar of PyCharm Python软件包, orPython控制台
  • or cmd, or python IDLEwait for installation
	pip install openai
  • Even, there is a smarter one-click installation:
    • When we copy the app.pycomplete sample package ( requirements.txt) in the above appendix 2 to our project directory, if your PyCharm connects to Github, the IDE will directly pop up the one-click installation of the dependent package.

install plugin

  • After the author's attempts, according to any of the above methods, all can't succeed in one pass. There will be multiple interruptions and repeated search for problems. Therefore, the method in this article can achieve successful installation with the least number of failures.

2. Installation prerequisites

  • Installation does not require external network conditions.
  • It is required for development and use key.
    • Although no external network conditions are required, anyone can install openaithe module , but if you want to develop directly based on openai in the future, you still need it key.
    • Unless the key is packaged and shared by a third party, many individuals or institutions in China are currently doing it, and there may already be "free".
    • By the way, according to OpenAI’s official website, its charging policy will change. Before August, it is 5$ for unlimited traffic and parameters (mainly temperature, token number, context, etc., see the figure below), and all parameters can be Try it, it will be charged according to TOKEN later. Of course, the version (ChatGPT-3.5) and application range (Engine) are still limited.
      openai-paras
  • The installation conditions of each machine are different.
    • The author specifically found a new machine (win11) for testing, and found that in addition to the dependent library, vc++ runtime library v11 or above must be installed (the installation process may be interrupted, and it will have an English prompt).
    • This needs to be found and installed independently on Microsoft's official website (the automatic jump installation process is not available).
      C++ V14

3. Dependency library

  • requirements.txt
  • It is preceded by the module name, followed by the minimum version number required.
    • During the installation process, the author found that sometimes the version number (the highest version number) is limited, which deletes the higher version of a certain module, but installs a lower version compatible with openai. But don't worry, this process does not require human intervention, it will happen automatically.
autopep8==1.6.0
certifi==2021.10.8
charset-normalizer==2.0.7
click==8.0.3
et-xmlfile==1.1.0
Flask==2.0.2
idna==3.3
itsdangerous==2.0.1
Jinja2==3.0.2
MarkupSafe==2.0.1
numpy==1.21.3
openai==0.19.0
openpyxl==3.0.9
pandas==1.3.4
pandas-stubs==1.2.0.35
pycodestyle==2.8.0
python-dateutil==2.8.2
python-dotenv==0.19.2
pytz==2021.3
requests==2.26.0
six==1.16.0
toml==0.10.2
tqdm==4.62.3
urllib3==1.26.7
Werkzeug==2.0.2

best installation process

1. Check VC++

Check whether VC++ is installed. If it is not installed, install it manually first, which will save trouble.

2. See which dependent libraries are not installed

Open the above requirements.txt file (which can be copied and pasted) in Pycharm, use RE 替换, 列编辑模式to quickly change it to the way of importing modules, and rename or copy it as test01.py.

import certifi
import idna
import numpy
import openpyxl
import requests
import six
import tqdm
import urllib3

import autopep8
import charset_normalizer
import click
import et_xmlfile
import flask
import itsdangerous
import jinja2
import markupsafe
import openai
import pandas
from pandas import pandas_stubs
import pycodestyle
import dateutil
import dotenv
import pytz
import toml
import werkzeug
  • On my machine, the first 8 are loaded modules, so they don't need to be loaded.
  • You just need to copy the above test01.py to your pycharm and open it.
  • If you want to do it yourself, please note:
    • Turn -to _, for example: et-xmlfileet_xmlfile
    • Convert uppercase to lowercase, for example: MarkupSafemarkupsafe
    • Remove the leading python, for example: python-dateutildateutil
    • One special: from pandas import pandas_stubsbecause dateutil is a submodule of pandas

3. Write the uninstalled module as test02.bat

  • The same applies RE 替换, 列编辑模式, if written as a .bat file, it can be executed in batches. It can also be executed manually one by one.
  • It is recommended to run cmdunder .
  • In addition, there are dependent modules in the installation process of modules in the dependent library, which are listed here. Suppose the name is: test03.bat. It is installed first than test02.bat, which has advantages: fast!
    It is also possible to directly run test02.bat without running test03.bat.

test03.bat

pip install charset-normalizer
pip install setuptools
pip install attrs
pip install multidict
pip install chardet
pip install aiohttp
pip install async-timeout
pip install yarl
pip install et-xmlfile
pip install python-dotenv
pip install toml

test02.bat

  • Because the first 8 of my machines have been installed, you can refer to your machine. The pip command will also automatically judge that it has been installed.
  • Indentation is not required, just to indicate dependencies
pip install certifi
pip install idna
pip install numpy
pip install openpyxl
pip install requests
pip install six
pip install tqdm
pip install urllib3

pip install charset-normalizer
pip install setuptools
pip install attrs
pip install multidict
pip install chardet
pip install aiohttp
pip install async-timeout
pip install yarl
pip install et-xmlfile
pip install python-dotenv
pip install toml

pip install pycodestyle
    pip install tomli
pip install autopep8

    pip install importlib_metadata
    pip install zipp
pip install click

    pip install jinja2
    pip install itsdangerous
    pip install werkzeug
    pip install markupsafe
pip install flask

    pip install pytz
    pip install python-dateutil
pip install pandas
    pip install typing-extensions
pip install pandas-stubs

pip install openai

4. Check
So far, openai and all its dependent libraries and dependent libraries of dependent libraries have been installed.
Open test01.py in PyCharm to see which modules have not been installed yet.

Next step

quote


  1. How to install third-party libraries in PyCharm? ↩︎

  2. Quick Start - Example of API Access: Naming Pets ↩︎

  3. One picture to understand openai module: ChatGPT API python library, data organization + notes (comprehensive) ↩︎

Guess you like

Origin blog.csdn.net/Ahcao2008/article/details/130377650