Skip to content

Commit a1494ea

Browse files
committed
chore: fix code samples
1 parent 8b48694 commit a1494ea

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

readme.md

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
# Golang OpenAI API Client
22

33
An Golang native implementation to easily interacting with OpenAI API.
4+
https://beta.openai.com/docs/api-reference/
45

56
## Usage
67

78
You can use environment variable to store API secret key
8-
```
9+
```sh
910
export OPENAI_KEY=[YOUR_KEY]
1011
```
1112

1213
To initialize engine, use this:
1314
```go
1415
e := openai.New(os.Getenv("OPENAI_KEY"))
1516
```
16-
17+
1718
### Tips
1819

1920
#### Model
@@ -22,22 +23,22 @@ If you want to use the most powerful model to generate text outputs, ensure that
2223
#### Text edition
2324
You can use the bundle Completion+Edit to regenerate the response based on the last context.
2425
```go
25-
e := openai.New(os.Getenv("OPENAI_KEY"))
26-
ctx := context.Background()
27-
completionResp, err := e.Completion(ctx, &openai.CompletionOptions{
28-
// Choose model, you can see list of available models in models.go file
29-
Model: openai.ModelTextDavinci001,
30-
// Number of completion tokens to generate response. By default - 1024
31-
MaxTokens: 1200,
32-
// Text to completion
33-
Prompt: []string{"Write a little bit of Wikipedia. What is that?"},
34-
})
35-
36-
editResp, err := e.Edit(ctx, &EditOptions{
37-
Model: ModelTextDavinci001,
38-
Input: completionResp.Choices[0],
39-
Instruction: "Please rewrite a bit more and add more information about Wikipedia in different aspects. Please build based on that for 4 topics",
40-
})
26+
e := openai.New(os.Getenv("OPENAI_KEY"))
27+
ctx := context.Background()
28+
completionResp, err := e.Completion(ctx, &openai.CompletionOptions{
29+
// Choose model, you can see list of available models in models.go file
30+
Model: openai.ModelTextDavinci001,
31+
// Number of completion tokens to generate response. By default - 1024
32+
MaxTokens: 1200,
33+
// Text to completion
34+
Prompt: []string{"Write a little bit of Wikipedia. What is that?"},
35+
})
36+
37+
editResp, err := e.Edit(ctx, &EditOptions{
38+
Model: ModelTextDavinci001,
39+
Input: completionResp.Choices[0],
40+
Instruction: "Please rewrite a bit more and add more information about Wikipedia in different aspects. Please build based on that for 4 topics",
41+
})
4142
```
4243

4344
### Text completion example
@@ -46,15 +47,15 @@ Given a prompt, the model will return one or more predicted completions.
4647
**Note**: the default number of completion tokens is 1024, if you want to increase or decrease this limit, you should change `MaxTokens` parameter.
4748

4849
```go
49-
e := openai.New(os.Getenv("OPENAI_KEY"))
50-
r, err := e.Completion(context.Background(), &openai.CompletionOptions{
51-
// Choose model, you can see list of available models in models.go file
52-
Model: openai.ModelTextDavinci001,
53-
// Number of completion tokens to generate response. By default - 1024
54-
MaxTokens: 1200,
55-
// Text to completion
56-
Prompt: []string{"Write a little bit of Wikipedia. What is that?"},
57-
})
50+
e := openai.New(os.Getenv("OPENAI_KEY"))
51+
r, err := e.Completion(context.Background(), &openai.CompletionOptions{
52+
// Choose model, you can see list of available models in models.go file
53+
Model: openai.ModelTextDavinci001,
54+
// Number of completion tokens to generate response. By default - 1024
55+
MaxTokens: 1200,
56+
// Text to completion
57+
Prompt: []string{"Write a little bit of Wikipedia. What is that?"},
58+
})
5859
```
5960

6061
You will get the next output:
@@ -95,27 +96,26 @@ import (
9596
"log"
9697
"os"
9798
"testing"
98-
99-
"github.com/0x9ef/openai-go"
100-
)
99+
"github.com/0x9ef/openai-go"
100+
)
101101

102102
func main() {
103-
e := openai.New(os.Getenv("OPENAI_KEY"))
104-
r, err := e.Completion(context.Background(), &openai.CompletionOptions{
105-
// Choose model, you can see list of available models in models.go file
106-
Model: openai.ModelTextDavinci001,
107-
// Text to completion
108-
Prompt: []string{"Write a little bit of Wikipedia. What is that?"}
109-
})
103+
e := openai.New(os.Getenv("OPENAI_KEY"))
104+
r, err := e.Completion(context.Background(), &openai.CompletionOptions{
105+
// Choose model, you can see list of available models in models.go file
106+
Model: openai.ModelTextDavinci001,
107+
// Text to completion
108+
Prompt: []string{"Write a little bit of Wikipedia. What is that?"},
109+
})
110110

111111
if b, err := json.MarshalIndent(r, "", " "); err != nil {
112112
panic(err)
113113
} else {
114114
fmt.Println(string(b))
115115
}
116116

117-
// Wikipedia is a free online encyclopedia, created and edited by volunteers.
118-
fmt.Println("What is the Wikipedia?", r.Choices[0].Text)
117+
// Wikipedia is a free online encyclopedia, created and edited by volunteers.
118+
fmt.Println("What is the Wikipedia?", r.Choices[0].Text)
119119
}
120120
```
121121

@@ -162,13 +162,13 @@ You will get the next output:
162162
To retrieve information about specified model instead of all models, you can do this:
163163

164164
```go
165-
e := openai.New(os.Getenv("OPENAI_KEY"))
166-
r, err := e.RetrieveModel(context.Background(), &openai.RetrieveModelOptions{
165+
e := openai.New(os.Getenv("OPENAI_KEY"))
166+
r, err := e.RetrieveModel(context.Background(), &openai.RetrieveModelOptions{
167167
ID: openai.ModelDavinci,
168168
})
169-
if err != nil {
170-
log.Fatal(err)
171-
}
169+
if err != nil {
170+
log.Fatal(err)
171+
}
172172
```
173173

174174
## License

0 commit comments

Comments
 (0)