作者 yangfu

feat-1.0 chat doc

正在显示 62 个修改的文件 包含 4255 行增加201 行删除

要显示太多修改。

为保证性能只显示 62 of 62+ 个文件。

@@ -8,7 +8,7 @@ Timeout: 30000 @@ -8,7 +8,7 @@ Timeout: 30000
8 LogRequest: true # 记录详细请求日志 8 LogRequest: true # 记录详细请求日志
9 9
10 Log: 10 Log:
11 - Mode: file 11 + #Mode: file
12 Encoding: plain 12 Encoding: plain
13 Level: debug # info 13 Level: debug # info
14 MaxSize: 1 # 2MB 14 MaxSize: 1 # 2MB
  1 +package chat
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  5 + "net/http"
  6 +
  7 + "github.com/zeromicro/go-zero/rest/httpx"
  8 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/chat"
  9 + "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"
  11 +)
  12 +
  13 +func ChatMySparkSessionsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  14 + return func(w http.ResponseWriter, r *http.Request) {
  15 + var req types.ChatSessionSearchRequest
  16 + if err := httpx.Parse(r, &req); err != nil {
  17 + httpx.ErrorCtx(r.Context(), w, err)
  18 + return
  19 + }
  20 +
  21 + l := chat.NewChatSessionSearchLogic(r.Context(), svcCtx)
  22 + req.Module = domain.ModuleSparkChat
  23 + resp, err := l.ChatSessionSearch(&req)
  24 + if err != nil {
  25 + httpx.ErrorCtx(r.Context(), w, err)
  26 + } else {
  27 + httpx.OkJsonCtx(r.Context(), w, resp)
  28 + }
  29 + }
  30 +}
1 package chat 1 package chat
2 2
3 import ( 3 import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
4 "net/http" 5 "net/http"
5 6
6 "github.com/zeromicro/go-zero/rest/httpx" 7 "github.com/zeromicro/go-zero/rest/httpx"
@@ -18,6 +19,8 @@ func ChatSessionSaveHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { @@ -18,6 +19,8 @@ func ChatSessionSaveHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
18 } 19 }
19 20
20 l := chat.NewChatSessionSaveLogic(r.Context(), svcCtx) 21 l := chat.NewChatSessionSaveLogic(r.Context(), svcCtx)
  22 + req.ChatSession.Module = domain.ModuleOpenaiChat
  23 + req.ChatSession.Type = domain.TypeChat
