Using ChatGPT in Python
ChatGPT-Wrapper
The first SDK is called ChatGPT-Wrapper (repo here). It opens a webpage in the background and records/streams the conversation to your command line interface or application.masnaato
Steps
pip install git+https://github.com/mmabrouk/chatgpt-wrapper
Behind the hood, it uses playwright to interact with the browser, so by installing this package, you will also install playwright Python package as a dependency (See below).
pip freeze | grep playwright playwright==1.28.0
2. Use playwright to install a Web browser
playwright install {browser}By default it installs Firefox .
3. Authentication with ChatGPT
chatgpt install
It will open up an authentication page in the web browser you installed using playwright. Like below, authenticate with your registered account.
Close the pages and restart the program.
The SDK provides a command-line interfacemasnaato
And a Python SDK (full response/streaming)
from chatgpt_wrapper import ChatGPTbot = ChatGPT()# return the full resultresponse = bot.ask("tell me a story about cats and dogs")print(response)# return the result in streaming (chunks)for chunk in bot.ask_stream("tell me a story about cats and dogs"): print(chunk)- Easy to use.masnaato
- Both shell and Python interactions.
- The biggest problem, if you have used it for a bit, is that it is pretty slow. Because it interacts with the web browser and then collects the result back to the console, it takes a while to see the response.
ChatGPT-Python
The second open-source SDK is chatgpt-python (repo here). This installation is much easier just like any other Python packages.
pip install -U chatgpt
Its usage is almost the same as the above, so I will not elaborate here. Only thing to remember is to put your credentials in the file config.json under the same directory or you can customize the location by using the config_path parameter in the Conversation() .
from chatgpt import Conversationconversation = Conversation(config_path={somewhere_you_specified})for chunk in conversation.stream("Hello"): print(chunk, end="")Pros and Cons
- There is a login issue as of the version
chatgpt==2.2212.0which is discussed here, and it has not been resolved yet at the moment.
Something Personal
Some limits of it are introduced on the official pages here. A few feelings about it,
- Sometimes it gives incorrect answers that sound like the truth, as it is well-educated and persuasive. We should use it critically.
- As a programmer, I am curious about its coding abilities. From the tests, it can solve some easy to medium-level Leetcode problems (I have ruled out the old questions that might have already appeared in its training). But it can also fail and get confused at some easy questions, and sometimes it gets stuck halfway. The funny thing is humans also get stuck during many implementations.
- It can help debug some fundamental errors. I think this really saves time when humans face huge lines of code with trivial errors. For more domain-specific errors, human is still irreplaceable.
- One thing it is really good at is naming variables since one of the biggest difficulties in programming is finding a good name for your variable and functions.