50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"rag-local/kb/dao"
|
|
"rag-local/kb/model/entity"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
)
|
|
|
|
var ConversationService = &conversationService{}
|
|
|
|
type conversationService struct{}
|
|
|
|
func (s *conversationService) List(ctx context.Context) ([]*entity.Conversation, error) {
|
|
return dao.Conversation.List(ctx)
|
|
}
|
|
|
|
func (s *conversationService) Save(ctx context.Context, c *entity.Conversation) (int64, error) {
|
|
if c.Id > 0 {
|
|
conv, err := dao.Conversation.GetOne(ctx, c.Id)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if conv == nil {
|
|
return 0, gerror.New("会话不存在")
|
|
}
|
|
if c.Title != "" && c.Title != conv.Title {
|
|
if err := dao.Conversation.UpdateTitle(ctx, c.Id, c.Title); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
if c.DatasetId > 0 && c.DatasetId != conv.DatasetId {
|
|
if err := dao.Conversation.UpdateDataset(ctx, c.Id, c.DatasetId); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
return c.Id, nil
|
|
}
|
|
if c.DatasetId <= 0 {
|
|
return 0, gerror.New("请选择知识库数据集")
|
|
}
|
|
return dao.Conversation.Insert(ctx, c.DatasetId, c.Title)
|
|
}
|
|
|
|
func (s *conversationService) Delete(ctx context.Context, id int64) error {
|
|
return dao.Conversation.Delete(ctx, id)
|
|
}
|