Compare commits
2 Commits
dcb251d2ad
...
7e9d4e5052
Author | SHA1 | Date |
---|---|---|
Yiyang Kang | 7e9d4e5052 | |
Yiyang Kang | 08985f6936 |
30
assistant.go
30
assistant.go
|
@ -31,6 +31,12 @@ func matchAssistantConversation(botUsr *tele.User, msg *tele.Message) []*tele.Me
|
|||
}
|
||||
|
||||
for _, msg := range thread {
|
||||
for _, ent := range msg.Entities {
|
||||
if ent.Type == tele.EntityCommand {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
from := msg.Sender
|
||||
chat := msg.Chat
|
||||
if from == nil || chat == nil {
|
||||
|
@ -84,13 +90,23 @@ func matchAssistantConversation(botUsr *tele.User, msg *tele.Message) []*tele.Me
|
|||
type assistantStreamedResponseCb func(text string, finished bool) (*tele.Message, error)
|
||||
|
||||
func assistantStreamedResponse(request openai.ChatRequest, cb assistantStreamedResponseCb) error {
|
||||
logger.Debugf("Openai chat request: %#+v", request)
|
||||
logger.Debugw("Openai chat request", "req", request)
|
||||
ai := openai.NewClient(config.OpenAIApiKey)
|
||||
|
||||
resp, err := ai.ChatCompletionStream(request)
|
||||
if err != nil {
|
||||
var (
|
||||
resp *openai.ChatResponseStream
|
||||
err error
|
||||
)
|
||||
tries := 2
|
||||
for {
|
||||
if tries--; tries < 0 {
|
||||
return err
|
||||
}
|
||||
if resp, err = ai.ChatCompletionStream(request); err == nil {
|
||||
break
|
||||
}
|
||||
logger.Warnw("assistant: failed to get response", "error", err)
|
||||
}
|
||||
|
||||
nErrs := 0
|
||||
go func() {
|
||||
|
@ -188,7 +204,7 @@ func handleAssistantConversation(c tele.Context, thread []*tele.Message) error {
|
|||
role := openai.ChatRoleUser
|
||||
from := thread[l].Sender
|
||||
if from != nil && from.ID == me.ID {
|
||||
role = openai.ChatRoleSystem
|
||||
role = openai.ChatRoleAssistant
|
||||
}
|
||||
|
||||
convMsgs = append(convMsgs, openai.ChatMessage{
|
||||
|
@ -221,11 +237,12 @@ func handleAssistantConversation(c tele.Context, thread []*tele.Message) error {
|
|||
var err error
|
||||
if replyMsg == nil {
|
||||
<-typingNotifyCh
|
||||
replyMsg, err = c.Bot().Reply(c.Message(), text, tele.Silent)
|
||||
replyMsg, err = c.Bot().Reply(lastMsg, text, tele.Silent)
|
||||
} else {
|
||||
replyMsg, err = c.Bot().Edit(replyMsg, text)
|
||||
}
|
||||
if finished && err == nil {
|
||||
replyMsg.ReplyTo = lastMsg // nasty bug
|
||||
if err := cacheMessage(replyMsg); err != nil {
|
||||
logger.Warnw("failed to cache message", "error", err)
|
||||
}
|
||||
|
@ -235,8 +252,9 @@ func handleAssistantConversation(c tele.Context, thread []*tele.Message) error {
|
|||
|
||||
if reqErr != nil {
|
||||
logger.Errorw("assistant: failed to complete conversation", "error", reqErr)
|
||||
return c.Reply("Sorry, there's a technical issue. 😵💫 Please try again later.", tele.Silent)
|
||||
}
|
||||
return reqErr
|
||||
return nil
|
||||
}
|
||||
|
||||
func assistantRemoveMention(msg, name string) string {
|
||||
|
|
Loading…
Reference in New Issue