充分利用ChatGPT,提高工作效率

331 阅读4分钟

现在每一个人都知道ChatGPT;这个十分强大且非常有趣(或可怕)的人工智能系统,它无所不能。从根据你给它的任何提示生成一本短篇小说,或者根据最富想象力的描述制作出从未见过的照片。但是你知道它也可以帮助你写代码,找到编程问题的解决方案吗?

这是真的!你可以要求 ChatGPT“用 PHP 为我编写一个‘Hello World’脚本,并将‘world’大写”,它会简单地生成代码来执行此操作!当然,你也可以向它提供更困难的问题和场景,但只要仔细观察并注意细节,您就可以利用该平台来处理频繁的任务和样板代码。

“最高效的开发人员不会凭记忆记住所有事情,他们会使用身边的工具来变得足智多谋。"

简单的ChatGPT示例

让我们用我最喜欢的语言--PHP生成一个简单的代码块。我们将保持控件简单并使用默认值:

  • Model = text-davinci-003(文本生成的最佳质量模型)
  • Temperature = 0.7(越低,输出应该越详细和可预测)*最大长度 = 246(对于这个简单的任务来说是合理的)
  • 其余控件不一定会影响像这样的简单任务的输出。

Simple ChatGPT request to generate some basic PHP Code

提示源是以下内容:


Coding Langauge: PHP

Generate me a script that can take an array of items and remove any duplicates. The array should contain the following items
- Apple
- Car
- Coffe
- Horse
- Car
- Desk
- Laptop
- A rubber duck

然后我们有它生成的脚本:

<?php 

$items = array("Apple", "Car", "Coffee", "Horse", "Car", "Desk", "Laptop", "A rubber duck"); 

$unique_items = array_unique($items); 

print_r($unique_items); 

?>

对我来说,看起来很不错作为一个能够胜任并且非常了解PHP的人,我可以迅速告诉它我想要什么,然后进行代码审查以确保输出既安全又能达到我的要求。

当然在这个简单的例子中,很容易发现错误或问题。但我们的想法是要快速推广ChatGPT的实用性,并几乎立即生成我们需要的代码块。

设置它并忘记它?

这不是一个能神奇地处理你所有任务、代码编写或日常计划的工具。它确实在很多方面有不足。在代码生成方面,你必须确保输出是合理的,并实际执行输入的任务。这是对有效使用它极为重要的部分,并能提高你的效率;它确实需要监督和审查。

让我们变得复杂

当然,上面这个简单的例子很容易被认为是 "傻瓜",它当然可以做到,并输出一套有效的代码指令。如果我们要求它做一些超越生成代码的事情呢?如果我们提出一个现实世界的问题,要求对代码进行分解,然后做一些个人调整呢?

提示来源:


Coding Langauge: PHP

I need to sort a one column CSV file with 100 records full of IP Address. I need to see which IP address shows up the most and have the code explained to me, please.

进入全屏模式 退出全屏模式

ChatGPT generating more complex PHP code

输出的来源:


The following code can be used to sort a one-column CSV file with 100 records full of IP addresses. 

<?php

//open the CSV file
$file = fopen("file.csv","r");

//initialize an array to store the IP addresses
$ips = array();

//read each line of the file
while(!feof($file)){

    //read the line
    $line = fgets($file);

    //if the line is not blank
    if($line !== ""){

        //add the IP address to the array
        $ips[] = $line;
    }
}

//close the file
fclose($file);

//sort the IP addresses by frequency
$ips_by_frequency = array_count_values($ips);

//sort the IP addresses by frequency in descending order
arsort($ips_by_frequency);

//output the most frequent IP address
echo "The most frequent IP address is: " . key($ips_by_frequency);

?>

The code first opens the CSV file. Then, it initializes an array to store the IP addresses. It then reads each line of the file and adds the IP address to the array. Finally, it sorts the IP addresses by frequency, in descending order, and outputs the most frequent IP address.

很令人印象深刻吧?看起来所有的代码都在那里,只要复制和粘贴,然后......

如果你真的把上面的内容粘贴到一个文件里,看起来会很奇怪或者出错。在PHP打开/关闭标签之外的文字感觉不对。最好的办法是把这些注释包含在开/关标签里面,以注释的形式。

个人的选择,是使用蛇形大写的变量。我更喜欢骆驼大写字母。那么,我们可以如何调整呢?虽然我们最初没有告诉它使用骆驼大写字母,但我们可以简单地在提示源中注入以下内容:

I need camel-case variables.

当然,它也调整了命名规则以使用骆驼大写字母:

Here is some code to sort a one column CSV file with 100 records that contain IP Addresses and count how many times each IP Address appears:

<?php

//Set path to CSV file
$csvFilePath = 'example.csv';

//Open CSV file
$csvFile = fopen($csvFilePath, 'r');

//Initialize a variable to hold counts of IP Addresses
$ipAddressCounts = array();

//Loop through CSV file
while (($line = fgetcsv($csvFile)) !== FALSE)
{
    //Get IP Address from each line of CSV file
    $ipAddress = $line[0];

    //Check if IP Address exists in array
    if(array_key_exists($ipAddress, $ipAddressCounts))
    {
        //Increment count for existing IP Address
        $ipAddressCounts[$ipAddress] += 1;
    }
    else
    {
        //Set count for new IP Address
        $ipAddressCounts[$ipAddress] = 1;
    }
}

//Close CSV file
fclose($csvFile);

//Print out IP Addresses and counts
foreach($ipAddressCounts as $ipAddress => $count)
{
    echo "$ipAddress: $count\n";
}

?>

我们可以看到,这是与以前相当不同的代码,只是基于一个简单的附加句子。上面的代码有点懒,它只是简单地输出所有的IP地址和计数;它并不像我要求的那样只输出重复次数最多的IP地址。虽然是一个额外的个人选择,但它也将开放和关闭的逻辑括号放在新的行中,对于基本的逻辑练习,我并不喜欢。

这是一个很好的例子,我们的指令看起来还不错,但我们现在需要做一些调整。不管怎么说,这让我们相当接近了,而且节省了不少时间。特别是对于我们中速度较慢的编码者来说,更是如此。

最后的思考

我为什么要问这个问题?因为当不可避免的事情发生时,机器会接管我们的工作和世界。撇开所有的玩笑不谈,这些工具并不会很快夺走我们的工作。我们应该学会与它们一起工作,以提高我们自己,提高我们的生产力,并尽可能地依靠它们来增强我们的技能和能力!