1
1
# Golang OpenAI API Client
2
2
3
3
An Golang native implementation to easily interacting with OpenAI API.
4
+ https://beta.openai.com/docs/api-reference/
4
5
5
6
## Usage
6
7
7
8
You can use environment variable to store API secret key
8
- ```
9
+ ``` sh
9
10
export OPENAI_KEY=[YOUR_KEY]
10
11
```
11
12
12
13
To initialize engine, use this:
13
14
``` go
14
15
e := openai.New (os.Getenv (" OPENAI_KEY" ))
15
16
```
16
-
17
+
17
18
### Tips
18
19
19
20
#### Model
@@ -22,22 +23,22 @@ If you want to use the most powerful model to generate text outputs, ensure that
22
23
#### Text edition
23
24
You can use the bundle Completion+Edit to regenerate the response based on the last context.
24
25
``` 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
+ })
41
42
```
42
43
43
44
### Text completion example
@@ -46,15 +47,15 @@ Given a prompt, the model will return one or more predicted completions.
46
47
** Note** : the default number of completion tokens is 1024, if you want to increase or decrease this limit, you should change ` MaxTokens ` parameter.
47
48
48
49
``` 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
+ })
58
59
```
59
60
60
61
You will get the next output:
@@ -95,27 +96,26 @@ import (
95
96
" log"
96
97
" os"
97
98
" testing"
98
-
99
- " github.com/0x9ef/openai-go"
100
- )
99
+ " github.com/0x9ef/openai-go"
100
+ )
101
101
102
102
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
+ })
110
110
111
111
if b , err := json.MarshalIndent (r, " " , " " ); err != nil {
112
112
panic (err)
113
113
} else {
114
114
fmt.Println (string (b))
115
115
}
116
116
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 )
119
119
}
120
120
```
121
121
@@ -162,13 +162,13 @@ You will get the next output:
162
162
To retrieve information about specified model instead of all models, you can do this:
163
163
164
164
``` 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 {
167
167
ID : openai.ModelDavinci ,
168
168
})
169
- if err != nil {
170
- log.Fatal (err)
171
- }
169
+ if err != nil {
170
+ log.Fatal (err)
171
+ }
172
172
```
173
173
174
174
## License
0 commit comments