使用ChatGPT提高研发生产力的10个姿势

ChatGPT 在编程方面的能力已经得到了无数开发者的认证,Github 更是将这部分能力移植到了其提供的AI辅助编程工具 Copilot X 中。

而作为普通开发者的我们,又该如何将 ChatGPT 的编程能力融合进我们日常的工作流程中,以更好地提升我们的开发效率呢?

针对这个问题,推特上有名网友就向我们分享了一张图——《面向开发人员的10个超有用的 ChatGPT 提示》。

图中列举了10个 ChatGPT 可以应用到的工作场景,并为每个场景编写了一个 Prompt 示例。

而本文撰写的目的,就是验证将 ChatGPT 应用到这10个工作场景的可行性。

由于笔者从事的是 Android 开发,因此文中的例子大多围绕着 Java/Kotlin 语言展开,但其底层的逻辑与思路是通用的,因而不必担心平台和语言会成为理解路上的障碍。

1

Prompt 1: 遵循代码指南

每家互联网公司内部基本都会制定一些编码规范,但无论是人工审查或还是编写自动化插件,都是一件极其漫长而无聊的事情,而 ChatGPT 编程能力的第1个应用——遵循代码指南,就可以帮我们完成这样一件事情。

原 Prompt 是:

Rewrite the code below following the Google style guidelines for javascript.{enter code}

遵循JavaScript的Google风格指南,重写一下代码:{输入代码}

这个 Prompt 有几个不好的地方:

  1. 没有提供指南的参考来源,无法验证其权威性。

  2. 没有指出不遵循指南的地方,而是直接重写,无法验证其风险性。

  3. 没有提供指南的原文描述,无法验证其准确性。

基于以上几点,我们将这个 Prompt 重写如下:

请参照以下由>分隔的编码规范,对以下由```分隔的代码片段进行审查,并按照下列步骤完成任务:

1.指出代码片段里不符合编码规范要求的地方

2.提供编码规范里对应的原文描述

3.提供符合编码规范要求的修改后的代码

>

1.总是使用不可变集合接口(Collection, List, Set, Map)来声明无需改变的集合。使用工厂函数创建集合实例时,尽可能选用返回不可变集合类型的函数:

2.对于由单个表达式构成的函数体,优先使用表达式形式。

3.使用 until 函数在一个开区间上循环

>

```

val allowedValues = arrayListOf("a", "b", "c")

fun foo(): Int { 
    return 1 
}

