36 lines
933 B
Go
36 lines
933 B
Go
package mdb
|
|
|
|
import (
|
|
"context"
|
|
"kurumibot/database"
|
|
"time"
|
|
|
|
"git.nix13.pw/scuroneko/laniakea"
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
)
|
|
|
|
func GetGptChatHistory(db *laniakea.DatabaseContext, chatId string) ([]AiChatMessage, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
col := database.GetMongoCollection(db, "gpt_chat_messages")
|
|
cursor, err := col.Find(ctx, bson.M{"chatId": chatId})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := make([]AiChatMessage, 0)
|
|
err = cursor.All(ctx, &result)
|
|
return result, err
|
|
}
|
|
func UpdateGptChatHistory(db *laniakea.DatabaseContext, chatId, role, message string) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
col := database.GetMongoCollection(db, "gpt_chat_messages")
|
|
_, err := col.InsertOne(ctx, AiChatMessage{
|
|
bson.NewObjectID(),
|
|
chatId,
|
|
role,
|
|
message, 0,
|
|
})
|
|
return err
|
|
}
|