OpenAI GPT3 + Flask 利用 text-davinci-003 API 制作自己的交互网页教程 | 附源码 和 Github链接
创始人
2024-05-23 18:22:06
0

1. OpenAI GPT3 text-davinci-003 API

最近ChatGPT很火,使用与InstructGPT相同的方法,使用来自人类反馈的强化学习 Reinforcement Learning from Human Feedback (RLHF) 来训练该模型,但数据收集设置略有不同。ChatGPT是在 GPT-3.5 系列中的一个模型进行微调的,该模型于 2022 年初完成训练。

现在因为官网 https://chat.openai.com/ 一直是满载状态,我决定使用GPT-3的公开API做一个基于python flask的网站。GPT-3的模型虽然比不上GPT-3.5的,但其功能仍然是十分强大。如果你有账号的话可以在https://platform.openai.com/playground运行GPT-3。

GPT-3 一共有4个模型,其中davinci是最有能力的模型,而ada是最快的模型。Davinci 最大请求量是4000个tokens,这其中包含了问题和回答。

在这里插入图片描述

目标:

这篇文章将教你如何把GPT-3的API用在自己的网站上,使用的是python flask框架。

2. 环境介绍

  1. OpenAI API Key
  2. Python 3
  3. python库:openai flask

第一步非常的复杂,需要先注册一个OpenAI的账号。这里不详细展开,可以tb或者查找其他教程(如sms虚拟电话)注册后获取API key,这里放一张图。
注意:添加一个key后需要在弹出的对话框内复制key的内容,关掉后将无法再查看,但是可以重新生成一个key。
在这里插入图片描述
其次,电脑要有python 3 的环境。这个也不展开。

最后需要确认用pip安装 openai 和 flask 两个库。

pip install openai
pip install flask

3. Python Flask 代码

python 文件为 main.py。

在代码openai.api_key = 'your API'的地方用自己的API key 进行替换。

from flask import Flask, request, render_template, redirect
import openaiopenai.api_key = 'your API'  # 用自己的API key 进行替换server = Flask(__name__)def send_gpt(prompt,tem):try:response = openai.Completion.create(model='text-davinci-003',prompt=prompt,temperature=tem,max_tokens=3500,   # prompt and answer together have 4096 tokenstop_p=1.0,frequency_penalty=0,presence_penalty=0)return response["choices"][0]["text"]except:mess = "Connection Error! Please try again."return mess@server.route('/', methods=['GET', 'POST'])
def get_request_json():if request.method == 'POST':if len(request.form['question']) < 1:return render_template('chat.html', question="NULL", res="Question can't be empty!")question = request.form['question']temperature = float(request.form['temperature'])print("======================================")print("Receive the question:", question)print("Receive the temperature:",temperature)res = send_gpt(question,temperature)print("Q:\n", question)print("A:\n", res)return render_template('chat.html', question=question, res=str(res), temperature=temperature)return render_template('chat.html', question=0)if __name__ == '__main__':server.run(debug=True, host='0.0.0.0', port=80)

4. 网页代码

html网页(chat.html)放在templates文件夹里,templates文件夹和python文件(main.py)放在同一目录下。


GPT 3

Davinci-003

Temperature Guide

Code Refactoring --- 0.2

Email Parser --- 0.3

Chat Bot --- 0.5

Summarise Text --- 0.5

Code Generation --- 0.8

Story Writing ---0.8




{% if message %} {{ message }} {% endif %}

Temperature


Waiting for the response...
{% if question %}
Legend:
{{ question }}

Davinci-003-Tem{{ temperature }}:
{{ res }}
{% endif %}

5. 网站效果展示

成功运行python代码将如下图显示,打开浏览器输入 127.0.0.1:80 或者 localhost 进行访问。

在这里插入图片描述

这个Flask网站可以自定义 Temperature 即GPT-3的回答随机度。网站上也给出了温度设置的建议,下面为效果图。
网站效果一般,主要是实现功能。如果还想加除Temperature之外的变量,可以照葫芦画瓢。

Github 链接:https://github.com/redemptionwxy/GPT3-API-Flask-Python_Chat_Website

在这里插入图片描述

以上。

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...