Compare commits
No commits in common. "7e9d4e5052ca5917c20e3b29064b254d1575edb3" and "dcb251d2ad07d317d13d1201997df7a376e6b481" have entirely different histories.
7e9d4e5052
...
dcb251d2ad
32
assistant.go
32
assistant.go
|
@ -31,12 +31,6 @@ func matchAssistantConversation(botUsr *tele.User, msg *tele.Message) []*tele.Me
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, msg := range thread {
|
for _, msg := range thread {
|
||||||
for _, ent := range msg.Entities {
|
|
||||||
if ent.Type == tele.EntityCommand {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
from := msg.Sender
|
from := msg.Sender
|
||||||
chat := msg.Chat
|
chat := msg.Chat
|
||||||
if from == nil || chat == nil {
|
if from == nil || chat == nil {
|
||||||
|
@ -90,22 +84,12 @@ func matchAssistantConversation(botUsr *tele.User, msg *tele.Message) []*tele.Me
|
||||||
type assistantStreamedResponseCb func(text string, finished bool) (*tele.Message, error)
|
type assistantStreamedResponseCb func(text string, finished bool) (*tele.Message, error)
|
||||||
|
|
||||||
func assistantStreamedResponse(request openai.ChatRequest, cb assistantStreamedResponseCb) error {
|
func assistantStreamedResponse(request openai.ChatRequest, cb assistantStreamedResponseCb) error {
|
||||||
logger.Debugw("Openai chat request", "req", request)
|
logger.Debugf("Openai chat request: %#+v", request)
|
||||||
ai := openai.NewClient(config.OpenAIApiKey)
|
ai := openai.NewClient(config.OpenAIApiKey)
|
||||||
|
|
||||||
var (
|
resp, err := ai.ChatCompletionStream(request)
|
||||||
resp *openai.ChatResponseStream
|
if err != nil {
|
||||||
err error
|
return err
|
||||||
)
|
|
||||||
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
|
nErrs := 0
|
||||||
|
@ -204,7 +188,7 @@ func handleAssistantConversation(c tele.Context, thread []*tele.Message) error {
|
||||||
role := openai.ChatRoleUser
|
role := openai.ChatRoleUser
|
||||||
from := thread[l].Sender
|
from := thread[l].Sender
|
||||||
if from != nil && from.ID == me.ID {
|
if from != nil && from.ID == me.ID {
|
||||||
role = openai.ChatRoleAssistant
|
role = openai.ChatRoleSystem
|
||||||
}
|
}
|
||||||
|
|
||||||
convMsgs = append(convMsgs, openai.ChatMessage{
|
convMsgs = append(convMsgs, openai.ChatMessage{
|
||||||
|
@ -237,12 +221,11 @@ func handleAssistantConversation(c tele.Context, thread []*tele.Message) error {
|
||||||
var err error
|
var err error
|
||||||
if replyMsg == nil {
|
if replyMsg == nil {
|
||||||
<-typingNotifyCh
|
<-typingNotifyCh
|
||||||
replyMsg, err = c.Bot().Reply(lastMsg, text, tele.Silent)
|
replyMsg, err = c.Bot().Reply(c.Message(), text, tele.Silent)
|
||||||
} else {
|
} else {
|
||||||
replyMsg, err = c.Bot().Edit(replyMsg, text)
|
replyMsg, err = c.Bot().Edit(replyMsg, text)
|
||||||
}
|
}
|
||||||
if finished && err == nil {
|
if finished && err == nil {
|
||||||
replyMsg.ReplyTo = lastMsg // nasty bug
|
|
||||||
if err := cacheMessage(replyMsg); err != nil {
|
if err := cacheMessage(replyMsg); err != nil {
|
||||||
logger.Warnw("failed to cache message", "error", err)
|
logger.Warnw("failed to cache message", "error", err)
|
||||||
}
|
}
|
||||||
|
@ -252,9 +235,8 @@ func handleAssistantConversation(c tele.Context, thread []*tele.Message) error {
|
||||||
|
|
||||||
if reqErr != nil {
|
if reqErr != nil {
|
||||||
logger.Errorw("assistant: failed to complete conversation", "error", reqErr)
|
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 nil
|
return reqErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func assistantRemoveMention(msg, name string) string {
|
func assistantRemoveMention(msg, name string) string {
|
||||||
|
|
Loading…
Reference in New Issue