for (i in 0..n - 1) { /*……*/ 

```

ChatGPT的回答如下:

 

可以看到,ChatGPT 参照规范为我们准确地指正了错误、提供了原文以及修复了代码,本场景验证通过!

2

Prompt 2: 编写代码测试

有效的单元测试可以验证应用中特定代码的逻辑是否正确,但由于人思维的局限性,有时候我们编写的测试用例仍无法覆盖比较边界的情况。幸运的是,只需提供少量的样本提示,ChatGPT就能很容易地进行模仿和扩写,由此便引申出了ChatGPT的第2个应用——编写代码测试。

原 Prompt 是:

Write test cases for the main edge cases that could happen to the below code snippet.First outline the test cases you'll write.Second, write the test cases in javascript using the Jest framework.{enter code}

为以下代码片段可能发生的主要边缘情况编写测试用例。首先概述您将编写的测试用例。其次,使用 Jest 框架在 javascript 中编写测试用例。{输入

这个 Prompt 有个不好的地方,就是它在描述测试用例的编写要求时过于笼统。合理的做法应该是提供少量的样本提示,并且描述测试用例的应用场景,才能让ChatGPT更有针对性地输出。

基于这点,我们将这个 Prompt 重写如下:

请参照以下由```分隔的代码示例,编写一个至少包含20个测试方法的 JUnit 4 测试类,以模拟用户输入不同邮箱内容后 isValidEmail() 方法的验证情况:

```

 
class EmailValidatorTest {

    @Test
    fun emailValidator_CorrectEmailSimple_ReturnsTrue() {
        assertTrue(EmailValidator.isValidEmail("name@email.com"))
    }
}

```

ChatGPT的回答如下:

 

可以看到,ChatGPT参照样本为我们准确地生成了符合场景要求的测试方法集,模拟了不同的用户输入,本场景验证通过!

3

Prompt 3: 编写代码注释

命名准确、实现优雅、逻辑清晰的代码都是有自解释的效果的,但一个团队里的成员水平不一,我们无法要求每个成员都能写出这样的代码,因此添加适当的注释是必要的,这项工作虽简单但无趣,完全可以交给 ChatGPT 编程能力的第3个应用——编写代码注释来为我们代劳。

原 Prompt 是:

Regenerate the code snippet below, but please include comments to each line of code {enter code}

重新生成下面的代码片段,但请在每行代码中添加注释。{输入代码}

这个 Prompt 有个不好的地方,它要求在每一行的代码中添加注释,实际上完全没有必要,过多的注释只会增加阅读的时间成本。

我们完全可以要求它在遵循既有注释规范的基础上,仅在必要的地方添加注释,基于这点,我们将这个 Prompt 修改如下:

请参照以下由>分隔的注释规范,对以下由```分隔的代码片段进行审查,并在注释规范要求的地方补上中文注释:

>

1.对于所有的方法,都需要使用 Javadoc 注释。注释内容除了包含返回值、参数和异常说明外,还需要指出该方法的作用或实现的功能。

2.对于所有的类,都需要添加创建者和创建日期的注释。

>

```

 
public abstract class TypeConverter<T, V> {
    private Class<T> mFromClass;
    private Class<V> mToClass;

    public TypeConverter(Class<T> fromClass, Class<V> toClass) {
        mFromClass = fromClass;
        mToClass = toClass;
    }

    Class<V> getTargetType() {
        return mToClass;
    }

    Class<T> getSourceType() {
        return mFromClass;
    }

    public abstract V convert(T value);
}

```

ChatGPT的回答如下:

可以看到,ChatGPT 参照规范为我们准确地添加了方法注释和类注释,对于未要求的属性注释则没有添加,本场景验证通过!

4

Prompt 4: 编写应用程序

ChatGPT 为一个完全不懂编程的小白开发出一款完整应用程序的新闻早已屡见不鲜,我们要做的仅仅是理清我们的需求,并罗列好步骤交给它,这里展现的就是 ChatGPT 编程能力的第4个应用——编写应用程序。

原 Prompt 是:

I will provide some specfic information about web app requirements, and it will be you job to develop an architecture and code for developing a secure app with Golang and Angular.{enter web app requirements}

我将提供有关 Web 应用程序需求的一些具体信息,你的工作是开发一个架构并使用 Golang 和 Angular 编写代码,以开发一个安全的应用程序。{输入 Web 应用程序需求}

编写一个完整应用程序的步骤太多,我们不好演示,但是编写一个简单的功能模块还是绰绰有余的。基于此,我们将这个 Prompt 重写如下:

请按照以下由>分隔的具体步骤,提供Android平台对应的完整Kotlin代码实现,包括Activity文件、布局文件以及AndroidManifest.xml文件,以完成一个“查看手机相片”的需求。

过程中要求:

1.提示我要在哪个目录创建哪个文件;

2.不使用任何第三方框架;

3.在必要的地方添加中文注释;

>

1.页面A提供一个按钮,点击可以打开Android系统的文件浏览器,并选择系统相册中的某一张图片;

2.在用户选中了某张图片后,跳转到页面B以全屏预览该图片

>

ChatGPT的回答如下:

 

输出过程中 ChatGPT 可能会因为单次回答超过了字数限制而中断,我们只需要提示它继续就可以了。

之后,我们将ChatGPT回答中的代码复制到新建的Android工程里,然后尝试运行如下:

可以看到,ChatGPT按照步骤为我们完整地生成了符合要求的、可运行的代码,本场景验证通过!

5

Prompt 5: 执行查询

客户端的主要职责,是展示UI以及与用户交互,与后端开发相比,其与数据库直接打交道的机会较少。这也造成了大部分客户端开发仅会基本的增删改查,而对于Join、索引、子查询等更加高级的数据库特性并不擅长。但现在,有了 ChatGPT 编程能力的第5个应用——执行查询后,这项工作就完全可以由ChatGPT代劳了。

原 Prompt 是:

The database contains tables named "Products”. "Users". "Orders" and "Suppliers." I will type queries, and you will reply with what the terminal shows.I want you to reply with a table of query results in a single code block.

数据库包含名为“Products”、“Users”、“Orders”和“Suppliers”的表。我将输入查询,你将使用终端显示的内容进行回复。我希望您在单个代码块中回复查询结果表。

这个 Prompt 写得有点奇怪,按字面意思,它是想让 ChatGPT 执行查询并返回一张结果表。且不说各种数据库图形界面本身就可以支持可视化查询,而如果不是以插件形式支持的话,还得向 ChatGPT 提供完整的数据库数据,操作起来相当麻烦。

基于此,我们还是回到让 ChatGPT 为我们撰写数据库操作语句的场景,将这个 Prompt 重写如下:

请参照以下由```分隔的几个实体类,每个实体对应数据库中的每一个表,实体的每个实例对应表中的每一行数据,实体的每个属性都对应表中的每一列的字段。

现在我需要查询所有歌曲的歌名以及每个歌曲的对应的播放列表名和每个播放列表对应的歌手名,请为我编写一个SQLite查询语句:

```

 
@Entity
data class Artist(
    @PrimaryKey val artist_id: Long,
    val artist_name: String,
    val artist_age: Int
)

@Entity
data class Playlist(
    @PrimaryKey val playlist_id: Long,
    val artist_id: Long,
    val playlist_name: String
)

@Entity
data class Song(
    @PrimaryKey val song_id: Long,
    val song_name: String,
    val playlist_id: Long,
)

```

ChatGPT的回答如下:

 

我们将ChatGPT回答中的查询语句复制到数据库图形操作界面,然后尝试运行如下:

可以看到,ChatGPT按照要求为我们准确地返回了查询结果,本场景验证通过!

6

Prompt 6: Git命令生成器

不得不承认,支持图形界面操作的Git版本管理系统确实有一种魔力,但在享受其便利性的同时,具体的Git命令也在被我们迅速遗忘,这也导致了一旦我们转换到一个新的IDE后,就会完全不知道从何下手。但现在,有了 ChatGPT 编程能力的第6个应用——Git命令生成器后,这种烦恼就不复存在了。

原 Prompt 是:

I want you to act like a Git commands generator. I'll explain to you what I need you to do and you will provide me with the right Git command. My first requirement is. {I want to push the example.txt file to the branch name example-branch}

我希望你像 Git 命令生成器一样工作。我会向您解释我需要您做什么,您会为我提供正确的 Git 命令。我的第一个要求是。{我要推送example.txt文件到分支名example-branch}

这个 Prompt 本身倒是没有什么问题,只是“推送XX文件到XX分支”这个要求容易让 ChatGPT 以为你只是想执行“push”这个操作,我们理应写的更详细一点。为此,我们将这个 Prompt 重写如下:

请按照以下由>分隔的要求描述,为我提供正确的 Git 命令:

>

将example.txt文件添加到Git仓库并推送到example-branch分支

>

ChatGPT的回答如下:

可以看到,在更为详细的要求描述下,ChatGPT为我们生成了正确的Git命令,本场景验证通过!

7

Prompt 7: 操作指南

本质上,操作指南是给用户或技术新手看的一类文档,是将包含很多专业术语的技术文档转换为更加接近自然语言的步骤指引或问题解答,以帮助读者快速上手开发或解决实际问题。但如何从既有的技术思维上抽离,并切换到用户或技术新手的视角去考虑操作指南怎么写是一个问题,这个时候,ChatGPT 编程能力的第7个应用——操作指南就可以帮上忙了。

原 Prompt 是:

I will provide you with basic steps of an app functionality {enter steps} and you will come up with an engaging article on how to do those basic steps.

我将为您提供应用程序功能的基本步骤{输入步骤},然后您将撰写一篇引人入胜的文章,介绍如何执行这些基本步骤。

这个 Prompt 本身没有什么太大的问题,但是形式上我认为可以改进一下,相比起较为死板的文档,我认为以问答机器人的形式展现会更直观和高效一点,为此,我们将这个 Prompt 重写如下:

你将扮演一个FAQ问答机器人,根据以下由>分隔的文档所提供的内容,为用户提出的问题提供一份简单而易上手的操作指南。你需要先向用户问好,询问用户有什么问题,然后等待用户输入,在这个过程中你必须遵循的要求是:

1.仅能以提供的文档为参考源,而不能以其他地方的资料为参考源;

2.如果在提供的文档中没有找到解决方案,请诚实告知用户,不能捏造答案。

>

Camera intents

To perform basic camera actions like capturing a photo or video using the device's default camera application, you do not need to integrate with a Camera library. Instead, use an Intent. Take a photo with a camera app

Android delegates actions to other applications by invoking an Intent. This process involves three pieces: the Intent itself, a call to start the external Activity, and some code to handle the image data when focus returns to your activity.

Here's a function that invokes an Intent to capture a photo.

 
val REQUEST_IMAGE_CAPTURE = 1

private fun dispatchTakePictureIntent() {
    val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    try {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
    } catch (e: ActivityNotFoundException) {
        // display error state to the user
    }
}

Record a video with a camera app

You can also invoke an Intent to capture a video.

 
val REQUEST_VIDEO_CAPTURE = 1

private fun dispatchTakeVideoIntent() {
    Intent(MediaStore.ACTION_VIDEO_CAPTURE).also { takeVideoIntent ->
        takeVideoIntent.resolveActivity(packageManager)?.also {
            startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE)
        } ?: run {
          //display error state to the user
        }
    }
}

