Skip to content

Commit ebef8ca

Browse files
committed
refactors readme
1 parent 23acf80 commit ebef8ca

9 files changed

+624
-629
lines changed

README.md

Lines changed: 46 additions & 629 deletions
Large diffs are not rendered by default.

code_editing_examples.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Code editing example
2+
3+
OpenAI's [edits](https://openai.com/blog/gpt-3-edit-insert/) endpoint is particularly useful for editing code.
4+
5+
Unlike completions, edits takes two inputs: the text to edit and an instruction.
6+
7+
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".
8+
9+
Example text input to `code-davinci-edit-001`:
10+
11+
```python
12+
def tribonacci(n):
13+
if n == 0:
14+
return 0
15+
elif n == 1:
16+
return 1
17+
elif n == 2:
18+
return 1
19+
elif n == 3:
20+
return 2
21+
else:
22+
return tribonacci(n-1) + tribonacci(n-2) + tribonacci(n-3)
23+
```
24+
25+
Example instruction inputs:
26+
27+
```text
28+
add a docstring
29+
```
30+
31+
```text
32+
Add typing, using Python 3.9 conventions
33+
```
34+
35+
```text
36+
improved the runtime
37+
```
38+
39+
```text
40+
Add a test.
41+
```
42+
43+
```text
44+
Translate to JavaScript (or Rust or Lisp or any language you like)
45+
```
46+
47+
Example output after improving the runtime and translating to JavaScript:
48+
49+
```JavaScript
50+
function tribonacci(n) {
51+
let a = 0;
52+
let b = 1;
53+
let c = 1;
54+
for (let i = 0; i < n; i++) {
55+
[a, b, c] = [b, c, a + b + c];
56+
}
57+
return a;
58+
}
59+
```
60+
61+
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.
62+
63+
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).

