Skip to content

Commit

Permalink
add readme exmaple: chatgpt support context (sashabaranov#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
A11Might authored Mar 16, 2023
1 parent 3c8be76 commit fd44d36
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,60 @@ c := openai.NewClientWithConfig(config)

See also: https://pkg.go.dev/github.com/sashabaranov/go-openai#ClientConfig
</details>

<details>
<summary>ChatGPT support context</summary>

```go
package main

import (
"bufio"
"context"
"fmt"
"os"
"strings"

"github.com/sashabaranov/go-openai"
)

func main() {
client := openai.NewClient("your token")
messages := make([]openai.ChatCompletionMessage, 0)
reader := bufio.NewReader(os.Stdin)
fmt.Println("Conversation")
fmt.Println("---------------------")

for {
fmt.Print("-> ")
text, _ := reader.ReadString('\n')
// convert CRLF to LF
text = strings.Replace(text, "\n", "", -1)
messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: text,
})

resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: messages,
},
)

if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
continue
}

content := resp.Choices[0].Message.Content
messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleAssistant,
Content: content,
})
fmt.Println(content)
}
}
```
</details>

0 comments on commit fd44d36

Please sign in to comment.