The startActivityForResult() method is protected by a condition that calls resolveActivity(), which returns the first activity component that can handle the Intent. Perform this check to ensure that you are invoking an Intent that won't crash your app.

>

ChatGPT的回答如下:

 

可以看到,经过我们的前期设定,ChatGPT回答中的代码基本参考自我们提供的文档,而下文的内容也是基于上面代码的解释,算是没有超纲。

然而,即便我们特别要求了,ChatGPT 有时候仍然会无法清醒认知自己的知识边界,从而出现了令人头疼的“幻觉”现象,在面对我们文档中没有提及到的问题时,它会给出它自有训练数据得出的答案:

这个现象,在基于 ChatGPT 4 的 New Bing 身上则没有出现:

所以,就操作指南这一场景的应用来讲,也算能勉强通过~

8

Prompt 8: 生成ReadMe文件

 

程序员有两件讨厌的事情:写文档,以及别人不写文档。而现在,有了 ChatGPT 编程能力的第8个应用——生成文档后,就可以帮程序员一次性解决这两件讨厌的事情了。

原 Prompt 是:

Generate documentation for the code below. You should include detailed instructions to allow a developer to run it on a local machine, explain what the code does, and list vulnerabilities that exist in this code, {enter code}

为下面的代码生成文档。您应该包括详细说明以允许开发人员在本地计算机上运行它,解释代码的作用,并列出此代码中存在的漏洞,{输入代码}

