Python 实现ChatGPT 代码,基于Flask

from flask import Flask, request, render_template, jsonify
from revChatGPT.V1 import Chatbot
import json

app = Flask(__name__)
chatbot = Chatbot(config={
    "access_token": "你的access_token 访问https://chat.openai.com/api/auth/session 获取"
})
 

@app.route("/chat",methods=["POST"])
def chat():

    if request.get_data() is None:
        return_dict['return_code'] = '5004'
        return_dict['return_info'] = '请求参数为空'
        return json.dumps(return_dict, ensure_ascii=False)
    # 获取传入的参数

    prompt = get_Data.get('prompt')

    response = chatbot_ask(prompt)
    return jsonify({
            "message": "请求成功",
            "data":  response,
            "status": "Success",
          })

def chatbot_ask(prompt):
    response = ''
    for data in chatbot.ask(prompt):
        response = data["message"]
    return response
    

if __name__ == "__main__": 
    app.run(host="0.0.0.0",port="8000")

简单写了一个基于flask 的chatgpt 请求接口

你可能感兴趣的:(ChatGPT,python,chatgpt,flask)