21 resp, err := l.ChatSessionSave(&req) 24 resp, err := l.ChatSessionSave(&req)
22 if err != nil { 25 if err != nil {
23 httpx.ErrorCtx(r.Context(), w, err) 26 httpx.ErrorCtx(r.Context(), w, err)
1 package chat 1 package chat
2 2
3 import ( 3 import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
4 "net/http" 5 "net/http"
5 6
6 "github.com/zeromicro/go-zero/rest/httpx" 7 "github.com/zeromicro/go-zero/rest/httpx"
@@ -18,6 +19,7 @@ func ChatSessionSearchHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { @@ -18,6 +19,7 @@ func ChatSessionSearchHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
18 } 19 }
19 20
20 l := chat.NewChatSessionSearchLogic(r.Context(), svcCtx) 21 l := chat.NewChatSessionSearchLogic(r.Context(), svcCtx)
  22 + req.Module = domain.ModuleOpenaiChat
21 resp, err := l.ChatSessionSearch(&req) 23 resp, err := l.ChatSessionSearch(&req)
22 if err != nil { 24 if err != nil {
23 httpx.ErrorCtx(r.Context(), w, err) 25 httpx.ErrorCtx(r.Context(), w, err)
  1 +package chat
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  5 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
  6 + "net/http"
  7 +
  8 + "github.com/zeromicro/go-zero/rest/httpx"
  9 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/chat"
  10 + "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/types"
  12 +)
  13 +
  14 +func ChatSparkSessionSaveHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  15 + return func(w http.ResponseWriter, r *http.Request) {
  16 + var req types.ChatSessionSaveRequest
  17 + if err := httpx.Parse(r, &req); err != nil {
  18 + httpx.ErrorCtx(r.Context(), w, err)
  19 + return
  20 + }
  21 +
  22 + l := chat.NewChatSessionSaveLogic(r.Context(), svcCtx)
  23 + req.ChatSession.Module = domain.ModuleSparkChat
  24 + if req.ChatSession.DatasetId > 0 {
  25 + req.ChatSession.Type = domain.TypeSparkDatasetChat
  26 + } else if len(req.ChatSession.DocumentIds) > 0 {
  27 + req.ChatSession.Type = domain.TypeSparkDocumentsChat
  28 + } else {
  29 + httpx.ErrorCtx(r.Context(), w, xerr.NewErrMsgErr("知识库、文档至少选一个", nil))
  30 + return
  31 + }
  32 + resp, err := l.ChatSessionSave(&req)
  33 + if err != nil {
  34 + httpx.ErrorCtx(r.Context(), w, err)
  35 + } else {
  36 + httpx.OkJsonCtx(r.Context(), w, resp)
  37 + }
  38 + }
  39 +}
  1 +package dataset
  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/dataset"
  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 ChatDatasetDeleteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  13 + return func(w http.ResponseWriter, r *http.Request) {
  14 + var req types.ChatDatasetDeleteRequest
  15 + if err := httpx.Parse(r, &req); err != nil {
  16 + httpx.ErrorCtx(r.Context(), w, err)
  17 + return
  18 + }
  19 +
  20 + l := dataset.NewChatDatasetDeleteLogic(r.Context(), svcCtx)
  21 + resp, err := l.ChatDatasetDelete(&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 dataset
  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/dataset"
  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 ChatDatasetGetHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  13 + return func(w http.ResponseWriter, r *http.Request) {
  14 + var req types.ChatDatasetGetRequest
  15 + if err := httpx.Parse(r, &req); err != nil {
  16 + httpx.ErrorCtx(r.Context(), w, err)
  17 + return
  18 + }
  19 +
  20 + l := dataset.NewChatDatasetGetLogic(r.Context(), svcCtx)
  21 + resp, err := l.ChatDatasetGet(&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 dataset
  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/dataset"
  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 ChatDatasetRenameHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  13 + return func(w http.ResponseWriter, r *http.Request) {
  14 + var req types.ChatDatasetRenameRequest
  15 + if err := httpx.Parse(r, &req); err != nil {
  16 + httpx.ErrorCtx(r.Context(), w, err)
  17 + return
  18 + }
  19 +
  20 + l := dataset.NewChatDatasetRenameLogic(r.Context(), svcCtx)
  21 + resp, err := l.ChatDatasetRename(&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 dataset
  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/dataset"
  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 ChatDatasetSaveHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  13 + return func(w http.ResponseWriter, r *http.Request) {
  14 + var req types.ChatDatasetSaveRequest
  15 + if err := httpx.Parse(r, &req); err != nil {
  16 + httpx.ErrorCtx(r.Context(), w, err)
  17 + return
  18 + }
  19 +
  20 + l := dataset.NewChatDatasetSaveLogic(r.Context(), svcCtx)
  21 + resp, err := l.ChatDatasetSave(&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 dataset
  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/dataset"
  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 ChatDatasetSearchHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  13 + return func(w http.ResponseWriter, r *http.Request) {
  14 + var req types.ChatDatasetSearchRequest
  15 + if err := httpx.Parse(r, &req); err != nil {
  16 + httpx.ErrorCtx(r.Context(), w, err)
  17 + return
  18 + }
  19 +
  20 + l := dataset.NewChatDatasetSearchLogic(r.Context(), svcCtx)
  21 + resp, err := l.ChatDatasetSearch(&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 dataset
  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/dataset"
  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 ChatDatasetUpdateHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  13 + return func(w http.ResponseWriter, r *http.Request) {
  14 + var req types.ChatDatasetUpdateRequest
  15 + if err := httpx.Parse(r, &req); err != nil {
  16 + httpx.ErrorCtx(r.Context(), w, err)
  17 + return
  18 + }
  19 +
  20 + l := dataset.NewChatDatasetUpdateLogic(r.Context(), svcCtx)
  21 + resp, err := l.ChatDatasetUpdate(&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 document
  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/document"
  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 ChatDocumentDeleteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  13 + return func(w http.ResponseWriter, r *http.Request) {
  14 + var req types.ChatDocumentDeleteRequest
  15 + if err := httpx.Parse(r, &req); err != nil {
  16 + httpx.ErrorCtx(r.Context(), w, err)
  17 + return
  18 + }
  19 +
  20 + l := document.NewChatDocumentDeleteLogic(r.Context(), svcCtx)
  21 + resp, err := l.ChatDocumentDelete(&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 document
  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/document"
  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 ChatDocumentGetHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  13 + return func(w http.ResponseWriter, r *http.Request) {
  14 + var req types.ChatDocumentGetRequest
  15 + if err := httpx.Parse(r, &req); err != nil {
  16 + httpx.ErrorCtx(r.Context(), w, err)
  17 + return
  18 + }
  19 +
  20 + l := document.NewChatDocumentGetLogic(r.Context(), svcCtx)
  21 + resp, err := l.ChatDocumentGet(&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 document
  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/document"
  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 ChatDocumentRenameHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  13 + return func(w http.ResponseWriter, r *http.Request) {
  14 + var req types.ChatDocumentRenameRequest
  15 + if err := httpx.Parse(r, &req); err != nil {
  16 + httpx.ErrorCtx(r.Context(), w, err)
  17 + return
  18 + }
  19 +
  20 + l := document.NewChatDocumentRenameLogic(r.Context(), svcCtx)
  21 + resp, err := l.ChatDocumentRename(&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 document
  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/document"
  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 ChatDocumentSaveHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  13 + return func(w http.ResponseWriter, r *http.Request) {
  14 + var req types.ChatDocumentSaveRequest
  15 + if err := httpx.Parse(r, &req); err != nil {
  16 + httpx.ErrorCtx(r.Context(), w, err)
  17 + return
  18 + }
  19 +
  20 + l := document.NewChatDocumentSaveLogic(r.Context(), svcCtx)
  21 + resp, err := l.ChatDocumentSave(&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 document
  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/document"
  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 ChatDocumentSearchHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
  13 + return func(w http.ResponseWriter, r *http.Request) {
  14 + var req types.ChatDocumentSearchRequest
  15 + if err := httpx.Parse(r, &req); err != nil {
  16 + httpx.ErrorCtx(r.Context(), w, err)
  17 + return
  18 + }
  19 +
  20 + l := document.NewChatDocumentSearchLogic(r.Context(), svcCtx)
  21 + resp, err := l.ChatDocumentSearch(&req)
  22 + if err != nil {
  23 + httpx.ErrorCtx(r.Context(), w, err)
  24 + } else {
  25 + httpx.OkJsonCtx(r.Context(), w, resp)
  26 + }
  27 + }
  28 +}
@@ -5,6 +5,8 @@ import ( @@ -5,6 +5,8 @@ import (
5 "net/http" 5 "net/http"
6 6
7 chat "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/handler/chat" 7 chat "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/handler/chat"
  8 + dataset "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/handler/dataset"
  9 + document "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/handler/document"
8 "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/svc"
9 11
10 "github.com/zeromicro/go-zero/rest" 12 "github.com/zeromicro/go-zero/rest"
@@ -52,6 +54,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -52,6 +54,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
52 }, 54 },
53 { 55 {
54 Method: http.MethodPost, 56 Method: http.MethodPost,
  57 + Path: "/chat/session/my_spark_sessions",
  58 + Handler: chat.ChatMySparkSessionsHandler(serverCtx),
  59 + },
  60 + {
  61 + Method: http.MethodPost,
  62 + Path: "/chat/spark_session",
  63 + Handler: chat.ChatSparkSessionSaveHandler(serverCtx),
  64 + },
  65 + {
  66 + Method: http.MethodPost,
55 Path: "/chat/session/records", 67 Path: "/chat/session/records",
56 Handler: chat.ChatSessionRecordsHandler(serverCtx), 68 Handler: chat.ChatSessionRecordsHandler(serverCtx),
57 }, 69 },
@@ -65,4 +77,79 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { @@ -65,4 +77,79 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
65 rest.WithJwt(serverCtx.Config.SystemAuth.AccessSecret), 77 rest.WithJwt(serverCtx.Config.SystemAuth.AccessSecret),
66 rest.WithPrefix("/v1"), 78 rest.WithPrefix("/v1"),
67 ) 79 )
  80 +
  81 + server.AddRoutes(
  82 + rest.WithMiddlewares(
  83 + []rest.Middleware{serverCtx.LogRequest},
  84 + []rest.Route{
  85 + {
  86 + Method: http.MethodGet,
  87 + Path: "/chat/document/:id",
  88 + Handler: document.ChatDocumentGetHandler(serverCtx),
  89 + },
  90 + {
  91 + Method: http.MethodPost,
  92 + Path: "/chat/document",
  93 + Handler: document.ChatDocumentSaveHandler(serverCtx),
  94 + },
  95 + {
  96 + Method: http.MethodDelete,
  97 + Path: "/chat/document/:id",
  98 + Handler: document.ChatDocumentDeleteHandler(serverCtx),
  99 + },
  100 + {
  101 + Method: http.MethodPost,
  102 + Path: "/chat/document/rename",
  103 + Handler: document.ChatDocumentRenameHandler(serverCtx),
  104 + },
  105 + {
  106 + Method: http.MethodPost,
  107 + Path: "/chat/document/search",
  108 + Handler: document.ChatDocumentSearchHandler(serverCtx),
  109 + },
  110 + }...,
  111 + ),
  112 + rest.WithJwt(serverCtx.Config.SystemAuth.AccessSecret),
  113 + rest.WithPrefix("/v1"),
  114 + )
  115 +
  116 + server.AddRoutes(
  117 + rest.WithMiddlewares(
  118 + []rest.Middleware{serverCtx.LogRequest},
  119 + []rest.Route{
  120 + {
  121 + Method: http.MethodGet,
  122 + Path: "/chat/dataset/:id",
  123 + Handler: dataset.ChatDatasetGetHandler(serverCtx),
  124 + },
  125 + {
  126 + Method: http.MethodPost,
  127 + Path: "/chat/dataset",
  128 + Handler: dataset.ChatDatasetSaveHandler(serverCtx),
  129 + },
  130 + {
  131 + Method: http.MethodDelete,
  132 + Path: "/chat/dataset/:id",
  133 + Handler: dataset.ChatDatasetDeleteHandler(serverCtx),
  134 + },
  135 + {
  136 + Method: http.MethodPut,
  137 + Path: "/chat/dataset/:id",
  138 + Handler: dataset.ChatDatasetUpdateHandler(serverCtx),
  139 + },
  140 + {
  141 + Method: http.MethodPost,
  142 + Path: "/chat/dataset/rename",
  143 + Handler: dataset.ChatDatasetRenameHandler(serverCtx),
  144 + },
  145 + {
  146 + Method: http.MethodPost,
  147 + Path: "/chat/dataset/search",
  148 + Handler: dataset.ChatDatasetSearchHandler(serverCtx),
  149 + },
  150 + }...,
  151 + ),
  152 + rest.WithJwt(serverCtx.Config.SystemAuth.AccessSecret),
  153 + rest.WithPrefix("/v1"),
  154 + )
68 } 155 }
@@ -28,6 +28,9 @@ func NewChatModelsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatMo @@ -28,6 +28,9 @@ func NewChatModelsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatMo
28 func (l *ChatModelsLogic) ChatModels(req *types.ChatModelsRequest) (resp *types.ChatModelsResponse, err error) { 28 func (l *ChatModelsLogic) ChatModels(req *types.ChatModelsRequest) (resp *types.ChatModelsResponse, err error) {
29 var models = make([]types.Model, 0) 29 var models = make([]types.Model, 0)
30 lo.ForEach(domain.DefaultChatModels, func(item *domain.ChatModel, index int) { 30 lo.ForEach(domain.DefaultChatModels, func(item *domain.ChatModel, index int) {
  31 + if item.Id == domain.SparkChatDocModelId {
  32 + return
  33 + }
31 models = append(models, types.Model{ 34 models = append(models, types.Model{
32 Id: item.Id, 35 Id: item.Id,
33 Name: item.Name, 36 Name: item.Name,
  1 +package chat
  2 +
  3 +import (
  4 + "context"
  5 +
  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/api/internal/types"
  8 +
  9 + "github.com/zeromicro/go-zero/core/logx"
  10 +)
  11 +
  12 +type ChatMySparkSessionsLogic struct {
  13 + logx.Logger
  14 + ctx context.Context
  15 + svcCtx *svc.ServiceContext
  16 +}
  17 +
  18 +func NewChatMySparkSessionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatMySparkSessionsLogic {
  19 + return &ChatMySparkSessionsLogic{
  20 + Logger: logx.WithContext(ctx),
  21 + ctx: ctx,
  22 + svcCtx: svcCtx,
  23 + }
  24 +}
  25 +
  26 +func (l *ChatMySparkSessionsLogic) ChatMySparkSessions(req *types.ChatSessionSearchRequest) (resp *types.ChatSessionSearchResponse, err error) {
  27 + // todo: add your logic here and delete this line
  28 +
  29 + return
  30 +}
@@ -2,10 +2,13 @@ package chat @@ -2,10 +2,13 @@ package chat
2 2
3 import ( 3 import (
4 "context" 4 "context"
  5 + "github.com/samber/lo"
  6 + "github.com/zeromicro/go-zero/core/fx"
5 "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"
6 "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/open" 8 "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/open"
7 "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/ai" 9 "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/ai"
8 "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/contextdata" 10 "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/contextdata"
  11 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/tool"
9 "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction" 12 "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
10 "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr" 13 "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
11 "net/http" 14 "net/http"
@@ -38,7 +41,6 @@ func (l *ChatSessionConversationLogic) ChatSessionConversation(w http.ResponseWr @@ -38,7 +41,6 @@ func (l *ChatSessionConversationLogic) ChatSessionConversation(w http.ResponseWr
38 token = contextdata.GetUserTokenFromCtx(l.ctx) 41 token = contextdata.GetUserTokenFromCtx(l.ctx)
39 session *domain.ChatSession 42 session *domain.ChatSession
40 user open.User 43 user open.User
41 - model *domain.ChatModel  
42 ok bool 44 ok bool
43 beginUnix = time.Now().UnixMilli() 45 beginUnix = time.Now().UnixMilli()
44 ) 46 )
@@ -51,8 +53,8 @@ func (l *ChatSessionConversationLogic) ChatSessionConversation(w http.ResponseWr @@ -51,8 +53,8 @@ func (l *ChatSessionConversationLogic) ChatSessionConversation(w http.ResponseWr
51 if user.Id != session.UserId { 53 if user.Id != session.UserId {
52 return nil, xerr.NewErrMsgErr("无权限", err) 54 return nil, xerr.NewErrMsgErr("无权限", err)
53 } 55 }
54 - if model, ok = domain.DefaultChatModels.Match(req.ModelId); !ok {  
55 - return nil, xerr.NewErrMsgErr("模型不存在", err) 56 + if session.Module == domain.ModuleSparkChat {
  57 + req.ModelId = domain.SparkChatDocModelId
56 } 58 }
57 dm = &domain.ChatSessionRecord{ 59 dm = &domain.ChatSessionRecord{
58 CompanyId: token.CompanyId, 60 CompanyId: token.CompanyId,
@@ -69,15 +71,16 @@ func (l *ChatSessionConversationLogic) ChatSessionConversation(w http.ResponseWr @@ -69,15 +71,16 @@ func (l *ChatSessionConversationLogic) ChatSessionConversation(w http.ResponseWr
69 var answer string 71 var answer string
70 var channel = make(chan string, 5) 72 var channel = make(chan string, 5)
71 // 异步访问AI接口 73 // 异步访问AI接口
72 - go func() {  
73 - // 异步访问AI接口  
74 - answer, err = Conversation(model, req.Text, channel)  
75 - }()  
76 - for {  
77 - if _, ok = <-channel; !ok {  
78 - break 74 + fx.Parallel(func() {
  75 + answer, err = Conversation(l.ctx, l.svcCtx, session, req.ModelId, req.Text, channel)
  76 + }, func() {
  77 + for {
  78 + if _, ok = <-channel; !ok {
  79 + break
  80 + }
  81 + logx.Info(1)
79 } 82 }
80 - } 83 + })
81 if err != nil { 84 if err != nil {
82 return nil, xerr.NewErrMsgErr("AI模型异常,稍后再试", err) 85 return nil, xerr.NewErrMsgErr("AI模型异常,稍后再试", err)
83 } 86 }
@@ -101,17 +104,63 @@ func (l *ChatSessionConversationLogic) ChatSessionConversation(w http.ResponseWr @@ -101,17 +104,63 @@ func (l *ChatSessionConversationLogic) ChatSessionConversation(w http.ResponseWr
101 return 104 return
102 } 105 }
103 106
104 -func Conversation(m *domain.ChatModel, text string, channel chan string) (answer string, err error) {  
105 - 107 +// Conversation 普通对话
  108 +func Conversation(ctx context.Context, svc *svc.ServiceContext, session *domain.ChatSession, modelId int64, text string, channel chan string) (answer string, err error) {
  109 + var (
  110 + m *domain.ChatModel
  111 + ok bool
  112 + )
  113 + defer close(channel)
  114 + if m, ok = domain.DefaultChatModels.Match(modelId); !ok {
  115 + err = xerr.NewErrMsgErr("模型不存在", err)
  116 + return
  117 + }
  118 + if session.Module == domain.ModuleSparkChat {
  119 + return SparkDocumentConversation(ctx, svc, session, m, text, channel)
  120 + }
106 switch m.Id { 121 switch m.Id {
107 // 星火3.5 122 // 星火3.5
108 case 1, 2, 3: 123 case 1, 2, 3:
109 answer, err = ai.ChatGPT(m.Code, m.Config.AppKey, text, channel) 124 answer, err = ai.ChatGPT(m.Code, m.Config.AppKey, text, channel)
110 case 4: 125 case 4:
111 - answer, err = ai.Spark(m.Config.AppId, m.Config.AppKey, m.Config.AppSecret, text, channel) 126 + answer, err = ai.ChatSpark(m.Config.AppId, m.Config.AppKey, m.Config.AppSecret, text, channel)
112 } 127 }
113 if err != nil { 128 if err != nil {
114 return "", err 129 return "", err
115 } 130 }
116 return 131 return
117 } 132 }
  133 +
  134 +// SparkDocumentConversation 星火文档对话
  135 +func SparkDocumentConversation(ctx context.Context, svcCtx *svc.ServiceContext, session *domain.ChatSession, m *domain.ChatModel, text string, channel chan string) (answer string, err error) {
  136 + var (
  137 + conn = svcCtx.DefaultDBConn()
  138 + )
  139 + // 获取文件ID列表
  140 + fileIds := make([]string, 0)
  141 + lazyDocument := tool.NewLazyLoadService(svcCtx.ChatDocumentRepository.FindOne)
  142 + if session.Type == domain.TypeSparkDatasetChat { // 知识库
  143 + _, documentMapping, _ := svcCtx.ChatDatasetDocumentMappingRepository.FindByDataset(ctx, conn, session.Metadata.DatasetId)
  144 + lo.ForEach(documentMapping, func(item *domain.ChatDatasetDocumentMapping, index int) {
  145 + if document, _ := lazyDocument.Load(ctx, conn, item.DocumentId); document != nil {
  146 + if len(document.Metadata.FileId) == 0 {
  147 + return
  148 + }
  149 + fileIds = append(fileIds, document.Metadata.FileId)
  150 + }
  151 + })
  152 + } else if session.Type == domain.TypeSparkDocumentsChat { // 多文档
  153 + lo.ForEach(session.Metadata.DocumentIds, func(item int64, index int) {
  154 + if document, _ := lazyDocument.Load(ctx, conn, item); document != nil {
  155 + if len(document.Metadata.FileId) == 0 {
  156 + return
  157 + }
  158 + fileIds = append(fileIds, document.Metadata.FileId)
  159 + }
  160 + })
  161 + }
  162 +
  163 + // 对话
  164 + c := m.Config
  165 + return ai.ChatSparkDocument(c.AppId, c.AppKey, c.AppSecret, fileIds, text, channel)
  166 +}
@@ -40,7 +40,6 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon @@ -40,7 +40,6 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon
40 token = contextdata.GetUserTokenFromCtx(l.ctx) 40 token = contextdata.GetUserTokenFromCtx(l.ctx)
41 session *domain.ChatSession 41 session *domain.ChatSession
42 user open.User 42 user open.User
43 - model *domain.ChatModel  
44 ok bool 43 ok bool
45 ) 44 )
46 if session, err = l.svcCtx.ChatSessionRepository.FindOne(l.ctx, conn, req.SessionId); err != nil { 45 if session, err = l.svcCtx.ChatSessionRepository.FindOne(l.ctx, conn, req.SessionId); err != nil {
@@ -52,12 +51,10 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon @@ -52,12 +51,10 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon
52 if user.Id != session.UserId { 51 if user.Id != session.UserId {
53 return nil, xerr.NewErrMsgErr("无权限", err) 52 return nil, xerr.NewErrMsgErr("无权限", err)
54 } 53 }
55 - if model, ok = domain.DefaultChatModels.Match(req.ModelId); !ok {  
56 - return nil, xerr.NewErrMsgErr("模型不存在", err) 54 + if session.Module == domain.ModuleSparkChat {
  55 + req.ModelId = domain.SparkChatDocModelId
57 } 56 }
58 -  
59 var answer string 57 var answer string
60 -  
61 var upgrader = websocket.Upgrader{ 58 var upgrader = websocket.Upgrader{
62 ReadBufferSize: 1024, 59 ReadBufferSize: 1024,
63 WriteBufferSize: 1024, 60 WriteBufferSize: 1024,
@@ -96,7 +93,7 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon @@ -96,7 +93,7 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon
96 } 93 }
97 fx.Parallel(func() { 94 fx.Parallel(func() {
98 // 异步访问AI接口 95 // 异步访问AI接口
99 - answer, err = Conversation(model, string(text), channel) 96 + answer, err = Conversation(l.ctx, l.svcCtx, session, req.ModelId, string(text), channel)
100 }, func() { 97 }, func() {
101 for { 98 for {
102 var v string 99 var v string
@@ -6,6 +6,7 @@ import ( @@ -6,6 +6,7 @@ import (
6 "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/contextdata" 6 "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/contextdata"
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"
  9 + "time"
9 10
10 "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"
11 "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"
@@ -39,7 +40,9 @@ func (l *ChatSessionSaveLogic) ChatSessionSave(req *types.ChatSessionSaveRequest @@ -39,7 +40,9 @@ func (l *ChatSessionSaveLogic) ChatSessionSave(req *types.ChatSessionSaveRequest
39 }, true); err != nil { 40 }, true); err != nil {
40 return nil, xerr.NewErrMsg("保存失败") 41 return nil, xerr.NewErrMsg("保存失败")
41 } 42 }
42 - resp = &types.ChatSessionSaveResponse{} 43 + resp = &types.ChatSessionSaveResponse{
  44 + ChatSession: NewTypesChatSession(dm),
  45 + }
43 return 46 return
44 } 47 }
45 48
@@ -53,6 +56,13 @@ func NewDomainChatSession(token contextdata.UserToken, item types.ChatSessionIte @@ -53,6 +56,13 @@ func NewDomainChatSession(token contextdata.UserToken, item types.ChatSessionIte
53 UserId: token.UserId, 56 UserId: token.UserId,
54 Title: title, 57 Title: title,
55 Abstract: title, 58 Abstract: title,
  59 + Module: item.Module,
  60 + Type: item.Type,
  61 + Metadata: &domain.SessionMetadata{
  62 + DatasetId: item.DatasetId,
  63 + DocumentIds: item.DocumentIds,
  64 + },
  65 + Rank: time.Now().Unix(),
56 } 66 }
57 } 67 }
58 68
@@ -4,6 +4,7 @@ import ( @@ -4,6 +4,7 @@ import (
4 "context" 4 "context"
5 "fmt" 5 "fmt"
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/contextdata"
7 8
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/svc"
9 "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types" 10 "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
@@ -30,11 +31,16 @@ func (l *ChatSessionSearchLogic) ChatSessionSearch(req *types.ChatSessionSearchR @@ -30,11 +31,16 @@ func (l *ChatSessionSearchLogic) ChatSessionSearch(req *types.ChatSessionSearchR
30 conn = l.svcCtx.DefaultDBConn() 31 conn = l.svcCtx.DefaultDBConn()
31 dms []*domain.ChatSession 32 dms []*domain.ChatSession
32 total int64 33 total int64
  34 + token = contextdata.GetUserTokenFromCtx(l.ctx)
33 ) 35 )
34 36
35 queryOptions := domain.NewQueryOptions(). 37 queryOptions := domain.NewQueryOptions().
36 - WithKV("title", fmt.Sprintf("%%%v%%", req.Title))  
37 - 38 + WithKV("module", req.Module).
  39 + WithKV("companyId", token.CompanyId).
  40 + WithKV("userId", token.UserId)
  41 + if req.Title != "" {
  42 + queryOptions.WithKV("title", fmt.Sprintf("%%%v%%", req.Title))
  43 + }
38 if req.Page != 0 && req.Size != 0 { 44 if req.Page != 0 && req.Size != 0 {
39 queryOptions.WithOffsetLimit(req.Page, req.Size) 45 queryOptions.WithOffsetLimit(req.Page, req.Size)
40 } 46 }
  1 +package chat
  2 +
  3 +import (
  4 + "context"
  5 +
  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/api/internal/types"
  8 +
  9 + "github.com/zeromicro/go-zero/core/logx"
  10 +)
  11 +
  12 +type ChatSparkSessionSaveLogic struct {
  13 + logx.Logger
  14 + ctx context.Context
  15 + svcCtx *svc.ServiceContext
  16 +}
  17 +
  18 +func NewChatSparkSessionSaveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatSparkSessionSaveLogic {
  19 + return &ChatSparkSessionSaveLogic{
  20 + Logger: logx.WithContext(ctx),
  21 + ctx: ctx,
  22 + svcCtx: svcCtx,
  23 + }
  24 +}
  25 +
  26 +func (l *ChatSparkSessionSaveLogic) ChatSparkSessionSave(req *types.ChatSessionSaveRequest) (resp *types.ChatSessionSaveResponse, err error) {
  27 + // todo: add your logic here and delete this line
  28 +
  29 + return
  30 +}
  1 +package dataset
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  6 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
  7 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
  8 +
  9 + "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"
  11 +
  12 + "github.com/zeromicro/go-zero/core/logx"
  13 +)
  14 +
  15 +type ChatDatasetDeleteLogic struct {
  16 + logx.Logger
  17 + ctx context.Context
  18 + svcCtx *svc.ServiceContext
  19 +}
  20 +
  21 +func NewChatDatasetDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDatasetDeleteLogic {
  22 + return &ChatDatasetDeleteLogic{
  23 + Logger: logx.WithContext(ctx),
  24 + ctx: ctx,
  25 + svcCtx: svcCtx,
  26 + }
  27 +}
  28 +
  29 +func (l *ChatDatasetDeleteLogic) ChatDatasetDelete(req *types.ChatDatasetDeleteRequest) (resp *types.ChatDatasetDeleteResponse, err error) {
  30 + var (
  31 + conn = l.svcCtx.DefaultDBConn()
  32 + dm *domain.ChatDataset
  33 + )
  34 + if dm, err = l.svcCtx.ChatDatasetRepository.FindOne(l.ctx, conn, req.Id); err != nil {
  35 + return nil, xerr.NewErrMsgErr("不存在", err)
  36 + }
  37 + if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
  38 + if dm, err = l.svcCtx.ChatDatasetRepository.Delete(l.ctx, conn, dm); err != nil {
  39 + return err
  40 + }
  41 + // 删除document & dataset 关联
  42 + if err = l.svcCtx.ChatDatasetDocumentMappingRepository.DeleteByDataset(l.ctx, conn, dm.Id); err != nil {
  43 + return err
  44 + }
  45 + return nil
  46 + }, true); err != nil {
  47 + return nil, xerr.NewErrMsgErr("移除失败", err)
  48 + }
  49 + resp = &types.ChatDatasetDeleteResponse{}
  50 + return
  51 +}
  1 +package dataset
  2 +
  3 +import (
  4 + "context"
  5 + "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/pkg/tool"
  8 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
  9 +
  10 + "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/types"
  12 +
  13 + "github.com/zeromicro/go-zero/core/logx"
  14 +)
  15 +
  16 +type ChatDatasetGetLogic struct {
  17 + logx.Logger
  18 + ctx context.Context
  19 + svcCtx *svc.ServiceContext
  20 +}
  21 +
  22 +func NewChatDatasetGetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDatasetGetLogic {
  23 + return &ChatDatasetGetLogic{
  24 + Logger: logx.WithContext(ctx),
  25 + ctx: ctx,
  26 + svcCtx: svcCtx,
  27 + }
  28 +}
  29 +
  30 +func (l *ChatDatasetGetLogic) ChatDatasetGet(req *types.ChatDatasetGetRequest) (resp *types.ChatDatasetGetResponse, err error) {
  31 + var (
  32 + conn = l.svcCtx.DefaultDBConn()
  33 + dm *domain.ChatDataset
  34 + documentMapping []*domain.ChatDatasetDocumentMapping
  35 + )
  36 + // 货号唯一
  37 + if dm, err = l.svcCtx.ChatDatasetRepository.FindOne(l.ctx, conn, req.Id); err != nil {
  38 + return nil, xerr.NewErrMsgErr("不存在", err)
  39 + }
  40 + documents := make([]types.ChatDocumentItem, 0)
  41 + _, documentMapping, _ = l.svcCtx.ChatDatasetDocumentMappingRepository.FindByDataset(l.ctx, conn, dm.Id)
  42 + lazyDocument := tool.NewLazyLoadService(l.svcCtx.ChatDocumentRepository.FindOne)
  43 + lo.ForEach(documentMapping, func(item *domain.ChatDatasetDocumentMapping, index int) {
  44 + if document, _ := lazyDocument.Load(l.ctx, conn, item.DocumentId); document != nil {
  45 + documents = append(documents, types.NewTypesChatDocument(document))
  46 + }
  47 + })
  48 + resp = &types.ChatDatasetGetResponse{
  49 + ChatDataset: NewTypesChatDataset(dm, nil),
  50 + Documents: documents,
  51 + }
  52 + return
  53 +}
  1 +package dataset
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  6 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
  7 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
  8 +
  9 + "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"
  11 +
  12 + "github.com/zeromicro/go-zero/core/logx"
  13 +)
  14 +
  15 +type ChatDatasetRenameLogic struct {
  16 + logx.Logger
  17 + ctx context.Context
  18 + svcCtx *svc.ServiceContext
  19 +}
  20 +
  21 +func NewChatDatasetRenameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDatasetRenameLogic {
  22 + return &ChatDatasetRenameLogic{
  23 + Logger: logx.WithContext(ctx),
  24 + ctx: ctx,
  25 + svcCtx: svcCtx,
  26 + }
  27 +}
  28 +
  29 +func (l *ChatDatasetRenameLogic) ChatDatasetRename(req *types.ChatDatasetRenameRequest) (resp *types.ChatDatasetRenameResponse, err error) {
  30 + var (
  31 + conn = l.svcCtx.DefaultDBConn()
  32 + dm *domain.ChatDataset
  33 + )
  34 + if dm, err = l.svcCtx.ChatDatasetRepository.FindOne(l.ctx, conn, req.Id); err != nil {
  35 + return nil, xerr.NewErrMsgErr("不存在", err)
  36 + }
  37 + // 赋值
  38 + if req.Name != "" {
  39 + dm.Name = req.Name
  40 + }
  41 + // 更新
  42 + if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
  43 + dm, err = l.svcCtx.ChatDatasetRepository.UpdateWithVersion(l.ctx, conn, dm)
  44 + return err
  45 + }, true); err != nil {
  46 + return nil, xerr.NewErrMsg("更新失败")
  47 + }
  48 + resp = &types.ChatDatasetRenameResponse{
  49 + ChatDataset: NewTypesChatDataset(dm, nil),
  50 + }
  51 + return
  52 +}
  1 +package dataset
  2 +
  3 +import (
  4 + "context"
  5 + "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/pkg/contextdata"
  8 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
  9 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
  10 +
  11 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
  12 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
  13 +
  14 + "github.com/zeromicro/go-zero/core/logx"
  15 +)
  16 +
  17 +type ChatDatasetSaveLogic struct {
  18 + logx.Logger
  19 + ctx context.Context
  20 + svcCtx *svc.ServiceContext
  21 +}
  22 +
  23 +func NewChatDatasetSaveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDatasetSaveLogic {
  24 + return &ChatDatasetSaveLogic{
  25 + Logger: logx.WithContext(ctx),
  26 + ctx: ctx,
  27 + svcCtx: svcCtx,
  28 + }
  29 +}
  30 +
  31 +func (l *ChatDatasetSaveLogic) ChatDatasetSave(req *types.ChatDatasetSaveRequest) (resp *types.ChatDatasetSaveResponse, err error) {
  32 + var (
  33 + dm *domain.ChatDataset
  34 + token = contextdata.GetUserTokenFromCtx(l.ctx)
  35 + )
  36 + if req.Name == "" {
  37 + req.Name = "新知识库"
  38 + }
  39 + dm = &domain.ChatDataset{
  40 + UserId: token.UserId,
  41 + CompanyId: token.CompanyId,
  42 + Name: req.Name,
  43 + Status: domain.Enable,
  44 + }
  45 + if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
  46 + if dm, err = l.svcCtx.ChatDatasetRepository.Insert(l.ctx, conn, dm); err != nil {
  47 + return err
  48 + }
  49 + if len(req.DocumentIds) > 0 {
  50 + for _, id := range req.DocumentIds {
  51 + l.svcCtx.ChatDatasetDocumentMappingRepository.Insert(l.ctx, conn, &domain.ChatDatasetDocumentMapping{
  52 + DatasetId: dm.Id,
  53 + DocumentId: id,
  54 + })
  55 + }
  56 + }
  57 + return err
  58 + }, true); err != nil {
  59 + return nil, xerr.NewErrMsg("保存失败")
  60 + }
  61 + resp = &types.ChatDatasetSaveResponse{
  62 + ChatDataset: NewTypesChatDataset(dm, lo.ToPtr(len(req.DocumentIds))),
  63 + }
  64 + return
  65 +}
  66 +
  67 +//func NewDomainChatDataset(item types.ChatDatasetItem) *domain.ChatDataset {
  68 +// return &domain.ChatDataset{
  69 +
  70 +// }
  71 +// }
  72 +func NewTypesChatDataset(item *domain.ChatDataset, fileNum *int) types.ChatDatasetItem {
  73 + return types.ChatDatasetItem{
  74 + Id: item.Id,
  75 + Name: lo.ToPtr(item.Name),
  76 + Desc: lo.ToPtr(item.Desc),
  77 + Status: lo.ToPtr(item.Status),
  78 + CreatedAt: item.CreatedAt,
  79 + UpdatedAt: item.UpdatedAt,
  80 + FileNumber: fileNum,
  81 + }
  82 +}
  1 +package dataset
  2 +
  3 +import (
  4 + "context"
  5 + "fmt"
  6 + "github.com/samber/lo"
  7 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  8 +
  9 + "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"
  11 +
  12 + "github.com/zeromicro/go-zero/core/logx"
  13 +)
  14 +
  15 +type ChatDatasetSearchLogic struct {
  16 + logx.Logger
  17 + ctx context.Context
  18 + svcCtx *svc.ServiceContext
  19 +}
  20 +
  21 +func NewChatDatasetSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDatasetSearchLogic {
  22 + return &ChatDatasetSearchLogic{
  23 + Logger: logx.WithContext(ctx),
  24 + ctx: ctx,
  25 + svcCtx: svcCtx,
  26 + }
  27 +}
  28 +
  29 +func (l *ChatDatasetSearchLogic) ChatDatasetSearch(req *types.ChatDatasetSearchRequest) (resp *types.ChatDatasetSearchResponse, err error) {
  30 + var (
  31 + conn = l.svcCtx.DefaultDBConn()
  32 + dms []*domain.ChatDataset
  33 + total int64
  34 + )
  35 +
  36 + queryOptions := domain.NewQueryOptions().WithOffsetLimit(req.Page, req.Size).
  37 + WithKV("status", req.Status)
  38 + if req.Name != "" {
  39 + queryOptions.WithKV("name", fmt.Sprintf("%%%v%%", req.Name))
  40 + }
  41 + total, dms, err = l.svcCtx.ChatDatasetRepository.Find(l.ctx, conn, queryOptions)
  42 + list := make([]types.ChatDatasetItem, 0)
  43 +
  44 + // 知识库关联的文档
  45 + ids := domain.Values(dms, domain.ChatDatasetId)
  46 + _, documentMappings, _ := l.svcCtx.ChatDatasetDocumentMappingRepository.FindByDataset(l.ctx, conn, ids...)
  47 + documentGroups := lo.GroupBy(documentMappings, func(item *domain.ChatDatasetDocumentMapping) int64 {
  48 + return item.DatasetId
  49 + })
  50 +
  51 + for i := range dms {
  52 + var num = 0
  53 + if v, ok := documentGroups[dms[i].Id]; ok {
  54 + num = len(v)
  55 + }
  56 + list = append(list, NewTypesChatDataset(dms[i], lo.ToPtr(num)))
  57 + }
  58 + resp = &types.ChatDatasetSearchResponse{
  59 + List: list,
  60 + Total: total,
  61 + }
  62 + return
  63 +}
  1 +package dataset
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  6 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
  7 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
  8 +
  9 + "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"
  11 +
  12 + "github.com/zeromicro/go-zero/core/logx"
  13 +)
  14 +
  15 +type ChatDatasetUpdateLogic struct {
  16 + logx.Logger
  17 + ctx context.Context
  18 + svcCtx *svc.ServiceContext
  19 +}
  20 +
  21 +func NewChatDatasetUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDatasetUpdateLogic {
  22 + return &ChatDatasetUpdateLogic{
  23 + Logger: logx.WithContext(ctx),
  24 + ctx: ctx,
  25 + svcCtx: svcCtx,
  26 + }
  27 +}
  28 +
  29 +func (l *ChatDatasetUpdateLogic) ChatDatasetUpdate(req *types.ChatDatasetUpdateRequest) (resp *types.ChatDatasetUpdateResponse, err error) {
  30 + var (
  31 + conn = l.svcCtx.DefaultDBConn()
  32 + dm *domain.ChatDataset
  33 + )
  34 + if dm, err = l.svcCtx.ChatDatasetRepository.FindOne(l.ctx, conn, req.Id); err != nil {
  35 + return nil, xerr.NewErrMsgErr("不存在", err)
  36 + }
  37 + // 赋值
  38 + if req.ChatDataset.Name != nil {
  39 + dm.Name = *req.ChatDataset.Name
  40 + }
  41 + if req.ChatDataset.Desc != nil {
  42 + dm.Desc = *req.ChatDataset.Desc
  43 + }
  44 + if req.ChatDataset.Status != nil {
  45 + dm.Status = *req.ChatDataset.Status
  46 + }
  47 + // 更新
  48 + if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
  49 + dm, err = l.svcCtx.ChatDatasetRepository.UpdateWithVersion(l.ctx, conn, dm)
  50 + return err
  51 + }, true); err != nil {
  52 + return nil, xerr.NewErrMsg("更新失败")
  53 + }
  54 + resp = &types.ChatDatasetUpdateResponse{}
  55 + return
  56 +}
  1 +package document
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  6 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
  7 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
  8 +
  9 + "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"
  11 +
  12 + "github.com/zeromicro/go-zero/core/logx"
  13 +)
  14 +
  15 +type ChatDocumentDeleteLogic struct {
  16 + logx.Logger
  17 + ctx context.Context
  18 + svcCtx *svc.ServiceContext
  19 +}
  20 +
  21 +func NewChatDocumentDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDocumentDeleteLogic {
  22 + return &ChatDocumentDeleteLogic{
  23 + Logger: logx.WithContext(ctx),
  24 + ctx: ctx,
  25 + svcCtx: svcCtx,
  26 + }
  27 +}
  28 +
  29 +func (l *ChatDocumentDeleteLogic) ChatDocumentDelete(req *types.ChatDocumentDeleteRequest) (resp *types.ChatDocumentDeleteResponse, err error) {
  30 + var (
  31 + conn = l.svcCtx.DefaultDBConn()
  32 + dm *domain.ChatDocument
  33 + )
  34 + if dm, err = l.svcCtx.ChatDocumentRepository.FindOne(l.ctx, conn, req.Id); err != nil {
  35 + return nil, xerr.NewErrMsgErr("不存在", err)
  36 + }
  37 + if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
  38 + if dm, err = l.svcCtx.ChatDocumentRepository.Delete(l.ctx, conn, dm); err != nil {
  39 + return err
  40 + }
  41 + // 删除document & dataset 关联
  42 + if err = l.svcCtx.ChatDatasetDocumentMappingRepository.DeleteByDocument(l.ctx, conn, dm.Id); err != nil {
  43 + return err
  44 + }
  45 + return nil
  46 + }, true); err != nil {
  47 + return nil, xerr.NewErrMsgErr("移除失败", err)
  48 + }
  49 + return
  50 +}
  1 +package document
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  6 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
  7 +
  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 + "github.com/zeromicro/go-zero/core/logx"
  12 +)
  13 +
  14 +type ChatDocumentGetLogic struct {
  15 + logx.Logger
  16 + ctx context.Context
  17 + svcCtx *svc.ServiceContext
  18 +}
  19 +
  20 +func NewChatDocumentGetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDocumentGetLogic {
  21 + return &ChatDocumentGetLogic{
  22 + Logger: logx.WithContext(ctx),
  23 + ctx: ctx,
  24 + svcCtx: svcCtx,
  25 + }
  26 +}
  27 +
  28 +func (l *ChatDocumentGetLogic) ChatDocumentGet(req *types.ChatDocumentGetRequest) (resp *types.ChatDocumentGetResponse, err error) {
  29 + var (
  30 + conn = l.svcCtx.DefaultDBConn()
  31 + dm *domain.ChatDocument
  32 + )
  33 + // 货号唯一
  34 + if dm, err = l.svcCtx.ChatDocumentRepository.FindOne(l.ctx, conn, req.Id); err != nil {
  35 + return nil, xerr.NewErrMsgErr("不存在", err)
  36 + }
  37 + resp = &types.ChatDocumentGetResponse{
  38 + ChatDocument: types.NewTypesChatDocument(dm),
  39 + }
  40 + return
  41 +}
  1 +package document
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  6 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
  7 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
  8 +
  9 + "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"
  11 +
  12 + "github.com/zeromicro/go-zero/core/logx"
  13 +)
  14 +
  15 +type ChatDocumentRenameLogic struct {
  16 + logx.Logger
  17 + ctx context.Context
  18 + svcCtx *svc.ServiceContext
  19 +}
  20 +
  21 +func NewChatDocumentRenameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDocumentRenameLogic {
  22 + return &ChatDocumentRenameLogic{
  23 + Logger: logx.WithContext(ctx),
  24 + ctx: ctx,
  25 + svcCtx: svcCtx,
  26 + }
  27 +}
  28 +
  29 +func (l *ChatDocumentRenameLogic) ChatDocumentRename(req *types.ChatDocumentRenameRequest) (resp *types.ChatDocumentRenameResponse, err error) {
  30 + var (
  31 + conn = l.svcCtx.DefaultDBConn()
  32 + dm *domain.ChatDocument
  33 + )
  34 + if dm, err = l.svcCtx.ChatDocumentRepository.FindOne(l.ctx, conn, req.Id); err != nil {
  35 + return nil, xerr.NewErrMsgErr("不存在", err)
  36 + }
  37 + // 赋值
  38 + dm.Name = req.Name
  39 + // 更新
  40 + if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
  41 + dm, err = l.svcCtx.ChatDocumentRepository.UpdateWithVersion(l.ctx, conn, dm)
  42 + return err
  43 + }, true); err != nil {
  44 + return nil, xerr.NewErrMsg("更新失败")
  45 + }
  46 + resp = &types.ChatDocumentRenameResponse{
  47 + ChatDocument: types.NewTypesChatDocument(dm),
  48 + }
  49 + return
  50 +}
  1 +package document
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
  6 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
  7 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  8 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/ai"
  9 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
  10 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
  11 +
  12 + "github.com/zeromicro/go-zero/core/logx"
  13 +)
  14 +
  15 +type ChatDocumentSaveLogic struct {
  16 + logx.Logger
  17 + ctx context.Context
  18 + svcCtx *svc.ServiceContext
  19 +}
  20 +
  21 +func NewChatDocumentSaveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDocumentSaveLogic {
  22 + return &ChatDocumentSaveLogic{
  23 + Logger: logx.WithContext(ctx),
  24 + ctx: ctx,
  25 + svcCtx: svcCtx,
  26 + }
  27 +}
  28 +
  29 +func (l *ChatDocumentSaveLogic) ChatDocumentSave(req *types.ChatDocumentSaveRequest) (resp *types.ChatDocumentSaveResponse, err error) {
  30 + var (
  31 + //conn = l.svcCtx.DefaultDBConn()
  32 + dm *domain.ChatDocument
  33 + model *domain.ChatModel
  34 + fileId string
  35 + )
  36 + model, _ = domain.DefaultChatModels.Match(4)
  37 + // 文件上传星火文档
  38 + // 设置回调
  39 + if fileId, err = ai.SparkUploadFile(model.Config.AppId, model.Config.AppKey, model.Config.AppSecret, ai.SparkFileRequest{
  40 + Url: req.Url,
  41 + FileName: req.FileName,
  42 + CallbackUrl: "",
  43 + }); err != nil {
  44 + return nil, xerr.NewErrMsgErr("上传文档失败", err)
  45 + }
  46 + // 唯一判断
  47 + dm = types.NewDomainChatDocument(req.FileName, req.Url, req.Size, fileId)
  48 + if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
  49 + dm, err = l.svcCtx.ChatDocumentRepository.Insert(l.ctx, conn, dm)
  50 + return err
  51 + }, true); err != nil {
  52 + return nil, xerr.NewErrMsg("保存失败")
  53 + }
  54 +
  55 + resp = &types.ChatDocumentSaveResponse{
  56 + ChatDocument: types.NewTypesChatDocument(dm),
  57 + }
  58 + return
  59 +}
  1 +package document
  2 +
  3 +import (
  4 + "context"
  5 + "fmt"
  6 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  7 +
  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 + "github.com/zeromicro/go-zero/core/logx"
  12 +)
  13 +
  14 +type ChatDocumentSearchLogic struct {
  15 + logx.Logger
  16 + ctx context.Context
  17 + svcCtx *svc.ServiceContext
  18 +}
  19 +
  20 +func NewChatDocumentSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDocumentSearchLogic {
  21 + return &ChatDocumentSearchLogic{
  22 + Logger: logx.WithContext(ctx),
  23 + ctx: ctx,
  24 + svcCtx: svcCtx,
  25 + }
  26 +}
  27 +
  28 +func (l *ChatDocumentSearchLogic) ChatDocumentSearch(req *types.ChatDocumentSearchRequest) (resp *types.ChatDocumentSearchResponse, err error) {
  29 + var (
  30 + conn = l.svcCtx.DefaultDBConn()
  31 + dms []*domain.ChatDocument
  32 + total int64
  33 + )
  34 +
  35 + queryOptions := domain.NewQueryOptions().WithOffsetLimit(req.Page, req.Size).
  36 + WithKV("fileType", req.FileType).
  37 + WithKV("status", req.Status)
  38 + if req.Name != "" {
  39 + queryOptions.WithKV("name", fmt.Sprintf("%%%v%%", req.Name))
  40 + }
  41 + total, dms, err = l.svcCtx.ChatDocumentRepository.Find(l.ctx, conn, queryOptions)
  42 + list := make([]types.ChatDocumentItem, 0)
  43 + for i := range dms {
  44 + list = append(list, types.NewTypesChatDocument(dms[i]))
  45 + }
  46 + resp = &types.ChatDocumentSearchResponse{
  47 + List: list,
  48 + Total: total,
  49 + }
  50 + return
  51 +}
@@ -15,13 +15,16 @@ import ( @@ -15,13 +15,16 @@ import (
15 ) 15 )
16 16
17 type ServiceContext struct { 17 type ServiceContext struct {
18 - Config config.Config  
19 - LogRequest rest.Middleware  
20 - Redis *redis.Redis  
21 - DB *gorm.DB  
22 - ChatSessionRepository domain.ChatSessionRepository  
23 - ChatSessionRecordRepository domain.ChatSessionRecordRepository  
24 - SystemOpen open.SystemOpen 18 + Config config.Config
  19 + LogRequest rest.Middleware
  20 + Redis *redis.Redis
  21 + DB *gorm.DB
  22 + ChatSessionRepository domain.ChatSessionRepository
  23 + ChatSessionRecordRepository domain.ChatSessionRecordRepository
  24 + ChatDocumentRepository domain.ChatDocumentRepository
  25 + ChatDatasetRepository domain.ChatDatasetRepository
  26 + ChatDatasetDocumentMappingRepository domain.ChatDatasetDocumentMappingRepository
  27 + SystemOpen open.SystemOpen
25 } 28 }
26 29
27 func NewServiceContext(c config.Config) *ServiceContext { 30 func NewServiceContext(c config.Config) *ServiceContext {
@@ -30,14 +33,16 @@ func NewServiceContext(c config.Config) *ServiceContext { @@ -30,14 +33,16 @@ func NewServiceContext(c config.Config) *ServiceContext {
30 mlCache := cache.NewMultiLevelCache([]string{c.Redis.Host}, c.Redis.Pass) 33 mlCache := cache.NewMultiLevelCache([]string{c.Redis.Host}, c.Redis.Pass)
31 redis, _ := redis.NewRedis(redis.RedisConf{Host: c.Redis.Host, Pass: c.Redis.Pass, Type: "node"}) 34 redis, _ := redis.NewRedis(redis.RedisConf{Host: c.Redis.Host, Pass: c.Redis.Pass, Type: "node"})
32 return &ServiceContext{ 35 return &ServiceContext{
33 - Config: c,  
34 - DB: db,  
35 - Redis: redis,  
36 - LogRequest: middleware.NewLogRequestMiddleware(c.LogRequest).Handle,  
37 - ChatSessionRepository: repository.NewChatSessionRepository(cache.NewCachedRepository(mlCache)),  
38 - ChatSessionRecordRepository: repository.NewChatSessionRecordRepository(cache.NewCachedRepository(mlCache)),  
39 -  
40 - SystemOpen: open.NewSystemOpen(c.Redis), 36 + Config: c,
  37 + DB: db,
  38 + Redis: redis,
  39 + LogRequest: middleware.NewLogRequestMiddleware(c.LogRequest).Handle,
  40 + ChatSessionRepository: repository.NewChatSessionRepository(cache.NewCachedRepository(mlCache)),
  41 + ChatSessionRecordRepository: repository.NewChatSessionRecordRepository(cache.NewCachedRepository(mlCache)),
  42 + ChatDocumentRepository: repository.NewChatDocumentRepository(cache.NewCachedRepository(mlCache)),
  43 + ChatDatasetRepository: repository.NewChatDatasetRepository(cache.NewCachedRepository(mlCache)),
  44 + ChatDatasetDocumentMappingRepository: repository.NewChatDatasetDocumentMappingRepository(cache.NewCachedRepository(mlCache)),
  45 + SystemOpen: open.NewSystemOpen(c.Redis),
41 } 46 }
42 } 47 }
43 48
  1 +package types
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  5 + "path/filepath"
  6 + "strings"
  7 +)
  8 +
  9 +func NewDomainChatDocument(name, url string, size float64, fileId string) *domain.ChatDocument {
  10 + ext := getExt(name)
  11 + return &domain.ChatDocument{
  12 + Name: name,
  13 + Status: domain.StatusInUsed,
  14 + FileType: getFileTypeByExt(ext),
  15 + Metadata: domain.DocumentMetadata{
  16 + OriginFileName: filepath.Base(name),
  17 + Ext: ext,
  18 + FileId: fileId,
  19 + FileUrl: url,
  20 + FileSize: size,
  21 + },
  22 + }
  23 +}
  24 +
  25 +func NewTypesChatDocument(item *domain.ChatDocument) ChatDocumentItem {
  26 + return ChatDocumentItem{
  27 + Id: item.Id,
  28 + Name: item.Name,
  29 + FileType: item.FileType,
  30 + Status: item.Status,
  31 + OriginFileName: item.Metadata.OriginFileName,
  32 + Ext: item.Metadata.Ext,
  33 + FileId: item.Metadata.FileId,
  34 + FileUrl: item.Metadata.FileUrl,
  35 + FileSize: item.Metadata.FileSize,
  36 + }
  37 +}
  38 +
  39 +func getFileTypeByExt(ext string) string {
  40 + switch ext {
  41 + case "md":
  42 + return "markdown"
  43 + case "pdf":
  44 + return ext
  45 + case "txt":
  46 + return ext
  47 + case "doc", "docx":
  48 + return "doc"
  49 + default:
  50 + return ""
  51 + }
  52 + return ""
  53 +}
  54 +
  55 +func getExt(file string) string {
  56 + return strings.TrimPrefix(filepath.Ext(file), ".")
  57 +}
@@ -15,6 +15,7 @@ type ChatSessionSaveRequest struct { @@ -15,6 +15,7 @@ type ChatSessionSaveRequest struct {
15 } 15 }
16 16
17 type ChatSessionSaveResponse struct { 17 type ChatSessionSaveResponse struct {
  18 + ChatSession ChatSessionItem `json:"session"`
18 } 19 }
19 20
20 type ChatSessionDeleteRequest struct { 21 type ChatSessionDeleteRequest struct {
@@ -33,9 +34,10 @@ type ChatSessionUpdateResponse struct { @@ -33,9 +34,10 @@ type ChatSessionUpdateResponse struct {
33 } 34 }
34 35
35 type ChatSessionSearchRequest struct { 36 type ChatSessionSearchRequest struct {
36 - Page int `json:"page,optional"`  
37 - Size int `json:"size,optional"`  
38 - Title string `json:"title,optional"` // 按标题搜索 37 + Page int `json:"page,optional"`
  38 + Size int `json:"size,optional"`
  39 + Title string `json:"title,optional"` // 按标题搜索
  40 + Module int `json:"module,optional"` // 模块 1:openai chat 2:星火文档问答
39 } 41 }
40 42
41 type ChatSessionSearchResponse struct { 43 type ChatSessionSearchResponse struct {
@@ -44,10 +46,14 @@ type ChatSessionSearchResponse struct { @@ -44,10 +46,14 @@ type ChatSessionSearchResponse struct {
44 } 46 }
45 47
46 type ChatSessionItem struct { 48 type ChatSessionItem struct {
47 - Id int64 `json:"id,optional,omitempty"` // 唯一标识  
48 - Title string `json:"title,optional,omitempty"` // 会话标题  
49 - Abstract string `json:"abstract,optional,omitempty"` // 摘要  
50 - CreatedAt int64 `json:"createdAt,optional,omitempty"` // 创建时间 49 + Id int64 `json:"id,optional,omitempty"` // 唯一标识
  50 + Title string `json:"title,optional,omitempty"` // 会话标题
  51 + Abstract string `json:"abstract,optional,omitempty"` // 摘要
  52 + CreatedAt int64 `json:"createdAt,optional,omitempty"` // 创建时间
  53 + Module int `json:"module,optional,omitempty,default=1"` // 1:openai chat 2:星火文档问答
  54 + Type string `json:"type,optional,omitempty,default=chat"` // 类型 chat:普通问答 spark_dataset_chat:星火知识库问答 spark_documents_chat:星火多文档问答
  55 + DatasetId int64 `json:"datasetId,optional,omitempty"` // 知识库
  56 + DocumentIds []int64 `json:"documentIds,optional,omitempty"` // 多文档
51 } 57 }
52 58
53 type ChatModelsRequest struct { 59 type ChatModelsRequest struct {
@@ -121,3 +127,136 @@ type User struct { @@ -121,3 +127,136 @@ type User struct {
121 Name string `json:"name"` // 名称 127 Name string `json:"name"` // 名称
122 Avatar string `json:"avatar"` // 头像 128 Avatar string `json:"avatar"` // 头像
123 } 129 }
  130 +
  131 +type ChatDocumentGetRequest struct {
  132 + Id int64 `path:"id"`
  133 +}
  134 +
  135 +type ChatDocumentGetResponse struct {
  136 + ChatDocument ChatDocumentItem `json:"document"`
  137 +}
  138 +
  139 +type ChatDocumentSaveRequest struct {
  140 + FileName string `json:"fileName"`
  141 + Url string `json:"url"`
  142 + Size float64 `json:"size"` // 文件大小(KB)
  143 +}
  144 +
  145 +type ChatDocumentSaveResponse struct {
  146 + ChatDocument ChatDocumentItem `json:"document"`
  147 +}
  148 +
  149 +type ChatDocumentDeleteRequest struct {
  150 + Id int64 `path:"id"`
  151 +}
  152 +
  153 +type ChatDocumentDeleteResponse struct {
  154 +}
  155 +
  156 +type ChatDocumentUpdateRequest struct {
  157 + Id int64 `path:"id"`
  158 + ChatDocument ChatDocumentItem `json:"document"`
  159 +}
  160 +
  161 +type ChatDocumentUpdateResponse struct {
  162 +}
  163 +
  164 +type ChatDocumentSearchRequest struct {
  165 + Page int `json:"page,optional"`
  166 + Size int `json:"size,optional"`
  167 + Name string `json:"name,optional"` // 文件名匹配
  168 + FileType string `json:"fileType,optional"` // 文件类型 markdown\pdf\txt\doc&docx
  169 + Status int `json:"status,optional"` // 文件状态 1.使用中、0.待处理、2.预处理中、3.处理失败
  170 +}
  171 +
  172 +type ChatDocumentSearchResponse struct {
  173 + List []ChatDocumentItem `json:"list"`
  174 + Total int64 `json:"total"`
  175 +}
  176 +
  177 +type ChatDocumentItem struct {
  178 + Id int64 `json:"id"`
  179 + Name string `json:"name,optional"` // 文件名
  180 + FileType string `json:"fileType"` // 文件类型 markdown\pdf\txt\doc&docx
  181 + Status int `json:"status"` // 1.使用中、0.待处理、2.预处理中、3.处理失败
  182 + OriginFileName string `json:"originFileName,omitempty"` // 源文件命
  183 + Ext string `json:"ext,omitempty"` // 格式
  184 + FileId string `json:"fileId,omitempty"` // 星火文件ID
  185 + FileUrl string `json:"fileUrl,omitempty"` // 文件地址
  186 + FileSize float64 `json:"fileSize,omitempty"` // 文件大小KB
  187 + CreatedAt int64 `json:"createdAt,omitempty"` // 创建时间
  188 + UpdatedAt int64 `json:"updatedAt,omitempty"` // 更新时间
  189 +}
  190 +
  191 +type ChatDocumentRenameRequest struct {
  192 + Id int64 `json:"id"` // 文档ID
  193 + Name string `json:"name"` // 新名称
  194 +}
  195 +
  196 +type ChatDocumentRenameResponse struct {
  197 + ChatDocument ChatDocumentItem `json:"document"`
  198 +}
  199 +
  200 +type ChatDatasetGetRequest struct {
  201 + Id int64 `path:"id"`
  202 +}
  203 +
  204 +type ChatDatasetGetResponse struct {
  205 + ChatDataset ChatDatasetItem `json:"dataset"`
  206 + Documents []ChatDocumentItem `json:"documents"`
  207 +}
  208 +
  209 +type ChatDatasetSaveRequest struct {
  210 + Name string `json:"name"` // 名称
  211 + DocumentIds []int64 `json:"documentIds"` // 文档ID列表
  212 +}
  213 +
  214 +type ChatDatasetSaveResponse struct {
  215 + ChatDataset ChatDatasetItem `json:"dataset"`
  216 +}
  217 +
  218 +type ChatDatasetDeleteRequest struct {
  219 + Id int64 `path:"id"`
  220 +}
  221 +
  222 +type ChatDatasetDeleteResponse struct {
  223 +}
  224 +
  225 +type ChatDatasetUpdateRequest struct {
  226 + Id int64 `path:"id"`
  227 + ChatDataset ChatDatasetItem `json:"dataset"`
  228 +}
  229 +
  230 +type ChatDatasetUpdateResponse struct {
  231 +}
  232 +
  233 +type ChatDatasetSearchRequest struct {
  234 + Page int `json:"page,optional"`
  235 + Size int `json:"size,optional"`
  236 + Name string `json:"name,optional"` // 文件名匹配
  237 + Status int `json:"status,optional,option=0|1|2"` // 1:启用 2:禁用
  238 +}
  239 +
  240 +type ChatDatasetSearchResponse struct {
  241 + List []ChatDatasetItem `json:"list"`
  242 + Total int64 `json:"total"`
  243 +}
  244 +
  245 +type ChatDatasetItem struct {
  246 + Id int64 `json:"id,optional,omitempty"`
  247 + Name *string `json:"name,optional,omitempty"` // 名称
  248 + Desc *string `json:"desc,optional,omitempty"` // 描述
  249 + Status *int `json:"status,optional,omitempty"` // 状态 1:启用 2:禁用
  250 + CreatedAt int64 `json:"createdAt,optional,omitempty"` // 创建时间
  251 + UpdatedAt int64 `json:"updatedAt,optional,omitempty"` // 更新时间
  252 + FileNumber *int `json:"fileNumber,optional,omitempty"` // 文件数量
  253 +}
  254 +
  255 +type ChatDatasetRenameRequest struct {
  256 + Id int64 `json:"id"` // 文档ID
  257 + Name string `json:"name"` // 新名称
  258 +}
  259 +
  260 +type ChatDatasetRenameResponse struct {
  261 + ChatDataset ChatDatasetItem `json:"dataset"`
  262 +}
@@ -15,3 +15,21 @@ CREATE TABLE `chat_model` @@ -15,3 +15,21 @@ CREATE TABLE `chat_model`
15 `id` int(0) NOT NULL COMMENT '唯一标识', 15 `id` int(0) NOT NULL COMMENT '唯一标识',
16 PRIMARY KEY (`id`) USING BTREE 16 PRIMARY KEY (`id`) USING BTREE
17 ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; 17 ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
  18 +
  19 +CREATE TABLE `chat_dataset`
  20 +(
  21 + `id` int(0) NOT NULL COMMENT '唯一标识',
  22 + PRIMARY KEY (`id`) USING BTREE
  23 +) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
  24 +
  25 +CREATE TABLE `chat_document`
  26 +(
  27 + `id` int(0) NOT NULL COMMENT '唯一标识',
  28 + PRIMARY KEY (`id`) USING BTREE
  29 +) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
  30 +
  31 +CREATE TABLE `chat_dataset_document_mapping`
  32 +(
  33 + `id` int(0) NOT NULL COMMENT '唯一标识',
  34 + PRIMARY KEY (`id`) USING BTREE
  35 +) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
1 syntax = "v1" 1 syntax = "v1"
2 2
3 -import "core/session.api"  
  3 +import "core/session.api"
  4 +import "core/document.api"
  5 +import "core/dataset.api"
  1 +syntax = "v1"
  2 +
  3 +// 后台接口
  4 +@server(
  5 + prefix: v1
  6 + group: dataset
  7 + middleware: LogRequest
  8 + jwt: SystemAuth
  9 +)
  10 +service Core {
  11 + @doc "知识库-详情"
  12 + @handler chatDatasetGet
  13 + get /chat/dataset/:id (ChatDatasetGetRequest) returns (ChatDatasetGetResponse)
  14 + @doc "知识库-保存"
  15 + @handler chatDatasetSave
  16 + post /chat/dataset (ChatDatasetSaveRequest) returns (ChatDatasetSaveResponse)
  17 + @doc "知识库-删除"
  18 + @handler chatDatasetDelete
  19 + delete /chat/dataset/:id (ChatDatasetDeleteRequest) returns (ChatDatasetDeleteResponse)
  20 + @doc "知识库-更新"
  21 + @handler chatDatasetUpdate
  22 + put /chat/dataset/:id (ChatDatasetUpdateRequest) returns (ChatDatasetUpdateResponse)
  23 + @doc "知识库-重命名"
  24 + @handler chatDatasetRename
  25 + post /chat/dataset/rename (ChatDatasetRenameRequest) returns (ChatDatasetRenameResponse)
  26 + @doc "知识库-搜索"
  27 + @handler chatDatasetSearch
  28 + post /chat/dataset/search (ChatDatasetSearchRequest) returns (ChatDatasetSearchResponse)
  29 +}
  30 +
  31 +type (
  32 + ChatDatasetGetRequest {
  33 + Id int64 `path:"id"`
  34 + }
  35 + ChatDatasetGetResponse {
  36 + ChatDataset ChatDatasetItem `json:"dataset"`
  37 + Documents []ChatDocumentItem `json:"documents"`
  38 + }
  39 +
  40 + ChatDatasetSaveRequest {
  41 + Name string `json:"name"` // 名称
  42 + DocumentIds []int64 `json:"documentIds"` // 文档ID列表
  43 + }
  44 + ChatDatasetSaveResponse {
  45 + ChatDataset ChatDatasetItem `json:"dataset"`
  46 + }
  47 +
  48 + ChatDatasetDeleteRequest {
  49 + Id int64 `path:"id"`
  50 + }
  51 + ChatDatasetDeleteResponse {}
  52 +
  53 + ChatDatasetUpdateRequest {
  54 + Id int64 `path:"id"`
  55 + ChatDataset ChatDatasetItem `json:"dataset"`
  56 + }
  57 + ChatDatasetUpdateResponse {}
  58 +
  59 + ChatDatasetSearchRequest {
  60 + Page int `json:"page,optional"`
  61 + Size int `json:"size,optional"`
  62 + Name string `json:"name,optional"` // 文件名匹配
  63 + Status int `json:"status,optional,option=0|1|2"`// 1:启用 2:禁用
  64 + }
  65 + ChatDatasetSearchResponse{
  66 + List []ChatDatasetItem `json:"list"`
  67 + Total int64 `json:"total"`
  68 + }
  69 + ChatDatasetItem {
  70 + Id int64 `json:"id,optional,omitempty"`
  71 + Name *string `json:"name,optional,omitempty"` // 名称
  72 + Desc *string `json:"desc,optional,omitempty"` // 描述
  73 + Status *int `json:"status,optional,omitempty"`// 状态 1:启用 2:禁用
  74 + CreatedAt int64 `json:"createdAt,optional,omitempty"` // 创建时间
  75 + UpdatedAt int64 `json:"updatedAt,optional,omitempty"` // 更新时间
  76 + FileNumber *int `json:"fileNumber,optional,omitempty"` // 文件数量
  77 + }
  78 +)
  79 +
  80 +type(
  81 + ChatDatasetRenameRequest{
  82 + Id int64 `json:"id"` // 文档ID
  83 + Name string `json:"name"` // 新名称
  84 + }
  85 + ChatDatasetRenameResponse{
  86 + ChatDataset ChatDatasetItem `json:"dataset"`
  87 + }
  88 +)
  1 +syntax = "v1"
  2 +
  3 +// 后台接口
  4 +@server(
  5 + prefix: v1
  6 + group: document
  7 + middleware: LogRequest
  8 + jwt: SystemAuth
  9 +)
  10 +service Core {
  11 + @doc "文档-详情"
  12 + @handler chatDocumentGet
  13 + get /chat/document/:id (ChatDocumentGetRequest) returns (ChatDocumentGetResponse)
  14 + @doc "文档-保存"
  15 + @handler chatDocumentSave
  16 + post /chat/document (ChatDocumentSaveRequest) returns (ChatDocumentSaveResponse)
  17 + @doc "文档-删除"
  18 + @handler chatDocumentDelete
  19 + delete /chat/document/:id (ChatDocumentDeleteRequest) returns (ChatDocumentDeleteResponse)
  20 + @doc "文档-删除"
  21 + @handler chatDocumentRename
  22 + post /chat/document/rename (ChatDocumentRenameRequest) returns (ChatDocumentRenameResponse)
  23 +// @doc "文档-更新"
  24 +// @handler chatDocumentUpdate
  25 +// put /chat/document/:id (ChatDocumentUpdateRequest) returns (ChatDocumentUpdateResponse)
  26 + @doc "文档-搜索"
  27 + @handler chatDocumentSearch
  28 + post /chat/document/search (ChatDocumentSearchRequest) returns (ChatDocumentSearchResponse)
  29 +}
  30 +
  31 +type (
  32 + ChatDocumentGetRequest {
  33 + Id int64 `path:"id"`
  34 + }
  35 + ChatDocumentGetResponse {
  36 + ChatDocument ChatDocumentItem `json:"document"`
  37 + }
  38 +
  39 + ChatDocumentSaveRequest {
  40 + FileName string `json:"fileName"`
  41 + Url string `json:"url"`
  42 + Size float64 `json:"size"` // 文件大小(KB)
  43 + }
  44 + ChatDocumentSaveResponse {
  45 + ChatDocument ChatDocumentItem `json:"document"`
  46 + }
  47 +
  48 + ChatDocumentDeleteRequest {
  49 + Id int64 `path:"id"`
  50 + }
  51 + ChatDocumentDeleteResponse {}
  52 +
  53 + ChatDocumentUpdateRequest {
  54 + Id int64 `path:"id"`
  55 + ChatDocument ChatDocumentItem `json:"document"`
  56 + }
  57 + ChatDocumentUpdateResponse {}
  58 +
  59 + ChatDocumentSearchRequest {
  60 + Page int `json:"page,optional"`
  61 + Size int `json:"size,optional"`
  62 + Name string `json:"name,optional"` // 文件名匹配
  63 + FileType string `json:"fileType,optional"` // 文件类型 markdown\pdf\txt\doc&docx
  64 + Status int `json:"status,optional"` // 文件状态 1.使用中、0.待处理、2.预处理中、3.处理失败
  65 + }
  66 + ChatDocumentSearchResponse{
  67 + List []ChatDocumentItem `json:"list"`
  68 + Total int64 `json:"total"`
  69 + }
  70 + ChatDocumentItem {
  71 + Id int64 `json:"id"`
  72 + Name string `json:"name,optional"` // 文件名
  73 + FileType string `json:"fileType"` // 文件类型 markdown\pdf\txt\doc&docx
  74 + Status int `json:"status"` // 1.使用中、0.待处理、2.预处理中、3.处理失败
  75 + OriginFileName string `json:"originFileName,omitempty"` // 源文件命
  76 + Ext string `json:"ext,omitempty"` // 格式
  77 + FileId string `json:"fileId,omitempty"` // 星火文件ID
  78 + FileUrl string `json:"fileUrl,omitempty"` // 文件地址
  79 + FileSize float64 `json:"fileSize,omitempty"` // 文件大小KB
  80 + CreatedAt int64 `json:"createdAt,omitempty"` // 创建时间
  81 + UpdatedAt int64 `json:"updatedAt,omitempty"` // 更新时间
  82 + }
  83 +)
  84 +
  85 +type(
  86 + ChatDocumentRenameRequest{
  87 + Id int64 `json:"id"` // 文档ID
  88 + Name string `json:"name"` // 新名称
  89 + }
  90 + ChatDocumentRenameResponse{
  91 + ChatDocument ChatDocumentItem `json:"document"`
  92 + }
  93 +)
@@ -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 chatMySparkSessions
  35 + post /chat/session/my_spark_sessions (ChatSessionSearchRequest) returns (ChatSessionSearchResponse)
  36 + @doc "星火聊天会话-保存"
  37 + @handler chatSparkSessionSave
  38 + post /chat/spark_session (ChatSessionSaveRequest) returns (ChatSessionSaveResponse)
  39 +
33 @doc "聊天会话-对话记录列表" 40 @doc "聊天会话-对话记录列表"
34 @handler chatSessionRecords 41 @handler chatSessionRecords
35 post /chat/session/records (ChatSessionRecordsRequest) returns (ChatSessionRecordsResponse) 42 post /chat/session/records (ChatSessionRecordsRequest) returns (ChatSessionRecordsResponse)
@@ -51,7 +58,9 @@ type ( @@ -51,7 +58,9 @@ type (
51 ChatSessionSaveRequest { 58 ChatSessionSaveRequest {
52 ChatSession ChatSessionItem `json:"session"` 59 ChatSession ChatSessionItem `json:"session"`
53 } 60 }
54 - ChatSessionSaveResponse {} 61 + ChatSessionSaveResponse {
  62 + ChatSession ChatSessionItem `json:"session"`
  63 + }
55 64
56 ChatSessionDeleteRequest { 65 ChatSessionDeleteRequest {
57 Id int64 `path:"id"` 66 Id int64 `path:"id"`
@@ -68,6 +77,7 @@ type ( @@ -68,6 +77,7 @@ type (
68 Page int `json:"page,optional"` 77 Page int `json:"page,optional"`
69 Size int `json:"size,optional"` 78 Size int `json:"size,optional"`
70 Title string `json:"title,optional"` // 按标题搜索 79 Title string `json:"title,optional"` // 按标题搜索
  80 + Module int `json:"module,optional"` // 模块 1:openai chat 2:星火文档问答
71 } 81 }
72 ChatSessionSearchResponse{ 82 ChatSessionSearchResponse{
73 List []ChatSessionItem `json:"list"` 83 List []ChatSessionItem `json:"list"`
@@ -78,6 +88,11 @@ type ( @@ -78,6 +88,11 @@ type (
78 Title string `json:"title,optional,omitempty"` // 会话标题 88 Title string `json:"title,optional,omitempty"` // 会话标题
79 Abstract string `json:"abstract,optional,omitempty"` // 摘要 89 Abstract string `json:"abstract,optional,omitempty"` // 摘要
80 CreatedAt int64 `json:"createdAt,optional,omitempty"` // 创建时间 90 CreatedAt int64 `json:"createdAt,optional,omitempty"` // 创建时间
  91 +
  92 + Module int `json:"module,optional,omitempty,default=1"` // 1:openai chat 2:星火文档问答
  93 + Type string `json:"type,optional,omitempty,default=chat"` // 类型 chat:普通问答 spark_dataset_chat:星火知识库问答 spark_documents_chat:星火多文档问答
  94 + DatasetId int64 `json:"datasetId,optional,omitempty"` // 知识库
  95 + DocumentIds []int64 `json:"documentIds,optional,omitempty"` // 多文档
81 } 96 }
82 ) 97 )
83 98
@@ -15,6 +15,294 @@ @@ -15,6 +15,294 @@
15 "application/json" 15 "application/json"
16 ], 16 ],
17 "paths": { 17 "paths": {
  18 + "v1/chat/dataset": {
  19 + "post": {
  20 + "summary": "知识库-保存",
  21 + "operationId": "chatDatasetSave",
  22 + "responses": {
  23 + "200": {
  24 + "description": "A successful response.",
  25 + "schema": {
  26 + "$ref": "#/definitions/ChatDatasetSaveResponse"
  27 + }
  28 + }
  29 + },
  30 + "parameters": [
  31 + {
  32 + "name": "body",
  33 + "in": "body",
  34 + "required": true,
  35 + "schema": {
  36 + "$ref": "#/definitions/ChatDatasetSaveRequest"
  37 + }
  38 + }
  39 + ],
  40 + "requestBody": {},
  41 + "tags": [
  42 + "dataset"
  43 + ]
  44 + }
  45 + },
  46 + "v1/chat/dataset/rename": {
  47 + "post": {
  48 + "summary": "知识库-重命名",
  49 + "operationId": "chatDatasetRename",
  50 + "responses": {
  51 + "200": {
  52 + "description": "A successful response.",
  53 + "schema": {
  54 + "$ref": "#/definitions/ChatDatasetUpdateResponse"
  55 + }
  56 + }
  57 + },
  58 + "parameters": [
  59 + {
  60 + "name": "body",
  61 + "in": "body",
  62 + "required": true,
  63 + "schema": {
  64 + "$ref": "#/definitions/ChatDatasetUpdateRequest"
  65 + }
  66 + }
  67 + ],
  68 + "requestBody": {},
  69 + "tags": [
  70 + "dataset"
  71 + ]
  72 + }
  73 + },
  74 + "v1/chat/dataset/search": {
  75 + "post": {
  76 + "summary": "知识库-搜索",
  77 + "operationId": "chatDatasetSearch",
  78 + "responses": {
  79 + "200": {
  80 + "description": "A successful response.",
  81 + "schema": {
  82 + "$ref": "#/definitions/ChatDatasetSearchResponse"
  83 + }
  84 + }
  85 + },
  86 + "parameters": [
  87 + {
  88 + "name": "body",
  89 + "in": "body",
  90 + "required": true,
  91 + "schema": {
  92 + "$ref": "#/definitions/ChatDatasetSearchRequest"
  93 + }
  94 + }
  95 + ],
  96 + "requestBody": {},
  97 + "tags": [
  98 + "dataset"
  99 + ]
  100 + }
  101 + },
  102 + "v1/chat/dataset/{id}": {
  103 + "get": {
  104 + "summary": "知识库-详情",
  105 + "operationId": "chatDatasetGet",
  106 + "responses": {
  107 + "200": {
  108 + "description": "A successful response.",
  109 + "schema": {
  110 + "$ref": "#/definitions/ChatDatasetGetResponse"
  111 + }
  112 + }
  113 + },
  114 + "parameters": [
  115 + {
  116 + "name": "id",
  117 + "in": "path",
  118 + "required": true,
  119 + "type": "string"
  120 + }
  121 + ],
  122 + "requestBody": {},
  123 + "tags": [
  124 + "dataset"
  125 + ]
  126 + },
  127 + "delete": {
  128 + "summary": "知识库-删除",
  129 + "operationId": "chatDatasetDelete",
  130 + "responses": {
  131 + "200": {
  132 + "description": "A successful response.",
  133 + "schema": {
  134 + "$ref": "#/definitions/ChatDatasetDeleteResponse"
  135 + }
  136 + }
  137 + },
  138 + "parameters": [
  139 + {
  140 + "name": "id",
  141 + "in": "path",
  142 + "required": true,
  143 + "type": "string"
  144 + },
  145 + {
  146 + "name": "body",
  147 + "in": "body",
  148 + "required": true,
  149 + "schema": {
  150 + "$ref": "#/definitions/ChatDatasetDeleteRequest"
  151 + }
  152 + }
  153 + ],
  154 + "requestBody": {},
  155 + "tags": [
  156 + "dataset"
  157 + ]
  158 + },
  159 + "put": {
  160 + "summary": "知识库-更新",
  161 + "operationId": "chatDatasetUpdate",
  162 + "responses": {
  163 + "200": {
  164 + "description": "A successful response.",
  165 + "schema": {
  166 + "$ref": "#/definitions/ChatDatasetUpdateResponse"
  167 + }
  168 + }
  169 + },
  170 + "parameters": [
  171 + {
  172 + "name": "id",
  173 + "in": "path",
  174 + "required": true,
  175 + "type": "string"
  176 + },
  177 + {
  178 + "name": "body",
  179 + "in": "body",
  180 + "required": true,
  181 + "schema": {
  182 + "$ref": "#/definitions/ChatDatasetUpdateRequest"
  183 + }
  184 + }
  185 + ],
  186 + "requestBody": {},
  187 + "tags": [
  188 + "dataset"
  189 + ]
  190 + }
  191 + },
  192 + "v1/chat/document": {
  193 + "post": {
  194 + "summary": "文档-保存",
  195 + "operationId": "chatDocumentSave",
  196 + "responses": {
  197 + "200": {
  198 + "description": "A successful response.",
  199 + "schema": {
  200 + "$ref": "#/definitions/ChatDocumentSaveResponse"
  201 + }
  202 + }
  203 + },
  204 + "parameters": [
  205 + {
  206 + "name": "body",
  207 + "in": "body",
  208 + "required": true,
  209 + "schema": {
  210 + "$ref": "#/definitions/ChatDocumentSaveRequest"
  211 + }
  212 + }
  213 + ],
  214 + "requestBody": {},
  215 + "tags": [
  216 + "document"
  217 + ]
  218 + }
  219 + },
  220 + "v1/chat/document/search": {
  221 + "post": {
  222 + "summary": "文档-搜索",
  223 + "operationId": "chatDocumentSearch",
  224 + "responses": {
  225 + "200": {
  226 + "description": "A successful response.",
  227 + "schema": {
  228 + "$ref": "#/definitions/ChatDocumentSearchResponse"
  229 + }
  230 + }
  231 + },
  232 + "parameters": [
  233 + {
  234 + "name": "body",
  235 + "in": "body",
  236 + "required": true,
  237 + "schema": {
  238 + "$ref": "#/definitions/ChatDocumentSearchRequest"
  239 + }
  240 + }
  241 + ],
  242 + "requestBody": {},
  243 + "tags": [
  244 + "document"
  245 + ]
  246 + }
  247 + },
  248 + "v1/chat/document/{id}": {
  249 + "get": {
  250 + "summary": "文档-详情",
  251 + "operationId": "chatDocumentGet",
  252 + "responses": {
  253 + "200": {
  254 + "description": "A successful response.",
  255 + "schema": {
  256 + "$ref": "#/definitions/ChatDocumentGetResponse"
  257 + }
  258 + }
  259 + },
  260 + "parameters": [
  261 + {
  262 + "name": "id",
  263 + "in": "path",
  264 + "required": true,
  265 + "type": "string"
  266 + }
  267 + ],
  268 + "requestBody": {},
  269 + "tags": [
  270 + "document"
  271 + ]
  272 + },
  273 + "delete": {
  274 + "summary": "文档-删除",
  275 + "operationId": "chatDocumentDelete",
  276 + "responses": {
  277 + "200": {
  278 + "description": "A successful response.",
  279 + "schema": {
  280 + "$ref": "#/definitions/ChatDocumentDeleteResponse"
  281 + }
  282 + }
  283 + },
  284 + "parameters": [
  285 + {
  286 + "name": "id",
  287 + "in": "path",
  288 + "required": true,
  289 + "type": "string"
  290 + },
  291 + {
  292 + "name": "body",
  293 + "in": "body",
  294 + "required": true,
  295 + "schema": {
  296 + "$ref": "#/definitions/ChatDocumentDeleteRequest"
  297 + }
  298 + }
  299 + ],
  300 + "requestBody": {},
  301 + "tags": [
  302 + "document"
  303 + ]
  304 + }
  305 + },
18 "v1/chat/models": { 306 "v1/chat/models": {
19 "get": { 307 "get": {
20 "summary": "模型列表", 308 "summary": "模型列表",
@@ -64,7 +352,7 @@ @@ -64,7 +352,7 @@
64 "v1/chat/session/conversation": { 352 "v1/chat/session/conversation": {
65 "get": { 353 "get": {
66 "summary": "聊天会话-对话", 354 "summary": "聊天会话-对话",
67 - "operationId": "chatSessionConversation", 355 + "operationId": "chatSessionConversationWs",
68 "responses": { 356 "responses": {
69 "200": { 357 "200": {
70 "description": "A successful response.", 358 "description": "A successful response.",
@@ -103,54 +391,234 @@ @@ -103,54 +391,234 @@
103 "in": "query", 391 "in": "query",
104 "required": true, 392 "required": true,
105 "type": "string" 393 "type": "string"
106 - },  
107 - {  
108 - "name": "fileUrl",  
109 - "description": " 文件地址",  
110 - "in": "query",  
111 - "required": true,  
112 - "type": "string"  
113 } 394 }
114 ], 395 ],
115 "requestBody": {}, 396 "requestBody": {},
116 "tags": [ 397 "tags": [
117 "chat" 398 "chat"
118 ] 399 ]
119 - }  
120 - },  
121 - "v1/chat/session/records": {  
122 - "get": {  
123 - "summary": "聊天会话-对话记录列表",  
124 - "operationId": "chatSessionRecords", 400 + },
  401 + "post": {
  402 + "summary": "聊天会话-对话",
  403 + "operationId": "chatSessionConversation",
125 "responses": { 404 "responses": {
126 "200": { 405 "200": {
127 "description": "A successful response.", 406 "description": "A successful response.",
128 "schema": { 407 "schema": {
129 - "$ref": "#/definitions/ChatSessionRecordsResponse" 408 + "$ref": "#/definitions/ChatSessionConversationResponse"
130 } 409 }
131 } 410 }
132 }, 411 },
133 "parameters": [ 412 "parameters": [
134 { 413 {
135 - "name": "page",  
136 - "in": "query",  
137 - "required": false,  
138 - "type": "integer",  
139 - "format": "int32"  
140 - },  
141 - {  
142 - "name": "size",  
143 - "in": "query",  
144 - "required": false,  
145 - "type": "integer",  
146 - "format": "int32" 414 + "name": "body",
  415 + "in": "body",
  416 + "required": true,
  417 + "schema": {
  418 + "$ref": "#/definitions/ChatSessionConversationRequest"
  419 + }
  420 + }
  421 + ],
  422 + "requestBody": {},
  423 + "tags": [
  424 + "chat"
  425 + ]
  426 + }
  427 + },
  428 + "v1/chat/session/my_spark_sessions": {
  429 + "post": {
  430 + "summary": "星火聊天会话-我的会话",
  431 + "operationId": "chatMySparkSessions",
  432 + "responses": {
  433 + "200": {
  434 + "description": "A successful response.",
  435 + "schema": {
  436 + "$ref": "#/definitions/ChatSessionSearchResponse"
  437 + }
  438 + }
  439 + },
  440 + "parameters": [
  441 + {
  442 + "name": "body",
  443 + "in": "body",
  444 + "required": true,
  445 + "schema": {
  446 + "$ref": "#/definitions/ChatSessionSearchRequest"
  447 + }
  448 + }
  449 + ],
  450 + "requestBody": {},
  451 + "tags": [
  452 + "chat"
  453 + ]
  454 + }
  455 + },
  456 + "v1/chat/session/records": {
  457 + "post": {
  458 + "summary": "聊天会话-对话记录列表",
  459 + "operationId": "chatSessionRecords",
  460 + "responses": {
  461 + "200": {
  462 + "description": "A successful response.",
  463 + "schema": {
  464 + "$ref": "#/definitions/ChatSessionRecordsResponse"
  465 + }
  466 + }
  467 + },
  468 + "parameters": [
  469 + {
  470 + "name": "body",
  471 + "in": "body",
  472 + "required": true,
  473 + "schema": {
  474 + "$ref": "#/definitions/ChatSessionRecordsRequest"
  475 + }
  476 + }
  477 + ],
  478 + "requestBody": {},
  479 + "tags": [
  480 + "chat"
  481 + ]
  482 + }
  483 + },
  484 + "v1/chat/session/search": {
  485 + "post": {
  486 + "summary": "聊天会话-搜索",
  487 + "operationId": "chatSessionSearch",
  488 + "responses": {
  489 + "200": {
  490 + "description": "A successful response.",
  491 + "schema": {
  492 + "$ref": "#/definitions/ChatSessionSearchResponse"
  493 + }
  494 + }
  495 + },
  496 + "parameters": [
  497 + {
  498 + "name": "body",
  499 + "in": "body",
  500 + "required": true,
  501 + "schema": {
  502 + "$ref": "#/definitions/ChatSessionSearchRequest"
  503 + }
  504 + }
  505 + ],
  506 + "requestBody": {},
  507 + "tags": [
  508 + "chat"
  509 + ]
  510 + }
  511 + },
  512 + "v1/chat/session/{id}": {
  513 + "get": {
  514 + "summary": "聊天会话-详情",
  515 + "operationId": "chatSessionGet",
  516 + "responses": {
  517 + "200": {
  518 + "description": "A successful response.",
  519 + "schema": {
  520 + "$ref": "#/definitions/ChatSessionGetResponse"
  521 + }
  522 + }
  523 + },
  524 + "parameters": [
  525 + {
  526 + "name": "id",
  527 + "in": "path",
  528 + "required": true,
  529 + "type": "string"
  530 + }
  531 + ],
  532 + "requestBody": {},
  533 + "tags": [
  534 + "chat"
  535 + ]
  536 + },
  537 + "delete": {
  538 + "summary": "聊天会话-删除",
  539 + "operationId": "chatSessionDelete",
  540 + "responses": {
  541 + "200": {
  542 + "description": "A successful response.",
  543 + "schema": {
  544 + "$ref": "#/definitions/ChatSessionDeleteResponse"
  545 + }
  546 + }
  547 + },
  548 + "parameters": [
  549 + {
  550 + "name": "id",
  551 + "in": "path",
  552 + "required": true,
  553 + "type": "string"
147 }, 554 },
148 { 555 {
149 - "name": "sessionId",  
150 - "in": "query", 556 + "name": "body",
  557 + "in": "body",
151 "required": true, 558 "required": true,
152 - "type": "integer",  
153 - "format": "int64" 559 + "schema": {
  560 + "$ref": "#/definitions/ChatSessionDeleteRequest"
  561 + }
  562 + }
  563 + ],
  564 + "requestBody": {},
  565 + "tags": [
  566 + "chat"
  567 + ]
  568 + },
  569 + "put": {
  570 + "summary": "聊天会话-更新",
  571 + "operationId": "chatSessionUpdate",
  572 + "responses": {
  573 + "200": {
  574 + "description": "A successful response.",
  575 + "schema": {
  576 + "$ref": "#/definitions/ChatSessionUpdateResponse"
  577 + }
  578 + }
  579 + },
  580 + "parameters": [
  581 + {
  582 + "name": "id",
  583 + "in": "path",
  584 + "required": true,
  585 + "type": "string"
  586 + },
  587 + {
  588 + "name": "body",
  589 + "in": "body",
  590 + "required": true,
  591 + "schema": {
  592 + "$ref": "#/definitions/ChatSessionUpdateRequest"
  593 + }
  594 + }
  595 + ],
  596 + "requestBody": {},
  597 + "tags": [
  598 + "chat"
  599 + ]
  600 + }
  601 + },
  602 + "v1/chat/spark_session": {
  603 + "post": {
  604 + "summary": "星火聊天会话-保存",
  605 + "operationId": "chatSparkSessionSave",
  606 + "responses": {
  607 + "200": {
  608 + "description": "A successful response.",
  609 + "schema": {
  610 + "$ref": "#/definitions/ChatSessionSaveResponse"
  611 + }
  612 + }
  613 + },
  614 + "parameters": [
  615 + {
  616 + "name": "body",
  617 + "in": "body",
  618 + "required": true,
  619 + "schema": {
  620 + "$ref": "#/definitions/ChatSessionSaveRequest"
  621 + }
154 } 622 }
155 ], 623 ],
156 "requestBody": {}, 624 "requestBody": {},
@@ -158,127 +626,414 @@ @@ -158,127 +626,414 @@
158 "chat" 626 "chat"
159 ] 627 ]
160 } 628 }
  629 + }
  630 + },
  631 + "definitions": {
  632 + "ChatDatasetDeleteRequest": {
  633 + "type": "object",
  634 + "properties": {
  635 + "id": {
  636 + "type": "integer",
  637 + "format": "int64"
  638 + }
  639 + },
  640 + "title": "ChatDatasetDeleteRequest",
  641 + "required": [
  642 + "id"
  643 + ]
  644 + },
  645 + "ChatDatasetDeleteResponse": {
  646 + "type": "object",
  647 + "title": "ChatDatasetDeleteResponse"
  648 + },
  649 + "ChatDatasetGetRequest": {
  650 + "type": "object",
  651 + "properties": {
  652 + "id": {
  653 + "type": "integer",
  654 + "format": "int64"
  655 + }
  656 + },
  657 + "title": "ChatDatasetGetRequest",
  658 + "required": [
  659 + "id"
  660 + ]
  661 + },
  662 + "ChatDatasetGetResponse": {
  663 + "type": "object",
  664 + "properties": {
  665 + "dataset": {
  666 + "$ref": "#/definitions/ChatDatasetItem"
  667 + },
  668 + "documents": {
  669 + "type": "array",
  670 + "items": {
  671 + "$ref": "#/definitions/ChatDocumentItem"
  672 + }
  673 + }
  674 + },
  675 + "title": "ChatDatasetGetResponse",
  676 + "required": [
  677 + "dataset",
  678 + "documents"
  679 + ]
  680 + },
  681 + "ChatDatasetItem": {
  682 + "type": "object",
  683 + "properties": {
  684 + "id": {
  685 + "type": "integer",
  686 + "format": "int64"
  687 + },
  688 + "name": {
  689 + "$ref": "#/definitions/string",
  690 + "description": " 名称"
  691 + },
  692 + "desc": {
  693 + "$ref": "#/definitions/string",
  694 + "description": " 描述"
  695 + },
  696 + "status": {
  697 + "$ref": "#/definitions/int",
  698 + "description": " 状态 1:启用 2:禁用"
  699 + },
  700 + "createdAt": {
  701 + "type": "integer",
  702 + "format": "int64",
  703 + "description": " 创建时间"
  704 + },
  705 + "updatedAt": {
  706 + "type": "integer",
  707 + "format": "int64",
  708 + "description": " 更新时间"
  709 + },
  710 + "fileNumber": {
  711 + "$ref": "#/definitions/int",
  712 + "description": " 文件数量"
  713 + }
  714 + },
  715 + "title": "ChatDatasetItem",
  716 + "required": [
  717 + "id",
  718 + "name",
  719 + "desc",
  720 + "status",
  721 + "createdAt",
  722 + "updatedAt",
  723 + "fileNumber"
  724 + ]
  725 + },
  726 + "ChatDatasetSaveRequest": {
  727 + "type": "object",
  728 + "properties": {
  729 + "name": {
  730 + "type": "string",
  731 + "description": " 名称"
  732 + },
  733 + "documentIds": {
  734 + "type": "array",
  735 + "items": {
  736 + "type": "integer",
  737 + "format": "int64"
  738 + },
  739 + "description": " 文档ID列表"
  740 + }
  741 + },
  742 + "title": "ChatDatasetSaveRequest",
  743 + "required": [
  744 + "name",
  745 + "documentIds"
  746 + ]
  747 + },
  748 + "ChatDatasetSaveResponse": {
  749 + "type": "object",
  750 + "properties": {
  751 + "dataset": {
  752 + "$ref": "#/definitions/ChatDatasetItem"
  753 + }
  754 + },
  755 + "title": "ChatDatasetSaveResponse",
  756 + "required": [
  757 + "dataset"
  758 + ]
  759 + },
  760 + "ChatDatasetSearchRequest": {
  761 + "type": "object",
  762 + "properties": {
  763 + "page": {
  764 + "type": "integer",
  765 + "format": "int32"
  766 + },
  767 + "size": {
  768 + "type": "integer",
  769 + "format": "int32"
  770 + },
  771 + "name": {
  772 + "type": "string",
  773 + "description": " 文件名匹配"
  774 + },
  775 + "name": {
  776 + "type": "integer",
  777 + "format": "int32",
  778 + "description": " 1:启用 2:禁用"
  779 + }
  780 + },
  781 + "title": "ChatDatasetSearchRequest",
  782 + "required": [
  783 + "name"
  784 + ]
  785 + },
  786 + "ChatDatasetSearchResponse": {
  787 + "type": "object",
  788 + "properties": {
  789 + "list": {
  790 + "type": "array",
  791 + "items": {
  792 + "$ref": "#/definitions/ChatDatasetItem"
  793 + }
  794 + },
  795 + "total": {
  796 + "type": "integer",
  797 + "format": "int64"
  798 + }
  799 + },
  800 + "title": "ChatDatasetSearchResponse",
  801 + "required": [
  802 + "list",
  803 + "total"
  804 + ]
  805 + },
  806 + "ChatDatasetUpdateRequest": {
  807 + "type": "object",
  808 + "properties": {
  809 + "id": {
  810 + "type": "integer",
  811 + "format": "int64"
  812 + },
  813 + "dataset": {
  814 + "$ref": "#/definitions/ChatDatasetItem"
  815 + }
  816 + },
  817 + "title": "ChatDatasetUpdateRequest",
  818 + "required": [
  819 + "id",
  820 + "dataset"
  821 + ]
  822 + },
  823 + "ChatDatasetUpdateResponse": {
  824 + "type": "object",
  825 + "title": "ChatDatasetUpdateResponse"
  826 + },
  827 + "ChatDocumentDeleteRequest": {
  828 + "type": "object",
  829 + "properties": {
  830 + "id": {
  831 + "type": "integer",
  832 + "format": "int64"
  833 + }
  834 + },
  835 + "title": "ChatDocumentDeleteRequest",
  836 + "required": [
  837 + "id"
  838 + ]
  839 + },
  840 + "ChatDocumentDeleteResponse": {
  841 + "type": "object",
  842 + "title": "ChatDocumentDeleteResponse"
  843 + },
  844 + "ChatDocumentGetRequest": {
  845 + "type": "object",
  846 + "properties": {
  847 + "id": {
  848 + "type": "integer",
  849 + "format": "int64"
  850 + }
  851 + },
  852 + "title": "ChatDocumentGetRequest",
  853 + "required": [
  854 + "id"
  855 + ]
  856 + },
  857 + "ChatDocumentGetResponse": {
  858 + "type": "object",
  859 + "properties": {
  860 + "document": {
  861 + "$ref": "#/definitions/ChatDocumentItem"
  862 + }
  863 + },
  864 + "title": "ChatDocumentGetResponse",
  865 + "required": [
  866 + "document"
  867 + ]
  868 + },
  869 + "ChatDocumentItem": {
  870 + "type": "object",
  871 + "properties": {
  872 + "id": {
  873 + "type": "integer",
  874 + "format": "int64"
  875 + },
  876 + "name": {
  877 + "type": "string",
  878 + "description": " 文件名"
  879 + },
  880 + "fileType": {
  881 + "type": "string",
  882 + "description": " 文件类型 markdown\\pdf\\txt\\doc\u0026docx"
  883 + },
  884 + "status": {
  885 + "type": "integer",
  886 + "format": "int32",
  887 + "description": " 1.使用中、0.待处理、2.预处理中、3.处理失败"
  888 + },
  889 + "originFileName": {
  890 + "type": "string",
  891 + "description": " 源文件命"
  892 + },
  893 + "ext": {
  894 + "type": "string",
  895 + "description": " 格式"
  896 + },
  897 + "fileId": {
  898 + "type": "string",
  899 + "description": " 星火文件ID"
  900 + },
  901 + "fileUrl": {
  902 + "type": "string",
  903 + "description": " 文件地址"
  904 + },
  905 + "fileSize": {
  906 + "type": "number",
  907 + "format": "double",
  908 + "description": " 文件大小KB"
  909 + },
  910 + "createdAt": {
  911 + "type": "integer",
  912 + "format": "int64",
  913 + "description": " 创建时间"
  914 + },
  915 + "updatedAt": {
  916 + "type": "integer",
  917 + "format": "int64",
  918 + "description": " 更新时间"
  919 + }
  920 + },
  921 + "title": "ChatDocumentItem",
  922 + "required": [
  923 + "id",
  924 + "fileType",
  925 + "status",
  926 + "originFileName",
  927 + "ext",
  928 + "fileId",
  929 + "fileUrl",
  930 + "fileSize",
  931 + "createdAt",
  932 + "updatedAt"
  933 + ]
161 }, 934 },
162 - "v1/chat/session/search": {  
163 - "post": {  
164 - "summary": "聊天会话-搜索",  
165 - "operationId": "chatSessionSearch",  
166 - "responses": {  
167 - "200": {  
168 - "description": "A successful response.",  
169 - "schema": {  
170 - "$ref": "#/definitions/ChatSessionSearchResponse"  
171 - }  
172 - } 935 + "ChatDocumentSaveRequest": {
  936 + "type": "object",
  937 + "properties": {
  938 + "fileName": {
  939 + "type": "string"
173 }, 940 },
174 - "parameters": [  
175 - {  
176 - "name": "body",  
177 - "in": "body",  
178 - "required": true,  
179 - "schema": {  
180 - "$ref": "#/definitions/ChatSessionSearchRequest"  
181 - }  
182 - }  
183 - ],  
184 - "requestBody": {},  
185 - "tags": [  
186 - "chat"  
187 - ]  
188 - } 941 + "url": {
  942 + "type": "string"
  943 + },
  944 + "size": {
  945 + "type": "number",
  946 + "format": "double",
  947 + "description": " 文件大小(KB)"
  948 + }
  949 + },
  950 + "title": "ChatDocumentSaveRequest",
  951 + "required": [
  952 + "fileName",
  953 + "url",
  954 + "size"
  955 + ]
189 }, 956 },
190 - "v1/chat/session/{id}": {  
191 - "get": {  
192 - "summary": "聊天会话-详情",  
193 - "operationId": "chatSessionGet",  
194 - "responses": {  
195 - "200": {  
196 - "description": "A successful response.",  
197 - "schema": {  
198 - "$ref": "#/definitions/ChatSessionGetResponse"  
199 - }  
200 - } 957 + "ChatDocumentSaveResponse": {
  958 + "type": "object",
  959 + "properties": {
  960 + "document": {
  961 + "$ref": "#/definitions/ChatDocumentItem"
  962 + }
  963 + },
  964 + "title": "ChatDocumentSaveResponse",
  965 + "required": [
  966 + "document"
  967 + ]
  968 + },
  969 + "ChatDocumentSearchRequest": {
  970 + "type": "object",
  971 + "properties": {
  972 + "page": {
  973 + "type": "integer",
  974 + "format": "int32"
201 }, 975 },
202 - "parameters": [  
203 - {  
204 - "name": "id",  
205 - "in": "path",  
206 - "required": true,  
207 - "type": "string"  
208 - }  
209 - ],  
210 - "requestBody": {},  
211 - "tags": [  
212 - "chat"  
213 - ] 976 + "size": {
  977 + "type": "integer",
  978 + "format": "int32"
  979 + },
  980 + "name": {
  981 + "type": "string",
  982 + "description": " 文件名匹配"
  983 + },
  984 + "fileType": {
  985 + "type": "string",
  986 + "description": " 文件类型 markdown\\pdf\\txt\\doc\u0026docx"
  987 + },
  988 + "status": {
  989 + "type": "integer",
  990 + "format": "int32",
  991 + "description": " 文件状态 1.使用中、0.待处理、2.预处理中、3.处理失败"
  992 + }
214 }, 993 },
215 - "delete": {  
216 - "summary": "聊天会话-删除",  
217 - "operationId": "chatSessionDelete",  
218 - "responses": {  
219 - "200": {  
220 - "description": "A successful response.",  
221 - "schema": {  
222 - "$ref": "#/definitions/ChatSessionDeleteResponse"  
223 - } 994 + "title": "ChatDocumentSearchRequest"
  995 + },
  996 + "ChatDocumentSearchResponse": {
  997 + "type": "object",
  998 + "properties": {
  999 + "list": {
  1000 + "type": "array",
  1001 + "items": {
  1002 + "$ref": "#/definitions/ChatDocumentItem"
224 } 1003 }
225 }, 1004 },
226 - "parameters": [  
227 - {  
228 - "name": "id",  
229 - "in": "path",  
230 - "required": true,  
231 - "type": "string"  
232 - },  
233 - {  
234 - "name": "body",  
235 - "in": "body",  
236 - "required": true,  
237 - "schema": {  
238 - "$ref": "#/definitions/ChatSessionDeleteRequest"  
239 - }  
240 - }  
241 - ],  
242 - "requestBody": {},  
243 - "tags": [  
244 - "chat"  
245 - ] 1005 + "total": {
  1006 + "type": "integer",
  1007 + "format": "int64"
  1008 + }
246 }, 1009 },
247 - "put": {  
248 - "summary": "聊天会话-更新",  
249 - "operationId": "chatSessionUpdate",  
250 - "responses": {  
251 - "200": {  
252 - "description": "A successful response.",  
253 - "schema": {  
254 - "$ref": "#/definitions/ChatSessionUpdateResponse"  
255 - }  
256 - } 1010 + "title": "ChatDocumentSearchResponse",
  1011 + "required": [
  1012 + "list",
  1013 + "total"
  1014 + ]
  1015 + },
  1016 + "ChatDocumentUpdateRequest": {
  1017 + "type": "object",
  1018 + "properties": {
  1019 + "id": {
  1020 + "type": "integer",
  1021 + "format": "int64"
257 }, 1022 },
258 - "parameters": [  
259 - {  
260 - "name": "id",  
261 - "in": "path",  
262 - "required": true,  
263 - "type": "string"  
264 - },  
265 - {  
266 - "name": "body",  
267 - "in": "body",  
268 - "required": true,  
269 - "schema": {  
270 - "$ref": "#/definitions/ChatSessionUpdateRequest"  
271 - }  
272 - }  
273 - ],  
274 - "requestBody": {},  
275 - "tags": [  
276 - "chat"  
277 - ]  
278 - }  
279 - }  
280 - },  
281 - "definitions": { 1023 + "document": {
  1024 + "$ref": "#/definitions/ChatDocumentItem"
  1025 + }
  1026 + },
  1027 + "title": "ChatDocumentUpdateRequest",
  1028 + "required": [
  1029 + "id",
  1030 + "document"
  1031 + ]
  1032 + },
  1033 + "ChatDocumentUpdateResponse": {
  1034 + "type": "object",
  1035 + "title": "ChatDocumentUpdateResponse"
  1036 + },
282 "ChatModelsRequest": { 1037 "ChatModelsRequest": {
283 "type": "object", 1038 "type": "object",
284 "title": "ChatModelsRequest" 1039 "title": "ChatModelsRequest"
@@ -329,13 +1084,62 @@ @@ -329,13 +1084,62 @@
329 "sessionId", 1084 "sessionId",
330 "modelId", 1085 "modelId",
331 "contentType", 1086 "contentType",
332 - "text",  
333 - "fileUrl" 1087 + "text"
  1088 + ]
  1089 + },
  1090 + "ChatSessionConversationRequestWs": {
  1091 + "type": "object",
  1092 + "properties": {
  1093 + "sessionId": {
  1094 + "type": "integer",
  1095 + "format": "int64",
  1096 + "description": " 会话ID"
  1097 + },
  1098 + "modelId": {
  1099 + "type": "integer",
  1100 + "format": "int64",
  1101 + "description": " 模型ID"
  1102 + },
  1103 + "contentType": {
  1104 + "type": "string",
  1105 + "description": " 内容类型 文本:text (图片:image 文档:document)"
  1106 + },
  1107 + "text": {
  1108 + "type": "string",
  1109 + "description": " 内容文本"
  1110 + }
  1111 + },
  1112 + "title": "ChatSessionConversationRequestWs",
  1113 + "required": [
  1114 + "sessionId",
  1115 + "modelId",
  1116 + "contentType",
  1117 + "text"
334 ] 1118 ]
335 }, 1119 },
336 "ChatSessionConversationResponse": { 1120 "ChatSessionConversationResponse": {
337 "type": "object", 1121 "type": "object",
338 - "title": "ChatSessionConversationResponse" 1122 + "properties": {
  1123 + "record": {
  1124 + "$ref": "#/definitions/Record"
  1125 + },
  1126 + "parts": {
  1127 + "type": "array",
  1128 + "items": {
  1129 + "type": "string"
  1130 + }
  1131 + },
  1132 + "finished": {
  1133 + "type": "boolean",
  1134 + "format": "boolean"
  1135 + }
  1136 + },
  1137 + "title": "ChatSessionConversationResponse",
  1138 + "required": [
  1139 + "record",
  1140 + "parts",
  1141 + "finished"
  1142 + ]
339 }, 1143 },
340 "ChatSessionDeleteRequest": { 1144 "ChatSessionDeleteRequest": {
341 "type": "object", 1145 "type": "object",
@@ -406,6 +1210,30 @@ @@ -406,6 +1210,30 @@
406 "type": "integer", 1210 "type": "integer",
407 "format": "int64", 1211 "format": "int64",
408 "description": " 创建时间" 1212 "description": " 创建时间"
  1213 + },
  1214 + "module": {
  1215 + "type": "integer",
  1216 + "format": "int32",
  1217 + "default": "1",
  1218 + "description": " 1:openai chat 2:星火文档问答"
  1219 + },
  1220 + "type": {
  1221 + "type": "string",
  1222 + "default": "chat",
  1223 + "description": " 类型 chat:普通问答 spark_dataset_chat:星火知识库问答 spark_documents_chat:星火多文档问答"
  1224 + },
  1225 + "datasetId": {
  1226 + "type": "integer",
  1227 + "format": "int64",
  1228 + "description": " 知识库"
  1229 + },
  1230 + "documentIds": {
  1231 + "type": "array",
  1232 + "items": {
  1233 + "type": "integer",
  1234 + "format": "int64"
  1235 + },
  1236 + "description": " 多文档"
409 } 1237 }
410 }, 1238 },
411 "title": "ChatSessionItem", 1239 "title": "ChatSessionItem",
@@ -413,7 +1241,11 @@ @@ -413,7 +1241,11 @@
413 "id", 1241 "id",
414 "title", 1242 "title",
415 "abstract", 1243 "abstract",
416 - "createdAt" 1244 + "createdAt",
  1245 + "module",
  1246 + "type",
  1247 + "datasetId",
  1248 + "documentIds"
417 ] 1249 ]
418 }, 1250 },
419 "ChatSessionRecordsRequest": { 1251 "ChatSessionRecordsRequest": {
@@ -471,7 +1303,15 @@ @@ -471,7 +1303,15 @@
471 }, 1303 },
472 "ChatSessionSaveResponse": { 1304 "ChatSessionSaveResponse": {
473 "type": "object", 1305 "type": "object",
474 - "title": "ChatSessionSaveResponse" 1306 + "properties": {
  1307 + "session": {
  1308 + "$ref": "#/definitions/ChatSessionItem"
  1309 + }
  1310 + },
  1311 + "title": "ChatSessionSaveResponse",
  1312 + "required": [
  1313 + "session"
  1314 + ]
475 }, 1315 },
476 "ChatSessionSearchRequest": { 1316 "ChatSessionSearchRequest": {
477 "type": "object", 1317 "type": "object",
@@ -487,6 +1327,11 @@ @@ -487,6 +1327,11 @@
487 "title": { 1327 "title": {
488 "type": "string", 1328 "type": "string",
489 "description": " 按标题搜索" 1329 "description": " 按标题搜索"
  1330 + },
  1331 + "module": {
  1332 + "type": "integer",
  1333 + "format": "int32",
  1334 + "description": " 模块 1:openai chat 2:星火文档问答"
490 } 1335 }
491 }, 1336 },
492 "title": "ChatSessionSearchRequest" 1337 "title": "ChatSessionSearchRequest"
  1 +
  2 +syntax = "v1"
  3 +
  4 +info(
  5 + title: "xx实例"
  6 + desc: "xx实例"
  7 + author: "author"
  8 + email: "email"
  9 + version: "v1"
  10 +)
  11 +
  12 +@server(
  13 + prefix: v1
  14 + group: chat_dataset
  15 + jwt: JwtAuth
  16 +)
  17 +service Core {
  18 + @doc "详情"
  19 + @handler chat_datasetGet
  20 + get /chat_dataset/:id (ChatDatasetGetRequest) returns (ChatDatasetGetResponse)
  21 + @doc "保存"
  22 + @handler chat_datasetSave
  23 + post /chat_dataset (ChatDatasetSaveRequest) returns (ChatDatasetSaveResponse)
  24 + @doc "删除"
  25 + @handler chat_datasetDelete
  26 + delete /chat_dataset/:id (ChatDatasetDeleteRequest) returns (ChatDatasetDeleteResponse)
  27 + @doc "更新"
  28 + @handler chat_datasetUpdate
  29 + put /chat_dataset/:id (ChatDatasetUpdateRequest) returns (ChatDatasetUpdateResponse)
  30 + @doc "搜索"
  31 + @handler chat_datasetSearch
  32 + post /chat_dataset/search (ChatDatasetSearchRequest) returns (ChatDatasetSearchResponse)
  33 +}
  34 +
  35 +type (
  36 + ChatDatasetGetRequest {
  37 + Id int64 `path:"id"`
  38 + }
  39 + ChatDatasetGetResponse {
  40 + ChatDataset ChatDatasetItem `json:"chat_dataset"`
  41 + }
  42 +
  43 + ChatDatasetSaveRequest {
  44 + ChatDataset ChatDatasetItem `json:"chat_dataset"`
  45 + }
  46 + ChatDatasetSaveResponse {}
  47 +
  48 + ChatDatasetDeleteRequest {
  49 + Id int64 `path:"id"`
  50 + }
  51 + ChatDatasetDeleteResponse {}
  52 +
  53 + ChatDatasetUpdateRequest {
  54 + Id int64 `path:"id"`
  55 + ChatDataset ChatDatasetItem `json:"chat_dataset"`
  56 + }
  57 + ChatDatasetUpdateResponse {}
  58 +
  59 + ChatDatasetSearchRequest {
  60 + Page int `json:"page"`
  61 + Size int `json:"size"`
  62 + }
  63 + ChatDatasetSearchResponse{
  64 + List []ChatDatasetItem `json:"list"`
  65 + Total int64 `json:"total"`
  66 + }
  67 + ChatDatasetItem {
  68 +
  69 + }
  70 +)
  71 +
  72 +// logic CRUD
  73 +// Save
  74 + //var (
  75 + // conn = l.svcCtx.DefaultDBConn()
  76 + // dm *domain.ChatDataset
  77 + //)
  78 + //// 唯一判断
  79 +
  80 + //dm = NewDomainChatDataset(req.ChatDataset)
  81 + //if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
  82 + // dm, err = l.svcCtx.ChatDatasetRepository.Insert(l.ctx, conn, dm)
  83 + // return err
  84 + //}, true); err != nil {
  85 + // return nil, xerr.NewErrMsg("保存失败")
  86 + //}
  87 + ////resp = &types.ChatDatasetSaveResponse{}
  88 + //return
  89 +
  90 +//func NewDomainChatDataset(item types.ChatDatasetItem) *domain.ChatDataset {
  91 +// return &domain.ChatDataset{
  92 +
  93 +// }
  94 +//}
  95 +//
  96 +//func NewTypesChatDataset(item *domain.ChatDataset) types.ChatDatasetItem {
  97 +// return types.ChatDatasetItem{
  98 +// Id: item.Id,
  99 +// }
  100 +//}
  101 +
  102 +// Get
  103 + //var (
  104 + // conn = l.svcCtx.DefaultDBConn()
  105 + // dm *domain.ChatDataset
  106 + //)
  107 + //// 货号唯一
  108 + //if dm, err = l.svcCtx.ChatDatasetRepository.FindOne(l.ctx, conn, req.Id); err != nil {
  109 + // return nil, xerr.NewErrMsgErr("不存在", err)
  110 + //}
  111 + //resp = &types.ChatDatasetGetResponse{
  112 + // ChatDataset: NewTypesChatDataset(dm),
  113 + //}
  114 + //return
  115 +
  116 +// Delete
  117 + //var (
  118 + // conn = l.svcCtx.DefaultDBConn()
  119 + // dm *domain.ChatDataset
  120 + //)
  121 + //if dm, err = l.svcCtx.ChatDatasetRepository.FindOne(l.ctx, conn, req.Id); err != nil {
  122 + // return nil, xerr.NewErrMsgErr("不存在", err)
  123 + //}
  124 + //if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
  125 + // if dm, err = l.svcCtx.ChatDatasetRepository.Delete(l.ctx, conn, dm); err != nil {
  126 + // return err
  127 + // }
  128 + // return nil
  129 + //}, true); err != nil {
  130 + // return nil, xerr.NewErrMsgErr("移除失败", err)
  131 + //}
  132 + //return
  133 +
  134 +// Search
  135 + //var (
  136 + // conn = l.svcCtx.DefaultDBConn()
  137 + // dms []*domain.ChatDataset
  138 + // total int64
  139 + //)
  140 + //
  141 + //queryOptions := domain.NewQueryOptions().WithOffsetLimit(req.Page, req.Size).
  142 + // WithKV("", "")
  143 +
  144 + //total, dms, err = l.svcCtx.ChatDatasetRepository.Find(l.ctx, conn, queryOptions)
  145 + //list := make([]types.ChatDatasetItem, 0)
  146 + //for i := range dms {
  147 + // list = append(list, NewTypesChatDataset(dms[i]))
  148 + //}
  149 + //resp = &types.ChatDatasetSearchResponse{
  150 + // List: list,
  151 + // Total: total,
  152 + //}
  153 + //return
  154 +
  155 +// Update
  156 + //var (
  157 + // conn = l.svcCtx.DefaultDBConn()
  158 + // dm *domain.ChatDataset
  159 + //)
  160 + //if dm, err = l.svcCtx.ChatDatasetRepository.FindOne(l.ctx, conn, req.Id); err != nil {
  161 + // return nil, xerr.NewErrMsgErr("不存在", err)
  162 + //}
  163 + //// 不可编辑判断
  164 +
  165 + //// 赋值
  166 +
  167 + //// 更新
  168 + //if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
  169 + // dm, err = l.svcCtx.ChatDatasetRepository.UpdateWithVersion(l.ctx, conn, dm)
  170 + // return err
  171 + //}, true); err != nil {
  172 + // return nil, xerr.NewErrMsg("更新失败")
  173 + //}
  174 + //resp = &types.ChatDatasetUpdateResponse{}
  175 + //return
  1 +
  2 +syntax = "v1"
  3 +
  4 +info(
  5 + title: "xx实例"
  6 + desc: "xx实例"
  7 + author: "author"
  8 + email: "email"
  9 + version: "v1"
  10 +)
  11 +
  12 +@server(
  13 + prefix: v1
  14 + group: chat_dataset_document_mapping
  15 + jwt: JwtAuth
  16 +)
  17 +service Core {
  18 + @doc "详情"
  19 + @handler chat_dataset_document_mappingGet
  20 + get /chat_dataset_document_mapping/:id (ChatDatasetDocumentMappingGetRequest) returns (ChatDatasetDocumentMappingGetResponse)
  21 + @doc "保存"
  22 + @handler chat_dataset_document_mappingSave
  23 + post /chat_dataset_document_mapping (ChatDatasetDocumentMappingSaveRequest) returns (ChatDatasetDocumentMappingSaveResponse)
  24 + @doc "删除"
  25 + @handler chat_dataset_document_mappingDelete
  26 + delete /chat_dataset_document_mapping/:id (ChatDatasetDocumentMappingDeleteRequest) returns (ChatDatasetDocumentMappingDeleteResponse)
  27 + @doc "更新"
  28 + @handler chat_dataset_document_mappingUpdate
  29 + put /chat_dataset_document_mapping/:id (ChatDatasetDocumentMappingUpdateRequest) returns (ChatDatasetDocumentMappingUpdateResponse)
  30 + @doc "搜索"
  31 + @handler chat_dataset_document_mappingSearch
  32 + post /chat_dataset_document_mapping/search (ChatDatasetDocumentMappingSearchRequest) returns (ChatDatasetDocumentMappingSearchResponse)
  33 +}
  34 +
  35 +type (
  36 + ChatDatasetDocumentMappingGetRequest {
  37 + Id int64 `path:"id"`
  38 + }
  39 + ChatDatasetDocumentMappingGetResponse {
  40 + ChatDatasetDocumentMapping ChatDatasetDocumentMappingItem `json:"chat_dataset_document_mapping"`
  41 + }
  42 +
  43 + ChatDatasetDocumentMappingSaveRequest {
  44 + ChatDatasetDocumentMapping ChatDatasetDocumentMappingItem `json:"chat_dataset_document_mapping"`
  45 + }
  46 + ChatDatasetDocumentMappingSaveResponse {}
  47 +
  48 + ChatDatasetDocumentMappingDeleteRequest {
  49 + Id int64 `path:"id"`
  50 + }
  51 + ChatDatasetDocumentMappingDeleteResponse {}
  52 +
  53 + ChatDatasetDocumentMappingUpdateRequest {
  54 + Id int64 `path:"id"`
  55 + ChatDatasetDocumentMapping ChatDatasetDocumentMappingItem `json:"chat_dataset_document_mapping"`
  56 + }
  57 + ChatDatasetDocumentMappingUpdateResponse {}
  58 +
  59 + ChatDatasetDocumentMappingSearchRequest {
  60 + Page int `json:"page"`
  61 + Size int `json:"size"`
  62 + }
  63 + ChatDatasetDocumentMappingSearchResponse{
  64 + List []ChatDatasetDocumentMappingItem `json:"list"`
  65 + Total int64 `json:"total"`
  66 + }
  67 + ChatDatasetDocumentMappingItem {
  68 +
  69 + }
  70 +)
  71 +
  72 +// logic CRUD
  73 +// Save
  74 + //var (
  75 + // conn = l.svcCtx.DefaultDBConn()
  76 + // dm *domain.ChatDatasetDocumentMapping
  77 + //)
  78 + //// 唯一判断
  79 +
  80 + //dm = NewDomainChatDatasetDocumentMapping(req.ChatDatasetDocumentMapping)
  81 + //if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
  82 + // dm, err = l.svcCtx.ChatDatasetDocumentMappingRepository.Insert(l.ctx, conn, dm)
  83 + // return err
  84 + //}, true); err != nil {
  85 + // return nil, xerr.NewErrMsg("保存失败")
  86 + //}
  87 + ////resp = &types.ChatDatasetDocumentMappingSaveResponse{}
  88 + //return
  89 +
  90 +//func NewDomainChatDatasetDocumentMapping(item types.ChatDatasetDocumentMappingItem) *domain.ChatDatasetDocumentMapping {
  91 +// return &domain.ChatDatasetDocumentMapping{
  92 +
  93 +// }
  94 +//}
  95 +//
  96 +//func NewTypesChatDatasetDocumentMapping(item *domain.ChatDatasetDocumentMapping) types.ChatDatasetDocumentMappingItem {
  97 +// return types.ChatDatasetDocumentMappingItem{
  98 +// Id: item.Id,
  99 +// }
  100 +//}
  101 +
  102 +// Get
  103 + //var (
  104 + // conn = l.svcCtx.DefaultDBConn()
  105 + // dm *domain.ChatDatasetDocumentMapping
  106 + //)
  107 + //// 货号唯一
  108 + //if dm, err = l.svcCtx.ChatDatasetDocumentMappingRepository.FindOne(l.ctx, conn, req.Id); err != nil {
  109 + // return nil, xerr.NewErrMsgErr("不存在", err)
  110 + //}
  111 + //resp = &types.ChatDatasetDocumentMappingGetResponse{
  112 + // ChatDatasetDocumentMapping: NewTypesChatDatasetDocumentMapping(dm),
  113 + //}
  114 + //return
  115 +
  116 +// Delete
  117 + //var (
  118 + // conn = l.svcCtx.DefaultDBConn()
  119 + // dm *domain.ChatDatasetDocumentMapping
  120 + //)
  121 + //if dm, err = l.svcCtx.ChatDatasetDocumentMappingRepository.FindOne(l.ctx, conn, req.Id); err != nil {
  122 + // return nil, xerr.NewErrMsgErr("不存在", err)
  123 + //}
  124 + //if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
  125 + // if dm, err = l.svcCtx.ChatDatasetDocumentMappingRepository.Delete(l.ctx, conn, dm); err != nil {
  126 + // return err
  127 + // }
  128 + // return nil
  129 + //}, true); err != nil {
  130 + // return nil, xerr.NewErrMsgErr("移除失败", err)
  131 + //}
  132 + //return
  133 +
  134 +// Search
  135 + //var (
  136 + // conn = l.svcCtx.DefaultDBConn()
  137 + // dms []*domain.ChatDatasetDocumentMapping
  138 + // total int64
  139 + //)
  140 + //
  141 + //queryOptions := domain.NewQueryOptions().WithOffsetLimit(req.Page, req.Size).
  142 + // WithKV("", "")
  143 +
  144 + //total, dms, err = l.svcCtx.ChatDatasetDocumentMappingRepository.Find(l.ctx, conn, queryOptions)
  145 + //list := make([]types.ChatDatasetDocumentMappingItem, 0)
  146 + //for i := range dms {
  147 + // list = append(list, NewTypesChatDatasetDocumentMapping(dms[i]))
  148 + //}
  149 + //resp = &types.ChatDatasetDocumentMappingSearchResponse{
  150 + // List: list,
  151 + // Total: total,
  152 + //}
  153 + //return
  154 +
  155 +// Update
  156 + //var (
  157 + // conn = l.svcCtx.DefaultDBConn()
  158 + // dm *domain.ChatDatasetDocumentMapping
  159 + //)
  160 + //if dm, err = l.svcCtx.ChatDatasetDocumentMappingRepository.FindOne(l.ctx, conn, req.Id); err != nil {
  161 + // return nil, xerr.NewErrMsgErr("不存在", err)
  162 + //}
  163 + //// 不可编辑判断
  164 +
  165 + //// 赋值
  166 +
  167 + //// 更新
  168 + //if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
  169 + // dm, err = l.svcCtx.ChatDatasetDocumentMappingRepository.UpdateWithVersion(l.ctx, conn, dm)
  170 + // return err
  171 + //}, true); err != nil {
  172 + // return nil, xerr.NewErrMsg("更新失败")
  173 + //}
  174 + //resp = &types.ChatDatasetDocumentMappingUpdateResponse{}
  175 + //return
  1 +
  2 +syntax = "v1"
  3 +
  4 +info(
  5 + title: "xx实例"
  6 + desc: "xx实例"
  7 + author: "author"
  8 + email: "email"
  9 + version: "v1"
  10 +)
  11 +
  12 +@server(
  13 + prefix: v1
  14 + group: chat_document
  15 + jwt: JwtAuth
  16 +)
  17 +service Core {
  18 + @doc "详情"
  19 + @handler chat_documentGet
  20 + get /chat_document/:id (ChatDocumentGetRequest) returns (ChatDocumentGetResponse)
  21 + @doc "保存"
  22 + @handler chat_documentSave
  23 + post /chat_document (ChatDocumentSaveRequest) returns (ChatDocumentSaveResponse)
  24 + @doc "删除"
  25 + @handler chat_documentDelete
  26 + delete /chat_document/:id (ChatDocumentDeleteRequest) returns (ChatDocumentDeleteResponse)
  27 + @doc "更新"
  28 + @handler chat_documentUpdate
  29 + put /chat_document/:id (ChatDocumentUpdateRequest) returns (ChatDocumentUpdateResponse)
  30 + @doc "搜索"
  31 + @handler chat_documentSearch
  32 + post /chat_document/search (ChatDocumentSearchRequest) returns (ChatDocumentSearchResponse)
  33 +}
  34 +
  35 +type (
  36 + ChatDocumentGetRequest {
  37 + Id int64 `path:"id"`
  38 + }
  39 + ChatDocumentGetResponse {
  40 + ChatDocument ChatDocumentItem `json:"chat_document"`
  41 + }
  42 +
  43 + ChatDocumentSaveRequest {
  44 + ChatDocument ChatDocumentItem `json:"chat_document"`
  45 + }
  46 + ChatDocumentSaveResponse {}
  47 +
  48 + ChatDocumentDeleteRequest {
  49 + Id int64 `path:"id"`
  50 + }
  51 + ChatDocumentDeleteResponse {}
  52 +
  53 + ChatDocumentUpdateRequest {
  54 + Id int64 `path:"id"`
  55 + ChatDocument ChatDocumentItem `json:"chat_document"`
  56 + }
  57 + ChatDocumentUpdateResponse {}
  58 +
  59 + ChatDocumentSearchRequest {
  60 + Page int `json:"page"`
  61 + Size int `json:"size"`
  62 + }
  63 + ChatDocumentSearchResponse{
  64 + List []ChatDocumentItem `json:"list"`
  65 + Total int64 `json:"total"`
  66 + }
  67 + ChatDocumentItem {
  68 +
  69 + }
  70 +)
  71 +
  72 +// logic CRUD
  73 +// Save
  74 + //var (
  75 + // conn = l.svcCtx.DefaultDBConn()
  76 + // dm *domain.ChatDocument
  77 + //)
  78 + //// 唯一判断
  79 +
  80 + //dm = NewDomainChatDocument(req.ChatDocument)
  81 + //if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
  82 + // dm, err = l.svcCtx.ChatDocumentRepository.Insert(l.ctx, conn, dm)
  83 + // return err
  84 + //}, true); err != nil {
  85 + // return nil, xerr.NewErrMsg("保存失败")
  86 + //}
  87 + ////resp = &types.ChatDocumentSaveResponse{}
  88 + //return
  89 +
  90 +//func NewDomainChatDocument(item types.ChatDocumentItem) *domain.ChatDocument {
  91 +// return &domain.ChatDocument{
  92 +
  93 +// }
  94 +//}
  95 +//
  96 +//func NewTypesChatDocument(item *domain.ChatDocument) types.ChatDocumentItem {
  97 +// return types.ChatDocumentItem{
  98 +// Id: item.Id,
  99 +// }
  100 +//}
  101 +
  102 +// Get
  103 + //var (
  104 + // conn = l.svcCtx.DefaultDBConn()
  105 + // dm *domain.ChatDocument
  106 + //)
  107 + //// 货号唯一
  108 + //if dm, err = l.svcCtx.ChatDocumentRepository.FindOne(l.ctx, conn, req.Id); err != nil {
  109 + // return nil, xerr.NewErrMsgErr("不存在", err)
  110 + //}
  111 + //resp = &types.ChatDocumentGetResponse{
  112 + // ChatDocument: NewTypesChatDocument(dm),
  113 + //}
  114 + //return
  115 +
  116 +// Delete
  117 + //var (
  118 + // conn = l.svcCtx.DefaultDBConn()
  119 + // dm *domain.ChatDocument
  120 + //)
  121 + //if dm, err = l.svcCtx.ChatDocumentRepository.FindOne(l.ctx, conn, req.Id); err != nil {
  122 + // return nil, xerr.NewErrMsgErr("不存在", err)
  123 + //}
  124 + //if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
  125 + // if dm, err = l.svcCtx.ChatDocumentRepository.Delete(l.ctx, conn, dm); err != nil {
  126 + // return err
  127 + // }
  128 + // return nil
  129 + //}, true); err != nil {
  130 + // return nil, xerr.NewErrMsgErr("移除失败", err)
  131 + //}
  132 + //return
  133 +
  134 +// Search
  135 + //var (
  136 + // conn = l.svcCtx.DefaultDBConn()
  137 + // dms []*domain.ChatDocument
  138 + // total int64
  139 + //)
  140 + //
  141 + //queryOptions := domain.NewQueryOptions().WithOffsetLimit(req.Page, req.Size).
  142 + // WithKV("", "")
  143 +
  144 + //total, dms, err = l.svcCtx.ChatDocumentRepository.Find(l.ctx, conn, queryOptions)
  145 + //list := make([]types.ChatDocumentItem, 0)
  146 + //for i := range dms {
  147 + // list = append(list, NewTypesChatDocument(dms[i]))
  148 + //}
  149 + //resp = &types.ChatDocumentSearchResponse{
  150 + // List: list,
  151 + // Total: total,
  152 + //}
  153 + //return
  154 +
  155 +// Update
  156 + //var (
  157 + // conn = l.svcCtx.DefaultDBConn()
  158 + // dm *domain.ChatDocument
  159 + //)
  160 + //if dm, err = l.svcCtx.ChatDocumentRepository.FindOne(l.ctx, conn, req.Id); err != nil {
  161 + // return nil, xerr.NewErrMsgErr("不存在", err)
  162 + //}
  163 + //// 不可编辑判断
  164 +
  165 + //// 赋值
  166 +
  167 + //// 更新
  168 + //if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
  169 + // dm, err = l.svcCtx.ChatDocumentRepository.UpdateWithVersion(l.ctx, conn, dm)
  170 + // return err
  171 + //}, true); err != nil {
  172 + // return nil, xerr.NewErrMsg("更新失败")
  173 + //}
  174 + //resp = &types.ChatDocumentUpdateResponse{}
  175 + //return
  1 +
  2 +syntax = "proto3";
  3 +
  4 +option go_package ="./pb";
  5 +
  6 +package pb;
  7 +
  8 +message ChatDatasetGetReq {
  9 + int64 Id = 1;
  10 +}
  11 +message ChatDatasetGetResp{
  12 + ChatDatasetItem ChatDataset = 1;
  13 +}
  14 +
  15 +message ChatDatasetSaveReq {
  16 +
  17 +}
  18 +message ChatDatasetSaveResp{
  19 +
  20 +}
  21 +
  22 +message ChatDatasetDeleteReq {
  23 + int64 Id = 1;
  24 +}
  25 +message ChatDatasetDeleteResp{
  26 +
  27 +}
  28 +
  29 +message ChatDatasetUpdateReq {
  30 + int64 Id = 1;
  31 +}
  32 +message ChatDatasetUpdateResp{
  33 +
  34 +}
  35 +
  36 +message ChatDatasetSearchReq {
  37 + int64 PageNumber = 1;
  38 + int64 PageSize = 2;
  39 +}
  40 +message ChatDatasetSearchResp{
  41 + repeated ChatDatasetItem List =1;
  42 + int64 Total =2;
  43 +}
  44 +message ChatDatasetItem {
  45 +
  46 +}
  47 +
  48 +service ChatDatasetService {
  49 + rpc ChatDatasetGet(ChatDatasetGetReq) returns(ChatDatasetGetResp);
  50 + rpc ChatDatasetSave(ChatDatasetSaveReq) returns(ChatDatasetSaveResp);
  51 + rpc ChatDatasetDelete(ChatDatasetDeleteReq) returns(ChatDatasetDeleteResp);
  52 + rpc ChatDatasetUpdate(ChatDatasetUpdateReq) returns(ChatDatasetUpdateResp);
  53 + rpc ChatDatasetSearch(ChatDatasetSearchReq) returns(ChatDatasetSearchResp);
  54 +}
  1 +
  2 +syntax = "proto3";
  3 +
  4 +option go_package ="./pb";
  5 +
  6 +package pb;
  7 +
  8 +message ChatDatasetDocumentMappingGetReq {
  9 + int64 Id = 1;
  10 +}
  11 +message ChatDatasetDocumentMappingGetResp{
  12 + ChatDatasetDocumentMappingItem ChatDatasetDocumentMapping = 1;
  13 +}
  14 +
  15 +message ChatDatasetDocumentMappingSaveReq {
  16 +
  17 +}
  18 +message ChatDatasetDocumentMappingSaveResp{
  19 +
  20 +}
  21 +
  22 +message ChatDatasetDocumentMappingDeleteReq {
  23 + int64 Id = 1;
  24 +}
  25 +message ChatDatasetDocumentMappingDeleteResp{
  26 +
  27 +}
  28 +
  29 +message ChatDatasetDocumentMappingUpdateReq {
  30 + int64 Id = 1;
  31 +}
  32 +message ChatDatasetDocumentMappingUpdateResp{
  33 +
  34 +}
  35 +
  36 +message ChatDatasetDocumentMappingSearchReq {
  37 + int64 PageNumber = 1;
  38 + int64 PageSize = 2;
  39 +}
  40 +message ChatDatasetDocumentMappingSearchResp{
  41 + repeated ChatDatasetDocumentMappingItem List =1;
  42 + int64 Total =2;
  43 +}
  44 +message ChatDatasetDocumentMappingItem {
  45 +
  46 +}
  47 +
  48 +service ChatDatasetDocumentMappingService {
  49 + rpc ChatDatasetDocumentMappingGet(ChatDatasetDocumentMappingGetReq) returns(ChatDatasetDocumentMappingGetResp);
  50 + rpc ChatDatasetDocumentMappingSave(ChatDatasetDocumentMappingSaveReq) returns(ChatDatasetDocumentMappingSaveResp);
  51 + rpc ChatDatasetDocumentMappingDelete(ChatDatasetDocumentMappingDeleteReq) returns(ChatDatasetDocumentMappingDeleteResp);
  52 + rpc ChatDatasetDocumentMappingUpdate(ChatDatasetDocumentMappingUpdateReq) returns(ChatDatasetDocumentMappingUpdateResp);
  53 + rpc ChatDatasetDocumentMappingSearch(ChatDatasetDocumentMappingSearchReq) returns(ChatDatasetDocumentMappingSearchResp);
  54 +}
  1 +
  2 +syntax = "proto3";
  3 +
  4 +option go_package ="./pb";
  5 +
  6 +package pb;
  7 +
  8 +message ChatDocumentGetReq {
  9 + int64 Id = 1;
  10 +}
  11 +message ChatDocumentGetResp{
  12 + ChatDocumentItem ChatDocument = 1;
  13 +}
  14 +
  15 +message ChatDocumentSaveReq {
  16 +
  17 +}
  18 +message ChatDocumentSaveResp{
  19 +
  20 +}
  21 +
  22 +message ChatDocumentDeleteReq {
  23 + int64 Id = 1;
  24 +}
  25 +message ChatDocumentDeleteResp{
  26 +
  27 +}
  28 +
  29 +message ChatDocumentUpdateReq {
  30 + int64 Id = 1;
  31 +}
  32 +message ChatDocumentUpdateResp{
  33 +
  34 +}
  35 +
  36 +message ChatDocumentSearchReq {
  37 + int64 PageNumber = 1;
  38 + int64 PageSize = 2;
  39 +}
  40 +message ChatDocumentSearchResp{
  41 + repeated ChatDocumentItem List =1;
  42 + int64 Total =2;
  43 +}
  44 +message ChatDocumentItem {
  45 +
  46 +}
  47 +
  48 +service ChatDocumentService {
  49 + rpc ChatDocumentGet(ChatDocumentGetReq) returns(ChatDocumentGetResp);
  50 + rpc ChatDocumentSave(ChatDocumentSaveReq) returns(ChatDocumentSaveResp);
  51 + rpc ChatDocumentDelete(ChatDocumentDeleteReq) returns(ChatDocumentDeleteResp);
  52 + rpc ChatDocumentUpdate(ChatDocumentUpdateReq) returns(ChatDocumentUpdateResp);
  53 + rpc ChatDocumentSearch(ChatDocumentSearchReq) returns(ChatDocumentSearchResp);
  54 +}
@@ -9,5 +9,8 @@ func Migrate(db *gorm.DB) { @@ -9,5 +9,8 @@ func Migrate(db *gorm.DB) {
9 db.AutoMigrate( 9 db.AutoMigrate(
10 &models.ChatSession{}, 10 &models.ChatSession{},
11 &models.ChatSessionRecord{}, 11 &models.ChatSessionRecord{},
  12 + &models.ChatDocument{},
  13 + &models.ChatDatasetDocumentMapping{},
  14 + &models.ChatDataset{},
12 ) 15 )
13 } 16 }
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  6 + "gorm.io/gorm"
  7 + "gorm.io/plugin/soft_delete"
  8 +)
  9 +
  10 +type ChatDataset struct {
  11 + Id int64 // 唯一标识
  12 + CompanyId int64
  13 + UserId int64
  14 + Name string // 知识库名称
  15 + Desc string // 描述
  16 + Status int // 1:启用 2:禁用
  17 + CreatedAt int64
  18 + UpdatedAt int64
  19 + DeletedAt int64
  20 + Version int
  21 + IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"`
  22 +}
  23 +
  24 +func (m *ChatDataset) TableName() string {
  25 + return "chat.dataset"
  26 +}
  27 +
  28 +func (m *ChatDataset) BeforeCreate(tx *gorm.DB) (err error) {
  29 + // m.CreatedAt = time.Now().Unix()
  30 + // m.UpdatedAt = time.Now().Unix()
  31 + return
  32 +}
  33 +
  34 +func (m *ChatDataset) BeforeUpdate(tx *gorm.DB) (err error) {
  35 + // m.UpdatedAt = time.Now().Unix()
  36 + return
  37 +}
  38 +
  39 +func (m *ChatDataset) CacheKeyFunc() string {
  40 + if m.Id == 0 {
  41 + return ""
  42 + }
  43 + return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
  44 +}
  45 +
  46 +func (m *ChatDataset) CacheKeyFuncByObject(obj interface{}) string {
  47 + if v, ok := obj.(*ChatDataset); ok {
  48 + return v.CacheKeyFunc()
  49 + }
  50 + return ""
  51 +}
  52 +
  53 +func (m *ChatDataset) CachePrimaryKeyFunc() string {
  54 + if len("") == 0 {
  55 + return ""
  56 + }
  57 + return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
  58 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  6 + "gorm.io/gorm"
  7 +)
  8 +
  9 +type ChatDatasetDocumentMapping struct {
  10 + Id int64 // 唯一标识
  11 + DatasetId int64
  12 + DocumentId int64
  13 + CreatedAt int64
  14 + UpdatedAt int64
  15 + Version int
  16 +}
  17 +
  18 +func (m *ChatDatasetDocumentMapping) TableName() string {
  19 + return "chat.dataset_document_mapping"
  20 +}
  21 +
  22 +func (m *ChatDatasetDocumentMapping) BeforeCreate(tx *gorm.DB) (err error) {
  23 + // m.CreatedAt = time.Now().Unix()
  24 + // m.UpdatedAt = time.Now().Unix()
  25 + return
  26 +}
  27 +
  28 +func (m *ChatDatasetDocumentMapping) BeforeUpdate(tx *gorm.DB) (err error) {
  29 + // m.UpdatedAt = time.Now().Unix()
  30 + return
  31 +}
  32 +
  33 +func (m *ChatDatasetDocumentMapping) CacheKeyFunc() string {
  34 + if m.Id == 0 {
  35 + return ""
  36 + }
  37 + return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
  38 +}
  39 +
  40 +func (m *ChatDatasetDocumentMapping) CacheKeyFuncByObject(obj interface{}) string {
  41 + if v, ok := obj.(*ChatDatasetDocumentMapping); ok {
  42 + return v.CacheKeyFunc()
  43 + }
  44 + return ""
  45 +}
  46 +
  47 +func (m *ChatDatasetDocumentMapping) CachePrimaryKeyFunc() string {
  48 + if len("") == 0 {
  49 + return ""
  50 + }
  51 + return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
  52 +}
  1 +package models
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  6 + "gorm.io/gorm"
  7 + "gorm.io/plugin/soft_delete"
  8 +)
  9 +
  10 +type ChatDocument struct {
  11 + Id int64 // 唯一标识
  12 + Name string
  13 + FileType string
  14 + Status int
  15 + Metadata domain.DocumentMetadata `gorm:"type:jsonb;serializer:json"`
  16 + CreatedAt int64
  17 + UpdatedAt int64
  18 + DeletedAt int64
  19 + Version int
  20 + IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"`
  21 +}
  22 +
  23 +func (m *ChatDocument) TableName() string {
  24 + return "chat.document"
  25 +}
  26 +
  27 +func (m *ChatDocument) BeforeCreate(tx *gorm.DB) (err error) {
  28 + // m.CreatedAt = time.Now().Unix()
  29 + // m.UpdatedAt = time.Now().Unix()
  30 + return
  31 +}
  32 +
  33 +func (m *ChatDocument) BeforeUpdate(tx *gorm.DB) (err error) {
  34 + // m.UpdatedAt = time.Now().Unix()
  35 + return
  36 +}
  37 +
  38 +func (m *ChatDocument) CacheKeyFunc() string {
  39 + if m.Id == 0 {
  40 + return ""
  41 + }
  42 + return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
  43 +}
  44 +
  45 +func (m *ChatDocument) CacheKeyFuncByObject(obj interface{}) string {
  46 + if v, ok := obj.(*ChatDocument); ok {
  47 + return v.CacheKeyFunc()
  48 + }
  49 + return ""
  50 +}
  51 +
  52 +func (m *ChatDocument) CachePrimaryKeyFunc() string {
  53 + if len("") == 0 {
  54 + return ""
  55 + }
  56 + return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
  57 +}
@@ -8,11 +8,15 @@ import ( @@ -8,11 +8,15 @@ import (
8 ) 8 )
9 9
10 type ChatSession struct { 10 type ChatSession struct {
11 - Id int64 // 唯一标识  
12 - CompanyId int64 // 公司ID  
13 - UserId int64 // 用户ID  
14 - Title string // 会话标题  
15 - Abstract string // 摘要 11 + Id int64 // 唯一标识
  12 + CompanyId int64 // 公司ID
  13 + UserId int64 // 用户ID
  14 + Title string // 会话标题
  15 + Abstract string // 摘要
  16 + Module int // 1:openai chat 2:星火文档问答
  17 + Type string // 类型 chat:普通问答 spark_dataset_chat:星火知识库问答 spark_documents_chat:星火多文档问答
  18 + Metadata *domain.SessionMetadata `gorm:"type:jsonb;serializer:json"` // 元数据
  19 + Rank int64 // 排序(时间秒,置顶更新当前会话)
16 CreatedAt int64 20 CreatedAt int64
17 UpdatedAt int64 21 UpdatedAt int64
18 DeletedAt int64 22 DeletedAt int64
  1 +package repository
  2 +
  3 +import (
  4 + "context"
  5 + "github.com/jinzhu/copier"
  6 + "github.com/pkg/errors"
  7 + "github.com/tiptok/gocomm/pkg/cache"
  8 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/db/models"
  9 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  10 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
  11 + "gorm.io/gorm"
  12 +)
  13 +
  14 +type ChatDatasetDocumentMappingRepository struct {
  15 + *cache.CachedRepository
  16 +}
  17 +
  18 +func (repository *ChatDatasetDocumentMappingRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ChatDatasetDocumentMapping) (*domain.ChatDatasetDocumentMapping, error) {
  19 + var (
  20 + err error
  21 + m = &models.ChatDatasetDocumentMapping{}
  22 + tx = conn.DB()
  23 + )
  24 + if m, err = repository.DomainModelToModel(dm); err != nil {
  25 + return nil, err
  26 + }
  27 + if tx = tx.Model(m).Save(m); tx.Error != nil {
  28 + return nil, tx.Error
  29 + }
  30 + dm.Id = m.Id
  31 + return repository.ModelToDomainModel(m)
  32 +
  33 +}
  34 +
  35 +func (repository *ChatDatasetDocumentMappingRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ChatDatasetDocumentMapping) (*domain.ChatDatasetDocumentMapping, error) {
  36 + var (
  37 + err error
  38 + m *models.ChatDatasetDocumentMapping
  39 + tx = conn.DB()
  40 + )
  41 + if m, err = repository.DomainModelToModel(dm); err != nil {
  42 + return nil, err
  43 + }
  44 + queryFunc := func() (interface{}, error) {
  45 + tx = tx.Model(m).Updates(m)
  46 + return nil, tx.Error
  47 + }
  48 + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  49 + return nil, err
  50 + }
  51 + return repository.ModelToDomainModel(m)
  52 +}
  53 +
  54 +func (repository *ChatDatasetDocumentMappingRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ChatDatasetDocumentMapping) (*domain.ChatDatasetDocumentMapping, error) {
  55 + var (
  56 + err error
  57 + m *models.ChatDatasetDocumentMapping
  58 + tx = transaction.DB()
  59 + )
  60 + if m, err = repository.DomainModelToModel(dm); err != nil {
  61 + return nil, err
  62 + }
  63 + oldVersion := dm.Version
  64 + m.Version += 1
  65 + queryFunc := func() (interface{}, error) {
  66 + tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
  67 + if tx.RowsAffected == 0 {
  68 + return nil, domain.ErrUpdateFail
  69 + }
  70 + return nil, tx.Error
  71 + }
  72 + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  73 + return nil, err
  74 + }
  75 + return repository.ModelToDomainModel(m)
  76 +}
  77 +
  78 +func (repository *ChatDatasetDocumentMappingRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ChatDatasetDocumentMapping) (*domain.ChatDatasetDocumentMapping, error) {
  79 + var (
  80 + tx = conn.DB()
  81 + m = &models.ChatDatasetDocumentMapping{Id: dm.Id}
  82 + )
  83 + queryFunc := func() (interface{}, error) {
  84 + tx = tx.Where("id = ?", m.Id).Delete(m)
  85 + return m, tx.Error
  86 + }
  87 + if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  88 + return dm, err
  89 + }
  90 + return repository.ModelToDomainModel(m)
  91 +}
  92 +
  93 +func (repository *ChatDatasetDocumentMappingRepository) DeleteByDataset(ctx context.Context, conn transaction.Conn, datasetId int64) error {
  94 + var (
  95 + tx = conn.DB()
  96 + m = &models.ChatDatasetDocumentMapping{}
  97 + )
  98 + queryFunc := func() (interface{}, error) {
  99 + tx = tx.Where("dataset_id = ?", datasetId).Delete(m)
  100 + return m, tx.Error
  101 + }
  102 + if _, err := repository.Query(queryFunc); err != nil {
  103 + return err
  104 + }
  105 + return nil
  106 +}
  107 +
  108 +func (repository *ChatDatasetDocumentMappingRepository) DeleteByDocument(ctx context.Context, conn transaction.Conn, documentId int64) error {
  109 + var (
  110 + tx = conn.DB()
  111 + m = &models.ChatDatasetDocumentMapping{}
  112 + )
  113 + queryFunc := func() (interface{}, error) {
  114 + tx = tx.Where("document_id = ?", documentId).Delete(m)
  115 + return m, tx.Error
  116 + }
  117 + if _, err := repository.Query(queryFunc); err != nil {
  118 + return err
  119 + }
  120 + return nil
  121 +}
  122 +
  123 +func (repository *ChatDatasetDocumentMappingRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ChatDatasetDocumentMapping, error) {
  124 + var (
  125 + err error
  126 + tx = conn.DB()
  127 + m = new(models.ChatDatasetDocumentMapping)
  128 + )
  129 + queryFunc := func() (interface{}, error) {
  130 + tx = tx.Model(m).Where("id = ?", id).First(m)
  131 + if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
  132 + return nil, domain.ErrNotFound
  133 + }
  134 + return m, tx.Error
  135 + }
  136 + cacheModel := new(models.ChatDatasetDocumentMapping)
  137 + cacheModel.Id = id
  138 + if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
  139 + return nil, err
  140 + }
  141 + return repository.ModelToDomainModel(m)
  142 +}
  143 +
  144 +func (repository *ChatDatasetDocumentMappingRepository) FindOneUnscoped(ctx context.Context, conn transaction.Conn, id int64) (*domain.ChatDatasetDocumentMapping, error) {
  145 + var (
  146 + err error
  147 + tx = conn.DB()
  148 + m = new(models.ChatDatasetDocumentMapping)
  149 + )
  150 + queryFunc := func() (interface{}, error) {
  151 + tx = tx.Model(m).Unscoped().Where("id = ?", id).First(m)
  152 + if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
  153 + return nil, domain.ErrNotFound
  154 + }
  155 + return m, tx.Error
  156 + }
  157 + cacheModel := new(models.ChatDatasetDocumentMapping)
  158 + cacheModel.Id = id
  159 + if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
  160 + return nil, err
  161 + }
  162 + return repository.ModelToDomainModel(m)
  163 +}
  164 +
  165 +func (repository *ChatDatasetDocumentMappingRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ChatDatasetDocumentMapping, error) {
  166 + var (
  167 + tx = conn.DB()
  168 + ms []*models.ChatDatasetDocumentMapping
  169 + dms = make([]*domain.ChatDatasetDocumentMapping, 0)
  170 + total int64
  171 + )
  172 + queryFunc := func() (interface{}, error) {
  173 + tx = tx.Model(&ms).Order("id desc")
  174 + if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
  175 + return dms, tx.Error
  176 + }
  177 + return dms, nil
  178 + }
  179 +
  180 + if _, err := repository.Query(queryFunc); err != nil {
  181 + return 0, nil, err
  182 + }
  183 +
  184 + for _, item := range ms {
  185 + if dm, err := repository.ModelToDomainModel(item); err != nil {
  186 + return 0, dms, err
  187 + } else {
  188 + dms = append(dms, dm)
  189 + }
  190 + }
  191 + return total, dms, nil
  192 +}
  193 +
  194 +func (repository *ChatDatasetDocumentMappingRepository) FindByDataset(ctx context.Context, conn transaction.Conn, datasetIds ...int64) (int64, []*domain.ChatDatasetDocumentMapping, error) {
  195 + var (
  196 + tx = conn.DB()
  197 + ms []*models.ChatDatasetDocumentMapping
  198 + dms = make([]*domain.ChatDatasetDocumentMapping, 0)
  199 + total int64
  200 + )
  201 + queryFunc := func() (interface{}, error) {
  202 + tx = tx.Model(&ms).
  203 + Where("dataset_id in (?)", datasetIds).
  204 + Order("id desc")
  205 + if total, tx = transaction.PaginationAndCount(ctx, tx, domain.NewQueryOptions().WithFindOnly(), &ms); tx.Error != nil {
  206 + return dms, tx.Error
  207 + }
  208 + return dms, nil
  209 + }
  210 +
  211 + if _, err := repository.Query(queryFunc); err != nil {
  212 + return 0, nil, err
  213 + }
  214 +
  215 + for _, item := range ms {
  216 + if dm, err := repository.ModelToDomainModel(item); err != nil {
  217 + return 0, dms, err
  218 + } else {
  219 + dms = append(dms, dm)
  220 + }
  221 + }
  222 + return total, dms, nil
  223 +}
  224 +
  225 +func (repository *ChatDatasetDocumentMappingRepository) ModelToDomainModel(from *models.ChatDatasetDocumentMapping) (*domain.ChatDatasetDocumentMapping, error) {
  226 + to := &domain.ChatDatasetDocumentMapping{}
  227 + err := copier.Copy(to, from)
  228 + return to, err
  229 +}
  230 +
  231 +func (repository *ChatDatasetDocumentMappingRepository) DomainModelToModel(from *domain.ChatDatasetDocumentMapping) (*models.ChatDatasetDocumentMapping, error) {
  232 + to := &models.ChatDatasetDocumentMapping{}
  233 + err := copier.Copy(to, from)
  234 + return to, err
  235 +}
  236 +
  237 +func NewChatDatasetDocumentMappingRepository(cache *cache.CachedRepository) domain.ChatDatasetDocumentMappingRepository {
  238 + return &ChatDatasetDocumentMappingRepository{CachedRepository: cache}
  239 +}
  1 +package repository
  2 +
  3 +import (
  4 + "context"
  5 + "github.com/jinzhu/copier"
  6 + "github.com/pkg/errors"
  7 + "github.com/tiptok/gocomm/pkg/cache"
  8 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/db/models"
  9 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  10 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
  11 + "gorm.io/gorm"
  12 +)
  13 +
  14 +type ChatDatasetRepository struct {
  15 + *cache.CachedRepository
  16 +}
  17 +
  18 +func (repository *ChatDatasetRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ChatDataset) (*domain.ChatDataset, error) {
  19 + var (
  20 + err error
  21 + m = &models.ChatDataset{}
  22 + tx = conn.DB()
  23 + )
  24 + if m, err = repository.DomainModelToModel(dm); err != nil {
  25 + return nil, err
  26 + }
  27 + if tx = tx.Model(m).Save(m); tx.Error != nil {
  28 + return nil, tx.Error
  29 + }
  30 + dm.Id = m.Id
  31 + return repository.ModelToDomainModel(m)
  32 +
  33 +}
  34 +
  35 +func (repository *ChatDatasetRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ChatDataset) (*domain.ChatDataset, error) {
  36 + var (
  37 + err error
  38 + m *models.ChatDataset
  39 + tx = conn.DB()
  40 + )
  41 + if m, err = repository.DomainModelToModel(dm); err != nil {
  42 + return nil, err
  43 + }
  44 + queryFunc := func() (interface{}, error) {
  45 + tx = tx.Model(m).Updates(m)
  46 + return nil, tx.Error
  47 + }
  48 + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  49 + return nil, err
  50 + }
  51 + return repository.ModelToDomainModel(m)
  52 +}
  53 +
  54 +func (repository *ChatDatasetRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ChatDataset) (*domain.ChatDataset, error) {
  55 + var (
  56 + err error
  57 + m *models.ChatDataset
  58 + tx = transaction.DB()
  59 + )
  60 + if m, err = repository.DomainModelToModel(dm); err != nil {
  61 + return nil, err
  62 + }
  63 + oldVersion := dm.Version
  64 + m.Version += 1
  65 + queryFunc := func() (interface{}, error) {
  66 + tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
  67 + if tx.RowsAffected == 0 {
  68 + return nil, domain.ErrUpdateFail
  69 + }
  70 + return nil, tx.Error
  71 + }
  72 + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  73 + return nil, err
  74 + }
  75 + return repository.ModelToDomainModel(m)
  76 +}
  77 +
  78 +func (repository *ChatDatasetRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ChatDataset) (*domain.ChatDataset, error) {
  79 + var (
  80 + tx = conn.DB()
  81 + m = &models.ChatDataset{Id: dm.Id}
  82 + )
  83 + queryFunc := func() (interface{}, error) {
  84 + tx = tx.Where("id = ?", m.Id).Delete(m)
  85 + return m, tx.Error
  86 + }
  87 + if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  88 + return dm, err
  89 + }
  90 + return repository.ModelToDomainModel(m)
  91 +}
  92 +
  93 +func (repository *ChatDatasetRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ChatDataset, error) {
  94 + var (
  95 + err error
  96 + tx = conn.DB()
  97 + m = new(models.ChatDataset)
  98 + )
  99 + queryFunc := func() (interface{}, error) {
  100 + tx = tx.Model(m).Where("id = ?", id).First(m)
  101 + if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
  102 + return nil, domain.ErrNotFound
  103 + }
  104 + return m, tx.Error
  105 + }
  106 + cacheModel := new(models.ChatDataset)
  107 + cacheModel.Id = id
  108 + if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
  109 + return nil, err
  110 + }
  111 + return repository.ModelToDomainModel(m)
  112 +}
  113 +
  114 +func (repository *ChatDatasetRepository) FindOneUnscoped(ctx context.Context, conn transaction.Conn, id int64) (*domain.ChatDataset, error) {
  115 + var (
  116 + err error
  117 + tx = conn.DB()
  118 + m = new(models.ChatDataset)
  119 + )
  120 + queryFunc := func() (interface{}, error) {
  121 + tx = tx.Model(m).Unscoped().Where("id = ?", id).First(m)
  122 + if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
  123 + return nil, domain.ErrNotFound
  124 + }
  125 + return m, tx.Error
  126 + }
  127 + cacheModel := new(models.ChatDataset)
  128 + cacheModel.Id = id
  129 + if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
  130 + return nil, err
  131 + }
  132 + return repository.ModelToDomainModel(m)
  133 +}
  134 +
  135 +func (repository *ChatDatasetRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ChatDataset, error) {
  136 + var (
  137 + tx = conn.DB()
  138 + ms []*models.ChatDataset
  139 + dms = make([]*domain.ChatDataset, 0)
  140 + total int64
  141 + )
  142 + queryFunc := func() (interface{}, error) {
  143 + tx = tx.Model(&ms).Order("id desc")
  144 + if v, ok := queryOptions["name"]; ok {
  145 + tx.Where("name like ?", v)
  146 + }
  147 + if v, ok := queryOptions["status"]; ok {
  148 + tx.Where("status = ?", v)
  149 + }
  150 + if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
  151 + return dms, tx.Error
  152 + }
  153 + return dms, nil
  154 + }
  155 +
  156 + if _, err := repository.Query(queryFunc); err != nil {
  157 + return 0, nil, err
  158 + }
  159 +
  160 + for _, item := range ms {
  161 + if dm, err := repository.ModelToDomainModel(item); err != nil {
  162 + return 0, dms, err
  163 + } else {
  164 + dms = append(dms, dm)
  165 + }
  166 + }
  167 + return total, dms, nil
  168 +}
  169 +
  170 +func (repository *ChatDatasetRepository) ModelToDomainModel(from *models.ChatDataset) (*domain.ChatDataset, error) {
  171 + to := &domain.ChatDataset{}
  172 + err := copier.Copy(to, from)
  173 + return to, err
  174 +}
  175 +
  176 +func (repository *ChatDatasetRepository) DomainModelToModel(from *domain.ChatDataset) (*models.ChatDataset, error) {
  177 + to := &models.ChatDataset{}
  178 + err := copier.Copy(to, from)
  179 + return to, err
  180 +}
  181 +
  182 +func NewChatDatasetRepository(cache *cache.CachedRepository) domain.ChatDatasetRepository {
  183 + return &ChatDatasetRepository{CachedRepository: cache}
  184 +}
  1 +package repository
  2 +
  3 +import (
  4 + "context"
  5 + "github.com/jinzhu/copier"
  6 + "github.com/pkg/errors"
  7 + "github.com/tiptok/gocomm/pkg/cache"
  8 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/db/models"
  9 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
  10 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
  11 + "gorm.io/gorm"
  12 +)
  13 +
  14 +type ChatDocumentRepository struct {
  15 + *cache.CachedRepository
  16 +}
  17 +
  18 +func (repository *ChatDocumentRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ChatDocument) (*domain.ChatDocument, error) {
  19 + var (
  20 + err error
  21 + m = &models.ChatDocument{}
  22 + tx = conn.DB()
  23 + )
  24 + if m, err = repository.DomainModelToModel(dm); err != nil {
  25 + return nil, err
  26 + }
  27 + if tx = tx.Model(m).Save(m); tx.Error != nil {
  28 + return nil, tx.Error
  29 + }
  30 + dm.Id = m.Id
  31 + return repository.ModelToDomainModel(m)
  32 +
  33 +}
  34 +
  35 +func (repository *ChatDocumentRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ChatDocument) (*domain.ChatDocument, error) {
  36 + var (
  37 + err error
  38 + m *models.ChatDocument
  39 + tx = conn.DB()
  40 + )
  41 + if m, err = repository.DomainModelToModel(dm); err != nil {
  42 + return nil, err
  43 + }
  44 + queryFunc := func() (interface{}, error) {
  45 + tx = tx.Model(m).Updates(m)
  46 + return nil, tx.Error
  47 + }
  48 + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  49 + return nil, err
  50 + }
  51 + return repository.ModelToDomainModel(m)
  52 +}
  53 +
  54 +func (repository *ChatDocumentRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ChatDocument) (*domain.ChatDocument, error) {
  55 + var (
  56 + err error
  57 + m *models.ChatDocument
  58 + tx = transaction.DB()
  59 + )
  60 + if m, err = repository.DomainModelToModel(dm); err != nil {
  61 + return nil, err
  62 + }
  63 + oldVersion := dm.Version
  64 + m.Version += 1
  65 + queryFunc := func() (interface{}, error) {
  66 + tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
  67 + if tx.RowsAffected == 0 {
  68 + return nil, domain.ErrUpdateFail
  69 + }
  70 + return nil, tx.Error
  71 + }
  72 + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  73 + return nil, err
  74 + }
  75 + return repository.ModelToDomainModel(m)
  76 +}
  77 +
  78 +func (repository *ChatDocumentRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ChatDocument) (*domain.ChatDocument, error) {
  79 + var (
  80 + tx = conn.DB()
  81 + m = &models.ChatDocument{Id: dm.Id}
  82 + )
  83 + queryFunc := func() (interface{}, error) {
  84 + tx = tx.Where("id = ?", m.Id).Delete(m)
  85 + return m, tx.Error
  86 + }
  87 + if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
  88 + return dm, err
  89 + }
  90 + return repository.ModelToDomainModel(m)
  91 +}
  92 +
  93 +func (repository *ChatDocumentRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ChatDocument, error) {
  94 + var (
  95 + err error
  96 + tx = conn.DB()
  97 + m = new(models.ChatDocument)
  98 + )
  99 + queryFunc := func() (interface{}, error) {
  100 + tx = tx.Model(m).Where("id = ?", id).First(m)
  101 + if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
  102 + return nil, domain.ErrNotFound
  103 + }
  104 + return m, tx.Error
  105 + }
  106 + cacheModel := new(models.ChatDocument)
  107 + cacheModel.Id = id
  108 + if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
  109 + return nil, err
  110 + }
  111 + return repository.ModelToDomainModel(m)
  112 +}
  113 +
  114 +func (repository *ChatDocumentRepository) FindOneUnscoped(ctx context.Context, conn transaction.Conn, id int64) (*domain.ChatDocument, error) {
  115 + var (
  116 + err error
  117 + tx = conn.DB()
  118 + m = new(models.ChatDocument)
  119 + )
  120 + queryFunc := func() (interface{}, error) {
  121 + tx = tx.Model(m).Unscoped().Where("id = ?", id).First(m)
  122 + if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
  123 + return nil, domain.ErrNotFound
  124 + }
  125 + return m, tx.Error
  126 + }
  127 + cacheModel := new(models.ChatDocument)
  128 + cacheModel.Id = id
  129 + if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
  130 + return nil, err
  131 + }
  132 + return repository.ModelToDomainModel(m)
  133 +}
  134 +
  135 +func (repository *ChatDocumentRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ChatDocument, error) {
  136 + var (
  137 + tx = conn.DB()
  138 + ms []*models.ChatDocument
  139 + dms = make([]*domain.ChatDocument, 0)
  140 + total int64
  141 + )
  142 + queryFunc := func() (interface{}, error) {
  143 + tx = tx.Model(&ms).Order("id desc")
  144 + if v, ok := queryOptions["name"]; ok {
  145 + tx.Where("name like ?", v)
  146 + }
  147 + if v, ok := queryOptions["fileType"]; ok {
  148 + tx.Where("file_type = ?", v)
  149 + }
  150 + if v, ok := queryOptions["status"]; ok {
  151 + tx.Where("status = ?", v)
  152 + }
  153 + if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
  154 + return dms, tx.Error
  155 + }
  156 + return dms, nil
  157 + }
  158 +
  159 + if _, err := repository.Query(queryFunc); err != nil {
  160 + return 0, nil, err
  161 + }
  162 +
  163 + for _, item := range ms {
  164 + if dm, err := repository.ModelToDomainModel(item); err != nil {
  165 + return 0, dms, err
  166 + } else {
  167 + dms = append(dms, dm)
  168 + }
  169 + }
  170 + return total, dms, nil
  171 +}
  172 +
  173 +func (repository *ChatDocumentRepository) ModelToDomainModel(from *models.ChatDocument) (*domain.ChatDocument, error) {
  174 + to := &domain.ChatDocument{}
  175 + err := copier.Copy(to, from)
  176 + return to, err
  177 +}
  178 +
  179 +func (repository *ChatDocumentRepository) DomainModelToModel(from *domain.ChatDocument) (*models.ChatDocument, error) {
  180 + to := &models.ChatDocument{}
  181 + err := copier.Copy(to, from)
  182 + return to, err
  183 +}
  184 +
  185 +func NewChatDocumentRepository(cache *cache.CachedRepository) domain.ChatDocumentRepository {
  186 + return &ChatDocumentRepository{CachedRepository: cache}
  187 +}
