ChatGPT Work Efficiency Improvement Goose Pass Secondary Development Batch API Docking Solution (Learning Record Synchronization, User Registration Synchronization, Rights Order Synchronization, Development Documentation)

ChatGPT Work Efficiency Improvement Series

  1. A Preliminary Exploration of the Way of ChatGPT Work Efficiency Improvement
  2. Ingenuity and instructions in program development for improving ChatGPT work efficiency (creating MySQL statements, PHP statements, Javascript usage, python interaction)
  3. ChatGPT work efficiency to generate development requirements and quotations and convert them to Excel format

ChatGPT is a real-time dialogue generation model that can help users quickly answer questions, provide information, and conduct dialogues smoothly, accurately understand the user's intentions, and respond accordingly.

The advantage of the ChatGPT model is that the naturalness and coherence of the generated text are very good, and there will be no obvious logical errors and grammatical errors. In addition, the training data set of ChatGPT is rich and diverse, which can deal with most problem scenarios. Due to the use of pre-trained models, it can be quickly customized into question-answer models for various application scenarios, which can greatly improve development efficiency. However, ChatGPT also has some disadvantages. Because it is a generative model based on a pre-trained model, it cannot adjust its answer logic processing like a traditional rule engine, so some meaningless or contradictory answers are generated.

Recently, in the actual development process of the Goose Communication project, the frequency of use of "ChatGPT-oriented development" has been increased. The relevant process is now recorded as follows:

insert image description here

1. Project requirements

Call the Leaning API interface to obtain the learning record information of the specified resource, xe.user.leaning_record_by_resource.get/1.0.0.
Request method and url

  • Request method: POST
  • Request header: Content-Type: application/json
  • Interface address: https://api.xiaoe-tech.com/xe.user.leaning_record_by_resource.get/1.0.0
  • Frequency limit: 3000 times in 10 seconds
  • Remarks: The data is quasi-real-time, with a delay of 4~5 minutes
  • The user_id array, the upper limit of the number of requests, and the request parameters are as shown in the figure:
    insert image description here
    At present, it is necessary to obtain the learning records of the resources specified by the user in batches under the restrictions of the above-mentioned API conditions in Xiaogetong. In php, there is a perfect algorithm for delaying the process, and it is easy for the API to exceed the limit or frequent requests to cause the token to become invalid.
    How to implement queue update?

1.chatGPT: Use php to output code in batches and time sharing

Write a 100-length array in php, and output the code in batches and time divisions

<?php

// 创建一个100长度的数组
$array = range(1, 100);

// 每批输出的长度
$batch_size = 10;

// 循环输出数组内容
for ($i = 0; $i < count($array); $i += $batch_size) {
    
    
    echo "批次 " . ($i / $batch_size + 1) . "\n";
    echo implode(", ", array_slice($array, $i, $batch_size)) . "\n";
    sleep(1); // 暂停1秒,以模拟分时输出
}

?>

sleep(1)

2.chatGPT uses javascript to output the code in batches and time sharing

How to write an array of length 100 in javascript, how to write the code for batch and time-sharing output

 var arr = Array(100).fill(null).map((_, i) => i + 1); // 创建长度为100的数组

    function outputArrayInBatch(arr, batchNum, interval) {
    
    
        var startIndex = batchNum * interval; // 当前批次的起始索引
        var endIndex = (batchNum + 1) * interval; // 当前批次的结束索引
        if (startIndex > arr.length - 1) {
    
    
            return; // 当所有元素都输出完毕后,退出递归
        }
        if (endIndex > arr.length) {
    
    
            endIndex = arr.length; // 当结束索引超过数组长度时,将结束索引设置为数组的最后一个元素的索引
        }
        var batchArr = arr.slice(startIndex, endIndex); // 当前批次需要输出的元素数组
     
        //对批次数组进行遍历操作;
        console.log(batchArr); // 输出当前批次的元素
        
        setTimeout(function () {
    
    
            outputArrayInBatch(arr, batchNum + 1, interval); // 递归调用下一批次的输出
        }, 2000); // 每隔1秒输出一批次
    }

    // 执行输出
    outputArrayInBatch(arr, 0, 10); // 一次输出10个元素,共分10批输出

