News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

How does The Force plan

posted on 2023-05-21 16:33     read(403)     comment(0)     like(27)     collect(5)


insert image description here

This article will demonstrate how to call the ChatGPT model in a local python project.


written in front

As a programmer, I often need to use ChatGPT to complete some tasks during the development process, but it is very troublesome to always use the web page interaction mode to access ChatGPT on the Web side. At this time, we can use the code to call the ChatGPT model to achieve The effect is the same locally and on the web.

This article will demonstrate the call of the Python development language to the ChatGPT model , which is generally divided into three steps:

  • Step 1 : Get the API Key, visit the API website under the OpenAI official website to get your own key;
  • Step 2 : Install the OpenAI third-party library;
  • Step 3 : Use Python to call the ChatGPT model API.

Step 1: Obtain API Key

Each account will have its own exclusive API key after successful registration. First, log in to OpenAI official website (https://platform.openai.com/overview) with our registered ChatGPT account password ;

insert image description here
After logging in, enter the following interface and select "View API keys";

insert image description here
If we come to this page for the first time, we need to create the key first, select "Create new secret key";

insert image description here
After the creation is successful, as follows, your own API key has been generated, just copy and use.

insert image description here

Step 2: Install OpenAI third-party library

Here, python needs to use pypi to install third-party libraries, enter " pypi " in the browser , you can see the Python Package Index, click to enter;

pypi : The acronym for Python Package Index, which means Python's Packageag index, which is Python's official index.

insert image description here

Then search for openai, you can find the current library of openai;

insert image description here
insert image description here
We choose openai 0.27.4 version and enter. This tells us how to install the openai third-party library: use the command pip install openai.

insert image description here
Next, use the local anaconda window to enter the command to download. Enter the prompt interface of anaconda;

insert image description here
Then enter the command in the prompt interface pip install openaito install the third-party library;

insert image description here
After the installation is complete, use the command pip listto view the installed packages. If you can see "openai" , it means that the third-party library is installed successfully.

insert image description here

Step 3: Call the ChatGPT model in the Python development environment

Here, OpenAI's third-party library is called based on the python development environment (Python project), so we need to have a built python development environment locally. If there is no python environment, please refer to this article:

Master the installation and use of Python, Anaconda, PyCharm in one article

Come to the development tool PyCharm, create a new project, the interface is as follows:

insert image description here
What catches the eye is a piece of entry-level basic code generated by default for the new project. After running, it will output "Hi, PyCharm". The successful operation shows that there is no problem with our python environment.

insert image description here
Next, create folders and .py files under the project folder, and you can write code;

insert image description here

# 1.引入必须的包
import os
import openai

# 2.获取api-key
openai.api_key = "你自己的API-key"

# 3.使用OpenAI的API完成ChatGPT模型调用
#    model:指的就是ChatGPT模型
#    prompt:向ChatGPT提出的问题
#    max_tokens:返回的最大字符个数
response = openai.Completion.create(
  model="text-davinci-003",
  prompt="请用python语言生成一个二分法查找算法",
  max_tokens=256,
)

# 4.打印结果
message=response.choices[0].text
print(message)

After the code is written, run this program, and you can see that the binary search algorithm has been generated ;

insert image description here
The ChatGPT model call was successful.



Category of website: technical article > Blog

Author:python98k

link:http://www.pythonblackhole.com/blog/article/25259/d372fcd7813e0f83c566/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

27 0
collect article
collected

Comment content: (supports up to 255 characters)