代码编写示例
GPT-3 能够编写代码和文本。
以下是code-davinci-002
编写 SQL 查询的示例:
SQL tables (and columns):
* Customers(customer_id, signup_date)
* Streaming(customer_id, video_id, watch_date, watch_minutes)A well-written SQL query that lists customers who signed up during March 2020 and watched more than 50 hours of video in their first 30 days:
```
输出:
SELECT c.customer_id
FROM Customers c
JOIN Streaming s
ON c.customer_id = s.customer_id
WHERE c.signup_date BETWEEN '2020-03-01' AND '2020-03-31'
AND s.watch_date BETWEEN c.signup_date AND DATE_ADD(c.signup_date, INTERVAL 30 DAY)
GROUP BY c.customer_id
HAVING SUM(s.watch_minutes) > 50 * 60
有用的是,code-davinci-002
能够从变量名中进行推断;例如,它推断以watch_minutes
分钟为单位,因此在与 50 小时比较之前需要转换为 60 倍。
为了更容易提示,您也可以尝试text-davinci-003
.
代码解释示例
GPT 对代码的理解可以应用于许多用例,例如:
- 生成代码内文档(例如,Python 文档字符串、git 提交消息)
- 生成代码外文档(例如,手册页)
- 交互式代码探索工具
- 通过自然语言界面将程序结果反馈给用户
例如,如果您想了解 SQL 查询,您可以给出code-davinci-002
以下示例提示:
A SQL query:
```
SELECT c.customer_id
FROM Customers c
JOIN Streaming s
ON c.customer_id = s.customer_id
WHERE c.signup_date BETWEEN '2020-03-01' AND '2020-03-31'
AND s.watch_date BETWEEN c.signup_date AND DATE_ADD(c.signup_date, INTERVAL 30 DAY)
GROUP BY c.customer_id
HAVING SUM(s.watch_minutes) > 50 * 60
```Questions:
1. What does the SQL query do?
2. Why might someone be interested in this time period?
3. Why might a company be interested in this SQL query?Answers:
1. The SQL query finds all customers who signed up in March 2020 and watched more than 50 hours of content in the first 30 days after signing up.
2. The time period is interesting because it is the first month of the COVID-19 pandemic.
3. A company might be interested in this SQL query because it can help them understand how the pandemic has affected their business.
请注意,code-davinci-002
没有接受过遵循说明的训练,因此通常需要示例或其他结构来帮助控制其输出,以及停止序列以停止生成。为了更容易提示,请尝试text-davinci-003
。
代码编辑示例
OpenAI 的编辑端点对于编辑代码特别有用。
与完成不同,编辑需要两个输入:要编辑的文本和指令。
例如,如果您想编辑一个 Python 函数,您可以提供该函数的文本和一条指令,如“添加文档字符串”。
示例文本输入到code-davinci-edit-001
:
def tribonacci(n):if n == 0:return 0elif n == 1:return 1elif n == 2:return 1elif n == 3:return 2else:return tribonacci(n-1) + tribonacci(n-2) + tribonacci(n-3)
示例指令输入:
add a docstring
Add typing, using Python 3.9 conventions
improved the runtime
Add a test.
Translate to JavaScript (or Rust or Lisp or any language you like)
改进运行时并转换为 JavaScript 后的示例输出:
function tribonacci(n) {let a = 0;let b = 1;let c = 1;for (let i = 0; i < n; i++) {[a, b, c] = [b, c, a + b + c];}return a;
}
如您所见,code-davinci-edit-001
能够成功地将函数的运行时间从指数减少到线性,以及从 Python 转换为 JavaScript。
code-davinci-edit-001
在OpenAI Playground中尝试使用代码编辑。