handle old conversations
This commit is contained in:
parent
9c77046f3b
commit
decaf484cd
|
@ -26,7 +26,7 @@ func (g GPT) Complete(conv *Conversation) (string, error) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
req := openai.ChatCompletionRequest{
|
req := openai.ChatCompletionRequest{
|
||||||
Model: openai.GPT3Dot5Turbo,
|
Model: openai.GPT4,
|
||||||
Messages: msg,
|
Messages: msg,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -119,19 +119,23 @@ func (m *Matrix) InviteHandler() (event.Type, mautrix.EventHandler) {
|
||||||
func (m *Matrix) RespondHandler() (event.Type, mautrix.EventHandler) {
|
func (m *Matrix) RespondHandler() (event.Type, mautrix.EventHandler) {
|
||||||
return event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
|
return event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
|
||||||
content := evt.Content.AsMessage()
|
content := evt.Content.AsMessage()
|
||||||
|
eventID := evt.ID
|
||||||
m.client.Log.Info().
|
m.client.Log.Info().
|
||||||
Str("content", content.Body).
|
Str("content", content.Body).
|
||||||
Msg("Received message")
|
Msg("received message")
|
||||||
|
|
||||||
|
conv := m.conversations.FindByEventID(eventID)
|
||||||
|
if conv != nil {
|
||||||
|
m.client.Log.Info().
|
||||||
|
Str("event_id", eventID.String()).
|
||||||
|
Msg("already known message, ignoring")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if evt.Sender != id.UserID(m.config.UserID) {
|
|
||||||
eventID := evt.ID
|
|
||||||
parentID := id.EventID("")
|
parentID := id.EventID("")
|
||||||
if relatesTo := content.GetRelatesTo(); relatesTo != nil {
|
if relatesTo := content.GetRelatesTo(); relatesTo != nil {
|
||||||
parentID = relatesTo.GetReplyTo()
|
parentID = relatesTo.GetReplyTo()
|
||||||
}
|
}
|
||||||
|
|
||||||
// find existing conversation and add message, or start a new one
|
|
||||||
var conv *Conversation
|
|
||||||
if parentID != "" {
|
if parentID != "" {
|
||||||
conv = m.conversations.FindByEventID(parentID)
|
conv = m.conversations.FindByEventID(parentID)
|
||||||
}
|
}
|
||||||
|
@ -142,12 +146,14 @@ func (m *Matrix) RespondHandler() (event.Type, mautrix.EventHandler) {
|
||||||
Role: openai.ChatMessageRoleUser,
|
Role: openai.ChatMessageRoleUser,
|
||||||
Content: content.Body,
|
Content: content.Body,
|
||||||
})
|
})
|
||||||
|
m.client.Log.Info().Msg("found parent, appending message to conversation")
|
||||||
} else {
|
} else {
|
||||||
conv = NewConversation(content.Body)
|
conv = NewConversation(content.Body)
|
||||||
m.conversations = append(m.conversations, conv)
|
m.conversations = append(m.conversations, conv)
|
||||||
|
m.client.Log.Info().Msg("no parent found, starting new conversation")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if evt.Sender != id.UserID(m.config.UserID) {
|
||||||
// get reply from GPT
|
// get reply from GPT
|
||||||
reply, err := m.gptClient.Complete(conv)
|
reply, err := m.gptClient.Complete(conv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -161,22 +167,12 @@ func (m *Matrix) RespondHandler() (event.Type, mautrix.EventHandler) {
|
||||||
EventID: eventID,
|
EventID: eventID,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
resp, err := m.client.SendMessageEvent(evt.RoomID, event.EventMessage, &formattedReply)
|
if _, err := m.client.SendMessageEvent(evt.RoomID, event.EventMessage, &formattedReply); err != nil {
|
||||||
if err != nil {
|
|
||||||
m.client.Log.Err(err).Msg("failed to send message")
|
m.client.Log.Err(err).Msg("failed to send message")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// add reply to conversation
|
|
||||||
conv.Add(Message{
|
|
||||||
EventID: resp.EventID,
|
|
||||||
Role: openai.ChatMessageRoleAssistant,
|
|
||||||
Content: reply,
|
|
||||||
ParentID: eventID,
|
|
||||||
})
|
|
||||||
|
|
||||||
m.client.Log.Info().Str("message", fmt.Sprintf("%+v", formattedReply.Body)).Msg("Sent reply")
|
m.client.Log.Info().Str("message", fmt.Sprintf("%+v", formattedReply.Body)).Msg("Sent reply")
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue