正在显示
12 个修改的文件
包含
352 行增加
和
46 行删除
1 | +package chat | ||
2 | + | ||
3 | +import ( | ||
4 | + "net/http" | ||
5 | + | ||
6 | + "github.com/zeromicro/go-zero/rest/httpx" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/chat" | ||
8 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc" | ||
9 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types" | ||
10 | +) | ||
11 | + | ||
12 | +func ChatSessionAddFilesHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | ||
13 | + return func(w http.ResponseWriter, r *http.Request) { | ||
14 | + var req types.ChatSessionAddFilesRequest | ||
15 | + if err := httpx.Parse(r, &req); err != nil { | ||
16 | + httpx.ErrorCtx(r.Context(), w, err) | ||
17 | + return | ||
18 | + } | ||
19 | + | ||
20 | + l := chat.NewChatSessionAddFilesLogic(r.Context(), svcCtx) | ||
21 | + resp, err := l.ChatSessionAddFiles(&req) | ||
22 | + if err != nil { | ||
23 | + httpx.ErrorCtx(r.Context(), w, err) | ||
24 | + } else { | ||
25 | + httpx.OkJsonCtx(r.Context(), w, resp) | ||
26 | + } | ||
27 | + } | ||
28 | +} |
1 | +package chat | ||
2 | + | ||
3 | +import ( | ||
4 | + "net/http" | ||
5 | + | ||
6 | + "github.com/zeromicro/go-zero/rest/httpx" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/chat" | ||
8 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc" | ||
9 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types" | ||
10 | +) | ||
11 | + | ||
12 | +func ChatSessionRemoveFilesHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | ||
13 | + return func(w http.ResponseWriter, r *http.Request) { | ||
14 | + var req types.ChatSessionAddFilesRequest | ||
15 | + if err := httpx.Parse(r, &req); err != nil { | ||
16 | + httpx.ErrorCtx(r.Context(), w, err) | ||
17 | + return | ||
18 | + } | ||
19 | + | ||
20 | + l := chat.NewChatSessionRemoveFilesLogic(r.Context(), svcCtx) | ||
21 | + resp, err := l.ChatSessionRemoveFiles(&req) | ||
22 | + if err != nil { | ||
23 | + httpx.ErrorCtx(r.Context(), w, err) | ||
24 | + } else { | ||
25 | + httpx.OkJsonCtx(r.Context(), w, resp) | ||
26 | + } | ||
27 | + } | ||
28 | +} |
@@ -54,6 +54,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | @@ -54,6 +54,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | ||
54 | }, | 54 | }, |
55 | { | 55 | { |
56 | Method: http.MethodPost, | 56 | Method: http.MethodPost, |
57 | + Path: "/chat/session/add_files", | ||
58 | + Handler: chat.ChatSessionAddFilesHandler(serverCtx), | ||
59 | + }, | ||
60 | + { | ||
61 | + Method: http.MethodPost, | ||
62 | + Path: "/chat/session/remove_files", | ||
63 | + Handler: chat.ChatSessionRemoveFilesHandler(serverCtx), | ||
64 | + }, | ||
65 | + { | ||
66 | + Method: http.MethodPost, | ||
57 | Path: "/chat/session/my_spark_sessions", | 67 | Path: "/chat/session/my_spark_sessions", |
58 | Handler: chat.ChatMySparkSessionsHandler(serverCtx), | 68 | Handler: chat.ChatMySparkSessionsHandler(serverCtx), |
59 | }, | 69 | }, |
1 | +package chat | ||
2 | + | ||
3 | +import ( | ||
4 | + "context" | ||
5 | + "github.com/samber/lo" | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/core" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain" | ||
8 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/contextdata" | ||
9 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction" | ||
10 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr" | ||
11 | + | ||
12 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc" | ||
13 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types" | ||
14 | + | ||
15 | + "github.com/zeromicro/go-zero/core/logx" | ||
16 | +) | ||
17 | + | ||
18 | +type ChatSessionAddFilesLogic struct { | ||
19 | + logx.Logger | ||
20 | + ctx context.Context | ||
21 | + svcCtx *svc.ServiceContext | ||
22 | +} | ||
23 | + | ||
24 | +func NewChatSessionAddFilesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatSessionAddFilesLogic { | ||
25 | + return &ChatSessionAddFilesLogic{ | ||
26 | + Logger: logx.WithContext(ctx), | ||
27 | + ctx: ctx, | ||
28 | + svcCtx: svcCtx, | ||
29 | + } | ||
30 | +} | ||
31 | + | ||
32 | +func (l *ChatSessionAddFilesLogic) ChatSessionAddFiles(req *types.ChatSessionAddFilesRequest) (resp *types.ChatSessionAddFilesResponse, err error) { | ||
33 | + var ( | ||
34 | + conn = l.svcCtx.DefaultDBConn() | ||
35 | + dm *domain.ChatSession | ||
36 | + token = contextdata.GetUserTokenFromCtx(l.ctx) | ||
37 | + ) | ||
38 | + if dm, err = l.svcCtx.ChatSessionRepository.FindOne(l.ctx, conn, req.Id); err != nil { | ||
39 | + return nil, xerr.NewErrMsgErr("不存在", err) | ||
40 | + } | ||
41 | + if dm.UserId != token.UserId { | ||
42 | + return nil, xerr.NewErrMsgErr("无权限", err) | ||
43 | + } | ||
44 | + // 赋值 | ||
45 | + if dm.Module != domain.ModuleSparkChat { | ||
46 | + return nil, xerr.NewErrMsgErr("类型有误,星火文档类型才可以添加文档", err) | ||
47 | + } | ||
48 | + | ||
49 | + // 更新 | ||
50 | + if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error { | ||
51 | + // 知识库移除 | ||
52 | + if dm.Type == domain.TypeSparkDatasetChat { | ||
53 | + if err = core.DatasetAddFiles(l.ctx, l.svcCtx, conn, dm.Metadata.DatasetId, req.DocumentIds); err != nil { | ||
54 | + return err | ||
55 | + } | ||
56 | + return nil | ||
57 | + } | ||
58 | + // 普通多文档移除 | ||
59 | + dm.Metadata.DocumentIds = lo.Union(dm.Metadata.DocumentIds, req.DocumentIds) | ||
60 | + dm, err = l.svcCtx.ChatSessionRepository.UpdateWithVersion(l.ctx, conn, dm) | ||
61 | + return err | ||
62 | + }, true); err != nil { | ||
63 | + return nil, xerr.NewErrMsg("更新失败") | ||
64 | + } | ||
65 | + resp = &types.ChatSessionAddFilesResponse{} | ||
66 | + return | ||
67 | +} |
1 | +package chat | ||
2 | + | ||
3 | +import ( | ||
4 | + "context" | ||
5 | + "github.com/samber/lo" | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/core" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain" | ||
8 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/contextdata" | ||
9 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction" | ||
10 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr" | ||
11 | + | ||
12 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc" | ||
13 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types" | ||
14 | + | ||
15 | + "github.com/zeromicro/go-zero/core/logx" | ||
16 | +) | ||
17 | + | ||
18 | +type ChatSessionRemoveFilesLogic struct { | ||
19 | + logx.Logger | ||
20 | + ctx context.Context | ||
21 | + svcCtx *svc.ServiceContext | ||
22 | +} | ||
23 | + | ||
24 | +func NewChatSessionRemoveFilesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatSessionRemoveFilesLogic { | ||
25 | + return &ChatSessionRemoveFilesLogic{ | ||
26 | + Logger: logx.WithContext(ctx), | ||
27 | + ctx: ctx, | ||
28 | + svcCtx: svcCtx, | ||
29 | + } | ||
30 | +} | ||
31 | + | ||
32 | +func (l *ChatSessionRemoveFilesLogic) ChatSessionRemoveFiles(req *types.ChatSessionAddFilesRequest) (resp *types.ChatSessionAddFilesResponse, err error) { | ||
33 | + var ( | ||
34 | + conn = l.svcCtx.DefaultDBConn() | ||
35 | + dm *domain.ChatSession | ||
36 | + token = contextdata.GetUserTokenFromCtx(l.ctx) | ||
37 | + ) | ||
38 | + if dm, err = l.svcCtx.ChatSessionRepository.FindOne(l.ctx, conn, req.Id); err != nil { | ||
39 | + return nil, xerr.NewErrMsgErr("不存在", err) | ||
40 | + } | ||
41 | + if dm.UserId != token.UserId { | ||
42 | + return nil, xerr.NewErrMsgErr("无权限", err) | ||
43 | + } | ||
44 | + // 赋值 | ||
45 | + if dm.Module != domain.ModuleSparkChat { | ||
46 | + return nil, xerr.NewErrMsgErr("类型有误,星火文档类型才可以添加文档", err) | ||
47 | + } | ||
48 | + | ||
49 | + // 更新 | ||
50 | + if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error { | ||
51 | + // 知识库移除 | ||
52 | + if dm.Type == domain.TypeSparkDatasetChat { | ||
53 | + if err = core.DatasetRemoveFiles(l.ctx, l.svcCtx, conn, dm.Metadata.DatasetId, req.DocumentIds); err != nil { | ||
54 | + return err | ||
55 | + } | ||
56 | + return nil | ||
57 | + } | ||
58 | + dm.Metadata.DocumentIds = lo.Without(dm.Metadata.DocumentIds, req.DocumentIds...) | ||
59 | + dm, err = l.svcCtx.ChatSessionRepository.UpdateWithVersion(l.ctx, conn, dm) | ||
60 | + return err | ||
61 | + }, true); err != nil { | ||
62 | + return nil, xerr.NewErrMsg("更新失败") | ||
63 | + } | ||
64 | + resp = &types.ChatSessionAddFilesResponse{} | ||
65 | + return | ||
66 | +} |
@@ -3,8 +3,10 @@ package chat | @@ -3,8 +3,10 @@ package chat | ||
3 | import ( | 3 | import ( |
4 | "context" | 4 | "context" |
5 | "fmt" | 5 | "fmt" |
6 | + "github.com/samber/lo" | ||
6 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain" | 7 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain" |
7 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/contextdata" | 8 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/contextdata" |
9 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/tool" | ||
8 | 10 | ||
9 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc" | 11 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc" |
10 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types" | 12 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types" |
@@ -47,8 +49,45 @@ func (l *ChatSessionSearchLogic) ChatSessionSearch(req *types.ChatSessionSearchR | @@ -47,8 +49,45 @@ func (l *ChatSessionSearchLogic) ChatSessionSearch(req *types.ChatSessionSearchR | ||
47 | 49 | ||
48 | total, dms, err = l.svcCtx.ChatSessionRepository.Find(l.ctx, conn, queryOptions) | 50 | total, dms, err = l.svcCtx.ChatSessionRepository.Find(l.ctx, conn, queryOptions) |
49 | list := make([]types.ChatSessionItem, 0) | 51 | list := make([]types.ChatSessionItem, 0) |
50 | - for i := range dms { | ||
51 | - list = append(list, NewTypesChatSession(dms[i])) | 52 | + |
53 | + // 1.分组dataset document mapping | ||
54 | + var datasetIds []int64 | ||
55 | + for _, session := range dms { | ||
56 | + if session.Type == domain.TypeSparkDatasetChat && session.Metadata.DatasetId > 0 { | ||
57 | + datasetIds = append(datasetIds, session.Metadata.DatasetId) | ||
58 | + } | ||
59 | + datasetIds = lo.Uniq(datasetIds) | ||
60 | + } | ||
61 | + var groupDocumentMappings map[int64][]*domain.ChatDatasetDocumentMapping | ||
62 | + if len(datasetIds) > 0 { | ||
63 | + _, documentMapping, _ := l.svcCtx.ChatDatasetDocumentMappingRepository.FindByDataset(l.ctx, conn, datasetIds...) | ||
64 | + groupDocumentMappings = lo.GroupBy(documentMapping, func(item *domain.ChatDatasetDocumentMapping) int64 { | ||
65 | + return item.DatasetId | ||
66 | + }) | ||
67 | + } | ||
68 | + | ||
69 | + // 2. 加载documents | ||
70 | + lazyDocument := tool.NewLazyLoadService(l.svcCtx.ChatDocumentRepository.FindOne) | ||
71 | + for i, session := range dms { | ||
72 | + var documents []types.ChatDocumentItem | ||
73 | + if session.Type == domain.TypeSparkDocumentsChat && len(session.Metadata.DocumentIds) > 0 { | ||
74 | + for _, id := range session.Metadata.DocumentIds { | ||
75 | + if document, _ := lazyDocument.Load(l.ctx, conn, id); document != nil { | ||
76 | + documents = append(documents, types.NewTypesChatDocument(document)) | ||
77 | + } | ||
78 | + } | ||
79 | + } else if session.Type == domain.TypeSparkDatasetChat && session.Metadata.DatasetId > 0 { | ||
80 | + if documentMapping, ok := groupDocumentMappings[session.Metadata.DatasetId]; ok { | ||
81 | + lo.ForEach(documentMapping, func(item *domain.ChatDatasetDocumentMapping, index int) { | ||
82 | + if document, _ := lazyDocument.Load(l.ctx, conn, item.DocumentId); document != nil { | ||
83 | + documents = append(documents, types.NewTypesChatDocument(document)) | ||
84 | + } | ||
85 | + }) | ||
86 | + } | ||
87 | + } | ||
88 | + typesSession := NewTypesChatSession(dms[i]) | ||
89 | + typesSession.Documents = documents | ||
90 | + list = append(list, typesSession) | ||
52 | } | 91 | } |
53 | resp = &types.ChatSessionSearchResponse{ | 92 | resp = &types.ChatSessionSearchResponse{ |
54 | List: list, | 93 | List: list, |
1 | +package core | ||
2 | + | ||
3 | +import ( | ||
4 | + "context" | ||
5 | + "github.com/samber/lo" | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain" | ||
8 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction" | ||
9 | +) | ||
10 | + | ||
11 | +func DatasetAddFiles(ctx context.Context, svcCtx *svc.ServiceContext, conn transaction.Conn, datasetId int64, documents []int64) error { | ||
12 | + var ( | ||
13 | + documentMapping []*domain.ChatDatasetDocumentMapping | ||
14 | + err error | ||
15 | + ) | ||
16 | + _, documentMapping, _ = svcCtx.ChatDatasetDocumentMappingRepository.FindByDataset(ctx, conn, datasetId) | ||
17 | + | ||
18 | + for _, id := range documents { | ||
19 | + if lo.ContainsBy(documentMapping, func(item *domain.ChatDatasetDocumentMapping) bool { | ||
20 | + return item.DocumentId == id | ||
21 | + }) { | ||
22 | + continue | ||
23 | + } | ||
24 | + if _, err = svcCtx.ChatDatasetDocumentMappingRepository.Insert(ctx, conn, &domain.ChatDatasetDocumentMapping{ | ||
25 | + DatasetId: datasetId, | ||
26 | + DocumentId: id, | ||
27 | + }); err != nil { | ||
28 | + return err | ||
29 | + } | ||
30 | + } | ||
31 | + return nil | ||
32 | +} |
1 | +package core | ||
2 | + | ||
3 | +import ( | ||
4 | + "context" | ||
5 | + "github.com/samber/lo" | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain" | ||
8 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction" | ||
9 | +) | ||
10 | + | ||
11 | +func DatasetRemoveFiles(ctx context.Context, svcCtx *svc.ServiceContext, conn transaction.Conn, datasetId int64, documents []int64) error { | ||
12 | + var ( | ||
13 | + documentMapping []*domain.ChatDatasetDocumentMapping | ||
14 | + err error | ||
15 | + ) | ||
16 | + _, documentMapping, _ = svcCtx.ChatDatasetDocumentMappingRepository.FindByDataset(ctx, conn, datasetId) | ||
17 | + | ||
18 | + for _, id := range documents { | ||
19 | + var found *domain.ChatDatasetDocumentMapping | ||
20 | + if !lo.ContainsBy(documentMapping, func(item *domain.ChatDatasetDocumentMapping) bool { | ||
21 | + if item.DocumentId == id { | ||
22 | + found = item | ||
23 | + } | ||
24 | + return item.DocumentId == id | ||
25 | + }) { | ||
26 | + continue | ||
27 | + } | ||
28 | + if _, err = svcCtx.ChatDatasetDocumentMappingRepository.Delete(ctx, conn, found); err != nil { | ||
29 | + return err | ||
30 | + } | ||
31 | + } | ||
32 | + return nil | ||
33 | +} |
@@ -2,7 +2,7 @@ package dataset | @@ -2,7 +2,7 @@ package dataset | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "context" | 4 | "context" |
5 | - "github.com/samber/lo" | 5 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/core" |
6 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain" | 6 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain" |
7 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction" | 7 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction" |
8 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr" | 8 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr" |
@@ -29,28 +29,16 @@ func NewChatDatasetAddFilesLogic(ctx context.Context, svcCtx *svc.ServiceContext | @@ -29,28 +29,16 @@ func NewChatDatasetAddFilesLogic(ctx context.Context, svcCtx *svc.ServiceContext | ||
29 | 29 | ||
30 | func (l *ChatDatasetAddFilesLogic) ChatDatasetAddFiles(req *types.ChatDatasetAddFilesRequest) (resp *types.ChatDatasetAddFilesResponse, err error) { | 30 | func (l *ChatDatasetAddFilesLogic) ChatDatasetAddFiles(req *types.ChatDatasetAddFilesRequest) (resp *types.ChatDatasetAddFilesResponse, err error) { |
31 | var ( | 31 | var ( |
32 | - conn = l.svcCtx.DefaultDBConn() | ||
33 | - dm *domain.ChatDataset | ||
34 | - documentMapping []*domain.ChatDatasetDocumentMapping | 32 | + conn = l.svcCtx.DefaultDBConn() |
33 | + dm *domain.ChatDataset | ||
35 | ) | 34 | ) |
36 | // 货号唯一 | 35 | // 货号唯一 |
37 | if dm, err = l.svcCtx.ChatDatasetRepository.FindOne(l.ctx, conn, req.Id); err != nil { | 36 | if dm, err = l.svcCtx.ChatDatasetRepository.FindOne(l.ctx, conn, req.Id); err != nil { |
38 | - return nil, xerr.NewErrMsgErr("不存在", err) | 37 | + return nil, xerr.NewErrMsgErr("知识库不存在", err) |
39 | } | 38 | } |
40 | - _, documentMapping, _ = l.svcCtx.ChatDatasetDocumentMappingRepository.FindByDataset(l.ctx, conn, dm.Id) | ||
41 | if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error { | 39 | if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error { |
42 | - for _, id := range req.DocumentIds { | ||
43 | - if lo.ContainsBy(documentMapping, func(item *domain.ChatDatasetDocumentMapping) bool { | ||
44 | - return item.DocumentId == id | ||
45 | - }) { | ||
46 | - continue | ||
47 | - } | ||
48 | - if _, err = l.svcCtx.ChatDatasetDocumentMappingRepository.Insert(l.ctx, conn, &domain.ChatDatasetDocumentMapping{ | ||
49 | - DatasetId: dm.Id, | ||
50 | - DocumentId: id, | ||
51 | - }); err != nil { | ||
52 | - return err | ||
53 | - } | 40 | + if err = core.DatasetAddFiles(l.ctx, l.svcCtx, conn, dm.Id, req.DocumentIds); err != nil { |
41 | + return err | ||
54 | } | 42 | } |
55 | return err | 43 | return err |
56 | }, true); err != nil { | 44 | }, true); err != nil { |
@@ -2,7 +2,7 @@ package dataset | @@ -2,7 +2,7 @@ package dataset | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "context" | 4 | "context" |
5 | - "github.com/samber/lo" | 5 | + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/core" |
6 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain" | 6 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain" |
7 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction" | 7 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction" |
8 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr" | 8 | "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr" |
@@ -29,29 +29,16 @@ func NewChatDatasetRemvoeFilesLogic(ctx context.Context, svcCtx *svc.ServiceCont | @@ -29,29 +29,16 @@ func NewChatDatasetRemvoeFilesLogic(ctx context.Context, svcCtx *svc.ServiceCont | ||
29 | 29 | ||
30 | func (l *ChatDatasetRemvoeFilesLogic) ChatDatasetRemvoeFiles(req *types.ChatDatasetAddFilesRequest) (resp *types.ChatDatasetAddFilesResponse, err error) { | 30 | func (l *ChatDatasetRemvoeFilesLogic) ChatDatasetRemvoeFiles(req *types.ChatDatasetAddFilesRequest) (resp *types.ChatDatasetAddFilesResponse, err error) { |
31 | var ( | 31 | var ( |
32 | - conn = l.svcCtx.DefaultDBConn() | ||
33 | - dm *domain.ChatDataset | ||
34 | - documentMapping []*domain.ChatDatasetDocumentMapping | 32 | + conn = l.svcCtx.DefaultDBConn() |
33 | + dm *domain.ChatDataset | ||
35 | ) | 34 | ) |
36 | // 货号唯一 | 35 | // 货号唯一 |
37 | if dm, err = l.svcCtx.ChatDatasetRepository.FindOne(l.ctx, conn, req.Id); err != nil { | 36 | if dm, err = l.svcCtx.ChatDatasetRepository.FindOne(l.ctx, conn, req.Id); err != nil { |
38 | return nil, xerr.NewErrMsgErr("不存在", err) | 37 | return nil, xerr.NewErrMsgErr("不存在", err) |
39 | } | 38 | } |
40 | - _, documentMapping, _ = l.svcCtx.ChatDatasetDocumentMappingRepository.FindByDataset(l.ctx, conn, dm.Id) | ||
41 | if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error { | 39 | if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error { |
42 | - for _, id := range req.DocumentIds { | ||
43 | - var found *domain.ChatDatasetDocumentMapping | ||
44 | - if !lo.ContainsBy(documentMapping, func(item *domain.ChatDatasetDocumentMapping) bool { | ||
45 | - if item.DocumentId == id { | ||
46 | - found = item | ||
47 | - } | ||
48 | - return item.DocumentId == id | ||
49 | - }) { | ||
50 | - continue | ||
51 | - } | ||
52 | - if _, err = l.svcCtx.ChatDatasetDocumentMappingRepository.Delete(l.ctx, conn, found); err != nil { | ||
53 | - return err | ||
54 | - } | 40 | + if err = core.DatasetRemoveFiles(l.ctx, l.svcCtx, conn, dm.Id, req.DocumentIds); err != nil { |
41 | + return err | ||
55 | } | 42 | } |
56 | return err | 43 | return err |
57 | }, true); err != nil { | 44 | }, true); err != nil { |
@@ -48,14 +48,15 @@ type ChatSessionSearchResponse struct { | @@ -48,14 +48,15 @@ type ChatSessionSearchResponse struct { | ||
48 | } | 48 | } |
49 | 49 | ||
50 | type ChatSessionItem struct { | 50 | type ChatSessionItem struct { |
51 | - Id int64 `json:"id,optional,omitempty"` // 唯一标识 | ||
52 | - Title string `json:"title,optional,omitempty"` // 会话标题 | ||
53 | - Abstract string `json:"abstract,optional,omitempty"` // 摘要 | ||
54 | - CreatedAt int64 `json:"createdAt,optional,omitempty"` // 创建时间 | ||
55 | - Module int `json:"module,optional,omitempty,default=1"` // 1:openai chat 2:星火文档问答 | ||
56 | - Type string `json:"type,optional,omitempty,default=chat"` // 类型 chat:普通问答 spark_dataset_chat:星火知识库问答 spark_documents_chat:星火多文档问答 | ||
57 | - DatasetId int64 `json:"datasetId,optional,omitempty"` // 知识库 | ||
58 | - DocumentIds []int64 `json:"documentIds,optional,omitempty"` // 多文档 | 51 | + Id int64 `json:"id,optional,omitempty"` // 唯一标识 |
52 | + Title string `json:"title,optional,omitempty"` // 会话标题 | ||
53 | + Abstract string `json:"abstract,optional,omitempty"` // 摘要 | ||
54 | + CreatedAt int64 `json:"createdAt,optional,omitempty"` // 创建时间 | ||
55 | + Module int `json:"module,optional,omitempty,default=1"` // 1:openai chat 2:星火文档问答 | ||
56 | + Type string `json:"type,optional,omitempty,default=chat"` // 类型 chat:普通问答 spark_dataset_chat:星火知识库问答 spark_documents_chat:星火多文档问答 | ||
57 | + DatasetId int64 `json:"datasetId,optional,omitempty"` // 知识库 | ||
58 | + DocumentIds []int64 `json:"documentIds,optional,omitempty"` // 多文档 | ||
59 | + Documents []ChatDocumentItem `json:"documents,optional,omitempty"` // 多文档 | ||
59 | } | 60 | } |
60 | 61 | ||
61 | type ChatModelsRequest struct { | 62 | type ChatModelsRequest struct { |
@@ -130,6 +131,14 @@ type User struct { | @@ -130,6 +131,14 @@ type User struct { | ||
130 | Avatar string `json:"avatar"` // 头像 | 131 | Avatar string `json:"avatar"` // 头像 |
131 | } | 132 | } |
132 | 133 | ||
134 | +type ChatSessionAddFilesRequest struct { | ||
135 | + Id int64 `json:"id"` // 文档ID | ||
136 | + DocumentIds []int64 `json:"documentIds"` // 文档ID列表 | ||
137 | +} | ||
138 | + | ||
139 | +type ChatSessionAddFilesResponse struct { | ||
140 | +} | ||
141 | + | ||
133 | type ChatDocumentGetRequest struct { | 142 | type ChatDocumentGetRequest struct { |
134 | Id int64 `path:"id"` | 143 | Id int64 `path:"id"` |
135 | } | 144 | } |
@@ -30,6 +30,13 @@ service Core { | @@ -30,6 +30,13 @@ service Core { | ||
30 | @handler chatSessionConversationWs | 30 | @handler chatSessionConversationWs |
31 | get /chat/session/conversation (ChatSessionConversationRequestWs) returns (ChatSessionConversationResponse) | 31 | get /chat/session/conversation (ChatSessionConversationRequestWs) returns (ChatSessionConversationResponse) |
32 | 32 | ||
33 | + @doc "聊天会话-添加文件" | ||
34 | + @handler chatSessionAddFiles | ||
35 | + post /chat/session/add_files (ChatSessionAddFilesRequest) returns (ChatSessionAddFilesResponse) | ||
36 | + @doc "聊天会话-移除文件" | ||
37 | + @handler chatSessionRemoveFiles | ||
38 | + post /chat/session/remove_files (ChatSessionAddFilesRequest) returns (ChatSessionAddFilesResponse) | ||
39 | + | ||
33 | @doc "星火聊天会话-我的会话" | 40 | @doc "星火聊天会话-我的会话" |
34 | @handler chatMySparkSessions | 41 | @handler chatMySparkSessions |
35 | post /chat/session/my_spark_sessions (ChatSessionSearchRequest) returns (ChatSessionSearchResponse) | 42 | post /chat/session/my_spark_sessions (ChatSessionSearchRequest) returns (ChatSessionSearchResponse) |
@@ -95,6 +102,7 @@ type ( | @@ -95,6 +102,7 @@ type ( | ||
95 | Type string `json:"type,optional,omitempty,default=chat"` // 类型 chat:普通问答 spark_dataset_chat:星火知识库问答 spark_documents_chat:星火多文档问答 | 102 | Type string `json:"type,optional,omitempty,default=chat"` // 类型 chat:普通问答 spark_dataset_chat:星火知识库问答 spark_documents_chat:星火多文档问答 |
96 | DatasetId int64 `json:"datasetId,optional,omitempty"` // 知识库 | 103 | DatasetId int64 `json:"datasetId,optional,omitempty"` // 知识库 |
97 | DocumentIds []int64 `json:"documentIds,optional,omitempty"` // 多文档 | 104 | DocumentIds []int64 `json:"documentIds,optional,omitempty"` // 多文档 |
105 | + Documents []ChatDocumentItem `json:"documents,optional,omitempty"` // 多文档 | ||
98 | } | 106 | } |
99 | ) | 107 | ) |
100 | 108 | ||
@@ -170,4 +178,15 @@ type( | @@ -170,4 +178,15 @@ type( | ||
170 | Name string `json:"name"` // 名称 | 178 | Name string `json:"name"` // 名称 |
171 | Avatar string `json:"avatar"` // 头像 | 179 | Avatar string `json:"avatar"` // 头像 |
172 | } | 180 | } |
181 | +) | ||
182 | + | ||
183 | +// 会话添加新文档 | ||
184 | +type( | ||
185 | + ChatSessionAddFilesRequest{ | ||
186 | + Id int64 `json:"id"` // 文档ID | ||
187 | + DocumentIds []int64 `json:"documentIds"` // 文档ID列表 | ||
188 | + } | ||
189 | + ChatSessionAddFilesResponse{ | ||
190 | + | ||
191 | + } | ||
173 | ) | 192 | ) |
-
请 注册 或 登录 后发表评论