🚀 在我们的新课程中掌握提示工程和构建 AI Agent!使用 PROMPTING20 享 8 折优惠 ➜ 立即注册
生成代码

生成代码

⚠️

本节正在开发中。

像 ChatGPT 这样的 LLM 在代码生成方面非常有效。在本节中,我们将介绍许多如何使用 ChatGPT 进行代码生成的示例。

以下所有示例均使用 OpenAI 的 Playground(聊天模式)和 gpt-3.5-turbo 模型。

与所有 OpenAI 聊天模型一样,您可以使用 System Message 来定义响应的行为和格式。我们将在提示示例中使用以下 System Message

You are a helpful code assistant that can teach a junior developer how to code. Your language of choice is Python. Don't explain the code, just generate the code block itself.

基本示例

User Message 将是提示本身,包含您希望模型生成的特定代码的指令。下面是第一个用于代码生成的基本提示的快照。请注意,最终代码是由 Assistant 生成的。

CODE1

如果您想自己尝试,提示如下

Write code that asks the user for their name and say "Hello"

将注释转为代码

您还可以利用这些 LLM 的代码生成能力,仅从注释生成代码。我们来看另一个将指令作为注释块传递的示例

提示

"""
1. Create a list of movies
2. Create a list of ratings for these movies
3. Combine them to make a json object of 10 movies with their ratings.
"""

输出

movies = ["The Shawshank Redemption", "The Godfather", "The Dark Knight", "Schindler's List", "Pulp Fiction", "The Lord of the Rings: The Return of the King", "Forrest Gump", "Star Wars: Episode V - The Empire Strikes Back", "Inception", "The Silence of the Lambs"]
 
ratings = [9.3, 9.2, 9.0, 8.9, 8.9, 8.9, 8.8, 8.7, 8.7, 8.6]
 
movie_ratings = {}
 
for i in range(len(movies)):
    movie_ratings[movies[i]] = ratings[i]
 
json_object = json.dumps(movie_ratings, indent=4)
 
print(json_object)

生成的代码看起来像是可用的。但是,输出中缺少一个重要的 import 语句,即 import json。虽然这些模型能生成可用代码,但您必须密切关注这些小细节,并务必测试您的代码。

完成函数或下一行

这些 LLM 也被整合到 GitHub Copilot 等工具中,这对开发者很有用。其中一个有用的功能是模型能够完成函数。

提示

# function to multiply two numbers and add 75 to it
 
def multiply(

输出

a, b):
    result = a * b
    result += 75
    return result

MySQL 查询生成

除了上面的基本代码生成示例之外,您还可以使用该模型生成有用的代码,这些代码可用于编程的其他方面,例如创建和测试 MySQL 查询。

假设您有一个包含一些信息的数据集,您可以将其作为提示的一部分,然后指示模型生成特定的查询。例如

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

输出

SELECT students.StudentId, students.StudentName
FROM students
INNER JOIN departments
ON students.DepartmentId = departments.DepartmentId
WHERE departments.DepartmentName = 'Computer Science';

模型生成了一个查询!但是,在这个示例中,我们是虚构的数据库,所以没有数据库来测试生成的查询。如果您正在测试这个示例,创建这个数据库可能会很有用。幸运的是,您也可以使用 ChatGPT 来生成它。一种方法是使用上述提示的修改版本,略微修改指令来生成数据库模式

Table departments, columns = [DepartmentId, DepartmentName]
Table students, columns = [DepartmentId, StudentId, StudentName]
Create a valid database schema with the above tables and columns

模型生成了以下模式

CREATE TABLE departments (
  DepartmentId INT PRIMARY KEY,
  DepartmentName VARCHAR(50)
);

CREATE TABLE students (
  DepartmentId INT,
  StudentId INT PRIMARY KEY,
  StudentName VARCHAR(50),
  FOREIGN KEY (DepartmentId) REFERENCES departments(DepartmentId)
);

您可以使用 sqliteonline.com (在新标签页中打开) 之类的工具创建临时数据库。

您还需要一些虚拟数据来测试原始生成的查询是否有效。我们也用 ChatGPT 来做吧!提示可能如下所示

CREATE TABLE departments (
  DepartmentId INT PRIMARY KEY,
  DepartmentName VARCHAR(50)
);

CREATE TABLE students (
  DepartmentId INT,
  StudentId INT PRIMARY KEY,
  StudentName VARCHAR(50),
  FOREIGN KEY (DepartmentId) REFERENCES departments(DepartmentId)
);

Given the database schema above, generate valid insert statements include 4 rows for each table.

带有 INSERT 语句的输出

INSERT INTO departments (DepartmentId, DepartmentName)
VALUES (1, 'Computer Science'), (2, 'Mathematics'), (3, 'Physics'), (4, 'Biology');

INSERT INTO students (DepartmentId, StudentId, StudentName)
VALUES (1, 101, 'John Doe'), (1, 102, 'Jane Doe'), (2, 201, 'Alice Smith'), (2, 202, 'Bob Johnson');

现在您有了数据库和虚拟数据,可以测试原始查询了。根据虚拟数据,结果应该有两行,分别是 Jane DoeJohn Doe

解释代码

如果您正在学习某种编程语言,提示模型解释某些代码片段可能会很有用。让我们重用上面生成的查询,并要求模型解释它。

如果您使用的是之前的 System Message,请注意您指示模型的方式。例如,如果我们使用以下示例,模型会抱怨它是 Python 代码助手,但它仍然提供了解释。有时模型会拒绝生成您指示的内容,因为它被设计为遵循 System Message。

提示

SELECT students.StudentId, students.StudentName
FROM students
INNER JOIN departments
ON students.DepartmentId = departments.DepartmentId
WHERE departments.DepartmentName = 'Computer Science';

Explain the above SQL statement.

输出

Sorry, as a code assistant in Python, I cannot generate SQL code. However, I can explain the purpose of the SQL statement provided. 

This SQL statement selects the `StudentId` and `StudentName` columns from the `students` table, where the `DepartmentName` column in the `departments` table is equal to 'Computer Science'. To achieve this, the statement performs an inner join on the `DepartmentId` column between the `students` and `departments` tables. 

In simpler terms, it is selecting the names and IDs of students who are enrolled in the Computer Science department.

编辑代码

示例即将推出!

调试代码

示例即将推出!

最佳实践

即将推出!