Skip to content

Commit

Permalink
Fix chat history extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
klipach committed Jan 15, 2025
1 parent e23b5ff commit ebddc21
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
25 changes: 19 additions & 6 deletions chat/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ const (
fromAI = "AI"
)

type firestoreMessage struct {
From string `firestore:"from"`
Message string `firestore:"message"`
}

type firestoreUser struct {
DisplayName string `firestore:"display_name"`
Chats []struct {
Messages []struct {
From string `firestore:"from"`
Message string `firestore:"message"`
} `firestore:"messages"`
ChatID int `firestore:"chat_id"`
Messages []firestoreMessage `firestore:"messages"`
} `firestore:"chats"`
}

Expand Down Expand Up @@ -54,12 +57,22 @@ func LoadChatHistory(ctx context.Context, userID string, chatID int) ([]llms.Mes
user := firestoreUser{}
userDoc.DataTo(&user)

if chatID >= len(user.Chats) {
var messages []firestoreMessage
chatFound := false
for _, c := range user.Chats {
if c.ChatID == chatID {
messages = c.Messages
logger.Printf("chat found: %d", chatID)
chatFound = true
break
}
}
if !chatFound {
logger.Printf("chat not found: %d", chatID)
return chatHistory, nil
}

for _, m := range user.Chats[chatID].Messages {
for _, m := range messages {
switch m.From {
case fromUser:
chatHistory = append(chatHistory, llms.TextParts(llms.ChatMessageTypeHuman, m.Message))
Expand Down
2 changes: 1 addition & 1 deletion prompts/short.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Ignore all previous conversations. Erase all prior dialogues.
##**Role**: You must analyze the conversation above and determine whether the most recent message requires external knowledge to answer.

- do not answer or repet the user's question
- if external knowledge is not required, respond with "no" and stop
- if external knowledge is not required, respond just "no" and stop
- if external knowledge is required, generate a prompt for an external LLM specifying exactly what information is needed

##** Today is: {{ .Today }}**
Expand Down

0 comments on commit ebddc21

Please sign in to comment.