这个 Prompt 有个不好的地方,就是一般类似文档一类的东西,我们会要求其结构尽量清晰分明,以提升其易读性,而这个 Prompt 并未要求 ChatGPT 结构化输出文档。

提前向 ChatGPT 指定要输出的文档结构以及输出的文档形式,是一个比较好的做法,为此,我们将这个 Prompt 重写如下:

请参照以下由>分隔的文档结构,为以下由```分隔的代码生成Markdown形式的中文文档:

>

1.类名

 

a.类介绍

2.公开方法概要(表格形式,第一列为返回值,第二列为方法名及一句话描述)

3.公开方法

 

a.方法名

b.方法签名(包含访问范围、返回值、参数)

c.方法介绍

>

```

 
/**
 * This class gives access to system locale services. These services allow applications to control
 * granular locale settings (such as per-app locales).
 *
 * <p> Third party applications should treat this as a write-side surface, and continue reading
 * locales via their in-process {@link LocaleList}s.
 */
@SystemService(Context.LOCALE_SERVICE)
public class LocaleManager {
    private static final String TAG = "LocaleManager";

    /** Context required for getting the user for which API calls are made. */
    private Context mContext;
    private ILocaleManager mService;

    /** @hide Instantiated by ContextImpl */
    public LocaleManager(Context context, ILocaleManager service) {
        mContext = context;
        mService = service;
    }

    /**
     * Sets the UI locales for the calling app.
     *
     * <p>Pass a {@link LocaleList#getEmptyLocaleList()} to reset to the system locale.
     *
     * <p><b>Note:</b> Changes to app locales will result in a configuration change (and potentially
     * an Activity lifecycle event) being applied to the calling application. For more information,
     * see the <a
     * href="https://developer.android.com/guide/topics/resources/runtime-changes">section on
     * handling configuration changes</a>. The set locales are persisted; they are backed up if the
     * user has enabled Backup & Restore.
     *
     * <p><b>Note:</b> Users' locale preferences are passed to applications by creating a union of
     * any app-specific locales and system locales, with the app-specific locales appearing first.
     * Language resources are then chosen per usual (as described in the <a
     * href="https://developer.android.com/guide/topics/resources/multilingual-support">section on
     * locale resolution</a>).
     *
     * @param locales the desired locales for the calling app.
     */
    @UserHandleAware
    public void setApplicationLocales(@NonNull LocaleList locales) {
        setApplicationLocales(mContext.getPackageName(), locales);
    }

