<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>chatGPT</title>
    <link rel="stylesheet" href="./layui/css/layui.css" media="all">
    <script type="text/javascript" src="./layui/layui.js"></script>
</head>
<body>
<div style="margin: 50px 0px 0px 50px">
    <form class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 16%">prompt</label>
            <div class="layui-input-inline" style="width: 76%">
                <input id="prompt" type="text" name="prompt" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-hide">
            <label class="layui-form-label" style="width: 16%">max_tokens</label>
            <div class="layui-input-inline" style="width: 76%">
                <input id="max_tokens" type="text" name="max_tokens" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-hide">
            <label class="layui-form-label" style="width: 16%">model</label>
            <div class="layui-input-inline" style="width: 76%">
                <input id="model" type="text" name="model" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <button class="layui-btn" lay-submit lay-filter="preAddElement">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
</body>
<script>

    // 自定义模块，这里只需要开放soulTable即可
    layui.config({
        base: '/layui/modules/'   // 第三方模块所在目录
        , layimAssetsPath: '../layui/modules/layim/dist/layim-assets/'
        , version: 'v1.6.4' // 插件版本号
    }).extend({
        layim: 'layim/dist/layim'
    });

    layui.use(['form', 'layer', 'layim'], function () {
        const form = layui.form
            , layim = layui.layim
            , $ = layui.$;

        //提交
        form.on('submit(preAddElement)', function (data) {
            data.field.max_tokens = 2048
            data.field.model = "text-davinci-003"
            $.ajax({
                type: 'post',
                url: 'https://api.openai.com/v1/completions',
                headers: {
                    "Content-Type": "application/json",
                    "Authorization": "Bearer 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"
                },
                dataType: 'json',
                data: JSON.stringify(data.field),
                success: function (res) {
                    console.log(res)
                },
                error: function (errMsg) {
                    console.log(errMsg);
                }
            })
            return false;
        });

        layim.config({
            brief: true //是否简约模式（如果true则不显示主面板）
        }).chat({
            name: '一个新窗口'
            ,type: 'friend'
            ,avatar: 'http://tp1.sinaimg.cn/5619439268/180/40030060651/1'
            ,id: -2
        });

    });

</script>
</html>