Files
YaeMikoBot/database/mdb/ai_chats.go

35 lines
877 B
Go

package mdb
import (
"context"
"time"
"ymgb/database"
"go.mongodb.org/mongo-driver/v2/bson"
)
func GetGptChatHistory(db *database.Context, 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 *database.Context, 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
}