@@ -141,6 +141,15 @@ func (repository *ChatSessionRepository) Find(ctx context.Context, conn transact @@ -141,6 +141,15 @@ func (repository *ChatSessionRepository) Find(ctx context.Context, conn transact
141 ) 141 )
142 queryFunc := func() (interface{}, error) { 142 queryFunc := func() (interface{}, error) {
143 tx = tx.Model(&ms).Order("id desc") 143 tx = tx.Model(&ms).Order("id desc")
  144 + if v, ok := queryOptions["companyId"]; ok {
  145 + tx.Where("company_id = ?", v)
  146 + }
  147 + if v, ok := queryOptions["userId"]; ok {
  148 + tx.Where("user_id = ?", v)
  149 + }
  150 + if v, ok := queryOptions["module"]; ok {
  151 + tx.Where("module = ?", v)
  152 + }
144 if v, ok := queryOptions["title"]; ok { 153 if v, ok := queryOptions["title"]; ok {
145 tx.Where("title like ?", v) 154 tx.Where("title like ?", v)
146 } 155 }
  1 +package domain
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
  6 +)
  7 +
  8 +type ChatDataset struct {
  9 + Id int64 // 唯一标识
  10 + CompanyId int64
  11 + UserId int64
  12 + Name string // 知识库名称
  13 + Desc string // 描述
  14 + Status int // 1:启用 2:禁用
  15 + CreatedAt int64 `json:",omitempty"`
  16 + UpdatedAt int64 `json:",omitempty"`
  17 + DeletedAt int64 `json:",omitempty"`
  18 + Version int `json:",omitempty"`
  19 +}
  20 +
  21 +func ChatDatasetId(m *ChatDataset) int64 {
  22 + return m.Id
  23 +}
  24 +
  25 +type ChatDatasetRepository interface {
  26 + Insert(ctx context.Context, conn transaction.Conn, dm *ChatDataset) (*ChatDataset, error)
  27 + Update(ctx context.Context, conn transaction.Conn, dm *ChatDataset) (*ChatDataset, error)
  28 + UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *ChatDataset) (*ChatDataset, error)
  29 + Delete(ctx context.Context, conn transaction.Conn, dm *ChatDataset) (*ChatDataset, error)
  30 + FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ChatDataset, error)
  31 + FindOneUnscoped(ctx context.Context, conn transaction.Conn, id int64) (*ChatDataset, error)
  32 + Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ChatDataset, error)
  33 +}
  1 +package domain
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
  6 +)
  7 +
  8 +type ChatDatasetDocumentMapping struct {
  9 + Id int64 // 唯一标识
  10 + DatasetId int64
  11 + DocumentId int64
  12 + CreatedAt int64 `json:",omitempty"`
  13 + UpdatedAt int64 `json:",omitempty"`
  14 + DeletedAt int64 `json:",omitempty"`
  15 + Version int `json:",omitempty"`
  16 +}
  17 +
  18 +type ChatDatasetDocumentMappingRepository interface {
  19 + Insert(ctx context.Context, conn transaction.Conn, dm *ChatDatasetDocumentMapping) (*ChatDatasetDocumentMapping, error)
  20 + Update(ctx context.Context, conn transaction.Conn, dm *ChatDatasetDocumentMapping) (*ChatDatasetDocumentMapping, error)
  21 + UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *ChatDatasetDocumentMapping) (*ChatDatasetDocumentMapping, error)
  22 + Delete(ctx context.Context, conn transaction.Conn, dm *ChatDatasetDocumentMapping) (*ChatDatasetDocumentMapping, error)
  23 + FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ChatDatasetDocumentMapping, error)
  24 + FindOneUnscoped(ctx context.Context, conn transaction.Conn, id int64) (*ChatDatasetDocumentMapping, error)
  25 + Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ChatDatasetDocumentMapping, error)
  26 + FindByDataset(ctx context.Context, conn transaction.Conn, datasetId ...int64) (int64, []*ChatDatasetDocumentMapping, error)
  27 + DeleteByDataset(ctx context.Context, conn transaction.Conn, datasetId int64) error
  28 + DeleteByDocument(ctx context.Context, conn transaction.Conn, documentId int64) error
  29 +}
  1 +package domain
  2 +
  3 +import (
  4 + "context"
  5 + "gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
  6 +)
  7 +
  8 +type ChatDocument struct {
  9 + Id int64 // 唯一标识
  10 + Name string // 文件命
  11 + FileType string // 文件类型 markdown\pdf\txt\doc&docx
  12 + Status int // 1.使用中、0.待处理、2.预处理中、3.处理失败
  13 + Metadata DocumentMetadata
  14 + CreatedAt int64 `json:",omitempty"`
  15 + UpdatedAt int64 `json:",omitempty"`
  16 + DeletedAt int64 `json:",omitempty"`
  17 + Version int `json:",omitempty"`
  18 +}
  19 +
  20 +const (
  21 + Enable = 1
  22 + Disable = 2
  23 +)
  24 +
  25 +const (
  26 + StatusInUsed = 1
  27 + StatusWaitProcess = 0
  28 + StatusPreProcess = 2
  29 + StatusProcessFail = 3
  30 +)
  31 +
  32 +type DocumentMetadata struct {
  33 + OriginFileName string `json:",omitempty"` // 源文件命
  34 + Ext string `json:",omitempty"` // 格式
  35 + FileId string `json:",omitempty"` // 星火文件ID
  36 + FileUrl string `json:",omitempty"` // 文件地址
  37 + FileSize float64 `json:",omitempty"` // 文件大小
  38 +}
  39 +
  40 +type ChatDocumentRepository interface {
  41 + Insert(ctx context.Context, conn transaction.Conn, dm *ChatDocument) (*ChatDocument, error)
  42 + Update(ctx context.Context, conn transaction.Conn, dm *ChatDocument) (*ChatDocument, error)
  43 + UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *ChatDocument) (*ChatDocument, error)
  44 + Delete(ctx context.Context, conn transaction.Conn, dm *ChatDocument) (*ChatDocument, error)
  45 + FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ChatDocument, error)
  46 + FindOneUnscoped(ctx context.Context, conn transaction.Conn, id int64) (*ChatDocument, error)
  47 + Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ChatDocument, error)
  48 +}