Skip to content

Commit

Permalink
support chat stream and update cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
idoubi committed Dec 12, 2022
1 parent c4b15cc commit 5161eb0
Show file tree
Hide file tree
Showing 16 changed files with 363 additions and 84 deletions.
163 changes: 136 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 1,14 @@
# chatgpt-go

chatgpt sdk writen with golang.
chatgpt sdk writen by golang.

## Rrecondition
## Preparation

you should login [ChatGPT website](https://chat.openai.com/chat), and get your session-token
you should login into the [ChatGPT Website](https://chat.openai.com/chat) firstly, find the Cookies named `cf_clearance`,`__Secure-next-auth.session-token`, and copy their values.

![](./chatgpt.png)
> update your cookies no more than 2 hours.
![](./images/chatgpt_cookies.png)

## Quick Start

Expand All @@ -19,79 21,186 @@ go get -u github.com/chatgp/chatgpt-go
2. chat in independent conversation

```go
package main
package main

import (
"fmt"
"log"
"net/http"
"time"

chatgpt "github.com/chatgp/chatgpt-go"
)

func main() {
// new chatgpt client
token := `YOUR-SESSION-TOKEN`
cli = chatgpt.NewClient(
chatgpt.WithToken(token),
chatgpt.WithTimeout(30*time.Second),
// new chatgpt client
token := `copy-from-cookies`
cfValue := "copy-from-cookies"

cookies := []*http.Cookie{
{
Name: "__Secure-next-auth.session-token",
Value: token,
},
{
Name: "cf_clearance",
Value: cfValue,
},
}

cli := chatgpt.NewClient(
chatgpt.WithDebug(true),
chatgpt.WithTimeout(60*time.Second),
chatgpt.WithCookies(cookies),
)

// chat in independent conversation
message := "hello"
// chat in independent conversation
message := "say hello to me"
text, err := cli.GetChatText(message)
if err != nil {
log.Fatalf("get chat text failed: %v", err)
}
fmt.Printf("q: %s, a: %s\n", message, text.Content)

log.Printf("q: %s, a: %s\n", message, text.Content)
}
```

3. chat in continuous conversation

```go
package main
package main

import (
"fmt"
"log"
"net/http"
"time"

chatgpt "github.com/chatgp/chatgpt-go"
)

func main() {
// new chatgpt client
token := `YOUR-SESSION-TOKEN`
cli = chatgpt.NewClient(
chatgpt.WithToken(token),
chatgpt.WithTimeout(30*time.Second),
// new chatgpt client
token := `copy-from-cookies`
cfValue := "copy-from-cookies"

cookies := []*http.Cookie{
{
Name: "__Secure-next-auth.session-token",
Value: token,
},
{
Name: "cf_clearance",
Value: cfValue,
},
}

cli := chatgpt.NewClient(
chatgpt.WithDebug(true),
chatgpt.WithTimeout(60*time.Second),
chatgpt.WithCookies(cookies),
)

// chat in continuous conversation
// chat in continuous conversation

// first message
message := "what's golang"
message := "say hello to me"
text, err := cli.GetChatText(message)
if err != nil {
log.Fatalf("get chat text failed: %v", err)
}
fmt.Printf("q: %s, a: %s\n", message, text.Content)

log.Printf("q: %s, a: %s\n", message, text.Content)

// continue conversation with new message
conversationID := text.ConversationID
parentMessage := text.MessageID
newMessage := "use it to write hello world"
newMessage := "again"

newText, err := cli.GetChatText(newMessage, conversationID, parentMessage)
if err != nil {
log.Fatalf("get chat text failed: %v", err)
}
fmt.Printf("q: %s, a: %s\n", newMessage, newText.Content)

log.Printf("q: %s, a: %s\n", newMessage, newText.Content)
}
```

> if you want to start a new conversation out of current conversation, you don't need to reset the client. just remove the `conversationID``parentMessage` arguments in `GetChatText` method and use it to get a new text reply.
4. get chat content with stream

```go
package main

import (
"log"
"net/http"
"time"

chatgpt "github.com/chatgp/chatgpt-go"
)

func main() {
// new chatgpt client
token := `copy-from-cookies`
cfValue := "copy-from-cookies"

cookies := []*http.Cookie{
{
Name: "__Secure-next-auth.session-token",
Value: token,
},
{
Name: "cf_clearance",
Value: cfValue,
},
}

cli := chatgpt.NewClient(
chatgpt.WithDebug(true),
chatgpt.WithTimeout(60*time.Second),
chatgpt.WithCookies(cookies),
)

message := "say hello to me"
stream, err := cli.GetChatStream(message)
if err != nil {
log.Fatalf("get chat stream failed: %v\n", err)
}

var answer string
for text := range stream.Stream {
log.Printf("stream text: %s\n", text.Content)
answer = text.Content
}

if stream.Err != nil {
log.Fatalf("stream closed with error: %v\n", stream.Err)
}

log.Printf("q: %s, a: %s\n", message, answer)
}
```

## Tests

under examples folder there are some tests, execute `go test` command and get outputs like below:

- get chat text

![](./images/chat_text.png)

- get chat text in continuous conversation

![](./images/continuous_chat_text.png)

- get chat text from stream

![](./images/chat_stream.png)

## Communication

- Telegram Group: [ChatGPT Creators](https://t.me/ YkEGeRxB5Q0zODY1)
- Wechat Group: [ChatGPT Creators](https://work.weixin.qq.com/gm/66944e9bd30628e9270c980bc756663d)

- Discord Server: [ChatGPT Creators](https://discord.gg/qWshJnJs)

- Wechat Group: [ChatGPT Creators](./images/chatgroup.jpg)
Loading

0 comments on commit 5161eb0

Please sign in to comment.