> For the complete documentation index, see [llms.txt](https://wiki.aig123.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.aig123.com/chatgpt-tutorial/ji-chu-pian/ji-ben-shi-yong-chang-jing-shi-yong-ji-qiao/chang-jing-4-wu-zhong-sheng-you-xie-dai-ma.md).

# 场景4：无中生有——写代码

### 场景介绍[​](https://learningprompt.wiki/docs/chatGPT/tutorial-basics/%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF%20&%20%E4%BD%BF%E7%94%A8%E6%8A%80%E5%B7%A7/%E5%9C%BA%E6%99%AF4%EF%BC%9A%E6%97%A0%E4%B8%AD%E7%94%9F%E6%9C%89%E2%80%94%E2%80%94%E5%86%99%E4%BB%A3%E7%A0%81#%E5%9C%BA%E6%99%AF%E4%BB%8B%E7%BB%8D) <a href="#chang-jing-jie-shao" id="chang-jing-jie-shao"></a>

除了回答问题外，另一个重要的场景是让 AI 根据你的要求完成一些内容生成任务，根据输出的结果差异，我将其概括为以下几个主要场景：

1. 无中生有
2. 锦上添花
3. 化繁为简

本章，我们先来聊聊「无中生有」场景。顾名思义，就是让 AI 给你生成一些内容。你只需要向 AI 描述你想写的内容，AI 就会按你的要求写出该内容。比如：

1. 撰写招聘信息
2. 撰写电商平台的货物描述
3. 撰写短视频脚本
4. 甚至让它写代码都可以

像撰写招聘信息等，你只需要明确目标，将目标定得比较明确即可生成能让你较为满意的答案。本章我想聊下写代码。下面是一个让 ChatGPT 写代码的案例。

Prompt：

```
Create a MySQL query for all students in the Computer Science Department:
Table departments, columns = [DepartmentId, DepartmentName]
Table students, columns = [DepartmentId, StudentId, StudentName]
```

Output：

```
SELECT students.*
FROM students
INNER JOIN departments
ON students.DepartmentId = departments.DepartmentId
WHERE departments.DepartmentName = 'Computer Science';
```

### **技巧 3：使用引导词，引导模型输出特定语内容**[​](https://learningprompt.wiki/docs/chatGPT/tutorial-basics/%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF%20&%20%E4%BD%BF%E7%94%A8%E6%8A%80%E5%B7%A7/%E5%9C%BA%E6%99%AF4%EF%BC%9A%E6%97%A0%E4%B8%AD%E7%94%9F%E6%9C%89%E2%80%94%E2%80%94%E5%86%99%E4%BB%A3%E7%A0%81#%E6%8A%80%E5%B7%A7-3%E4%BD%BF%E7%94%A8%E5%BC%95%E5%AF%BC%E8%AF%8D%E5%BC%95%E5%AF%BC%E6%A8%A1%E5%9E%8B%E8%BE%93%E5%87%BA%E7%89%B9%E5%AE%9A%E8%AF%AD%E5%86%85%E5%AE%B9) <a href="#ji-qiao-3-shi-yong-yin-dao-ci-yin-dao-mo-xing-shu-chu-te-ding-yu-nei-rong" id="ji-qiao-3-shi-yong-yin-dao-ci-yin-dao-mo-xing-shu-chu-te-ding-yu-nei-rong"></a>

在代码生成场景里，有一个小技巧，上面提到的案例，其 prompt 还可以继续优化，在 prompt 最后，增加一个代码的引导，告知 AI 我已经将条件描述完了，你可以写代码了。

在 prompt 的最后增加 SELECT 可以很好地提示 AI 可以写 SQL 代码了。Better prompt：

```
Create a MySQL query for all students in the Computer Science Department:
Table departments, columns = [DepartmentId, DepartmentName]
Table students, columns = [DepartmentId, StudentId, StudentName]
SELECT
```

同样的道理，如果你想让 AI 写 Python 代码，那 import 会是比较好的提示。但需要注意，这个只是告知 AI 可以写代码了，并不能告知 AI 写何种代码，你仍然需要在 prompt 里增加提示，告知 AI 要生成何种语言的代码。

在吴恩达的 ChatGPT Prompt Engineering [课程](https://www.deeplearning.ai/short-courses/chatgpt-prompt-engineering-for-developers/)中，也提到这个技巧，只是在课程中，引导词并不是放在最后，而是在 prompt 里直接说明，让 AI 生成一个 JSON 格式的内容。课程中的例子是这样的（注意这个是 python 代码）：

```
prompt = f"""
Generate a list of three made-up book titles along \
with their authors and genres.
Provide them in JSON format with the following keys:
book_id, title, author, genre.
"""
```

我简单解释下，其关键是在 prompt 里跟 AI 说明，需要 AI 按照 JSON 格式输出内容。
