Home > Technology peripherals > AI > When ChatGPT meets Python

When ChatGPT meets Python

PHPz
Release: 2023-04-12 14:07:03
forward
1323 people have browsed it

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!

chatgpt-wrapper

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.

Installation Configuration

The first step is to install and clone the project.

pip install git+https://github.com/mmabrouk/chatgpt-wrapper
Copy after login

Then we install Playwright.

pip install playwright
Copy after login

Next we install the browser in Playwright, such as firefox.

playwright install firefox
Copy after login

After the above is completed, we execute the following command in the terminal:

chatgpt install
Copy after login

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!

When ChatGPT meets Python

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.

Connect to Python

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
Copy after login

With just three lines of code, we seem to own the whole world!

OpenAI

The second method is through the OpenAI open interface.

We first install the OpenAI library.

pip install openai
Copy after login

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.

When ChatGPT meets Python

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)
Copy after login

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!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template