1
- # go-gpt3
2
- [ ![ GoDoc] ( http://img.shields.io/badge/GoDoc-Reference-blue.svg )] ( https://godoc.org/github.com/sashabaranov/go-gpt3 )
3
- [ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/sashabaranov/go-gpt3 )] ( https://goreportcard.com/report/github.com/sashabaranov/go-gpt3 )
1
+ # Go OpenAI
2
+ [ ![ GoDoc] ( http://img.shields.io/badge/GoDoc-Reference-blue.svg )] ( https://godoc.org/github.com/sashabaranov/go-openai )
3
+ [ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/sashabaranov/go-openai )] ( https://goreportcard.com/report/github.com/sashabaranov/go-openai )
4
4
5
5
6
- [ OpenAI ChatGPT] ( https://platform.openai.com/ ) , GPT-3, DALL·E 2, and Whisper API client for Go
6
+ This library provides Go clients for [ OpenAI API] ( https://platform.openai.com/ ) . We support:
7
+
8
+ * ChatGPT
9
+ * GPT-3
10
+ * DALL·E 2
11
+ * Whisper
7
12
8
13
Installation:
9
14
```
10
- go get github.com/sashabaranov/go-gpt3
15
+ go get github.com/sashabaranov/go-openai
11
16
```
12
17
13
18
14
- Example usage:
19
+ ChatGPT example usage:
15
20
16
21
``` go
17
22
package main
@@ -22,6 +27,48 @@ import (
22
27
gogpt " github.com/sashabaranov/go-gpt3"
23
28
)
24
29
30
+ func main () {
31
+ c := gogpt.NewClient (" your token" )
32
+ ctx := context.Background ()
33
+
34
+ resp , err := c.CreateChatCompletion (
35
+ ctx,
36
+ gogpt.ChatCompletionRequest {
37
+ Model: gogpt.GPT3Dot5Turbo ,
38
+ Messages: []gogpt.ChatCompletionMessage {
39
+ {
40
+ Role: " user" ,
41
+ Content: " Hello!" ,
42
+ },
43
+ },
44
+ },
45
+ )
46
+
47
+ if err != nil {
48
+ return
49
+ }
50
+
51
+ fmt.Println (resp.Choices [0 ].Message .Content )
52
+ }
53
+
54
+ ```
55
+
56
+
57
+
58
+ Other examples:
59
+
60
+ <details >
61
+ <summary >GPT-3 completion</summary >
62
+
63
+ ``` go
64
+ package main
65
+
66
+ import (
67
+ " context"
68
+ " fmt"
69
+ gogpt " github.com/sashabaranov/go-openai"
70
+ )
71
+
25
72
func main () {
26
73
c := gogpt.NewClient (" your token" )
27
74
ctx := context.Background ()
@@ -38,8 +85,10 @@ func main() {
38
85
fmt.Println (resp.Choices [0 ].Text )
39
86
}
40
87
```
88
+ </details >
41
89
42
- Streaming response example:
90
+ <details >
91
+ <summary >GPT-3 streaming completion</summary >
43
92
44
93
``` go
45
94
package main
@@ -49,7 +98,57 @@ import (
49
98
" context"
50
99
" fmt"
51
100
" io"
52
- gogpt " github.com/sashabaranov/go-gpt3"
101
+ gogpt " github.com/sashabaranov/go-openai"
102
+ )
103
+
104
+ func main () {
105
+ c := gogpt.NewClient (" your token" )
106
+ ctx := context.Background ()
107
+
108
+ req := gogpt.CompletionRequest {
109
+ Model: gogpt.GPT3Ada ,
110
+ MaxTokens: 5 ,
111
+ Prompt: " Lorem ipsum" ,
112
+ Stream: true ,
113
+ }
114
+ stream , err := c.CreateCompletionStream (ctx, req)
115
+ if err != nil {
116
+ return
117
+ }
118
+ defer stream.Close ()
119
+
120
+ for {
121
+ response , err := stream.Recv ()
122
+ if errors.Is (err, io.EOF ) {
123
+ fmt.Println (" Stream finished" )
124
+ return
125
+ }
126
+
127
+ if err != nil {
128
+ fmt.Printf (" Stream error: %v \n " , err)
129
+ return
130
+ }
131
+
132
+
133
+ fmt.Printf (" Stream response: %v \n " , response)
134
+ }
135
+ }
136
+ ```
137
+ </details >
138
+
139
+
140
+ <details >
141
+ <summary >GPT-3 streaming completion</summary >
142
+
143
+ ``` go
144
+ package main
145
+
146
+ import (
147
+ " errors"
148
+ " context"
149
+ " fmt"
150
+ " io"
151
+ gogpt " github.com/sashabaranov/go-openai"
53
152
)
54
153
55
154
func main () {
@@ -85,3 +184,4 @@ func main() {
85
184
}
86
185
}
87
186
```
187
+ </details >
0 commit comments