    /**
     * Sets the UI locales for a specified app (described by package name).
     *
     * <p>Pass a {@link LocaleList#getEmptyLocaleList()} to reset to the system locale.
     *
     * <p><b>Note:</b> Changes to app locales will result in a configuration change (and potentially
     * an Activity lifecycle event) being applied to the specified application. For more
     * information, see the <a
     * href="https://developer.android.com/guide/topics/resources/runtime-changes">section on
     * handling configuration changes</a>. The set locales are persisted; they are backed up if the
     * user has enabled Backup & Restore.
     *
     * <p><b>Note:</b> Users' locale preferences are passed to applications by creating a union of
     * any app-specific locales and system locales, with the app-specific locales appearing first.
     * Language resources are then chosen per usual (as described in the <a
     * href="https://developer.android.com/guide/topics/resources/multilingual-support">section on
     * locale resolution</a>).
     *
     * @param appPackageName the package name of the app for which to set the locales.
     * @param locales the desired locales for the specified app.
     * @hide
     */
    @SystemApi
    @RequiresPermission(Manifest.permission.CHANGE_CONFIGURATION)
    @UserHandleAware
    public void setApplicationLocales(@NonNull String appPackageName, @NonNull LocaleList locales) {
        try {
            mService.setApplicationLocales(appPackageName, mContext.getUser().getIdentifier(),
                    locales);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Returns the UI locales for the calling app.
     *
     * <p>Returns a {@link LocaleList#getEmptyLocaleList()} if no app-specific locales are set.
     */
    @UserHandleAware
    @NonNull
    public LocaleList getApplicationLocales() {
        return getApplicationLocales(mContext.getPackageName());
    }

    /**
     * Returns the current UI locales for a specified app (described by package name).
     *
     * <p>Returns a {@link LocaleList#getEmptyLocaleList()} if no app-specific locales are set.
     *
     * <p>This API can be used by an app's installer
     * (per {@link android.content.pm.InstallSourceInfo#getInstallingPackageName}) to retrieve
     * the app's locales.
     * All other cases require {@code android.Manifest.permission#READ_APP_SPECIFIC_LOCALES}.
     * Apps should generally retrieve their own locales via their in-process LocaleLists,
     * or by calling {@link #getApplicationLocales()}.
     *
     * @param appPackageName the package name of the app for which to retrieve the locales.
     */
    @RequiresPermission(value = Manifest.permission.READ_APP_SPECIFIC_LOCALES, conditional = true)
    @UserHandleAware
    @NonNull
    public LocaleList getApplicationLocales(@NonNull String appPackageName) {
        try {
            return mService.getApplicationLocales(appPackageName, mContext.getUser()
                    .getIdentifier());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Returns the current system locales, ignoring app-specific overrides.
     *
     * <p><b>Note:</b> Apps should generally access the user's locale preferences as indicated in
     * their in-process {@link LocaleList}s. However, in case an app-specific locale is set, this
     * method helps cater to rare use-cases which might require specifically knowing the system
     * locale.
     *
     * <p><b>Note:</b> This API is not user-aware. It returns the system locales for the foreground
     * user.
     */
    @NonNull
    public LocaleList getSystemLocales() {
        try {
            return mService.getSystemLocales();
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Sets the current system locales to the provided value.
     *
     * @hide
     */
    @TestApi
    public void setSystemLocales(@NonNull LocaleList locales) {
        try {
            Configuration conf = ActivityManager.getService().getConfiguration();
            conf.setLocales(locales);
            ActivityManager.getService().updatePersistentConfiguration(conf);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

}

```

ChatGPT 的回答如下:

可以看到,ChatGPT根据我们提供的文档结构,为我们生成了相对来说结构更清晰、可读性更强的API文档,本场景验证通过!

9

Prompt 9: 代码转换器

ChatGPT这种大型语言模型非常擅长将其输入转换为不同的格式,不同开发语言之间的转换自然也不在话下,于是便延伸出了ChatGPT 编程能力的第9个应用——代码转换器。

原 Prompt 是:

Translate this code from JavaScript to Python {Enter code}

将这段代码从 JavaScript 翻译成 Python {Enter code}

在Android中构建UI有两种方式,以XML标签为代表的静态布局,和以Java API 为代表的动态布局。前者常用于设置默认的布局样式,后者常用于运行时布局的动态调整。

如果想减少对多文件的管理,就需要将XML标签转为Java代码;而如果想精简视图类的Java代码,就需要将部分Java代码转为XML标签。

这种转换常常需要手动地重写,代码量一多的话简直是噩梦。现在,我们尝试把这项工作交给 ChatGPT,为此,我们将这个 Prompt 重写如下:

请将以下这个基于Android平台的XML布局文件(由```分隔)转换为Java代码实现,要求:

1.导入基于AndroidX的相关的类

2.以Activity类型为上下文

3.不同类型的视图用空行隔开并补上注释说明

```

 
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FirstFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp">

        <Button
            android:id="@+id/button_first"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/next"
            app:layout_constraintBottom_toTopOf="@id/textview_first"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textview_first"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="@string/lorem_ipsum"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/button_first" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

```

ChatGPT 的回答如下:

之后,我们将ChatGPT回答中的代码复制到Activity文件里,然后尝试运行如下:

可以看到,ChatGPT 按照要求为我们准确地将XML文件转换成了可运行的Java代码,本场景验证通过!

10

Prompt 10: 代码解释器

接手一份完全没有任何文档和注释的离职同事的代码,是每个程序员的至暗时刻之一,因为我们并不能保证每次都有足够的时间和精力来逐行阅读和理解代码,而 ChatGPT 编程能力的第10个应用——代码解释器正好可以帮我们解决这种困扰。

原 Prompt 是:

What will following code snippet do {Enter code}

以下代码片段将做什么{输入代码}

这里有一个问题,就是ChatGPT限制了单次输入的字数,而我们的代码片段经常动辄就是几百上千行,为了保证 ChatGPT 能够处理完整的代码片段,我们需要先和 ChatGPT 达成一个约定,要求其必须在我们确认输入完毕之后,才开始进行处理工作。

基于这点,我们将这个 Prompt 重写如下:

我将发送一个代码片段,该代码片段可能过长,超出了字数限制,我会分割成多次发送。现要求你在收到“代码发送结束”这一指令前之前请勿开始处理,而是继续等待,直到我发出了“代码发送结束”的指令之后,你再统一处理这多次发送的代码片段,并解释一下这个代码片段的含义。

ChatGPT的回答如下:

但有时候,即便我们这么要求了,而 ChatGPT 也这么回应了,在 ChatGPT 3.5 上它仍然会抽风地在我们发送中间部分的内容时就开始处理了(在基于ChatGPT 4.0的 New Bing 上则是正常的)。

这个时候我们不用管它,直接点击停止生成按钮,然后继续发送其余部分,并在最后发出“代码发送结束”的指令,ChatGPT 就会按我们最初的要求,统一处理这多次发送的代码片段并解释其含义。

可以看到,ChatGPT 对于我们提供的 TypeConverter 这个类的解释基本正确,本场景验证通过!

11

写在最后

最后说明一下,考虑到目前大部分人很难拿到 ChatGPT Plus 的账号,因此本文是基于现有的 ChatGPT 3.5 进行验证的,但由于 ChatGPT 3.5 是 无法访问互联网的,因此文中需要引用的文档都是节选其中的一段放到问题里的。

而到了ChatGPT 4之后,由于可以通过插件来访问互联网了,也就可以不用像文中那么麻烦了,直接丢给它一个链接,让它基于这个链接的内容回答问题就好了。同时 ChatGPT 4 对于我们发出的指令,也能更好地理解与遵从,而不是像 ChatGPT 3.5 一样一身反骨。

另外一件事情就是,开头就说了,本文的主要目的是验证将 ChatGPT 应用到这10个工作场景的可行性,而不是提供10个完美的 Prompt。即便你用了我文中重写过的 Prompt,也可能输出和我并不完全一致的结果,关于这点,ChatGPT 自身的解释如下:

如果是以 API 的形式访问 ChatGPT,尚可通过调节 tempreture 参数来让结果输出较为固定,而直接在 Web 应用程序上对话的形式,我尚未研究出有效的方案,如果你知道,还请在评论区告知我,谢谢~

总体来说,目前市面上绝大多数的所谓AI应用,都是对现有AI大模型的一个封装,扮演的其实就是类似Prompt工程师的角色。所以,如何写好 Prompt 确实是一项基础但很重要的事情,掌握这个能力后,只要你有想法,就可以挖掘出比以上10个更丰富的场景。

 

转自:使用ChatGPT提高研发生产力的10个姿势!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值