no classification
no tag
no datas
posted on 2023-05-21 16:33 read(403) comment(0) like(27) collect(5)
This article will demonstrate how to call the ChatGPT model in a local python project.
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.
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 ;
After logging in, enter the following interface and select "View API keys";
If we come to this page for the first time, we need to create the key first, select "Create new secret key";
After the creation is successful, as follows, your own API key has been generated, just copy and use.
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.
Then search for openai, you can find the current library of openai;
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
.
Next, use the local anaconda window to enter the command to download. Enter the prompt interface of anaconda;
Then enter the command in the prompt interface pip install openai
to install the third-party library;
After the installation is complete, use the command pip list
to view the installed packages. If you can see "openai" , it means that the third-party library is installed successfully.
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:
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.
Next, create folders and .py files under the project folder, and you can write code;
# 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 ;
The ChatGPT model call was successful.
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.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!