This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
/
chatbot.go
70 lines (63 loc) · 1.59 KB
/
chatbot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package edgegpt
// ChatBot Combines everything to make it seamless
type ChatBot struct {
cookiePath string
cookies []map[string]interface{}
proxy string
chatHub *ChatHub
addr string
path string
}
func NewChatBot(cookiePath string, cookies []map[string]interface{}, proxy string) *ChatBot {
addr := "sydney.bing.com"
path := "/sydney/ChatHub"
bot := &ChatBot{
cookiePath: cookiePath,
cookies: cookies,
proxy: proxy,
addr: addr,
path: path,
chatHub: nil,
}
return bot
}
func (bot *ChatBot) Init() error {
conversation := NewConversation(bot.cookiePath, bot.cookies, bot.proxy)
err := conversation.Init()
if err != nil {
return err
}
bot.chatHub = NewChatHub(bot.addr, bot.path, conversation)
//log.Println("init success")
// too many tracers
return nil
}
/*
// Ask a question to the bot
The callback function is streaming,
it will be called every time data is received,
if you only want to get the final result,
you can use `answer.IsDone()` to judge whether it is finished
*/
func (bot *ChatBot) Ask(prompt string, conversationStyle ConversationStyle, callback func(answer *Answer)) error {
// defer bot.chatHub.Close()
err := bot.chatHub.newConnect()
if err != nil {
return err
}
defer bot.chatHub.Close()
//log.Println("connect chathub success")
return bot.chatHub.askStream(prompt, conversationStyle, callback)
}
func (bot *ChatBot) Close() error {
return bot.chatHub.Close()
}
// Reset the conversation
func (bot *ChatBot) Reset() error {
bot.chatHub.Close()
err := bot.Init()
if err != nil {
return err
}
return nil
}