The code first creates an array of length 100. The outputArrayInBatch() function receives three parameters: the array to be output, the current batch number, and the number of elements to be output in each batch. In the function, first calculate the current array of elements to be output according to the number of batches and the number of output elements; then output the elements of the current batch, and call the function recursively through the setTimeout() function to output the elements of the next batch.

The output here is to output a batch of elements every 1 second, and the output time interval can be adjusted according to specific needs.
insert image description here

2. Batch repair API interface

1. Repair unit does not exist API

public function upDepart()
    {
    
    
        global $db, $res;
        dbc();
        @$user_phone = get_param('user_phone');
        if ($user_phone == "") {
    
    
            die("参数错误");
        }
        //读取单位认证
        $row = $db->fetch('depart_audit', '*', array('user_phone' => $user_phone), ' audit_id DESC');

        //获取单位ID
        $row_depart = $db->fetch('depart', 'depart_id', array('depart_name' => $row['depart_name']), ' depart_id DESC');
        if ($row_depart['depart_id'] == "") {
    
    
            die("单位不存在或未验证");
        }

        //更新单位库
        $user_roles = 1;//单位管理员权限;
        $user_menu = 0;//可操作
        $user_auth = 1;//已审
        $db->insert('admin', array('user_phone' => $user_phone, 'user_name' => $row['user_name'], 'user_pwd' => $row['user_pwd'], 'user_mail' => $row['user_mail'], 'depart_id' => $row_depart['depart_id'], 'user_depart' => $row['depart_name'],
            'user_branch' => $row['user_branch'], 'user_titles' => $row['user_titles'], 'user_province' => $row['province'], 'user_city' => $row['city'], 'user_area' => $row['area'],
            'user_roles' => $user_roles, 'user_menu' => $user_menu, 'user_auth' => $user_auth));

        $res["data"] = $user_phone . "-更新成功";
        die(json_encode_lockdata($res));
    }

2. Repair unit depart_id does not exist

public function upDeID()
    {
    
    
        global $db, $res;
        dbc();
        //读取管理员信息
        $sql = "select depart_id,user_depart FROM " . $db->table('admin') . " WHERE depart_id IS NULL";
        $sql .= " ORDER BY user_id DESC";
        $row = $db->queryall($sql);
        for ($i = 0; $i < count($row); $i++) {
    
    
            // echo $row[$i]['user_depart'] . '<br>';
            //获取单位ID
            $row_depart = $db->fetch('depart', 'depart_id,depart_name', array('depart_name' => $row[$i]['user_depart']), ' depart_id DESC');
            if ($row_depart) {
    
    
                $db->update('admin', array('depart_id' => $row_depart['depart_id']), array('user_depart' => $row[$i]['user_depart']));
            }
        }

        $res["data"] = "更新成功";
        die(json_encode_lockdata($res));
    }

3. Batch update login permissions

public function upAuth()
    {
    
    
        global $db, $res;
        dbc();
        $db->update('user', array('user_auth' => 1), array('user_auth' => 0));
        $res["data"] = "更新成功";
        die(json_encode_lockdata($res));
    }

3. Problems encountered in development

1. The sql statement whose query field is empty

How to write a query statement in mysql to filter a field that is not empty

In MySQL, you can use IS NOT NULLto . For example, suppose there is a table named my_table, which contains field1the field , to find field1the data whose field is not empty, you can use the following SQL query statement:

SELECT * FROM my_table WHERE field1 IS NOT NULL;

Among them SELECT *, means to select all columns, FROM my_tablemeans to query data from my_tablethe table , WHERE field1 IS NOT NULLmeans to filter the data field1whose field is not empty.

If you want to filter out the data whose field value is empty, you can use IS NULLto indicate, for example:

SELECT * FROM my_table WHERE field1 IS NULL;