code_explanation_examples.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Code explanation examples
2+
3+
GPT's understanding of code can be applied to many use cases, e.g.:
4+
5+
* Generating in-code documentation (e.g., Python docstrings, git commit messages)
6+
* Generating out-of-code documentation (e.g., man pages)
7+
* An interactive code exploration tool
8+
* Communicating program results back to users via a natural language interface
9+
10+
For example, if you wanted to understand a SQL query, you could give `code-davinci-002` the following example prompt:
11+
12+
````text
13+
A SQL query:
14+
```
15+
SELECT c.customer_id
16+
FROM Customers c
17+
JOIN Streaming s
18+
ON c.customer_id = s.customer_id
19+
WHERE c.signup_date BETWEEN '2020-03-01' AND '2020-03-31'
20+
AND s.watch_date BETWEEN c.signup_date AND DATE_ADD(c.signup_date, INTERVAL 30 DAY)
21+
GROUP BY c.customer_id
22+
HAVING SUM(s.watch_minutes) > 50 * 60
23+
```
24+
25+
Questions:
26+
1. What does the SQL query do?
27+
2. Why might someone be interested in this time period?
28+
3. Why might a company be interested in this SQL query?
29+
30+
Answers:
31+
````
32+
33+
[Output]((https://beta.openai.com/playground/p/Sv1VQKbJV1TZKmiTK9r6nlj3)):
34+
35+
```text
36+
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.
37+
2. The time period is interesting because it is the first month of the COVID-19 pandemic.
38+
3. A company might be interested in this SQL query because it can help them understand how the pandemic has affected their business.
39+
```
40+
41+
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`.

code_writing_examples.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Code writing examples
2+
3+
GPT-3 is able to write code as well as text.
4+
5+
Here's an example of `code-davinci-002` writing a SQL query:
6+
7+
````text
8+
SQL tables (and columns):
9+
* Customers(customer_id, signup_date)
10+
* Streaming(customer_id, video_id, watch_date, watch_minutes)
11+
12+
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:
13+
```
14+
````
15+
16+
[Output](https://beta.openai.com/playground/p/r2mw99cANoa0TJHok725CeaC):
17+
18+
```sql
19+
SELECT c.customer_id
20+
FROM Customers c
21+
JOIN Streaming s
22+
ON c.customer_id = s.customer_id
23+
WHERE c.signup_date BETWEEN '2020-03-01' AND '2020-03-31'
24+
AND s.watch_date BETWEEN c.signup_date AND DATE_ADD(c.signup_date, INTERVAL 30 DAY)
25+
GROUP BY c.customer_id
26+
HAVING SUM(s.watch_minutes) > 50 * 60
27+
```
28+
29+
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.
30+
31+
For easier prompting, you can also try `text-davinci-003`.
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# How to work with large language models
2+
3+
## How large language models work
4+
5+
[Large language models][Large language models Blog Post] are functions that map text to text. Given an input string of text, a large language model predicts the text that should come next.
6+
7+
The magic of large language models is that by being trained to minimize this prediction error over vast quantities of text, the models end up learning concepts useful for these predictions. For example, they learn:
8+
9+
* how to spell
10+
* how grammar works
11+
* how to paraphrase
12+
* how to answer questions
13+
* how to hold a conversation
14+
* how to write in many languages
15+
* how to code
16+
* etc.
17+
18+
None of these capabilities are explicitly programmed in—they all emerge as a result of training.
19+
20+
GPT-3 powers [hundreds of software products][GPT3 Apps Blog Post], including productivity apps, education apps, games, and more.
21+
22+
## How to control a large language model
23+
24+
Of all the inputs to a large language model, by far the most influential is the text prompt.
25+
26+
Large language models can be prompted to produce output in a few ways:
27+
28+
* **Instruction**: Tell the model what you want
29+
* **Completion**: Induce the model to complete the beginning of what you want
30+
* **Demonstration**: Show the model what you want, with either:
31+
* A few examples in the prompt
32+
* Many hundreds or thousands of examples in a fine-tuning training dataset
33+
34+
An example of each is shown below.
35+
36+
### Instruction prompts
37+
38+
Instruction-following models (e.g., `text-davinci-003` or any model beginning with `text-`) are specially designed to follow instructions. Write your instruction at the top of the prompt (or at the bottom, or both), and the model will do its best to follow the instruction and then stop. Instructions can be detailed, so don't be afraid to write a paragraph explicitly detailing the output you want.
39+
40+
Example instruction prompt:
41+
42+
```text
43+
Extract the name of the author from the quotation below.
44+
45+
“Some humans theorize that intelligent species go extinct before they can expand into outer space. If they're correct, then the hush of the night sky is the silence of the graveyard.”
46+
― Ted Chiang, Exhalation
47+
```
48+
49+
Output:
50+
51+
```text
52+
Ted Chiang
53+
```
54+
55+
### Completion prompt example
56+
57+
Completion-style prompts take advantage of how large language models try to write text they think is mostly likely to come next. To steer the model, try beginning a pattern or sentence that will be completed by the output you want to see. Relative to direct instructions, this mode of steering large language models can take more care and experimentation. In addition, the models won't necessarily know where to stop, so you will often need stop sequences or post-processing to cut off text generated beyond the desired output.
58+
59+
Example completion prompt:
60+
61+
```text
62+
“Some humans theorize that intelligent species go extinct before they can expand into outer space. If they're correct, then the hush of the night sky is the silence of the graveyard.”
63+
― Ted Chiang, Exhalation
64+
65+
The author of this quote is
66+
```
67+
68+
Output:
69+
70+
```text
71+
Ted Chiang
72+
```
73+
74+
### Demonstration prompt example (few-shot learning)
75+
76+
Similar to completion-style prompts, demonstrations can show the model what you want it to do. This approach is sometimes called few-shot learning, as the model learns from a few examples provided in the prompt.
77+
78+
Example demonstration prompt:
79+
80+
```text
81+
Quote:
82+
“When the reasoning mind is forced to confront the impossible again and again, it has no choice but to adapt.”
83+
― N.K. Jemisin, The Fifth Season
84+
Author: N.K. Jemisin
85+
86+
Quote:
87+
“Some humans theorize that intelligent species go extinct before they can expand into outer space. If they're correct, then the hush of the night sky is the silence of the graveyard.”
88+
― Ted Chiang, Exhalation
89+
Author:
90+
```
91+
92+
Output:
93+
94+
```text
95+
Ted Chiang
96+
```
97+
98+
### Fine-tuned prompt example
99+
100+
With enough training examples, you can [fine-tune][Fine Tuning Docs] a custom model. In this case, instructions become unnecessary, as the model can learn the task from the training data provided. However, it can be helpful to include separator sequences (e.g., `->` or `###` or any string that doesn't commonly appear in your inputs) to tell the model when the prompt has ended and the output should begin. Without separator sequences, there is a risk that the model continues elaborating on the input text rather than starting on the answer you want to see.
101+
102+
Example fine-tuned prompt (for a model that has been custom trained on similar prompt-completion pairs):
103+
104+
```text
105+
“Some humans theorize that intelligent species go extinct before they can expand into outer space. If they're correct, then the hush of the night sky is the silence of the graveyard.”
106+
― Ted Chiang, Exhalation
107+
108+
###
109+
110+
111+
```
112+
113+
Output:
114+
115+
```text
116+
Ted Chiang
117+
```
118+
119+
## Code Capabilities
120+
121+
Large language models aren't only great at text - they can be great at code too. OpenAI's specialized code model is called [Codex].
122+
123+
Codex powers [more than 70 products][Codex Apps Blog Post], including:
124+
125+
* [GitHub Copilot] (autocompletes code in VS Code and other IDEs)
126+
* [Pygma](https://pygma.app/) (turns Figma designs into code)
127+
* [Replit](https://replit.com/) (has an 'Explain code' button and other features)
128+
* [Warp](https://www.warp.dev/) (a smart terminal with AI command search)
129+
* [Machinet](https://machinet.net/) (writes Java unit test templates)
130+
131+
Note that unlike instruction-following text models (e.g., `text-davinci-002`), Codex is *not* trained to follow instructions. As a result, designing good prompts can take more care.
132+
133+
### More prompt advice
134+
135+
For more prompt examples, visit [OpenAI Examples][OpenAI Examples].
136+
137+
In general, the input prompt is the best lever for improving model outputs. You can try tricks like:
138+
139+
* **Give more explicit instructions.** E.g., if you want the output to be a comma separated list, ask it to return a comma separated list. If you want it to say "I don't know" when the it doesn't know the answer, tell it 'Say "I don't know" if you do not know the answer.'
140+
* **Supply better examples.** If you're demonstrating examples in your prompt, make sure that your examples are diverse and high quality.
141+
* **Ask the model to answer as if it was an expert.** Explicitly asking the model to produce high quality output or output as if it was written by an expert can induce the model to give higher quality answers that it thinks an expert would write. E.g., "The following answer is correct, high-quality, and written by an expert."
142+
* **Prompt the model to write down the series of steps explaining its reasoning.** E.g., prepend your answer with something like "[Let's think step by step](https://arxiv.org/pdf/2205.11916v1.pdf)." Prompting the model to give an explanation of its reasoning before its final answer can increase the likelihood that its final answer is consistent and correct.
143+
144+
145+
146+
[Fine Tuning Docs]: https://beta.openai.com/docs/guides/fine-tuning
147+
[Codex Apps Blog Post]: https://openai.com/blog/codex-apps/
148+
[Large language models Blog Post]: https://openai.com/blog/better-language-models/
149+
[GitHub Copilot]: https://copilot.github.com/
150+
[Codex]: https://openai.com/blog/openai-codex/
151+
[GPT3 Apps Blog Post]: https://openai.com/blog/gpt-3-apps/
152+
[OpenAI Examples]: https://beta.openai.com/examples

text_comparison_examples.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Text comparison examples
2+
3+
The [OpenAI API embeddings endpoint](https://beta.openai.com/docs/guides/embeddings) can be used to measure relatedness or similarity between pieces of text.
4+
5+
By leveraging GPT-3's understanding of text, these embeddings [achieved state-of-the-art results](https://arxiv.org/abs/2201.10005) on benchmarks in unsupervised learning and transfer learning settings.
6+
7+
Embeddings can be used for semantic search, recommendations, cluster analysis, near-duplicate detection, and more.
8+
9+
For more information, read OpenAI's blog post announcements:
10+
11+
* [Introducing Text and Code Embeddings (Jan 2022)](https://openai.com/blog/introducing-text-and-code-embeddings/)
12+
* [New and Improved Embedding Model (Dec 2022)](https://openai.com/blog/new-and-improved-embedding-model/)
13+
14+
## Semantic search
15+
16+
Embeddings can be used for search either by themselves or as a feature in a larger system.
17+
18+
The simplest way to use embeddings for search is as follows:
19+
20+
* Before the search (precompute):
21+
* Split your text corpus into chunks smaller than the token limit (8,191 tokens for `text-embedding-ada-002`)
22+
* Embed each chunk of text
23+
* Store those embeddings in your own database or in a vector search provider like [Pinecone](https://www.pinecone.io) or [Weaviate](https://weaviate.io)
24+
* At the time of the search (live compute):
25+
* Embed the search query
26+
* Find the closest embeddings in your database
27+
* Return the top results
28+
29+
An example of how to use embeddings for search is shown in [Semantic_text_search_using_embeddings.ipynb](examples/Semantic_text_search_using_embeddings.ipynb).
30+
31+
In more advanced search systems, the the cosine similarity of embeddings can be used as one feature among many in ranking search results.
32+
33+
## Question answering
34+
35+
The best way to get reliably honest answers from GPT-3 is to give it source documents in which it can locate correct answers. Using the semantic search procedure above, you can cheaply search a corpus of documents for relevant information and then give that information to GPT-3, via the prompt, to answer a question. We demonstrate in [Question_answering_using_embeddings.ipynb](examples/Question_answering_using_embeddings.ipynb).
36+
37+
## Recommendations
38+
39+
Recommendations are quite similar to search, except that instead of a free-form text query, the inputs are items in a set.
40+
41+
An example of how to use embeddings for recommendations is shown in [Recommendation_using_embeddings.ipynb](examples/Recommendation_using_embeddings.ipynb).
42+
43+
Similar to search, these cosine similarity scores can either be used on their own to rank items or as features in larger ranking algorithms.
44+
45+
## Customizing Embeddings
46+
47+
Although OpenAI's embedding model weights cannot be fine-tuned, you can nevertheless use training data to customize embeddings to your application.
48+
49+
In [Customizing_embeddings.ipynb](examples/Customizing_embeddings.ipynb), we provide an example method for customizing your embeddings using training data. The idea of the method is to train a custom matrix to multiply embedding vectors by in order to get new customized embeddings. With good training data, this custom matrix will help emphasize the features relevant to your training labels. You can equivalently consider the matrix multiplication as (a) a modification of the embeddings or (b) a modification of the distance function used to measure the distances between embeddings.

0 commit comments

Comments
 (0)