dcat admin 表单加入ai元素

2024-05-31 17:28:27 阅读:2 编辑
E:/edison/zhyai/vendor/dcat/laravel-admin/src/Form/Field/Textarea.php
 public function ai($ai_template,$ai_stream = true)
    {
        $this->addVariables(["has_ai"=>true,"ai_template"=>$ai_template,"ai_stream"=>$ai_stream ? 1 :0]);
        return $this;
    }
E:/edison/zhyai/vendor/dcat/laravel-admin/resources/views/form/ai.blade.php
@if($has_ai)

<div style="margin-top: 10px">
    <span class="btn btn-success"  onclick="ai('{{$name}}','{{$ai_template}}','{{$ai_stream}}')">AI生成</span>
</div>
@endif
E:/edison/zhyai/vendor/dcat/laravel-admin/resources/views/form/textarea.blade.php
 @include('admin::form.ai')
E:/edison/zhyai/vendor/dcat/laravel-admin/src/Layout/Asset.php
var g_ai_doing = false;
function ai(field,ai_template,ai_stream){
    console.log("field",field);
    console.log("ai_template",ai_template);
    let value = "";
    let field_dom = $("input[name='"+field+"']");
    value = field_dom.val();
    if(!value){
        field_dom = $("textarea[name='"+field+"']");
        value = field_dom.val(); 
    }else{

    }

     console.log("value",value);
     let question = ai_template.replace("{question}",value);
      console.log("question",question);
    //return;
            if(g_ai_doing){
                return;
            }
            g_ai_doing = true;

            let url ="http://123.249.34.10:1234/api/chatgpt";
            let data = {
                  question:question,
                    stream:0,
                    is_web:1,
            }
            if(ai_stream == 0){
                     $.ajax({
                    type: "POST",
                    data: data,
                    dataType: "json",
                    url:url,
                    timeout: 16000,
                    success: function (res) {
                        console.log("ai post");
                        console.log(res);
                        field_dom.val(res.data.content);
                        g_ai_doing = false;
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        g_ai_doing = false;
                    }
                }); 
            }else{
                data.stream = 1;
                url +="?";
                for (var i in data) {
                    url += i + "=" + data[i] + "&"
                }
                var eventSource = new EventSource(url);
                let that = this;
                eventSource.addEventListener('message', function (e) {
                    console.log(e);

                    if (e.lastEventId == -1) {
                        eventSource.close();
                         g_ai_doing = false;
                    } else {
                        let old_text = field_dom.val();
                        old_text +=e.data;
                        field_dom.val(old_text);
                        //that.toSetAnswer(e.data);
                    }
                });
            }

        }
使用
 return Form::make(new Question(), function (Form $form) {

            $options = Platform::pluck("name", "id");
            $form->select("platform_id", "用户平台")->default(1)->options($options);
            $form->text('name', '名称')->default("福建厦门")->ai("{question}土特产有哪些?,列出3个,字数不超过30个字");
            $form->textarea('text', '问题')->default("福建龙岩")->ai("{question}3个景点");
        });