This SQL query statement will return all data of records field1whose field is empty, and other fields may be empty or have values.

In the actual development process, when writing the sql statement with the query field empty, the user_eid is empty in the is null lookup table, and the result is no result. The reason is that only the field that is displayed (null) is a real null value. If there is nothing in the delete value box after assigning a value to the field before, it should be written as follows:user_eid = ''

    public function getEid()
    {
    
    
        checkAdminAuth($_COOKIE['admin_roles'], '0', 1);//超管权限
        global $db, $res;
        dbc();
        //读取管理员信息
        $sql = "select user_eid,user_name,user_phone FROM " . $db->table('user') . " WHERE user_eid = ''";
        $sql .= " ORDER BY user_id DESC";// LIMIT 100
        $row = $db->queryall($sql);
        $res["data"] = $row;
        die(json_encode_lockdata($res));
    }

2. Set the login cookie to never expire

PHP how to set the login cookie never expires

To set the PHP login cookie to never expire, you can specify a large expiration time, such as January 19, 2038, which is the maximum date PHP can handle.

You can use the setcookie() function to set cookies, the specific examples are as follows:

$cookie_name = "user";
$cookie_value = "John Doe";
$expiry_date = strtotime('2038-01-19');
setcookie($cookie_name, $cookie_value, $expiry_date, "/");

In the example above, the $expiry_date variable is set to a timestamp of January 19, 2038, which will be set as the cookie's expiration time in the setcookie() function. In this example, the path of the cookie is set to "/", which means that the cookie is associated with all pages on the server, not just the current page.

Once set in this way, the user's login cookie will be stored permanently until the browser deletes it or reaches the maximum duration specified on the server.

3. What is the code to realize SMS verification code

In addition to Redis, there are many other ways to implement SMS verification code flow control. Here is a sample code based on the file system:

<?php

// 定义短信验证码保存目录
define('SMS_DIR', '/path/to/sms/dir/');

// 获取客户端IP地址
$ip = $_SERVER['REMOTE_ADDR'];

// 获取手机号码
$phone = 'your_phone_number';

// 定义文件名规则
$filename = md5($ip . $phone) . '.json';

// 检查文件是否存在
if (file_exists(SMS_DIR . $filename)) {
    
    
    // 如果文件存在,则读取文件内容
    $content = file_get_contents(SMS_DIR . $filename);

    // 解析JSON内容
    $data = json_decode($content, true);

    // 如果超过发送次数限制,则提示用户并退出
    if ($data['count'] >= 5 && time() - $data['timestamp'] < 300) {
    
    
        echo "发送频率过快,请稍后再试。";
        exit;
    } else {
    
    
        // 如果可以发送,则将发送次数加1,并更新时间戳
        $data['count']++;
        $data['timestamp'] = time();
    }
} else {
    
    
    // 如果文件不存在,则创建新文件并写入初始内容
    $data = [
        'count' => 1,
        'timestamp' => time()
    ];

    $content = json_encode($data);

    file_put_contents(SMS_DIR . $filename, $content);
}

// 发送短信验证码

?>

In the above code, the number of sending SMS verification codes is saved in a JSON file, which is associated with the client IP address and mobile phone number. The md5 value is used in the code to generate the file name, and the content of the file is a string in JSON format, including two fields, the number of sending times and the last sending timestamp.

When sending the SMS verification code, first generate the file name according to the request parameters, and then check whether the corresponding file exists. If the file exists, read the content of the file and check whether the number of sending times and the timestamp exceed the limit. If it can be sent, add 1 to the number of sending times, and write the updated content back to the file; if the number of sending times has reached the limit, prompt the user to try again later. If the file does not exist, create a new file and write the initial send count and timestamp.

The performance of the file system-based method is slightly lower than that of Redis or other memory-based databases, and it is also more susceptible to concurrent access. Therefore, it is recommended to use more efficient cache servers such as Redis in high-concurrency scenarios.


@leak time sometimes

Guess you like

Origin blog.csdn.net/weixin_41290949/article/details/130855275