Skip to content

Commit fd44d36

Browse files
authored
add readme exmaple: chatgpt support context (#166)
1 parent 3c8be76 commit fd44d36

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

README.md

+57
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,60 @@ c := openai.NewClientWithConfig(config)
271271

272272
See also: https://pkg.go.dev/github.com/sashabaranov/go-openai#ClientConfig
273273
</details>
274+
275+
<details>
276+
<summary>ChatGPT support context</summary>
277+
278+
```go
279+
package main
280+
281+
import (
282+
"bufio"
283+
"context"
284+
"fmt"
285+
"os"
286+
"strings"
287+
288+
"github.com/sashabaranov/go-openai"
289+
)
290+
291+
func main() {
292+
client := openai.NewClient("your token")
293+
messages := make([]openai.ChatCompletionMessage, 0)
294+
reader := bufio.NewReader(os.Stdin)
295+
fmt.Println("Conversation")
296+
fmt.Println("---------------------")
297+
298+
for {
299+
fmt.Print("-> ")
300+
text, _ := reader.ReadString('\n')
301+
// convert CRLF to LF
302+
text = strings.Replace(text, "\n", "", -1)
303+
messages = append(messages, openai.ChatCompletionMessage{
304+
Role: openai.ChatMessageRoleUser,
305+
Content: text,
306+
})
307+
308+
resp, err := client.CreateChatCompletion(
309+
context.Background(),
310+
openai.ChatCompletionRequest{
311+
Model: openai.GPT3Dot5Turbo,
312+
Messages: messages,
313+
},
314+
)
315+
316+
if err != nil {
317+
fmt.Printf("ChatCompletion error: %v\n", err)
318+
continue
319+
}
320+
321+
content := resp.Choices[0].Message.Content
322+
messages = append(messages, openai.ChatCompletionMessage{
323+
Role: openai.ChatMessageRoleAssistant,
324+
Content: content,
325+
})
326+
fmt.Println(content)
327+
}
328+
}
329+
```
330+
</details>

0 commit comments

Comments
 (0)