File tree Expand file tree Collapse file tree 1 file changed +56
-1
lines changed Expand file tree Collapse file tree 1 file changed +56
-1
lines changed Original file line number Diff line number Diff line change @@ -58,6 +58,61 @@ func main() {
58
58
59
59
Other examples:
60
60
61
+ <details >
62
+ <summary >ChatGPT streaming completion</summary >
63
+
64
+ ``` go
65
+ package main
66
+
67
+ import (
68
+ " context"
69
+ " errors"
70
+ " fmt"
71
+ " io"
72
+ openai " github.com/sashabaranov/go-openai"
73
+ )
74
+
75
+ func main () {
76
+ c := openai.NewClient (" your token" )
77
+ ctx := context.Background ()
78
+
79
+ req := openai.ChatCompletionRequest {
80
+ Model: openai.GPT3Dot5Turbo ,
81
+ MaxTokens: 20 ,
82
+ Messages: []openai.ChatCompletionMessage {
83
+ {
84
+ Role: openai.ChatMessageRoleUser ,
85
+ Content: " Lorem ipsum" ,
86
+ },
87
+ },
88
+ Stream: true ,
89
+ }
90
+ stream , err := c.CreateChatCompletionStream (ctx, req)
91
+ if err != nil {
92
+ fmt.Printf (" ChatCompletionStream error: %v \n " , err)
93
+ return
94
+ }
95
+ defer stream.Close ()
96
+
97
+ fmt.Printf (" Stream response: " )
98
+ for {
99
+ response , err := stream.Recv ()
100
+ if errors.Is (err, io.EOF ) {
101
+ fmt.Println (" \n Stream finished" )
102
+ return
103
+ }
104
+
105
+ if err != nil {
106
+ fmt.Printf (" \n Stream error: %v \n " , err)
107
+ return
108
+ }
109
+
110
+ fmt.Printf (response.Choices [0 ].Delta .Content )
111
+ }
112
+ }
113
+ ```
114
+ </details >
115
+
61
116
<details >
62
117
<summary >GPT-3 completion</summary >
63
118
@@ -327,4 +382,4 @@ func main() {
327
382
}
328
383
}
329
384
```
330
- </details >
385
+ </details >
You can’t perform that action at this time.
0 commit comments