Home >Technology peripherals >AI >When ChatGPT meets Python
In the raging ChatGPT trend, a variety of peripheral products have been derived.
Python is a famous snake oil tool, how can it not be there? Today we will introduce two methods of calling ChatGPT through Python, let’s take a look!
This is an open source project on GitHub that drives the call to CHatGPT through the automation tool Playwright.
After we configure this tool, we can use ChatGPT in the command line and Python code.
The first step is to install and clone the project.
pip install git+https://github.com/mmabrouk/chatgpt-wrapper
Then we install Playwright.
pip install playwright
Next we install the browser in Playwright, such as firefox.
playwright install firefox
After the above is completed, we execute the following command in the terminal:
chatgpt install
Next, a ChatGPT login page will pop up. Enter the account password and click login. If you do not have a ChatGPT account yet, For the password, you can send "chatgpt" in the background of the official account to obtain the shared test account, first come, first served!
After the login is completed, we can restart the terminal and enter chatgpt in it. At this time, we can start playing happily with CHatGPT.
Of course we must be able to embed all of this into Python code.
from chatgpt_wrapper import ChatGPT bot = ChatGPT() response = bot.ask("Hello, world!") print(response)# prints the response from chatGPT
With just three lines of code, we seem to own the whole world!
The second method is through the OpenAI open interface.
We first install the OpenAI library.
pip install openai
Then you need to obtain the api key. You need to visit the following website https://platform.openai.com/account/api-key, add the corresponding key, and save the key.
Next we can write the code, which is very simple.
import openai # 设置 API Key openai.api_key = "上面保存的api key" # 设置请求参数 model_engine = "text-davinci-002" prompt = "Hello World" completions = openai.Completion.create( engine=model_engine, prompt=prompt, max_tokens=1024, n=1, stop=None, temperature=0.5, ) # 获取 ChatGPT 的回复 message = completions.choices[0].text print(message)
In the above code, we noticed that the response of ChatGPT can be adjusted by changing the request parameters in the code, such as prompt, model, temperature, etc.
However, it should be noted that OpenAI’s API has request limits, so we cannot call the interface without restraint.
The above is the detailed content of When ChatGPT meets Python. For more information, please follow other related articles on the PHP Chinese website!