-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathhandler.go
107 lines (100 loc) · 3.14 KB
/
handler.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"context"
"io"
"log"
"net/http"
"strings"
"github.com/riba2534/openai-on-wechat/ai"
"github.com/riba2534/openai-on-wechat/config"
"github.com/riba2534/openai-on-wechat/consts"
"github.com/riba2534/openwechat"
"github.com/sashabaranov/go-openai"
)
func MessageHandler(msg *openwechat.Message) {
if !msg.IsText() {
return
}
ctx := context.Background()
systemPrompt := config.Prompt
switch {
case strings.HasPrefix(msg.Content, config.C.WechatConfig.TextConfig.TriggerPrefix):
// 文字回复
if config.C.ContextConfig.SwitchOn {
go textSessionReplyHandler(ctx, msg, config.C.WechatConfig.TextConfig.TriggerPrefix, openai.GPT3Dot5Turbo, systemPrompt)
} else {
go textReplyHandler(ctx, msg, config.C.WechatConfig.TextConfig.TriggerPrefix, openai.GPT3Dot5Turbo, systemPrompt)
}
case strings.HasPrefix(msg.Content, config.C.WechatConfig.ImageConfig.TriggerPrefix):
// 图片回复
go imageReplyHandler(ctx, msg, config.C.WechatConfig.ImageConfig.TriggerPrefix)
}
}
// 文字回复处理
func textReplyHandler(ctx context.Context, msg *openwechat.Message, prefix, model, systemPrompt string) {
log.Printf("[text] Request: %s", msg.Content) // 输出请求消息到日志
q := strings.TrimSpace(strings.TrimPrefix(msg.Content, prefix))
reply := ai.CreateChatCompletion(ctx, model, []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: systemPrompt,
},
{
Role: openai.ChatMessageRoleUser,
Content: q,
},
})
log.Printf("[text] Response: %s", reply) // 输出回复消息到日志
_, err := msg.ReplyText(reply)
if err != nil {
log.Printf("msg.ReplyText Error: %+v", err)
}
}
// 带有上下文的文字回复
func textSessionReplyHandler(ctx context.Context, msg *openwechat.Message, prefix, model, systemPrompt string) {
log.Printf("[text session] Request: %s", msg.Content) // 输出请求消息到日志
user := func() string {
s := msg.FromUserName
if msg.IsSendBySelf() {
s = msg.ToUserName
}
return s
}()
q := strings.TrimSpace(strings.TrimPrefix(msg.Content, prefix))
reply := ai.GetSessionOpenAITextReply(ctx, q, user, model, systemPrompt)
log.Printf("[text session] Response: %s", reply) // 输出回复消息到日志
_, err := msg.ReplyText(reply)
if err != nil {
log.Printf("msg.ReplyText Error: %+v", err)
}
}
// 回复图片
func imageReplyHandler(ctx context.Context, msg *openwechat.Message, prefix string) {
log.Printf("[image] Request: %s", msg.Content)
q := strings.TrimSpace(strings.TrimPrefix(msg.Content, prefix))
url := ai.CreateImageReply(ctx, q)
if url == "" {
log.Printf("[image] Response: url 为空")
msg.ReplyText(consts.ErrTips)
return
}
log.Printf("[image] Response: url = %s", url)
image, err := downloadImage(url)
if err != nil {
log.Printf("[image] downloadImage err, err=%+v", err)
msg.ReplyText(consts.ErrTips)
return
}
_, err = msg.ReplyImage(image)
if err != nil {
log.Printf("msg.ReplyImage Error: %+v", err)
}
}
func downloadImage(url string) (io.Reader, error) {
response, err := http.Get(url)
if err != nil {
log.Printf("downloadImage failed, err=%+v", err)
return nil, err
}
return response.Body, nil
}