forked from openai/openai-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request openai#85 from openai/ted/various-updates-and-addi…
…tions Ted/various updates and additions
- Loading branch information
Showing
13 changed files
with
11,105 additions
and
645 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Code editing example | ||
|
||
OpenAI's [edits](https://openai.com/blog/gpt-3-edit-insert/) endpoint is particularly useful for editing code. | ||
|
||
Unlike completions, edits takes two inputs: the text to edit and an instruction. | ||
|
||
For example, if you wanted to edit a Python function, you could supply the text of the function and an instruction like "add a docstring". | ||
|
||
Example text input to `code-davinci-edit-001`: | ||
|
||
```python | ||
def tribonacci(n): | ||
if n == 0: | ||
return 0 | ||
elif n == 1: | ||
return 1 | ||
elif n == 2: | ||
return 1 | ||
elif n == 3: | ||
return 2 | ||
else: | ||
return tribonacci(n-1) + tribonacci(n-2) + tribonacci(n-3) | ||
``` | ||
|
||
Example instruction inputs: | ||
|
||
```text | ||
add a docstring | ||
``` | ||
|
||
```text | ||
Add typing, using Python 3.9 conventions | ||
``` | ||
|
||
```text | ||
improved the runtime | ||
``` | ||
|
||
```text | ||
Add a test. | ||
``` | ||
|
||
```text | ||
Translate to JavaScript (or Rust or Lisp or any language you like) | ||
``` | ||
|
||
Example output after improving the runtime and translating to JavaScript: | ||
|
||
```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; | ||
} | ||
``` | ||
|
||
As you can see, `code-davinci-edit-001` was able to successfully reduce the function's runtime from exponential down to linear, as well as convert from Python to JavaScript. | ||
|
||
Experiment with code editing using `code-davinci-edit-001` in the [OpenAI Playground](https://beta.openai.com/playground?mode=edit&model=code-davinci-edit-001). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Code explanation examples | ||
|
||
GPT's understanding of code can be applied to many use cases, e.g.: | ||
|
||
* Generating in-code documentation (e.g., Python docstrings, git commit messages) | ||
* Generating out-of-code documentation (e.g., man pages) | ||
* An interactive code exploration tool | ||
* Communicating program results back to users via a natural language interface | ||
|
||
For example, if you wanted to understand a SQL query, you could give `code-davinci-002` the following example prompt: | ||
|
||
````text | ||
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: | ||
```` | ||
|
||
[Output]((https://beta.openai.com/playground/p/Sv1VQKbJV1TZKmiTK9r6nlj3)): | ||
|
||
```text | ||
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. | ||
``` | ||
|
||
Note that `code-davinci-002` is not trained to follow instructions and therefore usually needs examples or other structure to help steer its output, as well as stop sequences to stop generating. For easier prompting, try `text-davinci-003`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Code writing examples | ||
|
||
GPT-3 is able to write code as well as text. | ||
|
||
Here's an example of `code-davinci-002` writing a SQL query: | ||
|
||
````text | ||
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: | ||
``` | ||
```` | ||
|
||
[Output](https://beta.openai.com/playground/p/r2mw99cANoa0TJHok725CeaC): | ||
|
||
```sql | ||
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 | ||
``` | ||
|
||
Helpfully, `code-davinci-002` is able to make inferences from variable names; for example, it infers that `watch_minutes` has units of minutes and therefore needs to be converted by a factor of 60 before being compared with 50 hours. | ||
|
||
For easier prompting, you can also try `text-davinci-003`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.