作者 yangfu

feat-1.0 chat doc

正在显示 84 个修改的文件 包含 5073 行增加223 行删除
... ... @@ -8,7 +8,7 @@ Timeout: 30000
LogRequest: true # 记录详细请求日志
Log:
Mode: file
#Mode: file
Encoding: plain
Level: debug # info
MaxSize: 1 # 2MB
... ...
package chat
import (
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/chat"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
)
func ChatMySparkSessionsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ChatSessionSearchRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := chat.NewChatSessionSearchLogic(r.Context(), svcCtx)
req.Module = domain.ModuleSparkChat
resp, err := l.ChatSessionSearch(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package chat
import (
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
... ... @@ -18,6 +19,8 @@ func ChatSessionSaveHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
}
l := chat.NewChatSessionSaveLogic(r.Context(), svcCtx)
req.ChatSession.Module = domain.ModuleOpenaiChat
req.ChatSession.Type = domain.TypeChat
resp, err := l.ChatSessionSave(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
... ...
package chat
import (
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
... ... @@ -18,6 +19,7 @@ func ChatSessionSearchHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
}
l := chat.NewChatSessionSearchLogic(r.Context(), svcCtx)
req.Module = domain.ModuleOpenaiChat
resp, err := l.ChatSessionSearch(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
... ...
package chat
import (
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/chat"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
)
func ChatSparkSessionSaveHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ChatSessionSaveRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := chat.NewChatSessionSaveLogic(r.Context(), svcCtx)
req.ChatSession.Module = domain.ModuleSparkChat
if req.ChatSession.DatasetId > 0 {
req.ChatSession.Type = domain.TypeSparkDatasetChat
} else if len(req.ChatSession.DocumentIds) > 0 {
req.ChatSession.Type = domain.TypeSparkDocumentsChat
} else {
httpx.ErrorCtx(r.Context(), w, xerr.NewErrMsgErr("知识库、文档至少选一个", nil))
return
}
resp, err := l.ChatSessionSave(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package dataset
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/dataset"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
)
func ChatDatasetDeleteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ChatDatasetDeleteRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := dataset.NewChatDatasetDeleteLogic(r.Context(), svcCtx)
resp, err := l.ChatDatasetDelete(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package dataset
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/dataset"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
)
func ChatDatasetGetHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ChatDatasetGetRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := dataset.NewChatDatasetGetLogic(r.Context(), svcCtx)
resp, err := l.ChatDatasetGet(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package dataset
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/dataset"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
)
func ChatDatasetRenameHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ChatDatasetRenameRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := dataset.NewChatDatasetRenameLogic(r.Context(), svcCtx)
resp, err := l.ChatDatasetRename(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package dataset
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/dataset"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
)
func ChatDatasetSaveHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ChatDatasetSaveRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := dataset.NewChatDatasetSaveLogic(r.Context(), svcCtx)
resp, err := l.ChatDatasetSave(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package dataset
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/dataset"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
)
func ChatDatasetSearchHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ChatDatasetSearchRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := dataset.NewChatDatasetSearchLogic(r.Context(), svcCtx)
resp, err := l.ChatDatasetSearch(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package dataset
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/dataset"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
)
func ChatDatasetUpdateHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ChatDatasetUpdateRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := dataset.NewChatDatasetUpdateLogic(r.Context(), svcCtx)
resp, err := l.ChatDatasetUpdate(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package document
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/document"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
)
func ChatDocumentDeleteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ChatDocumentDeleteRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := document.NewChatDocumentDeleteLogic(r.Context(), svcCtx)
resp, err := l.ChatDocumentDelete(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package document
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/document"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
)
func ChatDocumentGetHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ChatDocumentGetRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := document.NewChatDocumentGetLogic(r.Context(), svcCtx)
resp, err := l.ChatDocumentGet(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package document
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/document"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
)
func ChatDocumentRenameHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ChatDocumentRenameRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := document.NewChatDocumentRenameLogic(r.Context(), svcCtx)
resp, err := l.ChatDocumentRename(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package document
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/document"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
)
func ChatDocumentSaveHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ChatDocumentSaveRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := document.NewChatDocumentSaveLogic(r.Context(), svcCtx)
resp, err := l.ChatDocumentSave(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package document
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/logic/document"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
)
func ChatDocumentSearchHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ChatDocumentSearchRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := document.NewChatDocumentSearchLogic(r.Context(), svcCtx)
resp, err := l.ChatDocumentSearch(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
... ... @@ -5,6 +5,8 @@ import (
"net/http"
chat "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/handler/chat"
dataset "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/handler/dataset"
document "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/handler/document"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"github.com/zeromicro/go-zero/rest"
... ... @@ -52,6 +54,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
},
{
Method: http.MethodPost,
Path: "/chat/session/my_spark_sessions",
Handler: chat.ChatMySparkSessionsHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/chat/spark_session",
Handler: chat.ChatSparkSessionSaveHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/chat/session/records",
Handler: chat.ChatSessionRecordsHandler(serverCtx),
},
... ... @@ -65,4 +77,79 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
rest.WithJwt(serverCtx.Config.SystemAuth.AccessSecret),
rest.WithPrefix("/v1"),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LogRequest},
[]rest.Route{
{
Method: http.MethodGet,
Path: "/chat/document/:id",
Handler: document.ChatDocumentGetHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/chat/document",
Handler: document.ChatDocumentSaveHandler(serverCtx),
},
{
Method: http.MethodDelete,
Path: "/chat/document/:id",
Handler: document.ChatDocumentDeleteHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/chat/document/rename",
Handler: document.ChatDocumentRenameHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/chat/document/search",
Handler: document.ChatDocumentSearchHandler(serverCtx),
},
}...,
),
rest.WithJwt(serverCtx.Config.SystemAuth.AccessSecret),
rest.WithPrefix("/v1"),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LogRequest},
[]rest.Route{
{
Method: http.MethodGet,
Path: "/chat/dataset/:id",
Handler: dataset.ChatDatasetGetHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/chat/dataset",
Handler: dataset.ChatDatasetSaveHandler(serverCtx),
},
{
Method: http.MethodDelete,
Path: "/chat/dataset/:id",
Handler: dataset.ChatDatasetDeleteHandler(serverCtx),
},
{
Method: http.MethodPut,
Path: "/chat/dataset/:id",
Handler: dataset.ChatDatasetUpdateHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/chat/dataset/rename",
Handler: dataset.ChatDatasetRenameHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/chat/dataset/search",
Handler: dataset.ChatDatasetSearchHandler(serverCtx),
},
}...,
),
rest.WithJwt(serverCtx.Config.SystemAuth.AccessSecret),
rest.WithPrefix("/v1"),
)
}
... ...
... ... @@ -28,6 +28,9 @@ func NewChatModelsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatMo
func (l *ChatModelsLogic) ChatModels(req *types.ChatModelsRequest) (resp *types.ChatModelsResponse, err error) {
var models = make([]types.Model, 0)
lo.ForEach(domain.DefaultChatModels, func(item *domain.ChatModel, index int) {
if item.Id == domain.SparkChatDocModelId {
return
}
models = append(models, types.Model{
Id: item.Id,
Name: item.Name,
... ...
package chat
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ChatMySparkSessionsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewChatMySparkSessionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatMySparkSessionsLogic {
return &ChatMySparkSessionsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ChatMySparkSessionsLogic) ChatMySparkSessions(req *types.ChatSessionSearchRequest) (resp *types.ChatSessionSearchResponse, err error) {
// todo: add your logic here and delete this line
return
}
... ...
... ... @@ -2,10 +2,13 @@ package chat
import (
"context"
"github.com/samber/lo"
"github.com/zeromicro/go-zero/core/fx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/open"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/ai"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/tool"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
"net/http"
... ... @@ -38,7 +41,6 @@ func (l *ChatSessionConversationLogic) ChatSessionConversation(w http.ResponseWr
token = contextdata.GetUserTokenFromCtx(l.ctx)
session *domain.ChatSession
user open.User
model *domain.ChatModel
ok bool
beginUnix = time.Now().UnixMilli()
)
... ... @@ -51,8 +53,8 @@ func (l *ChatSessionConversationLogic) ChatSessionConversation(w http.ResponseWr
if user.Id != session.UserId {
return nil, xerr.NewErrMsgErr("无权限", err)
}
if model, ok = domain.DefaultChatModels.Match(req.ModelId); !ok {
return nil, xerr.NewErrMsgErr("模型不存在", err)
if session.Module == domain.ModuleSparkChat {
req.ModelId = domain.SparkChatDocModelId
}
dm = &domain.ChatSessionRecord{
CompanyId: token.CompanyId,
... ... @@ -69,15 +71,16 @@ func (l *ChatSessionConversationLogic) ChatSessionConversation(w http.ResponseWr
var answer string
var channel = make(chan string, 5)
// 异步访问AI接口
go func() {
// 异步访问AI接口
answer, err = Conversation(model, req.Text, channel)
}()
fx.Parallel(func() {
answer, err = Conversation(l.ctx, l.svcCtx, session, req.ModelId, req.Text, channel)
}, func() {
for {
if _, ok = <-channel; !ok {
break
}
logx.Info(1)
}
})
if err != nil {
return nil, xerr.NewErrMsgErr("AI模型异常,稍后再试", err)
}
... ... @@ -101,17 +104,63 @@ func (l *ChatSessionConversationLogic) ChatSessionConversation(w http.ResponseWr
return
}
func Conversation(m *domain.ChatModel, text string, channel chan string) (answer string, err error) {
// Conversation 普通对话
func Conversation(ctx context.Context, svc *svc.ServiceContext, session *domain.ChatSession, modelId int64, text string, channel chan string) (answer string, err error) {
var (
m *domain.ChatModel
ok bool
)
defer close(channel)
if m, ok = domain.DefaultChatModels.Match(modelId); !ok {
err = xerr.NewErrMsgErr("模型不存在", err)
return
}
if session.Module == domain.ModuleSparkChat {
return SparkDocumentConversation(ctx, svc, session, m, text, channel)
}
switch m.Id {
// 星火3.5
case 1, 2, 3:
answer, err = ai.ChatGPT(m.Code, m.Config.AppKey, text, channel)
case 4:
answer, err = ai.Spark(m.Config.AppId, m.Config.AppKey, m.Config.AppSecret, text, channel)
answer, err = ai.ChatSpark(m.Config.AppId, m.Config.AppKey, m.Config.AppSecret, text, channel)
}
if err != nil {
return "", err
}
return
}
// SparkDocumentConversation 星火文档对话
func SparkDocumentConversation(ctx context.Context, svcCtx *svc.ServiceContext, session *domain.ChatSession, m *domain.ChatModel, text string, channel chan string) (answer string, err error) {
var (
conn = svcCtx.DefaultDBConn()
)
// 获取文件ID列表
fileIds := make([]string, 0)
lazyDocument := tool.NewLazyLoadService(svcCtx.ChatDocumentRepository.FindOne)
if session.Type == domain.TypeSparkDatasetChat { // 知识库
_, documentMapping, _ := svcCtx.ChatDatasetDocumentMappingRepository.FindByDataset(ctx, conn, session.Metadata.DatasetId)
lo.ForEach(documentMapping, func(item *domain.ChatDatasetDocumentMapping, index int) {
if document, _ := lazyDocument.Load(ctx, conn, item.DocumentId); document != nil {
if len(document.Metadata.FileId) == 0 {
return
}
fileIds = append(fileIds, document.Metadata.FileId)
}
})
} else if session.Type == domain.TypeSparkDocumentsChat { // 多文档
lo.ForEach(session.Metadata.DocumentIds, func(item int64, index int) {
if document, _ := lazyDocument.Load(ctx, conn, item); document != nil {
if len(document.Metadata.FileId) == 0 {
return
}
fileIds = append(fileIds, document.Metadata.FileId)
}
})
}
// 对话
c := m.Config
return ai.ChatSparkDocument(c.AppId, c.AppKey, c.AppSecret, fileIds, text, channel)
}
... ...
... ... @@ -40,7 +40,6 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon
token = contextdata.GetUserTokenFromCtx(l.ctx)
session *domain.ChatSession
user open.User
model *domain.ChatModel
ok bool
)
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
if user.Id != session.UserId {
return nil, xerr.NewErrMsgErr("无权限", err)
}
if model, ok = domain.DefaultChatModels.Match(req.ModelId); !ok {
return nil, xerr.NewErrMsgErr("模型不存在", err)
if session.Module == domain.ModuleSparkChat {
req.ModelId = domain.SparkChatDocModelId
}
var answer string
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
... ... @@ -96,7 +93,7 @@ func (l *ChatSessionConversationWsLogic) ChatSessionConversationWs(w http.Respon
}
fx.Parallel(func() {
// 异步访问AI接口
answer, err = Conversation(model, string(text), channel)
answer, err = Conversation(l.ctx, l.svcCtx, session, req.ModelId, string(text), channel)
}, func() {
for {
var v string
... ...
... ... @@ -6,6 +6,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
"time"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
... ... @@ -39,7 +40,9 @@ func (l *ChatSessionSaveLogic) ChatSessionSave(req *types.ChatSessionSaveRequest
}, true); err != nil {
return nil, xerr.NewErrMsg("保存失败")
}
resp = &types.ChatSessionSaveResponse{}
resp = &types.ChatSessionSaveResponse{
ChatSession: NewTypesChatSession(dm),
}
return
}
... ... @@ -53,6 +56,13 @@ func NewDomainChatSession(token contextdata.UserToken, item types.ChatSessionIte
UserId: token.UserId,
Title: title,
Abstract: title,
Module: item.Module,
Type: item.Type,
Metadata: &domain.SessionMetadata{
DatasetId: item.DatasetId,
DocumentIds: item.DocumentIds,
},
Rank: time.Now().Unix(),
}
}
... ...
... ... @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
... ... @@ -30,11 +31,16 @@ func (l *ChatSessionSearchLogic) ChatSessionSearch(req *types.ChatSessionSearchR
conn = l.svcCtx.DefaultDBConn()
dms []*domain.ChatSession
total int64
token = contextdata.GetUserTokenFromCtx(l.ctx)
)
queryOptions := domain.NewQueryOptions().
WithKV("title", fmt.Sprintf("%%%v%%", req.Title))
WithKV("module", req.Module).
WithKV("companyId", token.CompanyId).
WithKV("userId", token.UserId)
if req.Title != "" {
queryOptions.WithKV("title", fmt.Sprintf("%%%v%%", req.Title))
}
if req.Page != 0 && req.Size != 0 {
queryOptions.WithOffsetLimit(req.Page, req.Size)
}
... ...
package chat
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ChatSparkSessionSaveLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewChatSparkSessionSaveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatSparkSessionSaveLogic {
return &ChatSparkSessionSaveLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ChatSparkSessionSaveLogic) ChatSparkSessionSave(req *types.ChatSessionSaveRequest) (resp *types.ChatSessionSaveResponse, err error) {
// todo: add your logic here and delete this line
return
}
... ...
package dataset
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ChatDatasetDeleteLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewChatDatasetDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDatasetDeleteLogic {
return &ChatDatasetDeleteLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ChatDatasetDeleteLogic) ChatDatasetDelete(req *types.ChatDatasetDeleteRequest) (resp *types.ChatDatasetDeleteResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
dm *domain.ChatDataset
)
if dm, err = l.svcCtx.ChatDatasetRepository.FindOne(l.ctx, conn, req.Id); err != nil {
return nil, xerr.NewErrMsgErr("不存在", err)
}
if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
if dm, err = l.svcCtx.ChatDatasetRepository.Delete(l.ctx, conn, dm); err != nil {
return err
}
// 删除document & dataset 关联
if err = l.svcCtx.ChatDatasetDocumentMappingRepository.DeleteByDataset(l.ctx, conn, dm.Id); err != nil {
return err
}
return nil
}, true); err != nil {
return nil, xerr.NewErrMsgErr("移除失败", err)
}
resp = &types.ChatDatasetDeleteResponse{}
return
}
... ...
package dataset
import (
"context"
"github.com/samber/lo"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/tool"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ChatDatasetGetLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewChatDatasetGetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDatasetGetLogic {
return &ChatDatasetGetLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ChatDatasetGetLogic) ChatDatasetGet(req *types.ChatDatasetGetRequest) (resp *types.ChatDatasetGetResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
dm *domain.ChatDataset
documentMapping []*domain.ChatDatasetDocumentMapping
)
// 货号唯一
if dm, err = l.svcCtx.ChatDatasetRepository.FindOne(l.ctx, conn, req.Id); err != nil {
return nil, xerr.NewErrMsgErr("不存在", err)
}
documents := make([]types.ChatDocumentItem, 0)
_, documentMapping, _ = l.svcCtx.ChatDatasetDocumentMappingRepository.FindByDataset(l.ctx, conn, dm.Id)
lazyDocument := tool.NewLazyLoadService(l.svcCtx.ChatDocumentRepository.FindOne)
lo.ForEach(documentMapping, func(item *domain.ChatDatasetDocumentMapping, index int) {
if document, _ := lazyDocument.Load(l.ctx, conn, item.DocumentId); document != nil {
documents = append(documents, types.NewTypesChatDocument(document))
}
})
resp = &types.ChatDatasetGetResponse{
ChatDataset: NewTypesChatDataset(dm, nil),
Documents: documents,
}
return
}
... ...
package dataset
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ChatDatasetRenameLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewChatDatasetRenameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDatasetRenameLogic {
return &ChatDatasetRenameLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ChatDatasetRenameLogic) ChatDatasetRename(req *types.ChatDatasetRenameRequest) (resp *types.ChatDatasetRenameResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
dm *domain.ChatDataset
)
if dm, err = l.svcCtx.ChatDatasetRepository.FindOne(l.ctx, conn, req.Id); err != nil {
return nil, xerr.NewErrMsgErr("不存在", err)
}
// 赋值
if req.Name != "" {
dm.Name = req.Name
}
// 更新
if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
dm, err = l.svcCtx.ChatDatasetRepository.UpdateWithVersion(l.ctx, conn, dm)
return err
}, true); err != nil {
return nil, xerr.NewErrMsg("更新失败")
}
resp = &types.ChatDatasetRenameResponse{
ChatDataset: NewTypesChatDataset(dm, nil),
}
return
}
... ...
package dataset
import (
"context"
"github.com/samber/lo"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ChatDatasetSaveLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewChatDatasetSaveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDatasetSaveLogic {
return &ChatDatasetSaveLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ChatDatasetSaveLogic) ChatDatasetSave(req *types.ChatDatasetSaveRequest) (resp *types.ChatDatasetSaveResponse, err error) {
var (
dm *domain.ChatDataset
token = contextdata.GetUserTokenFromCtx(l.ctx)
)
if req.Name == "" {
req.Name = "新知识库"
}
dm = &domain.ChatDataset{
UserId: token.UserId,
CompanyId: token.CompanyId,
Name: req.Name,
Status: domain.Enable,
}
if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
if dm, err = l.svcCtx.ChatDatasetRepository.Insert(l.ctx, conn, dm); err != nil {
return err
}
if len(req.DocumentIds) > 0 {
for _, id := range req.DocumentIds {
l.svcCtx.ChatDatasetDocumentMappingRepository.Insert(l.ctx, conn, &domain.ChatDatasetDocumentMapping{
DatasetId: dm.Id,
DocumentId: id,
})
}
}
return err
}, true); err != nil {
return nil, xerr.NewErrMsg("保存失败")
}
resp = &types.ChatDatasetSaveResponse{
ChatDataset: NewTypesChatDataset(dm, lo.ToPtr(len(req.DocumentIds))),
}
return
}
//func NewDomainChatDataset(item types.ChatDatasetItem) *domain.ChatDataset {
// return &domain.ChatDataset{
// }
// }
func NewTypesChatDataset(item *domain.ChatDataset, fileNum *int) types.ChatDatasetItem {
return types.ChatDatasetItem{
Id: item.Id,
Name: lo.ToPtr(item.Name),
Desc: lo.ToPtr(item.Desc),
Status: lo.ToPtr(item.Status),
CreatedAt: item.CreatedAt,
UpdatedAt: item.UpdatedAt,
FileNumber: fileNum,
}
}
... ...
package dataset
import (
"context"
"fmt"
"github.com/samber/lo"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ChatDatasetSearchLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewChatDatasetSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDatasetSearchLogic {
return &ChatDatasetSearchLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ChatDatasetSearchLogic) ChatDatasetSearch(req *types.ChatDatasetSearchRequest) (resp *types.ChatDatasetSearchResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
dms []*domain.ChatDataset
total int64
)
queryOptions := domain.NewQueryOptions().WithOffsetLimit(req.Page, req.Size).
WithKV("status", req.Status)
if req.Name != "" {
queryOptions.WithKV("name", fmt.Sprintf("%%%v%%", req.Name))
}
total, dms, err = l.svcCtx.ChatDatasetRepository.Find(l.ctx, conn, queryOptions)
list := make([]types.ChatDatasetItem, 0)
// 知识库关联的文档
ids := domain.Values(dms, domain.ChatDatasetId)
_, documentMappings, _ := l.svcCtx.ChatDatasetDocumentMappingRepository.FindByDataset(l.ctx, conn, ids...)
documentGroups := lo.GroupBy(documentMappings, func(item *domain.ChatDatasetDocumentMapping) int64 {
return item.DatasetId
})
for i := range dms {
var num = 0
if v, ok := documentGroups[dms[i].Id]; ok {
num = len(v)
}
list = append(list, NewTypesChatDataset(dms[i], lo.ToPtr(num)))
}
resp = &types.ChatDatasetSearchResponse{
List: list,
Total: total,
}
return
}
... ...
package dataset
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ChatDatasetUpdateLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewChatDatasetUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDatasetUpdateLogic {
return &ChatDatasetUpdateLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ChatDatasetUpdateLogic) ChatDatasetUpdate(req *types.ChatDatasetUpdateRequest) (resp *types.ChatDatasetUpdateResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
dm *domain.ChatDataset
)
if dm, err = l.svcCtx.ChatDatasetRepository.FindOne(l.ctx, conn, req.Id); err != nil {
return nil, xerr.NewErrMsgErr("不存在", err)
}
// 赋值
if req.ChatDataset.Name != nil {
dm.Name = *req.ChatDataset.Name
}
if req.ChatDataset.Desc != nil {
dm.Desc = *req.ChatDataset.Desc
}
if req.ChatDataset.Status != nil {
dm.Status = *req.ChatDataset.Status
}
// 更新
if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
dm, err = l.svcCtx.ChatDatasetRepository.UpdateWithVersion(l.ctx, conn, dm)
return err
}, true); err != nil {
return nil, xerr.NewErrMsg("更新失败")
}
resp = &types.ChatDatasetUpdateResponse{}
return
}
... ...
package document
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ChatDocumentDeleteLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewChatDocumentDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDocumentDeleteLogic {
return &ChatDocumentDeleteLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ChatDocumentDeleteLogic) ChatDocumentDelete(req *types.ChatDocumentDeleteRequest) (resp *types.ChatDocumentDeleteResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
dm *domain.ChatDocument
)
if dm, err = l.svcCtx.ChatDocumentRepository.FindOne(l.ctx, conn, req.Id); err != nil {
return nil, xerr.NewErrMsgErr("不存在", err)
}
if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
if dm, err = l.svcCtx.ChatDocumentRepository.Delete(l.ctx, conn, dm); err != nil {
return err
}
// 删除document & dataset 关联
if err = l.svcCtx.ChatDatasetDocumentMappingRepository.DeleteByDocument(l.ctx, conn, dm.Id); err != nil {
return err
}
return nil
}, true); err != nil {
return nil, xerr.NewErrMsgErr("移除失败", err)
}
return
}
... ...
package document
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ChatDocumentGetLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewChatDocumentGetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDocumentGetLogic {
return &ChatDocumentGetLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ChatDocumentGetLogic) ChatDocumentGet(req *types.ChatDocumentGetRequest) (resp *types.ChatDocumentGetResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
dm *domain.ChatDocument
)
// 货号唯一
if dm, err = l.svcCtx.ChatDocumentRepository.FindOne(l.ctx, conn, req.Id); err != nil {
return nil, xerr.NewErrMsgErr("不存在", err)
}
resp = &types.ChatDocumentGetResponse{
ChatDocument: types.NewTypesChatDocument(dm),
}
return
}
... ...
package document
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ChatDocumentRenameLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewChatDocumentRenameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDocumentRenameLogic {
return &ChatDocumentRenameLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ChatDocumentRenameLogic) ChatDocumentRename(req *types.ChatDocumentRenameRequest) (resp *types.ChatDocumentRenameResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
dm *domain.ChatDocument
)
if dm, err = l.svcCtx.ChatDocumentRepository.FindOne(l.ctx, conn, req.Id); err != nil {
return nil, xerr.NewErrMsgErr("不存在", err)
}
// 赋值
dm.Name = req.Name
// 更新
if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
dm, err = l.svcCtx.ChatDocumentRepository.UpdateWithVersion(l.ctx, conn, dm)
return err
}, true); err != nil {
return nil, xerr.NewErrMsg("更新失败")
}
resp = &types.ChatDocumentRenameResponse{
ChatDocument: types.NewTypesChatDocument(dm),
}
return
}
... ...
package document
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/ai"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
"github.com/zeromicro/go-zero/core/logx"
)
type ChatDocumentSaveLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewChatDocumentSaveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDocumentSaveLogic {
return &ChatDocumentSaveLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ChatDocumentSaveLogic) ChatDocumentSave(req *types.ChatDocumentSaveRequest) (resp *types.ChatDocumentSaveResponse, err error) {
var (
//conn = l.svcCtx.DefaultDBConn()
dm *domain.ChatDocument
model *domain.ChatModel
fileId string
)
model, _ = domain.DefaultChatModels.Match(4)
// 文件上传星火文档
// 设置回调
if fileId, err = ai.SparkUploadFile(model.Config.AppId, model.Config.AppKey, model.Config.AppSecret, ai.SparkFileRequest{
Url: req.Url,
FileName: req.FileName,
CallbackUrl: "",
}); err != nil {
return nil, xerr.NewErrMsgErr("上传文档失败", err)
}
// 唯一判断
dm = types.NewDomainChatDocument(req.FileName, req.Url, req.Size, fileId)
if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
dm, err = l.svcCtx.ChatDocumentRepository.Insert(l.ctx, conn, dm)
return err
}, true); err != nil {
return nil, xerr.NewErrMsg("保存失败")
}
resp = &types.ChatDocumentSaveResponse{
ChatDocument: types.NewTypesChatDocument(dm),
}
return
}
... ...
package document
import (
"context"
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ChatDocumentSearchLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewChatDocumentSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatDocumentSearchLogic {
return &ChatDocumentSearchLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ChatDocumentSearchLogic) ChatDocumentSearch(req *types.ChatDocumentSearchRequest) (resp *types.ChatDocumentSearchResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
dms []*domain.ChatDocument
total int64
)
queryOptions := domain.NewQueryOptions().WithOffsetLimit(req.Page, req.Size).
WithKV("fileType", req.FileType).
WithKV("status", req.Status)
if req.Name != "" {
queryOptions.WithKV("name", fmt.Sprintf("%%%v%%", req.Name))
}
total, dms, err = l.svcCtx.ChatDocumentRepository.Find(l.ctx, conn, queryOptions)
list := make([]types.ChatDocumentItem, 0)
for i := range dms {
list = append(list, types.NewTypesChatDocument(dms[i]))
}
resp = &types.ChatDocumentSearchResponse{
List: list,
Total: total,
}
return
}
... ...
... ... @@ -21,6 +21,9 @@ type ServiceContext struct {
DB *gorm.DB
ChatSessionRepository domain.ChatSessionRepository
ChatSessionRecordRepository domain.ChatSessionRecordRepository
ChatDocumentRepository domain.ChatDocumentRepository
ChatDatasetRepository domain.ChatDatasetRepository
ChatDatasetDocumentMappingRepository domain.ChatDatasetDocumentMappingRepository
SystemOpen open.SystemOpen
}
... ... @@ -36,7 +39,9 @@ func NewServiceContext(c config.Config) *ServiceContext {
LogRequest: middleware.NewLogRequestMiddleware(c.LogRequest).Handle,
ChatSessionRepository: repository.NewChatSessionRepository(cache.NewCachedRepository(mlCache)),
ChatSessionRecordRepository: repository.NewChatSessionRecordRepository(cache.NewCachedRepository(mlCache)),
ChatDocumentRepository: repository.NewChatDocumentRepository(cache.NewCachedRepository(mlCache)),
ChatDatasetRepository: repository.NewChatDatasetRepository(cache.NewCachedRepository(mlCache)),
ChatDatasetDocumentMappingRepository: repository.NewChatDatasetDocumentMappingRepository(cache.NewCachedRepository(mlCache)),
SystemOpen: open.NewSystemOpen(c.Redis),
}
}
... ...
package types
import (
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"path/filepath"
"strings"
)
func NewDomainChatDocument(name, url string, size float64, fileId string) *domain.ChatDocument {
ext := getExt(name)
return &domain.ChatDocument{
Name: name,
Status: domain.StatusInUsed,
FileType: getFileTypeByExt(ext),
Metadata: domain.DocumentMetadata{
OriginFileName: filepath.Base(name),
Ext: ext,
FileId: fileId,
FileUrl: url,
FileSize: size,
},
}
}
func NewTypesChatDocument(item *domain.ChatDocument) ChatDocumentItem {
return ChatDocumentItem{
Id: item.Id,
Name: item.Name,
FileType: item.FileType,
Status: item.Status,
OriginFileName: item.Metadata.OriginFileName,
Ext: item.Metadata.Ext,
FileId: item.Metadata.FileId,
FileUrl: item.Metadata.FileUrl,
FileSize: item.Metadata.FileSize,
}
}
func getFileTypeByExt(ext string) string {
switch ext {
case "md":
return "markdown"
case "pdf":
return ext
case "txt":
return ext
case "doc", "docx":
return "doc"
default:
return ""
}
return ""
}
func getExt(file string) string {
return strings.TrimPrefix(filepath.Ext(file), ".")
}
... ...
... ... @@ -15,6 +15,7 @@ type ChatSessionSaveRequest struct {
}
type ChatSessionSaveResponse struct {
ChatSession ChatSessionItem `json:"session"`
}
type ChatSessionDeleteRequest struct {
... ... @@ -36,6 +37,7 @@ type ChatSessionSearchRequest struct {
Page int `json:"page,optional"`
Size int `json:"size,optional"`
Title string `json:"title,optional"` // 按标题搜索
Module int `json:"module,optional"` // 模块 1:openai chat 2:星火文档问答
}
type ChatSessionSearchResponse struct {
... ... @@ -48,6 +50,10 @@ type ChatSessionItem struct {
Title string `json:"title,optional,omitempty"` // 会话标题
Abstract string `json:"abstract,optional,omitempty"` // 摘要
CreatedAt int64 `json:"createdAt,optional,omitempty"` // 创建时间
Module int `json:"module,optional,omitempty,default=1"` // 1:openai chat 2:星火文档问答
Type string `json:"type,optional,omitempty,default=chat"` // 类型 chat:普通问答 spark_dataset_chat:星火知识库问答 spark_documents_chat:星火多文档问答
DatasetId int64 `json:"datasetId,optional,omitempty"` // 知识库
DocumentIds []int64 `json:"documentIds,optional,omitempty"` // 多文档
}
type ChatModelsRequest struct {
... ... @@ -121,3 +127,136 @@ type User struct {
Name string `json:"name"` // 名称
Avatar string `json:"avatar"` // 头像
}
type ChatDocumentGetRequest struct {
Id int64 `path:"id"`
}
type ChatDocumentGetResponse struct {
ChatDocument ChatDocumentItem `json:"document"`
}
type ChatDocumentSaveRequest struct {
FileName string `json:"fileName"`
Url string `json:"url"`
Size float64 `json:"size"` // 文件大小(KB)
}
type ChatDocumentSaveResponse struct {
ChatDocument ChatDocumentItem `json:"document"`
}
type ChatDocumentDeleteRequest struct {
Id int64 `path:"id"`
}
type ChatDocumentDeleteResponse struct {
}
type ChatDocumentUpdateRequest struct {
Id int64 `path:"id"`
ChatDocument ChatDocumentItem `json:"document"`
}
type ChatDocumentUpdateResponse struct {
}
type ChatDocumentSearchRequest struct {
Page int `json:"page,optional"`
Size int `json:"size,optional"`
Name string `json:"name,optional"` // 文件名匹配
FileType string `json:"fileType,optional"` // 文件类型 markdown\pdf\txt\doc&docx
Status int `json:"status,optional"` // 文件状态 1.使用中、0.待处理、2.预处理中、3.处理失败
}
type ChatDocumentSearchResponse struct {
List []ChatDocumentItem `json:"list"`
Total int64 `json:"total"`
}
type ChatDocumentItem struct {
Id int64 `json:"id"`
Name string `json:"name,optional"` // 文件名
FileType string `json:"fileType"` // 文件类型 markdown\pdf\txt\doc&docx
Status int `json:"status"` // 1.使用中、0.待处理、2.预处理中、3.处理失败
OriginFileName string `json:"originFileName,omitempty"` // 源文件命
Ext string `json:"ext,omitempty"` // 格式
FileId string `json:"fileId,omitempty"` // 星火文件ID
FileUrl string `json:"fileUrl,omitempty"` // 文件地址
FileSize float64 `json:"fileSize,omitempty"` // 文件大小KB
CreatedAt int64 `json:"createdAt,omitempty"` // 创建时间
UpdatedAt int64 `json:"updatedAt,omitempty"` // 更新时间
}
type ChatDocumentRenameRequest struct {
Id int64 `json:"id"` // 文档ID
Name string `json:"name"` // 新名称
}
type ChatDocumentRenameResponse struct {
ChatDocument ChatDocumentItem `json:"document"`
}
type ChatDatasetGetRequest struct {
Id int64 `path:"id"`
}
type ChatDatasetGetResponse struct {
ChatDataset ChatDatasetItem `json:"dataset"`
Documents []ChatDocumentItem `json:"documents"`
}
type ChatDatasetSaveRequest struct {
Name string `json:"name"` // 名称
DocumentIds []int64 `json:"documentIds"` // 文档ID列表
}
type ChatDatasetSaveResponse struct {
ChatDataset ChatDatasetItem `json:"dataset"`
}
type ChatDatasetDeleteRequest struct {
Id int64 `path:"id"`
}
type ChatDatasetDeleteResponse struct {
}
type ChatDatasetUpdateRequest struct {
Id int64 `path:"id"`
ChatDataset ChatDatasetItem `json:"dataset"`
}
type ChatDatasetUpdateResponse struct {
}
type ChatDatasetSearchRequest struct {
Page int `json:"page,optional"`
Size int `json:"size,optional"`
Name string `json:"name,optional"` // 文件名匹配
Status int `json:"status,optional,option=0|1|2"` // 1:启用 2:禁用
}
type ChatDatasetSearchResponse struct {
List []ChatDatasetItem `json:"list"`
Total int64 `json:"total"`
}
type ChatDatasetItem struct {
Id int64 `json:"id,optional,omitempty"`
Name *string `json:"name,optional,omitempty"` // 名称
Desc *string `json:"desc,optional,omitempty"` // 描述
Status *int `json:"status,optional,omitempty"` // 状态 1:启用 2:禁用
CreatedAt int64 `json:"createdAt,optional,omitempty"` // 创建时间
UpdatedAt int64 `json:"updatedAt,optional,omitempty"` // 更新时间
FileNumber *int `json:"fileNumber,optional,omitempty"` // 文件数量
}
type ChatDatasetRenameRequest struct {
Id int64 `json:"id"` // 文档ID
Name string `json:"name"` // 新名称
}
type ChatDatasetRenameResponse struct {
ChatDataset ChatDatasetItem `json:"dataset"`
}
... ...
... ... @@ -15,3 +15,21 @@ CREATE TABLE `chat_model`
`id` int(0) NOT NULL COMMENT '唯一标识',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
CREATE TABLE `chat_dataset`
(
`id` int(0) NOT NULL COMMENT '唯一标识',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
CREATE TABLE `chat_document`
(
`id` int(0) NOT NULL COMMENT '唯一标识',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
CREATE TABLE `chat_dataset_document_mapping`
(
`id` int(0) NOT NULL COMMENT '唯一标识',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
... ...
syntax = "v1"
import "core/session.api"
import "core/document.api"
import "core/dataset.api"
\ No newline at end of file
... ...
syntax = "v1"
// 后台接口
@server(
prefix: v1
group: dataset
middleware: LogRequest
jwt: SystemAuth
)
service Core {
@doc "知识库-详情"
@handler chatDatasetGet
get /chat/dataset/:id (ChatDatasetGetRequest) returns (ChatDatasetGetResponse)
@doc "知识库-保存"
@handler chatDatasetSave
post /chat/dataset (ChatDatasetSaveRequest) returns (ChatDatasetSaveResponse)
@doc "知识库-删除"
@handler chatDatasetDelete
delete /chat/dataset/:id (ChatDatasetDeleteRequest) returns (ChatDatasetDeleteResponse)
@doc "知识库-更新"
@handler chatDatasetUpdate
put /chat/dataset/:id (ChatDatasetUpdateRequest) returns (ChatDatasetUpdateResponse)
@doc "知识库-重命名"
@handler chatDatasetRename
post /chat/dataset/rename (ChatDatasetRenameRequest) returns (ChatDatasetRenameResponse)
@doc "知识库-搜索"
@handler chatDatasetSearch
post /chat/dataset/search (ChatDatasetSearchRequest) returns (ChatDatasetSearchResponse)
}
type (
ChatDatasetGetRequest {
Id int64 `path:"id"`
}
ChatDatasetGetResponse {
ChatDataset ChatDatasetItem `json:"dataset"`
Documents []ChatDocumentItem `json:"documents"`
}
ChatDatasetSaveRequest {
Name string `json:"name"` // 名称
DocumentIds []int64 `json:"documentIds"` // 文档ID列表
}
ChatDatasetSaveResponse {
ChatDataset ChatDatasetItem `json:"dataset"`
}
ChatDatasetDeleteRequest {
Id int64 `path:"id"`
}
ChatDatasetDeleteResponse {}
ChatDatasetUpdateRequest {
Id int64 `path:"id"`
ChatDataset ChatDatasetItem `json:"dataset"`
}
ChatDatasetUpdateResponse {}
ChatDatasetSearchRequest {
Page int `json:"page,optional"`
Size int `json:"size,optional"`
Name string `json:"name,optional"` // 文件名匹配
Status int `json:"status,optional,option=0|1|2"`// 1:启用 2:禁用
}
ChatDatasetSearchResponse{
List []ChatDatasetItem `json:"list"`
Total int64 `json:"total"`
}
ChatDatasetItem {
Id int64 `json:"id,optional,omitempty"`
Name *string `json:"name,optional,omitempty"` // 名称
Desc *string `json:"desc,optional,omitempty"` // 描述
Status *int `json:"status,optional,omitempty"`// 状态 1:启用 2:禁用
CreatedAt int64 `json:"createdAt,optional,omitempty"` // 创建时间
UpdatedAt int64 `json:"updatedAt,optional,omitempty"` // 更新时间
FileNumber *int `json:"fileNumber,optional,omitempty"` // 文件数量
}
)
type(
ChatDatasetRenameRequest{
Id int64 `json:"id"` // 文档ID
Name string `json:"name"` // 新名称
}
ChatDatasetRenameResponse{
ChatDataset ChatDatasetItem `json:"dataset"`
}
)
\ No newline at end of file
... ...
syntax = "v1"
// 后台接口
@server(
prefix: v1
group: document
middleware: LogRequest
jwt: SystemAuth
)
service Core {
@doc "文档-详情"
@handler chatDocumentGet
get /chat/document/:id (ChatDocumentGetRequest) returns (ChatDocumentGetResponse)
@doc "文档-保存"
@handler chatDocumentSave
post /chat/document (ChatDocumentSaveRequest) returns (ChatDocumentSaveResponse)
@doc "文档-删除"
@handler chatDocumentDelete
delete /chat/document/:id (ChatDocumentDeleteRequest) returns (ChatDocumentDeleteResponse)
@doc "文档-删除"
@handler chatDocumentRename
post /chat/document/rename (ChatDocumentRenameRequest) returns (ChatDocumentRenameResponse)
// @doc "文档-更新"
// @handler chatDocumentUpdate
// put /chat/document/:id (ChatDocumentUpdateRequest) returns (ChatDocumentUpdateResponse)
@doc "文档-搜索"
@handler chatDocumentSearch
post /chat/document/search (ChatDocumentSearchRequest) returns (ChatDocumentSearchResponse)
}
type (
ChatDocumentGetRequest {
Id int64 `path:"id"`
}
ChatDocumentGetResponse {
ChatDocument ChatDocumentItem `json:"document"`
}
ChatDocumentSaveRequest {
FileName string `json:"fileName"`
Url string `json:"url"`
Size float64 `json:"size"` // 文件大小(KB)
}
ChatDocumentSaveResponse {
ChatDocument ChatDocumentItem `json:"document"`
}
ChatDocumentDeleteRequest {
Id int64 `path:"id"`
}
ChatDocumentDeleteResponse {}
ChatDocumentUpdateRequest {
Id int64 `path:"id"`
ChatDocument ChatDocumentItem `json:"document"`
}
ChatDocumentUpdateResponse {}
ChatDocumentSearchRequest {
Page int `json:"page,optional"`
Size int `json:"size,optional"`
Name string `json:"name,optional"` // 文件名匹配
FileType string `json:"fileType,optional"` // 文件类型 markdown\pdf\txt\doc&docx
Status int `json:"status,optional"` // 文件状态 1.使用中、0.待处理、2.预处理中、3.处理失败
}
ChatDocumentSearchResponse{
List []ChatDocumentItem `json:"list"`
Total int64 `json:"total"`
}
ChatDocumentItem {
Id int64 `json:"id"`
Name string `json:"name,optional"` // 文件名
FileType string `json:"fileType"` // 文件类型 markdown\pdf\txt\doc&docx
Status int `json:"status"` // 1.使用中、0.待处理、2.预处理中、3.处理失败
OriginFileName string `json:"originFileName,omitempty"` // 源文件命
Ext string `json:"ext,omitempty"` // 格式
FileId string `json:"fileId,omitempty"` // 星火文件ID
FileUrl string `json:"fileUrl,omitempty"` // 文件地址
FileSize float64 `json:"fileSize,omitempty"` // 文件大小KB
CreatedAt int64 `json:"createdAt,omitempty"` // 创建时间
UpdatedAt int64 `json:"updatedAt,omitempty"` // 更新时间
}
)
type(
ChatDocumentRenameRequest{
Id int64 `json:"id"` // 文档ID
Name string `json:"name"` // 新名称
}
ChatDocumentRenameResponse{
ChatDocument ChatDocumentItem `json:"document"`
}
)
\ No newline at end of file
... ...
... ... @@ -30,6 +30,13 @@ service Core {
@handler chatSessionConversationWs
get /chat/session/conversation (ChatSessionConversationRequestWs) returns (ChatSessionConversationResponse)
@doc "星火聊天会话-我的会话"
@handler chatMySparkSessions
post /chat/session/my_spark_sessions (ChatSessionSearchRequest) returns (ChatSessionSearchResponse)
@doc "星火聊天会话-保存"
@handler chatSparkSessionSave
post /chat/spark_session (ChatSessionSaveRequest) returns (ChatSessionSaveResponse)
@doc "聊天会话-对话记录列表"
@handler chatSessionRecords
post /chat/session/records (ChatSessionRecordsRequest) returns (ChatSessionRecordsResponse)
... ... @@ -51,7 +58,9 @@ type (
ChatSessionSaveRequest {
ChatSession ChatSessionItem `json:"session"`
}
ChatSessionSaveResponse {}
ChatSessionSaveResponse {
ChatSession ChatSessionItem `json:"session"`
}
ChatSessionDeleteRequest {
Id int64 `path:"id"`
... ... @@ -68,6 +77,7 @@ type (
Page int `json:"page,optional"`
Size int `json:"size,optional"`
Title string `json:"title,optional"` // 按标题搜索
Module int `json:"module,optional"` // 模块 1:openai chat 2:星火文档问答
}
ChatSessionSearchResponse{
List []ChatSessionItem `json:"list"`
... ... @@ -78,6 +88,11 @@ type (
Title string `json:"title,optional,omitempty"` // 会话标题
Abstract string `json:"abstract,optional,omitempty"` // 摘要
CreatedAt int64 `json:"createdAt,optional,omitempty"` // 创建时间
Module int `json:"module,optional,omitempty,default=1"` // 1:openai chat 2:星火文档问答
Type string `json:"type,optional,omitempty,default=chat"` // 类型 chat:普通问答 spark_dataset_chat:星火知识库问答 spark_documents_chat:星火多文档问答
DatasetId int64 `json:"datasetId,optional,omitempty"` // 知识库
DocumentIds []int64 `json:"documentIds,optional,omitempty"` // 多文档
}
)
... ...
... ... @@ -15,6 +15,294 @@
"application/json"
],
"paths": {
"v1/chat/dataset": {
"post": {
"summary": "知识库-保存",
"operationId": "chatDatasetSave",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatDatasetSaveResponse"
}
}
},
"parameters": [
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ChatDatasetSaveRequest"
}
}
],
"requestBody": {},
"tags": [
"dataset"
]
}
},
"v1/chat/dataset/rename": {
"post": {
"summary": "知识库-重命名",
"operationId": "chatDatasetRename",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatDatasetUpdateResponse"
}
}
},
"parameters": [
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ChatDatasetUpdateRequest"
}
}
],
"requestBody": {},
"tags": [
"dataset"
]
}
},
"v1/chat/dataset/search": {
"post": {
"summary": "知识库-搜索",
"operationId": "chatDatasetSearch",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatDatasetSearchResponse"
}
}
},
"parameters": [
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ChatDatasetSearchRequest"
}
}
],
"requestBody": {},
"tags": [
"dataset"
]
}
},
"v1/chat/dataset/{id}": {
"get": {
"summary": "知识库-详情",
"operationId": "chatDatasetGet",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatDatasetGetResponse"
}
}
},
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string"
}
],
"requestBody": {},
"tags": [
"dataset"
]
},
"delete": {
"summary": "知识库-删除",
"operationId": "chatDatasetDelete",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatDatasetDeleteResponse"
}
}
},
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ChatDatasetDeleteRequest"
}
}
],
"requestBody": {},
"tags": [
"dataset"
]
},
"put": {
"summary": "知识库-更新",
"operationId": "chatDatasetUpdate",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatDatasetUpdateResponse"
}
}
},
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ChatDatasetUpdateRequest"
}
}
],
"requestBody": {},
"tags": [
"dataset"
]
}
},
"v1/chat/document": {
"post": {
"summary": "文档-保存",
"operationId": "chatDocumentSave",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatDocumentSaveResponse"
}
}
},
"parameters": [
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ChatDocumentSaveRequest"
}
}
],
"requestBody": {},
"tags": [
"document"
]
}
},
"v1/chat/document/search": {
"post": {
"summary": "文档-搜索",
"operationId": "chatDocumentSearch",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatDocumentSearchResponse"
}
}
},
"parameters": [
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ChatDocumentSearchRequest"
}
}
],
"requestBody": {},
"tags": [
"document"
]
}
},
"v1/chat/document/{id}": {
"get": {
"summary": "文档-详情",
"operationId": "chatDocumentGet",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatDocumentGetResponse"
}
}
},
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string"
}
],
"requestBody": {},
"tags": [
"document"
]
},
"delete": {
"summary": "文档-删除",
"operationId": "chatDocumentDelete",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatDocumentDeleteResponse"
}
}
},
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ChatDocumentDeleteRequest"
}
}
],
"requestBody": {},
"tags": [
"document"
]
}
},
"v1/chat/models": {
"get": {
"summary": "模型列表",
... ... @@ -64,7 +352,7 @@
"v1/chat/session/conversation": {
"get": {
"summary": "聊天会话-对话",
"operationId": "chatSessionConversation",
"operationId": "chatSessionConversationWs",
"responses": {
"200": {
"description": "A successful response.",
... ... @@ -90,195 +378,662 @@
"type": "integer",
"format": "int64"
},
{
"name": "contentType",
"description": " 内容类型 文本:text (图片:image 文档:document)",
"in": "query",
"required": true,
{
"name": "contentType",
"description": " 内容类型 文本:text (图片:image 文档:document)",
"in": "query",
"required": true,
"type": "string"
},
{
"name": "text",
"description": " 内容文本",
"in": "query",
"required": true,
"type": "string"
}
],
"requestBody": {},
"tags": [
"chat"
]
},
"post": {
"summary": "聊天会话-对话",
"operationId": "chatSessionConversation",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatSessionConversationResponse"
}
}
},
"parameters": [
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ChatSessionConversationRequest"
}
}
],
"requestBody": {},
"tags": [
"chat"
]
}
},
"v1/chat/session/my_spark_sessions": {
"post": {
"summary": "星火聊天会话-我的会话",
"operationId": "chatMySparkSessions",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatSessionSearchResponse"
}
}
},
"parameters": [
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ChatSessionSearchRequest"
}
}
],
"requestBody": {},
"tags": [
"chat"
]
}
},
"v1/chat/session/records": {
"post": {
"summary": "聊天会话-对话记录列表",
"operationId": "chatSessionRecords",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatSessionRecordsResponse"
}
}
},
"parameters": [
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ChatSessionRecordsRequest"
}
}
],
"requestBody": {},
"tags": [
"chat"
]
}
},
"v1/chat/session/search": {
"post": {
"summary": "聊天会话-搜索",
"operationId": "chatSessionSearch",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatSessionSearchResponse"
}
}
},
"parameters": [
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ChatSessionSearchRequest"
}
}
],
"requestBody": {},
"tags": [
"chat"
]
}
},
"v1/chat/session/{id}": {
"get": {
"summary": "聊天会话-详情",
"operationId": "chatSessionGet",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatSessionGetResponse"
}
}
},
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string"
}
],
"requestBody": {},
"tags": [
"chat"
]
},
"delete": {
"summary": "聊天会话-删除",
"operationId": "chatSessionDelete",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatSessionDeleteResponse"
}
}
},
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ChatSessionDeleteRequest"
}
}
],
"requestBody": {},
"tags": [
"chat"
]
},
"put": {
"summary": "聊天会话-更新",
"operationId": "chatSessionUpdate",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatSessionUpdateResponse"
}
}
},
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ChatSessionUpdateRequest"
}
}
],
"requestBody": {},
"tags": [
"chat"
]
}
},
"v1/chat/spark_session": {
"post": {
"summary": "星火聊天会话-保存",
"operationId": "chatSparkSessionSave",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatSessionSaveResponse"
}
}
},
"parameters": [
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ChatSessionSaveRequest"
}
}
],
"requestBody": {},
"tags": [
"chat"
]
}
}
},
"definitions": {
"ChatDatasetDeleteRequest": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
}
},
"title": "ChatDatasetDeleteRequest",
"required": [
"id"
]
},
"ChatDatasetDeleteResponse": {
"type": "object",
"title": "ChatDatasetDeleteResponse"
},
"ChatDatasetGetRequest": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
}
},
"title": "ChatDatasetGetRequest",
"required": [
"id"
]
},
"ChatDatasetGetResponse": {
"type": "object",
"properties": {
"dataset": {
"$ref": "#/definitions/ChatDatasetItem"
},
"documents": {
"type": "array",
"items": {
"$ref": "#/definitions/ChatDocumentItem"
}
}
},
"title": "ChatDatasetGetResponse",
"required": [
"dataset",
"documents"
]
},
"ChatDatasetItem": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"$ref": "#/definitions/string",
"description": " 名称"
},
"desc": {
"$ref": "#/definitions/string",
"description": " 描述"
},
"status": {
"$ref": "#/definitions/int",
"description": " 状态 1:启用 2:禁用"
},
"createdAt": {
"type": "integer",
"format": "int64",
"description": " 创建时间"
},
"updatedAt": {
"type": "integer",
"format": "int64",
"description": " 更新时间"
},
"fileNumber": {
"$ref": "#/definitions/int",
"description": " 文件数量"
}
},
"title": "ChatDatasetItem",
"required": [
"id",
"name",
"desc",
"status",
"createdAt",
"updatedAt",
"fileNumber"
]
},
"ChatDatasetSaveRequest": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": " 名称"
},
"documentIds": {
"type": "array",
"items": {
"type": "integer",
"format": "int64"
},
"description": " 文档ID列表"
}
},
"title": "ChatDatasetSaveRequest",
"required": [
"name",
"documentIds"
]
},
"ChatDatasetSaveResponse": {
"type": "object",
"properties": {
"dataset": {
"$ref": "#/definitions/ChatDatasetItem"
}
},
"title": "ChatDatasetSaveResponse",
"required": [
"dataset"
]
},
"ChatDatasetSearchRequest": {
"type": "object",
"properties": {
"page": {
"type": "integer",
"format": "int32"
},
"size": {
"type": "integer",
"format": "int32"
},
"name": {
"type": "string",
"description": " 文件名匹配"
},
"name": {
"type": "integer",
"format": "int32",
"description": " 1:启用 2:禁用"
}
},
"title": "ChatDatasetSearchRequest",
"required": [
"name"
]
},
"ChatDatasetSearchResponse": {
"type": "object",
"properties": {
"list": {
"type": "array",
"items": {
"$ref": "#/definitions/ChatDatasetItem"
}
},
"total": {
"type": "integer",
"format": "int64"
}
},
"title": "ChatDatasetSearchResponse",
"required": [
"list",
"total"
]
},
"ChatDatasetUpdateRequest": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"dataset": {
"$ref": "#/definitions/ChatDatasetItem"
}
},
"title": "ChatDatasetUpdateRequest",
"required": [
"id",
"dataset"
]
},
"ChatDatasetUpdateResponse": {
"type": "object",
"title": "ChatDatasetUpdateResponse"
},
"ChatDocumentDeleteRequest": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
}
},
"title": "ChatDocumentDeleteRequest",
"required": [
"id"
]
},
"ChatDocumentDeleteResponse": {
"type": "object",
"title": "ChatDocumentDeleteResponse"
},
"ChatDocumentGetRequest": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
}
},
"title": "ChatDocumentGetRequest",
"required": [
"id"
]
},
"ChatDocumentGetResponse": {
"type": "object",
"properties": {
"document": {
"$ref": "#/definitions/ChatDocumentItem"
}
},
"title": "ChatDocumentGetResponse",
"required": [
"document"
]
},
"ChatDocumentItem": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string",
"description": " 文件名"
},
"fileType": {
"type": "string",
"description": " 文件类型 markdown\\pdf\\txt\\doc\u0026docx"
},
"status": {
"type": "integer",
"format": "int32",
"description": " 1.使用中、0.待处理、2.预处理中、3.处理失败"
},
"originFileName": {
"type": "string",
"description": " 源文件命"
},
"ext": {
"type": "string",
"description": " 格式"
},
"fileId": {
"type": "string",
"description": " 星火文件ID"
},
"fileUrl": {
"type": "string",
"description": " 文件地址"
},
"fileSize": {
"type": "number",
"format": "double",
"description": " 文件大小KB"
},
"createdAt": {
"type": "integer",
"format": "int64",
"description": " 创建时间"
},
"updatedAt": {
"type": "integer",
"format": "int64",
"description": " 更新时间"
}
},
"title": "ChatDocumentItem",
"required": [
"id",
"fileType",
"status",
"originFileName",
"ext",
"fileId",
"fileUrl",
"fileSize",
"createdAt",
"updatedAt"
]
},
"ChatDocumentSaveRequest": {
"type": "object",
"properties": {
"fileName": {
"type": "string"
},
{
"name": "text",
"description": " 内容文本",
"in": "query",
"required": true,
"url": {
"type": "string"
},
{
"name": "fileUrl",
"description": " 文件地址",
"in": "query",
"required": true,
"type": "string"
"size": {
"type": "number",
"format": "double",
"description": " 文件大小(KB)"
}
],
"requestBody": {},
"tags": [
"chat"
},
"title": "ChatDocumentSaveRequest",
"required": [
"fileName",
"url",
"size"
]
}
},
"v1/chat/session/records": {
"get": {
"summary": "聊天会话-对话记录列表",
"operationId": "chatSessionRecords",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatSessionRecordsResponse"
}
"ChatDocumentSaveResponse": {
"type": "object",
"properties": {
"document": {
"$ref": "#/definitions/ChatDocumentItem"
}
},
"parameters": [
{
"name": "page",
"in": "query",
"required": false,
"title": "ChatDocumentSaveResponse",
"required": [
"document"
]
},
"ChatDocumentSearchRequest": {
"type": "object",
"properties": {
"page": {
"type": "integer",
"format": "int32"
},
{
"name": "size",
"in": "query",
"required": false,
"size": {
"type": "integer",
"format": "int32"
},
{
"name": "sessionId",
"in": "query",
"required": true,
"type": "integer",
"format": "int64"
}
],
"requestBody": {},
"tags": [
"chat"
]
}
"name": {
"type": "string",
"description": " 文件名匹配"
},
"v1/chat/session/search": {
"post": {
"summary": "聊天会话-搜索",
"operationId": "chatSessionSearch",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatSessionSearchResponse"
}
}
"fileType": {
"type": "string",
"description": " 文件类型 markdown\\pdf\\txt\\doc\u0026docx"
},
"parameters": [
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ChatSessionSearchRequest"
}
}
],
"requestBody": {},
"tags": [
"chat"
]
"status": {
"type": "integer",
"format": "int32",
"description": " 文件状态 1.使用中、0.待处理、2.预处理中、3.处理失败"
}
},
"v1/chat/session/{id}": {
"get": {
"summary": "聊天会话-详情",
"operationId": "chatSessionGet",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatSessionGetResponse"
}
}
"title": "ChatDocumentSearchRequest"
},
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string"
"ChatDocumentSearchResponse": {
"type": "object",
"properties": {
"list": {
"type": "array",
"items": {
"$ref": "#/definitions/ChatDocumentItem"
}
],
"requestBody": {},
"tags": [
"chat"
]
},
"delete": {
"summary": "聊天会话-删除",
"operationId": "chatSessionDelete",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatSessionDeleteResponse"
}
"total": {
"type": "integer",
"format": "int64"
}
},
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ChatSessionDeleteRequest"
}
}
],
"requestBody": {},
"tags": [
"chat"
"title": "ChatDocumentSearchResponse",
"required": [
"list",
"total"
]
},
"put": {
"summary": "聊天会话-更新",
"operationId": "chatSessionUpdate",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/ChatSessionUpdateResponse"
}
}
},
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string"
"ChatDocumentUpdateRequest": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/ChatSessionUpdateRequest"
"document": {
"$ref": "#/definitions/ChatDocumentItem"
}
}
],
"requestBody": {},
"tags": [
"chat"
},
"title": "ChatDocumentUpdateRequest",
"required": [
"id",
"document"
]
}
}
},
"definitions": {
"ChatDocumentUpdateResponse": {
"type": "object",
"title": "ChatDocumentUpdateResponse"
},
"ChatModelsRequest": {
"type": "object",
"title": "ChatModelsRequest"
... ... @@ -329,13 +1084,62 @@
"sessionId",
"modelId",
"contentType",
"text",
"fileUrl"
"text"
]
},
"ChatSessionConversationRequestWs": {
"type": "object",
"properties": {
"sessionId": {
"type": "integer",
"format": "int64",
"description": " 会话ID"
},
"modelId": {
"type": "integer",
"format": "int64",
"description": " 模型ID"
},
"contentType": {
"type": "string",
"description": " 内容类型 文本:text (图片:image 文档:document)"
},
"text": {
"type": "string",
"description": " 内容文本"
}
},
"title": "ChatSessionConversationRequestWs",
"required": [
"sessionId",
"modelId",
"contentType",
"text"
]
},
"ChatSessionConversationResponse": {
"type": "object",
"title": "ChatSessionConversationResponse"
"properties": {
"record": {
"$ref": "#/definitions/Record"
},
"parts": {
"type": "array",
"items": {
"type": "string"
}
},
"finished": {
"type": "boolean",
"format": "boolean"
}
},
"title": "ChatSessionConversationResponse",
"required": [
"record",
"parts",
"finished"
]
},
"ChatSessionDeleteRequest": {
"type": "object",
... ... @@ -406,6 +1210,30 @@
"type": "integer",
"format": "int64",
"description": " 创建时间"
},
"module": {
"type": "integer",
"format": "int32",
"default": "1",
"description": " 1:openai chat 2:星火文档问答"
},
"type": {
"type": "string",
"default": "chat",
"description": " 类型 chat:普通问答 spark_dataset_chat:星火知识库问答 spark_documents_chat:星火多文档问答"
},
"datasetId": {
"type": "integer",
"format": "int64",
"description": " 知识库"
},
"documentIds": {
"type": "array",
"items": {
"type": "integer",
"format": "int64"
},
"description": " 多文档"
}
},
"title": "ChatSessionItem",
... ... @@ -413,7 +1241,11 @@
"id",
"title",
"abstract",
"createdAt"
"createdAt",
"module",
"type",
"datasetId",
"documentIds"
]
},
"ChatSessionRecordsRequest": {
... ... @@ -471,7 +1303,15 @@
},
"ChatSessionSaveResponse": {
"type": "object",
"title": "ChatSessionSaveResponse"
"properties": {
"session": {
"$ref": "#/definitions/ChatSessionItem"
}
},
"title": "ChatSessionSaveResponse",
"required": [
"session"
]
},
"ChatSessionSearchRequest": {
"type": "object",
... ... @@ -487,6 +1327,11 @@
"title": {
"type": "string",
"description": " 按标题搜索"
},
"module": {
"type": "integer",
"format": "int32",
"description": " 模块 1:openai chat 2:星火文档问答"
}
},
"title": "ChatSessionSearchRequest"
... ...
syntax = "v1"
info(
title: "xx实例"
desc: "xx实例"
author: "author"
email: "email"
version: "v1"
)
@server(
prefix: v1
group: chat_dataset
jwt: JwtAuth
)
service Core {
@doc "详情"
@handler chat_datasetGet
get /chat_dataset/:id (ChatDatasetGetRequest) returns (ChatDatasetGetResponse)
@doc "保存"
@handler chat_datasetSave
post /chat_dataset (ChatDatasetSaveRequest) returns (ChatDatasetSaveResponse)
@doc "删除"
@handler chat_datasetDelete
delete /chat_dataset/:id (ChatDatasetDeleteRequest) returns (ChatDatasetDeleteResponse)
@doc "更新"
@handler chat_datasetUpdate
put /chat_dataset/:id (ChatDatasetUpdateRequest) returns (ChatDatasetUpdateResponse)
@doc "搜索"
@handler chat_datasetSearch
post /chat_dataset/search (ChatDatasetSearchRequest) returns (ChatDatasetSearchResponse)
}
type (
ChatDatasetGetRequest {
Id int64 `path:"id"`
}
ChatDatasetGetResponse {
ChatDataset ChatDatasetItem `json:"chat_dataset"`
}
ChatDatasetSaveRequest {
ChatDataset ChatDatasetItem `json:"chat_dataset"`
}
ChatDatasetSaveResponse {}
ChatDatasetDeleteRequest {
Id int64 `path:"id"`
}
ChatDatasetDeleteResponse {}
ChatDatasetUpdateRequest {
Id int64 `path:"id"`
ChatDataset ChatDatasetItem `json:"chat_dataset"`
}
ChatDatasetUpdateResponse {}
ChatDatasetSearchRequest {
Page int `json:"page"`
Size int `json:"size"`
}
ChatDatasetSearchResponse{
List []ChatDatasetItem `json:"list"`
Total int64 `json:"total"`
}
ChatDatasetItem {
}
)
// logic CRUD
// Save
//var (
// conn = l.svcCtx.DefaultDBConn()
// dm *domain.ChatDataset
//)
//// 唯一判断
//dm = NewDomainChatDataset(req.ChatDataset)
//if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
// dm, err = l.svcCtx.ChatDatasetRepository.Insert(l.ctx, conn, dm)
// return err
//}, true); err != nil {
// return nil, xerr.NewErrMsg("保存失败")
//}
////resp = &types.ChatDatasetSaveResponse{}
//return
//func NewDomainChatDataset(item types.ChatDatasetItem) *domain.ChatDataset {
// return &domain.ChatDataset{
// }
//}
//
//func NewTypesChatDataset(item *domain.ChatDataset) types.ChatDatasetItem {
// return types.ChatDatasetItem{
// Id: item.Id,
// }
//}
// Get
//var (
// conn = l.svcCtx.DefaultDBConn()
// dm *domain.ChatDataset
//)
//// 货号唯一
//if dm, err = l.svcCtx.ChatDatasetRepository.FindOne(l.ctx, conn, req.Id); err != nil {
// return nil, xerr.NewErrMsgErr("不存在", err)
//}
//resp = &types.ChatDatasetGetResponse{
// ChatDataset: NewTypesChatDataset(dm),
//}
//return
// Delete
//var (
// conn = l.svcCtx.DefaultDBConn()
// dm *domain.ChatDataset
//)
//if dm, err = l.svcCtx.ChatDatasetRepository.FindOne(l.ctx, conn, req.Id); err != nil {
// return nil, xerr.NewErrMsgErr("不存在", err)
//}
//if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
// if dm, err = l.svcCtx.ChatDatasetRepository.Delete(l.ctx, conn, dm); err != nil {
// return err
// }
// return nil
//}, true); err != nil {
// return nil, xerr.NewErrMsgErr("移除失败", err)
//}
//return
// Search
//var (
// conn = l.svcCtx.DefaultDBConn()
// dms []*domain.ChatDataset
// total int64
//)
//
//queryOptions := domain.NewQueryOptions().WithOffsetLimit(req.Page, req.Size).
// WithKV("", "")
//total, dms, err = l.svcCtx.ChatDatasetRepository.Find(l.ctx, conn, queryOptions)
//list := make([]types.ChatDatasetItem, 0)
//for i := range dms {
// list = append(list, NewTypesChatDataset(dms[i]))
//}
//resp = &types.ChatDatasetSearchResponse{
// List: list,
// Total: total,
//}
//return
// Update
//var (
// conn = l.svcCtx.DefaultDBConn()
// dm *domain.ChatDataset
//)
//if dm, err = l.svcCtx.ChatDatasetRepository.FindOne(l.ctx, conn, req.Id); err != nil {
// return nil, xerr.NewErrMsgErr("不存在", err)
//}
//// 不可编辑判断
//// 赋值
//// 更新
//if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
// dm, err = l.svcCtx.ChatDatasetRepository.UpdateWithVersion(l.ctx, conn, dm)
// return err
//}, true); err != nil {
// return nil, xerr.NewErrMsg("更新失败")
//}
//resp = &types.ChatDatasetUpdateResponse{}
//return
... ...
syntax = "v1"
info(
title: "xx实例"
desc: "xx实例"
author: "author"
email: "email"
version: "v1"
)
@server(
prefix: v1
group: chat_dataset_document_mapping
jwt: JwtAuth
)
service Core {
@doc "详情"
@handler chat_dataset_document_mappingGet
get /chat_dataset_document_mapping/:id (ChatDatasetDocumentMappingGetRequest) returns (ChatDatasetDocumentMappingGetResponse)
@doc "保存"
@handler chat_dataset_document_mappingSave
post /chat_dataset_document_mapping (ChatDatasetDocumentMappingSaveRequest) returns (ChatDatasetDocumentMappingSaveResponse)
@doc "删除"
@handler chat_dataset_document_mappingDelete
delete /chat_dataset_document_mapping/:id (ChatDatasetDocumentMappingDeleteRequest) returns (ChatDatasetDocumentMappingDeleteResponse)
@doc "更新"
@handler chat_dataset_document_mappingUpdate
put /chat_dataset_document_mapping/:id (ChatDatasetDocumentMappingUpdateRequest) returns (ChatDatasetDocumentMappingUpdateResponse)
@doc "搜索"
@handler chat_dataset_document_mappingSearch
post /chat_dataset_document_mapping/search (ChatDatasetDocumentMappingSearchRequest) returns (ChatDatasetDocumentMappingSearchResponse)
}
type (
ChatDatasetDocumentMappingGetRequest {
Id int64 `path:"id"`
}
ChatDatasetDocumentMappingGetResponse {
ChatDatasetDocumentMapping ChatDatasetDocumentMappingItem `json:"chat_dataset_document_mapping"`
}
ChatDatasetDocumentMappingSaveRequest {
ChatDatasetDocumentMapping ChatDatasetDocumentMappingItem `json:"chat_dataset_document_mapping"`
}
ChatDatasetDocumentMappingSaveResponse {}
ChatDatasetDocumentMappingDeleteRequest {
Id int64 `path:"id"`
}
ChatDatasetDocumentMappingDeleteResponse {}
ChatDatasetDocumentMappingUpdateRequest {
Id int64 `path:"id"`
ChatDatasetDocumentMapping ChatDatasetDocumentMappingItem `json:"chat_dataset_document_mapping"`
}
ChatDatasetDocumentMappingUpdateResponse {}
ChatDatasetDocumentMappingSearchRequest {
Page int `json:"page"`
Size int `json:"size"`
}
ChatDatasetDocumentMappingSearchResponse{
List []ChatDatasetDocumentMappingItem `json:"list"`
Total int64 `json:"total"`
}
ChatDatasetDocumentMappingItem {
}
)
// logic CRUD
// Save
//var (
// conn = l.svcCtx.DefaultDBConn()
// dm *domain.ChatDatasetDocumentMapping
//)
//// 唯一判断
//dm = NewDomainChatDatasetDocumentMapping(req.ChatDatasetDocumentMapping)
//if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
// dm, err = l.svcCtx.ChatDatasetDocumentMappingRepository.Insert(l.ctx, conn, dm)
// return err
//}, true); err != nil {
// return nil, xerr.NewErrMsg("保存失败")
//}
////resp = &types.ChatDatasetDocumentMappingSaveResponse{}
//return
//func NewDomainChatDatasetDocumentMapping(item types.ChatDatasetDocumentMappingItem) *domain.ChatDatasetDocumentMapping {
// return &domain.ChatDatasetDocumentMapping{
// }
//}
//
//func NewTypesChatDatasetDocumentMapping(item *domain.ChatDatasetDocumentMapping) types.ChatDatasetDocumentMappingItem {
// return types.ChatDatasetDocumentMappingItem{
// Id: item.Id,
// }
//}
// Get
//var (
// conn = l.svcCtx.DefaultDBConn()
// dm *domain.ChatDatasetDocumentMapping
//)
//// 货号唯一
//if dm, err = l.svcCtx.ChatDatasetDocumentMappingRepository.FindOne(l.ctx, conn, req.Id); err != nil {
// return nil, xerr.NewErrMsgErr("不存在", err)
//}
//resp = &types.ChatDatasetDocumentMappingGetResponse{
// ChatDatasetDocumentMapping: NewTypesChatDatasetDocumentMapping(dm),
//}
//return
// Delete
//var (
// conn = l.svcCtx.DefaultDBConn()
// dm *domain.ChatDatasetDocumentMapping
//)
//if dm, err = l.svcCtx.ChatDatasetDocumentMappingRepository.FindOne(l.ctx, conn, req.Id); err != nil {
// return nil, xerr.NewErrMsgErr("不存在", err)
//}
//if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
// if dm, err = l.svcCtx.ChatDatasetDocumentMappingRepository.Delete(l.ctx, conn, dm); err != nil {
// return err
// }
// return nil
//}, true); err != nil {
// return nil, xerr.NewErrMsgErr("移除失败", err)
//}
//return
// Search
//var (
// conn = l.svcCtx.DefaultDBConn()
// dms []*domain.ChatDatasetDocumentMapping
// total int64
//)
//
//queryOptions := domain.NewQueryOptions().WithOffsetLimit(req.Page, req.Size).
// WithKV("", "")
//total, dms, err = l.svcCtx.ChatDatasetDocumentMappingRepository.Find(l.ctx, conn, queryOptions)
//list := make([]types.ChatDatasetDocumentMappingItem, 0)
//for i := range dms {
// list = append(list, NewTypesChatDatasetDocumentMapping(dms[i]))
//}
//resp = &types.ChatDatasetDocumentMappingSearchResponse{
// List: list,
// Total: total,
//}
//return
// Update
//var (
// conn = l.svcCtx.DefaultDBConn()
// dm *domain.ChatDatasetDocumentMapping
//)
//if dm, err = l.svcCtx.ChatDatasetDocumentMappingRepository.FindOne(l.ctx, conn, req.Id); err != nil {
// return nil, xerr.NewErrMsgErr("不存在", err)
//}
//// 不可编辑判断
//// 赋值
//// 更新
//if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
// dm, err = l.svcCtx.ChatDatasetDocumentMappingRepository.UpdateWithVersion(l.ctx, conn, dm)
// return err
//}, true); err != nil {
// return nil, xerr.NewErrMsg("更新失败")
//}
//resp = &types.ChatDatasetDocumentMappingUpdateResponse{}
//return
... ...
syntax = "v1"
info(
title: "xx实例"
desc: "xx实例"
author: "author"
email: "email"
version: "v1"
)
@server(
prefix: v1
group: chat_document
jwt: JwtAuth
)
service Core {
@doc "详情"
@handler chat_documentGet
get /chat_document/:id (ChatDocumentGetRequest) returns (ChatDocumentGetResponse)
@doc "保存"
@handler chat_documentSave
post /chat_document (ChatDocumentSaveRequest) returns (ChatDocumentSaveResponse)
@doc "删除"
@handler chat_documentDelete
delete /chat_document/:id (ChatDocumentDeleteRequest) returns (ChatDocumentDeleteResponse)
@doc "更新"
@handler chat_documentUpdate
put /chat_document/:id (ChatDocumentUpdateRequest) returns (ChatDocumentUpdateResponse)
@doc "搜索"
@handler chat_documentSearch
post /chat_document/search (ChatDocumentSearchRequest) returns (ChatDocumentSearchResponse)
}
type (
ChatDocumentGetRequest {
Id int64 `path:"id"`
}
ChatDocumentGetResponse {
ChatDocument ChatDocumentItem `json:"chat_document"`
}
ChatDocumentSaveRequest {
ChatDocument ChatDocumentItem `json:"chat_document"`
}
ChatDocumentSaveResponse {}
ChatDocumentDeleteRequest {
Id int64 `path:"id"`
}
ChatDocumentDeleteResponse {}
ChatDocumentUpdateRequest {
Id int64 `path:"id"`
ChatDocument ChatDocumentItem `json:"chat_document"`
}
ChatDocumentUpdateResponse {}
ChatDocumentSearchRequest {
Page int `json:"page"`
Size int `json:"size"`
}
ChatDocumentSearchResponse{
List []ChatDocumentItem `json:"list"`
Total int64 `json:"total"`
}
ChatDocumentItem {
}
)
// logic CRUD
// Save
//var (
// conn = l.svcCtx.DefaultDBConn()
// dm *domain.ChatDocument
//)
//// 唯一判断
//dm = NewDomainChatDocument(req.ChatDocument)
//if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
// dm, err = l.svcCtx.ChatDocumentRepository.Insert(l.ctx, conn, dm)
// return err
//}, true); err != nil {
// return nil, xerr.NewErrMsg("保存失败")
//}
////resp = &types.ChatDocumentSaveResponse{}
//return
//func NewDomainChatDocument(item types.ChatDocumentItem) *domain.ChatDocument {
// return &domain.ChatDocument{
// }
//}
//
//func NewTypesChatDocument(item *domain.ChatDocument) types.ChatDocumentItem {
// return types.ChatDocumentItem{
// Id: item.Id,
// }
//}
// Get
//var (
// conn = l.svcCtx.DefaultDBConn()
// dm *domain.ChatDocument
//)
//// 货号唯一
//if dm, err = l.svcCtx.ChatDocumentRepository.FindOne(l.ctx, conn, req.Id); err != nil {
// return nil, xerr.NewErrMsgErr("不存在", err)
//}
//resp = &types.ChatDocumentGetResponse{
// ChatDocument: NewTypesChatDocument(dm),
//}
//return
// Delete
//var (
// conn = l.svcCtx.DefaultDBConn()
// dm *domain.ChatDocument
//)
//if dm, err = l.svcCtx.ChatDocumentRepository.FindOne(l.ctx, conn, req.Id); err != nil {
// return nil, xerr.NewErrMsgErr("不存在", err)
//}
//if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
// if dm, err = l.svcCtx.ChatDocumentRepository.Delete(l.ctx, conn, dm); err != nil {
// return err
// }
// return nil
//}, true); err != nil {
// return nil, xerr.NewErrMsgErr("移除失败", err)
//}
//return
// Search
//var (
// conn = l.svcCtx.DefaultDBConn()
// dms []*domain.ChatDocument
// total int64
//)
//
//queryOptions := domain.NewQueryOptions().WithOffsetLimit(req.Page, req.Size).
// WithKV("", "")
//total, dms, err = l.svcCtx.ChatDocumentRepository.Find(l.ctx, conn, queryOptions)
//list := make([]types.ChatDocumentItem, 0)
//for i := range dms {
// list = append(list, NewTypesChatDocument(dms[i]))
//}
//resp = &types.ChatDocumentSearchResponse{
// List: list,
// Total: total,
//}
//return
// Update
//var (
// conn = l.svcCtx.DefaultDBConn()
// dm *domain.ChatDocument
//)
//if dm, err = l.svcCtx.ChatDocumentRepository.FindOne(l.ctx, conn, req.Id); err != nil {
// return nil, xerr.NewErrMsgErr("不存在", err)
//}
//// 不可编辑判断
//// 赋值
//// 更新
//if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
// dm, err = l.svcCtx.ChatDocumentRepository.UpdateWithVersion(l.ctx, conn, dm)
// return err
//}, true); err != nil {
// return nil, xerr.NewErrMsg("更新失败")
//}
//resp = &types.ChatDocumentUpdateResponse{}
//return
... ...
syntax = "proto3";
option go_package ="./pb";
package pb;
message ChatDatasetGetReq {
int64 Id = 1;
}
message ChatDatasetGetResp{
ChatDatasetItem ChatDataset = 1;
}
message ChatDatasetSaveReq {
}
message ChatDatasetSaveResp{
}
message ChatDatasetDeleteReq {
int64 Id = 1;
}
message ChatDatasetDeleteResp{
}
message ChatDatasetUpdateReq {
int64 Id = 1;
}
message ChatDatasetUpdateResp{
}
message ChatDatasetSearchReq {
int64 PageNumber = 1;
int64 PageSize = 2;
}
message ChatDatasetSearchResp{
repeated ChatDatasetItem List =1;
int64 Total =2;
}
message ChatDatasetItem {
}
service ChatDatasetService {
rpc ChatDatasetGet(ChatDatasetGetReq) returns(ChatDatasetGetResp);
rpc ChatDatasetSave(ChatDatasetSaveReq) returns(ChatDatasetSaveResp);
rpc ChatDatasetDelete(ChatDatasetDeleteReq) returns(ChatDatasetDeleteResp);
rpc ChatDatasetUpdate(ChatDatasetUpdateReq) returns(ChatDatasetUpdateResp);
rpc ChatDatasetSearch(ChatDatasetSearchReq) returns(ChatDatasetSearchResp);
}
... ...
syntax = "proto3";
option go_package ="./pb";
package pb;
message ChatDatasetDocumentMappingGetReq {
int64 Id = 1;
}
message ChatDatasetDocumentMappingGetResp{
ChatDatasetDocumentMappingItem ChatDatasetDocumentMapping = 1;
}
message ChatDatasetDocumentMappingSaveReq {
}
message ChatDatasetDocumentMappingSaveResp{
}
message ChatDatasetDocumentMappingDeleteReq {
int64 Id = 1;
}
message ChatDatasetDocumentMappingDeleteResp{
}
message ChatDatasetDocumentMappingUpdateReq {
int64 Id = 1;
}
message ChatDatasetDocumentMappingUpdateResp{
}
message ChatDatasetDocumentMappingSearchReq {
int64 PageNumber = 1;
int64 PageSize = 2;
}
message ChatDatasetDocumentMappingSearchResp{
repeated ChatDatasetDocumentMappingItem List =1;
int64 Total =2;
}
message ChatDatasetDocumentMappingItem {
}
service ChatDatasetDocumentMappingService {
rpc ChatDatasetDocumentMappingGet(ChatDatasetDocumentMappingGetReq) returns(ChatDatasetDocumentMappingGetResp);
rpc ChatDatasetDocumentMappingSave(ChatDatasetDocumentMappingSaveReq) returns(ChatDatasetDocumentMappingSaveResp);
rpc ChatDatasetDocumentMappingDelete(ChatDatasetDocumentMappingDeleteReq) returns(ChatDatasetDocumentMappingDeleteResp);
rpc ChatDatasetDocumentMappingUpdate(ChatDatasetDocumentMappingUpdateReq) returns(ChatDatasetDocumentMappingUpdateResp);
rpc ChatDatasetDocumentMappingSearch(ChatDatasetDocumentMappingSearchReq) returns(ChatDatasetDocumentMappingSearchResp);
}
... ...
syntax = "proto3";
option go_package ="./pb";
package pb;
message ChatDocumentGetReq {
int64 Id = 1;
}
message ChatDocumentGetResp{
ChatDocumentItem ChatDocument = 1;
}
message ChatDocumentSaveReq {
}
message ChatDocumentSaveResp{
}
message ChatDocumentDeleteReq {
int64 Id = 1;
}
message ChatDocumentDeleteResp{
}
message ChatDocumentUpdateReq {
int64 Id = 1;
}
message ChatDocumentUpdateResp{
}
message ChatDocumentSearchReq {
int64 PageNumber = 1;
int64 PageSize = 2;
}
message ChatDocumentSearchResp{
repeated ChatDocumentItem List =1;
int64 Total =2;
}
message ChatDocumentItem {
}
service ChatDocumentService {
rpc ChatDocumentGet(ChatDocumentGetReq) returns(ChatDocumentGetResp);
rpc ChatDocumentSave(ChatDocumentSaveReq) returns(ChatDocumentSaveResp);
rpc ChatDocumentDelete(ChatDocumentDeleteReq) returns(ChatDocumentDeleteResp);
rpc ChatDocumentUpdate(ChatDocumentUpdateReq) returns(ChatDocumentUpdateResp);
rpc ChatDocumentSearch(ChatDocumentSearchReq) returns(ChatDocumentSearchResp);
}
... ...
... ... @@ -9,5 +9,8 @@ func Migrate(db *gorm.DB) {
db.AutoMigrate(
&models.ChatSession{},
&models.ChatSessionRecord{},
&models.ChatDocument{},
&models.ChatDatasetDocumentMapping{},
&models.ChatDataset{},
)
}
... ...
package models
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gorm.io/gorm"
"gorm.io/plugin/soft_delete"
)
type ChatDataset struct {
Id int64 // 唯一标识
CompanyId int64
UserId int64
Name string // 知识库名称
Desc string // 描述
Status int // 1:启用 2:禁用
CreatedAt int64
UpdatedAt int64
DeletedAt int64
Version int
IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"`
}
func (m *ChatDataset) TableName() string {
return "chat.dataset"
}
func (m *ChatDataset) BeforeCreate(tx *gorm.DB) (err error) {
// m.CreatedAt = time.Now().Unix()
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *ChatDataset) BeforeUpdate(tx *gorm.DB) (err error) {
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *ChatDataset) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *ChatDataset) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*ChatDataset); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *ChatDataset) CachePrimaryKeyFunc() string {
if len("") == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}
... ...
package models
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gorm.io/gorm"
)
type ChatDatasetDocumentMapping struct {
Id int64 // 唯一标识
DatasetId int64
DocumentId int64
CreatedAt int64
UpdatedAt int64
Version int
}
func (m *ChatDatasetDocumentMapping) TableName() string {
return "chat.dataset_document_mapping"
}
func (m *ChatDatasetDocumentMapping) BeforeCreate(tx *gorm.DB) (err error) {
// m.CreatedAt = time.Now().Unix()
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *ChatDatasetDocumentMapping) BeforeUpdate(tx *gorm.DB) (err error) {
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *ChatDatasetDocumentMapping) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *ChatDatasetDocumentMapping) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*ChatDatasetDocumentMapping); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *ChatDatasetDocumentMapping) CachePrimaryKeyFunc() string {
if len("") == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}
... ...
package models
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gorm.io/gorm"
"gorm.io/plugin/soft_delete"
)
type ChatDocument struct {
Id int64 // 唯一标识
Name string
FileType string
Status int
Metadata domain.DocumentMetadata `gorm:"type:jsonb;serializer:json"`
CreatedAt int64
UpdatedAt int64
DeletedAt int64
Version int
IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"`
}
func (m *ChatDocument) TableName() string {
return "chat.document"
}
func (m *ChatDocument) BeforeCreate(tx *gorm.DB) (err error) {
// m.CreatedAt = time.Now().Unix()
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *ChatDocument) BeforeUpdate(tx *gorm.DB) (err error) {
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *ChatDocument) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *ChatDocument) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*ChatDocument); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *ChatDocument) CachePrimaryKeyFunc() string {
if len("") == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}
... ...
... ... @@ -13,6 +13,10 @@ type ChatSession struct {
UserId int64 // 用户ID
Title string // 会话标题
Abstract string // 摘要
Module int // 1:openai chat 2:星火文档问答
Type string // 类型 chat:普通问答 spark_dataset_chat:星火知识库问答 spark_documents_chat:星火多文档问答
Metadata *domain.SessionMetadata `gorm:"type:jsonb;serializer:json"` // 元数据
Rank int64 // 排序(时间秒,置顶更新当前会话)
CreatedAt int64
UpdatedAt int64
DeletedAt int64
... ...
package repository
import (
"context"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/tiptok/gocomm/pkg/cache"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/db/models"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
"gorm.io/gorm"
)
type ChatDatasetDocumentMappingRepository struct {
*cache.CachedRepository
}
func (repository *ChatDatasetDocumentMappingRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ChatDatasetDocumentMapping) (*domain.ChatDatasetDocumentMapping, error) {
var (
err error
m = &models.ChatDatasetDocumentMapping{}
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
if tx = tx.Model(m).Save(m); tx.Error != nil {
return nil, tx.Error
}
dm.Id = m.Id
return repository.ModelToDomainModel(m)
}
func (repository *ChatDatasetDocumentMappingRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ChatDatasetDocumentMapping) (*domain.ChatDatasetDocumentMapping, error) {
var (
err error
m *models.ChatDatasetDocumentMapping
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Updates(m)
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ChatDatasetDocumentMappingRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ChatDatasetDocumentMapping) (*domain.ChatDatasetDocumentMapping, error) {
var (
err error
m *models.ChatDatasetDocumentMapping
tx = transaction.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
oldVersion := dm.Version
m.Version += 1
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
if tx.RowsAffected == 0 {
return nil, domain.ErrUpdateFail
}
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ChatDatasetDocumentMappingRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ChatDatasetDocumentMapping) (*domain.ChatDatasetDocumentMapping, error) {
var (
tx = conn.DB()
m = &models.ChatDatasetDocumentMapping{Id: dm.Id}
)
queryFunc := func() (interface{}, error) {
tx = tx.Where("id = ?", m.Id).Delete(m)
return m, tx.Error
}
if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return dm, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ChatDatasetDocumentMappingRepository) DeleteByDataset(ctx context.Context, conn transaction.Conn, datasetId int64) error {
var (
tx = conn.DB()
m = &models.ChatDatasetDocumentMapping{}
)
queryFunc := func() (interface{}, error) {
tx = tx.Where("dataset_id = ?", datasetId).Delete(m)
return m, tx.Error
}
if _, err := repository.Query(queryFunc); err != nil {
return err
}
return nil
}
func (repository *ChatDatasetDocumentMappingRepository) DeleteByDocument(ctx context.Context, conn transaction.Conn, documentId int64) error {
var (
tx = conn.DB()
m = &models.ChatDatasetDocumentMapping{}
)
queryFunc := func() (interface{}, error) {
tx = tx.Where("document_id = ?", documentId).Delete(m)
return m, tx.Error
}
if _, err := repository.Query(queryFunc); err != nil {
return err
}
return nil
}
func (repository *ChatDatasetDocumentMappingRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ChatDatasetDocumentMapping, error) {
var (
err error
tx = conn.DB()
m = new(models.ChatDatasetDocumentMapping)
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Where("id = ?", id).First(m)
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, domain.ErrNotFound
}
return m, tx.Error
}
cacheModel := new(models.ChatDatasetDocumentMapping)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ChatDatasetDocumentMappingRepository) FindOneUnscoped(ctx context.Context, conn transaction.Conn, id int64) (*domain.ChatDatasetDocumentMapping, error) {
var (
err error
tx = conn.DB()
m = new(models.ChatDatasetDocumentMapping)
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Unscoped().Where("id = ?", id).First(m)
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, domain.ErrNotFound
}
return m, tx.Error
}
cacheModel := new(models.ChatDatasetDocumentMapping)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ChatDatasetDocumentMappingRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ChatDatasetDocumentMapping, error) {
var (
tx = conn.DB()
ms []*models.ChatDatasetDocumentMapping
dms = make([]*domain.ChatDatasetDocumentMapping, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
return dms, tx.Error
}
return dms, nil
}
if _, err := repository.Query(queryFunc); err != nil {
return 0, nil, err
}
for _, item := range ms {
if dm, err := repository.ModelToDomainModel(item); err != nil {
return 0, dms, err
} else {
dms = append(dms, dm)
}
}
return total, dms, nil
}
func (repository *ChatDatasetDocumentMappingRepository) FindByDataset(ctx context.Context, conn transaction.Conn, datasetIds ...int64) (int64, []*domain.ChatDatasetDocumentMapping, error) {
var (
tx = conn.DB()
ms []*models.ChatDatasetDocumentMapping
dms = make([]*domain.ChatDatasetDocumentMapping, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).
Where("dataset_id in (?)", datasetIds).
Order("id desc")
if total, tx = transaction.PaginationAndCount(ctx, tx, domain.NewQueryOptions().WithFindOnly(), &ms); tx.Error != nil {
return dms, tx.Error
}
return dms, nil
}
if _, err := repository.Query(queryFunc); err != nil {
return 0, nil, err
}
for _, item := range ms {
if dm, err := repository.ModelToDomainModel(item); err != nil {
return 0, dms, err
} else {
dms = append(dms, dm)
}
}
return total, dms, nil
}
func (repository *ChatDatasetDocumentMappingRepository) ModelToDomainModel(from *models.ChatDatasetDocumentMapping) (*domain.ChatDatasetDocumentMapping, error) {
to := &domain.ChatDatasetDocumentMapping{}
err := copier.Copy(to, from)
return to, err
}
func (repository *ChatDatasetDocumentMappingRepository) DomainModelToModel(from *domain.ChatDatasetDocumentMapping) (*models.ChatDatasetDocumentMapping, error) {
to := &models.ChatDatasetDocumentMapping{}
err := copier.Copy(to, from)
return to, err
}
func NewChatDatasetDocumentMappingRepository(cache *cache.CachedRepository) domain.ChatDatasetDocumentMappingRepository {
return &ChatDatasetDocumentMappingRepository{CachedRepository: cache}
}
... ...
package repository
import (
"context"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/tiptok/gocomm/pkg/cache"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/db/models"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
"gorm.io/gorm"
)
type ChatDatasetRepository struct {
*cache.CachedRepository
}
func (repository *ChatDatasetRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ChatDataset) (*domain.ChatDataset, error) {
var (
err error
m = &models.ChatDataset{}
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
if tx = tx.Model(m).Save(m); tx.Error != nil {
return nil, tx.Error
}
dm.Id = m.Id
return repository.ModelToDomainModel(m)
}
func (repository *ChatDatasetRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ChatDataset) (*domain.ChatDataset, error) {
var (
err error
m *models.ChatDataset
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Updates(m)
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ChatDatasetRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ChatDataset) (*domain.ChatDataset, error) {
var (
err error
m *models.ChatDataset
tx = transaction.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
oldVersion := dm.Version
m.Version += 1
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
if tx.RowsAffected == 0 {
return nil, domain.ErrUpdateFail
}
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ChatDatasetRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ChatDataset) (*domain.ChatDataset, error) {
var (
tx = conn.DB()
m = &models.ChatDataset{Id: dm.Id}
)
queryFunc := func() (interface{}, error) {
tx = tx.Where("id = ?", m.Id).Delete(m)
return m, tx.Error
}
if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return dm, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ChatDatasetRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ChatDataset, error) {
var (
err error
tx = conn.DB()
m = new(models.ChatDataset)
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Where("id = ?", id).First(m)
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, domain.ErrNotFound
}
return m, tx.Error
}
cacheModel := new(models.ChatDataset)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ChatDatasetRepository) FindOneUnscoped(ctx context.Context, conn transaction.Conn, id int64) (*domain.ChatDataset, error) {
var (
err error
tx = conn.DB()
m = new(models.ChatDataset)
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Unscoped().Where("id = ?", id).First(m)
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, domain.ErrNotFound
}
return m, tx.Error
}
cacheModel := new(models.ChatDataset)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ChatDatasetRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ChatDataset, error) {
var (
tx = conn.DB()
ms []*models.ChatDataset
dms = make([]*domain.ChatDataset, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
if v, ok := queryOptions["name"]; ok {
tx.Where("name like ?", v)
}
if v, ok := queryOptions["status"]; ok {
tx.Where("status = ?", v)
}
if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
return dms, tx.Error
}
return dms, nil
}
if _, err := repository.Query(queryFunc); err != nil {
return 0, nil, err
}
for _, item := range ms {
if dm, err := repository.ModelToDomainModel(item); err != nil {
return 0, dms, err
} else {
dms = append(dms, dm)
}
}
return total, dms, nil
}
func (repository *ChatDatasetRepository) ModelToDomainModel(from *models.ChatDataset) (*domain.ChatDataset, error) {
to := &domain.ChatDataset{}
err := copier.Copy(to, from)
return to, err
}
func (repository *ChatDatasetRepository) DomainModelToModel(from *domain.ChatDataset) (*models.ChatDataset, error) {
to := &models.ChatDataset{}
err := copier.Copy(to, from)
return to, err
}
func NewChatDatasetRepository(cache *cache.CachedRepository) domain.ChatDatasetRepository {
return &ChatDatasetRepository{CachedRepository: cache}
}
... ...
package repository
import (
"context"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/tiptok/gocomm/pkg/cache"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/db/models"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/chat/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
"gorm.io/gorm"
)
type ChatDocumentRepository struct {
*cache.CachedRepository
}
func (repository *ChatDocumentRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ChatDocument) (*domain.ChatDocument, error) {
var (
err error
m = &models.ChatDocument{}
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
if tx = tx.Model(m).Save(m); tx.Error != nil {
return nil, tx.Error
}
dm.Id = m.Id
return repository.ModelToDomainModel(m)
}
func (repository *ChatDocumentRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ChatDocument) (*domain.ChatDocument, error) {
var (
err error
m *models.ChatDocument
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Updates(m)
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ChatDocumentRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ChatDocument) (*domain.ChatDocument, error) {
var (
err error
m *models.ChatDocument
tx = transaction.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
oldVersion := dm.Version
m.Version += 1
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
if tx.RowsAffected == 0 {
return nil, domain.ErrUpdateFail
}
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ChatDocumentRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ChatDocument) (*domain.ChatDocument, error) {
var (
tx = conn.DB()
m = &models.ChatDocument{Id: dm.Id}
)
queryFunc := func() (interface{}, error) {
tx = tx.Where("id = ?", m.Id).Delete(m)
return m, tx.Error
}
if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return dm, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ChatDocumentRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ChatDocument, error) {
var (
err error
tx = conn.DB()
m = new(models.ChatDocument)
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Where("id = ?", id).First(m)
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, domain.ErrNotFound
}
return m, tx.Error
}
cacheModel := new(models.ChatDocument)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ChatDocumentRepository) FindOneUnscoped(ctx context.Context, conn transaction.Conn, id int64) (*domain.ChatDocument, error) {
var (
err error
tx = conn.DB()
m = new(models.ChatDocument)
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Unscoped().Where("id = ?", id).First(m)
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, domain.ErrNotFound
}
return m, tx.Error
}
cacheModel := new(models.ChatDocument)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ChatDocumentRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.ChatDocument, error) {
var (
tx = conn.DB()
ms []*models.ChatDocument
dms = make([]*domain.ChatDocument, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
if v, ok := queryOptions["name"]; ok {
tx.Where("name like ?", v)
}
if v, ok := queryOptions["fileType"]; ok {
tx.Where("file_type = ?", v)
}
if v, ok := queryOptions["status"]; ok {
tx.Where("status = ?", v)
}
if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
return dms, tx.Error
}
return dms, nil
}
if _, err := repository.Query(queryFunc); err != nil {
return 0, nil, err
}
for _, item := range ms {
if dm, err := repository.ModelToDomainModel(item); err != nil {
return 0, dms, err
} else {
dms = append(dms, dm)
}
}
return total, dms, nil
}
func (repository *ChatDocumentRepository) ModelToDomainModel(from *models.ChatDocument) (*domain.ChatDocument, error) {
to := &domain.ChatDocument{}
err := copier.Copy(to, from)
return to, err
}
func (repository *ChatDocumentRepository) DomainModelToModel(from *domain.ChatDocument) (*models.ChatDocument, error) {
to := &models.ChatDocument{}
err := copier.Copy(to, from)
return to, err
}
func NewChatDocumentRepository(cache *cache.CachedRepository) domain.ChatDocumentRepository {
return &ChatDocumentRepository{CachedRepository: cache}
}
... ...
... ... @@ -141,6 +141,15 @@ func (repository *ChatSessionRepository) Find(ctx context.Context, conn transact
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
if v, ok := queryOptions["companyId"]; ok {
tx.Where("company_id = ?", v)
}
if v, ok := queryOptions["userId"]; ok {
tx.Where("user_id = ?", v)
}
if v, ok := queryOptions["module"]; ok {
tx.Where("module = ?", v)
}
if v, ok := queryOptions["title"]; ok {
tx.Where("title like ?", v)
}
... ...
package domain
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
)
type ChatDataset struct {
Id int64 // 唯一标识
CompanyId int64
UserId int64
Name string // 知识库名称
Desc string // 描述
Status int // 1:启用 2:禁用
CreatedAt int64 `json:",omitempty"`
UpdatedAt int64 `json:",omitempty"`
DeletedAt int64 `json:",omitempty"`
Version int `json:",omitempty"`
}
func ChatDatasetId(m *ChatDataset) int64 {
return m.Id
}
type ChatDatasetRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *ChatDataset) (*ChatDataset, error)
Update(ctx context.Context, conn transaction.Conn, dm *ChatDataset) (*ChatDataset, error)
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *ChatDataset) (*ChatDataset, error)
Delete(ctx context.Context, conn transaction.Conn, dm *ChatDataset) (*ChatDataset, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ChatDataset, error)
FindOneUnscoped(ctx context.Context, conn transaction.Conn, id int64) (*ChatDataset, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ChatDataset, error)
}
... ...
package domain
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
)
type ChatDatasetDocumentMapping struct {
Id int64 // 唯一标识
DatasetId int64
DocumentId int64
CreatedAt int64 `json:",omitempty"`
UpdatedAt int64 `json:",omitempty"`
DeletedAt int64 `json:",omitempty"`
Version int `json:",omitempty"`
}
type ChatDatasetDocumentMappingRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *ChatDatasetDocumentMapping) (*ChatDatasetDocumentMapping, error)
Update(ctx context.Context, conn transaction.Conn, dm *ChatDatasetDocumentMapping) (*ChatDatasetDocumentMapping, error)
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *ChatDatasetDocumentMapping) (*ChatDatasetDocumentMapping, error)
Delete(ctx context.Context, conn transaction.Conn, dm *ChatDatasetDocumentMapping) (*ChatDatasetDocumentMapping, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ChatDatasetDocumentMapping, error)
FindOneUnscoped(ctx context.Context, conn transaction.Conn, id int64) (*ChatDatasetDocumentMapping, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ChatDatasetDocumentMapping, error)
FindByDataset(ctx context.Context, conn transaction.Conn, datasetId ...int64) (int64, []*ChatDatasetDocumentMapping, error)
DeleteByDataset(ctx context.Context, conn transaction.Conn, datasetId int64) error
DeleteByDocument(ctx context.Context, conn transaction.Conn, documentId int64) error
}
... ...
package domain
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
)
type ChatDocument struct {
Id int64 // 唯一标识
Name string // 文件命
FileType string // 文件类型 markdown\pdf\txt\doc&docx
Status int // 1.使用中、0.待处理、2.预处理中、3.处理失败
Metadata DocumentMetadata
CreatedAt int64 `json:",omitempty"`
UpdatedAt int64 `json:",omitempty"`
DeletedAt int64 `json:",omitempty"`
Version int `json:",omitempty"`
}
const (
Enable = 1
Disable = 2
)
const (
StatusInUsed = 1
StatusWaitProcess = 0
StatusPreProcess = 2
StatusProcessFail = 3
)
type DocumentMetadata struct {
OriginFileName string `json:",omitempty"` // 源文件命
Ext string `json:",omitempty"` // 格式
FileId string `json:",omitempty"` // 星火文件ID
FileUrl string `json:",omitempty"` // 文件地址
FileSize float64 `json:",omitempty"` // 文件大小
}
type ChatDocumentRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *ChatDocument) (*ChatDocument, error)
Update(ctx context.Context, conn transaction.Conn, dm *ChatDocument) (*ChatDocument, error)
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *ChatDocument) (*ChatDocument, error)
Delete(ctx context.Context, conn transaction.Conn, dm *ChatDocument) (*ChatDocument, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ChatDocument, error)
FindOneUnscoped(ctx context.Context, conn transaction.Conn, id int64) (*ChatDocument, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ChatDocument, error)
}
... ...
... ... @@ -40,11 +40,14 @@ func (list ChatModels) Match(id int64) (*ChatModel, bool) {
})
}
const SparkChatDocModelId = 5
var DefaultChatModels ChatModels = []*ChatModel{
NewChatModels(1, "ChatGPT 4o", openai.GPT4o, "", NewModelConfig("", "sk-proj-0odTHgsYqzGpjPMWYuw2T3BlbkFJpaIsQ9I6j8kYc8P7l60H", "")),
NewChatModels(2, "ChatGPT 4-turbo", openai.GPT4Turbo, "", NewModelConfig("", "sk-proj-0odTHgsYqzGpjPMWYuw2T3BlbkFJpaIsQ9I6j8kYc8P7l60H", "")),
NewChatModels(3, "ChatGPT 3.5", openai.GPT3Dot5Turbo, "", NewModelConfig("", "sk-proj-0odTHgsYqzGpjPMWYuw2T3BlbkFJpaIsQ9I6j8kYc8P7l60H", "")),
NewChatModels(4, "星火大模型V3.5", "spark3.5", "", NewModelConfig("4fd8694e", "4a4081a20e9ba0fb1b9686ed93221989", "NTVkM2FjNzk2NzQ5MzBkNWMwYTUwNjAz")),
NewChatModels(1, "ChatGPT 4o", openai.GPT4o, "https://tiptok.oss-cn-shanghai.aliyuncs.com/youdao-note/ChatGPT_logo.svg.png", NewModelConfig("", "sk-proj-0odTHgsYqzGpjPMWYuw2T3BlbkFJpaIsQ9I6j8kYc8P7l60H", "")),
NewChatModels(2, "ChatGPT 4-turbo", openai.GPT4Turbo, "https://tiptok.oss-cn-shanghai.aliyuncs.com/youdao-note/ChatGPT_logo.svg.png", NewModelConfig("", "sk-proj-0odTHgsYqzGpjPMWYuw2T3BlbkFJpaIsQ9I6j8kYc8P7l60H", "")),
NewChatModels(3, "ChatGPT 3.5", openai.GPT3Dot5Turbo, "https://tiptok.oss-cn-shanghai.aliyuncs.com/youdao-note/ChatGPT_logo.svg.png", NewModelConfig("", "sk-proj-0odTHgsYqzGpjPMWYuw2T3BlbkFJpaIsQ9I6j8kYc8P7l60H", "")),
NewChatModels(4, "星火大模型V3.5", "spark3.5", "https://tiptok.oss-cn-shanghai.aliyuncs.com/youdao-note/XUN_Spark.webp", NewModelConfig("4fd8694e", "4a4081a20e9ba0fb1b9686ed93221989", "NTVkM2FjNzk2NzQ5MzBkNWMwYTUwNjAz")),
NewChatModels(SparkChatDocModelId, "星火文档问答", "spark chat doc", "https://tiptok.oss-cn-shanghai.aliyuncs.com/youdao-note/logo-icon-spark-doc.png", NewModelConfig("4fd8694e", "4a4081a20e9ba0fb1b9686ed93221989", "NTVkM2FjNzk2NzQ5MzBkNWMwYTUwNjAz")),
}
type ModelConfig struct {
... ...
... ... @@ -11,12 +11,32 @@ type ChatSession struct {
UserId int64 `json:",omitempty"` // 用户ID
Title string `json:",omitempty"` // 会话标题
Abstract string `json:",omitempty"` // 摘要
Module int `json:",omitempty"` // 1:openai chat 2:星火文档问答
Type string `json:",omitempty"` // 类型 chat:普通问答 spark_dataset_chat:星火知识库问答 spark_documents_chat:星火多文档问答
Metadata *SessionMetadata `json:",omitempty"` // 元数据
Rank int64 `json:",omitempty"` // 排序(时间秒,置顶更新当前会话)
CreatedAt int64 `json:",omitempty"`
UpdatedAt int64 `json:",omitempty"`
DeletedAt int64 `json:",omitempty"`
Version int `json:",omitempty"`
}
const (
ModuleOpenaiChat = 1
ModuleSparkChat = 2
)
const (
TypeChat = "chat"
TypeSparkDatasetChat = "spark_dataset_chat"
TypeSparkDocumentsChat = "spark_documents_chat"
)
type SessionMetadata struct {
DatasetId int64 `json:",omitempty"` // 知识库
DocumentIds []int64 `json:",omitempty"` // 多文档
}
type ChatSessionRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *ChatSession) (*ChatSession, error)
Update(ctx context.Context, conn transaction.Conn, dm *ChatSession) (*ChatSession, error)
... ...
... ... @@ -76,7 +76,7 @@ func RoutersOpenapi(svc *svc.ServiceContext) []rest.Route {
return []rest.Route{
{
Method: http.MethodPost,
Path: "/openapi/sms/send-sms-code",
Path: "/v1/openapi/sms/send-sms-code",
Handler: smslib.SendSmsCodeHandler(svc.SmsService),
},
{
... ...
package company
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/logic/company"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/types"
)
func SystemCompanyInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.SystemCompanyInfoRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := company.NewSystemCompanyInfoLogic(r.Context(), svcCtx)
resp, err := l.SystemCompanyInfo(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package company
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/logic/company"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/types"
)
func SystemCompanyInfoUpdateHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.SystemCompanyUpdateRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := company.NewSystemCompanyInfoUpdateLogic(r.Context(), svcCtx)
resp, err := l.SystemCompanyInfoUpdate(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package department
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/logic/department"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/types"
)
func SystemCompanyDepartmentsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.DepartmentSearchRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := department.NewSystemCompanyDepartmentsLogic(r.Context(), svcCtx)
resp, err := l.SystemCompanyDepartments(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
... ... @@ -5,6 +5,7 @@ import (
"net/http"
auth "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/handler/auth"
company "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/handler/company"
department "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/handler/department"
employee "gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/handler/employee"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/svc"
... ... @@ -53,6 +54,26 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
[]rest.Route{
{
Method: http.MethodGet,
Path: "/system/company_info",
Handler: company.SystemCompanyInfoHandler(serverCtx),
},
{
Method: http.MethodPut,
Path: "/system/company_info",
Handler: company.SystemCompanyInfoUpdateHandler(serverCtx),
},
}...,
),
rest.WithJwt(serverCtx.Config.SystemAuth.AccessSecret),
rest.WithPrefix("/v1"),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.LogRequest},
[]rest.Route{
{
Method: http.MethodGet,
Path: "/system/department/:id",
Handler: department.SystemDepartmentGetHandler(serverCtx),
},
... ... @@ -91,6 +112,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/system/department/export",
Handler: department.SystemDepartmentExportHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/system/company-departments",
Handler: department.SystemCompanyDepartmentsHandler(serverCtx),
},
}...,
),
rest.WithJwt(serverCtx.Config.SystemAuth.AccessSecret),
... ...
package company
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type SystemCompanyInfoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSystemCompanyInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemCompanyInfoLogic {
return &SystemCompanyInfoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SystemCompanyInfoLogic) SystemCompanyInfo(req *types.SystemCompanyInfoRequest) (resp *types.SystemCompanyInfoResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
company *domain.SysCompany
token = contextdata.GetUserTokenFromCtx(l.ctx)
)
if company, err = l.svcCtx.CompanyRepository.FindOne(l.ctx, conn, token.CompanyId); err != nil {
return nil, xerr.NewErrMsgErr("公司不存在", err)
}
resp = &types.SystemCompanyInfoResponse{
Company: NewTypesSysCompany(company),
}
return
}
func NewDomainSysCompany(item types.Company, adminId int64) *domain.SysCompany {
return &domain.SysCompany{
Id: item.Id,
Name: item.Name,
Logo: item.Logo,
AdminId: adminId,
BaseInfo: domain.CompanyBaseInfo{
UnifiedSocialCreditCode: item.UnifiedSocialCreditCode,
ContactPhone: item.ContactPhone,
ContactName: item.ContactName,
Address: item.Address,
Remark: item.Remark,
BusinessLicenseImage: item.BusinessLicenseImage,
},
Status: domain.Enable,
}
}
func NewTypesSysCompany(item *domain.SysCompany) types.Company {
return types.Company{
Id: item.Id,
Name: item.Name,
Logo: item.Logo,
UnifiedSocialCreditCode: item.BaseInfo.UnifiedSocialCreditCode,
ContactPhone: item.BaseInfo.ContactPhone,
ContactName: item.BaseInfo.ContactName,
Address: item.BaseInfo.Address,
Remark: item.BaseInfo.Remark,
BusinessLicenseImage: item.BaseInfo.BusinessLicenseImage,
}
}
... ...
package company
import (
"context"
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/logic/core"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type SystemCompanyInfoUpdateLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSystemCompanyInfoUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemCompanyInfoUpdateLogic {
return &SystemCompanyInfoUpdateLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SystemCompanyInfoUpdateLogic) SystemCompanyInfoUpdate(req *types.SystemCompanyUpdateRequest) (resp *types.SystemCompanyUpdateResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
company *domain.SysCompany
token = contextdata.GetUserTokenFromCtx(l.ctx)
user *domain.SysUser
)
if company, err = l.svcCtx.CompanyRepository.FindOne(l.ctx, conn, token.CompanyId); err != nil {
return nil, xerr.NewErrMsgErr("公司不存在", err)
}
if err = transaction.MustUseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
// 新的管理员
if user, err = core.SaveCompanyUser(l.ctx, l.svcCtx, conn, req.Company.ContactPhone, req.Company.Name, company.Id); err != nil {
return fmt.Errorf("公司联系人不存在")
}
company.Name = req.Company.Name
company.Logo = req.Company.Logo
company.BaseInfo.UnifiedSocialCreditCode = req.Company.UnifiedSocialCreditCode
company.BaseInfo.ContactName = req.Company.ContactName
company.BaseInfo.ContactPhone = req.Company.ContactPhone
company.BaseInfo.Address = req.Company.Address
company.BaseInfo.Remark = req.Company.Remark
company.BaseInfo.BusinessLicenseImage = req.Company.BusinessLicenseImage
company.AdminId = user.Id
company, err = l.svcCtx.CompanyRepository.UpdateWithVersion(l.ctx, conn, company)
return err
}); err != nil {
return nil, xerr.NewErrMsgErr(err.Error(), err)
}
resp = &types.SystemCompanyUpdateResponse{
Company: NewTypesSysCompany(company),
}
return
}
... ...
package core
import (
"context"
"errors"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/xerr"
)
func SaveCompanyUser(ctx context.Context, svcCtx *svc.ServiceContext, conn transaction.Conn, phone string, name string, companyId int64) (*domain.SysUser, error) {
var (
user *domain.SysUser
err error
)
// 2.当前手机号用户
if user, err = svcCtx.UserRepository.FindOneByPhone(ctx, conn, phone); err != nil {
if errors.Is(err, domain.ErrNotFound) {
user = nil
err = nil
} else {
return nil, xerr.NewErrMsgErr("查找用户失败", err)
}
}
if user == nil {
user = domain.NewDomainSysUser(phone, name, "")
if user, err = svcCtx.UserRepository.Insert(ctx, conn, user); err != nil {
return nil, err
}
}
// 当前用户已绑定该公司
var employee *domain.SysEmployee
if employee, err = svcCtx.EmployeeRepository.FindOneByUserId(ctx, conn, companyId, user.Id); employee != nil {
return user, nil
}
// 注册默认员工
employee = &domain.SysEmployee{
UserId: user.Id,
CompanyId: companyId,
//Departments: []int64{department.Id},
AccountStatus: domain.Enable,
EmployeeType: domain.FullTime,
BaseInfo: domain.EmployeeBaseInfo{},
WorkInfo: domain.EmployeeWorkInfo{},
}
if employee, err = svcCtx.EmployeeRepository.Insert(ctx, conn, employee); err != nil {
return nil, err
}
// 公司默认部门
var defaultDepartment *domain.SysDepartment
if defaultDepartment, err = svcCtx.DepartmentRepository.FindRootDepartmentByCompany(ctx, conn, companyId); err != nil {
return nil, err
}
// 员工部门
if err = svcCtx.UserDepartmentRepository.ResetByUser(ctx, conn, companyId, user.Id, employee.Id, []int64{defaultDepartment.Id}); err != nil {
return nil, err
}
return user, nil
}
... ...
package department
import (
"context"
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/internal/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/contextdata"
"strings"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/cmd/ep/system/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type SystemCompanyDepartmentsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSystemCompanyDepartmentsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemCompanyDepartmentsLogic {
return &SystemCompanyDepartmentsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SystemCompanyDepartmentsLogic) SystemCompanyDepartments(req *types.DepartmentSearchRequest) (resp *types.DepartmentSearchResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
departments []*domain.SysDepartment
token = contextdata.GetUserTokenFromCtx(l.ctx)
)
queryOptions := domain.NewQueryOptions().WithKV("name", fmt.Sprintf("%%%v%%", req.DepartmentName)).WithFindOnly()
_, departments, err = l.svcCtx.DepartmentRepository.Find(l.ctx, conn, token.CompanyId, queryOptions)
list := make([]types.Department, 0)
for i, department := range departments {
if req.DepartmentName != "" && !strings.Contains(department.Name, req.DepartmentName) {
continue
}
typesDepartment := NewTypesDepartment(departments[i], nil)
list = append(list, typesDepartment)
}
resp = &types.DepartmentSearchResponse{
List: list,
Total: int64(len(list)),
}
return
}
... ...
... ... @@ -7,11 +7,6 @@ type User struct {
Phone string `json:"phone,optional,omitempty"` // 用户手机号
}
type Company struct {
Id int64 `json:"id,optional,omitempty"` // 唯一标识
Name string `json:"name,omitempty"` // 名称
}
type CompanyRegisterRequest struct {
Name string `json:"name"` // 公司名称
Logo string `json:"logo"` // 公司LOGO
... ... @@ -49,6 +44,33 @@ type UserInfoResponse struct {
CompanyList []*Company `json:"companies"` // 拥有的公司
}
type SystemCompanyInfoRequest struct {
}
type SystemCompanyInfoResponse struct {
Company Company `json:"company"`
}
type SystemCompanyUpdateRequest struct {
Company Company `json:"company"`
}
type SystemCompanyUpdateResponse struct {
Company Company `json:"company"`
}
type Company struct {
Id int64 `json:"id,optional,omitempty"` // 唯一标识
Name string `json:"name,omitempty"` // 公司名称
Logo string `json:"logo,optional,omitempty"` // 公司图标
UnifiedSocialCreditCode string `json:"unifiedSocialCreditCode,optional,omitempty"` // 统一社会信用代码
ContactName string `json:"contactName,omitempty"` // 联系人
ContactPhone string `json:"contactPhone,omitempty"` // 联系人电话
Address string `json:"address,optional,omitempty"` // 惬意地址
Remark string `json:"remark,optional,omitempty"` // 备注信息
BusinessLicenseImage string `json:"businessLicenseImage,optional,omitempty"` // 营业执照-照片地址
}
type DepartmentGetRequest struct {
Id int64 `path:"id"`
}
... ...
syntax = "v1"
// 后台接口
@server(
prefix: v1
group: company
middleware: LogRequest
jwt: SystemAuth
)
service Core {
@doc "企业信息-详情"
@handler systemCompanyInfo
get /system/company_info (SystemCompanyInfoRequest) returns (SystemCompanyInfoResponse)
@doc "企业信息-更新"
@handler systemCompanyInfoUpdate
put /system/company_info (SystemCompanyUpdateRequest) returns (SystemCompanyUpdateResponse)
}
// 企业信息-详情
type(
SystemCompanyInfoRequest{
}
SystemCompanyInfoResponse{
Company Company `json:"company"`
}
)
// 企业信息-更新
type(
SystemCompanyUpdateRequest{
Company Company `json:"company"`
}
SystemCompanyUpdateResponse{
Company Company `json:"company"`
}
)
type Company{
Id int64 `json:"id,optional,omitempty"` // 唯一标识
Name string `json:"name,omitempty"` // 公司名称
Logo string `json:"logo,optional,omitempty"` // 公司图标
UnifiedSocialCreditCode string `json:"unifiedSocialCreditCode,optional,omitempty"` // 统一社会信用代码
ContactName string `json:"contactName,omitempty"` // 联系人
ContactPhone string `json:"contactPhone,omitempty"` // 联系人电话
Address string `json:"address,optional,omitempty"` // 惬意地址
Remark string `json:"remark,optional,omitempty"` // 备注信息
BusinessLicenseImage string `json:"businessLicenseImage,optional,omitempty"` // 营业执照-照片地址
}
\ No newline at end of file
... ...
... ... @@ -32,6 +32,10 @@ service Core {
@doc "部门-批量导出"
@handler systemDepartmentExport
delete /system/department/export (DepartmentSearchRequest) returns (DepartmentSearchResponse)
@doc "公司部门"
@handler systemCompanyDepartments
post /system/company-departments (DepartmentSearchRequest) returns (DepartmentSearchResponse)
}
type(
... ...
... ... @@ -46,11 +46,6 @@ type(
// 系统公司注册
type(
Company{
Id int64 `json:"id,optional,omitempty"` // 唯一标识
Name string `json:"name,omitempty"` // 名称
}
CompanyRegisterRequest{
Name string `json:"name"` // 公司名称
Logo string `json:"logo"` // 公司LOGO
... ...
... ... @@ -15,7 +15,7 @@
"application/json"
],
"paths": {
"v1/system/auth/company_sign_up": {
"v1/auth/company_sign_up": {
"post": {
"summary": "公司注册",
"operationId": "systemAuthRegister",
... ... @@ -43,7 +43,7 @@
]
}
},
"v1/system/auth/login": {
"v1/auth/login": {
"post": {
"summary": "系统用户登录",
"operationId": "systemAuthLogin",
... ... @@ -71,7 +71,7 @@
]
}
},
"v1/system/auth/user_info": {
"v1/auth/user_info": {
"post": {
"summary": "系统用户信息",
"operationId": "systemAuthUserInfo",
... ... @@ -99,6 +99,78 @@
]
}
},
"v1/system/company-departments": {
"post": {
"summary": "公司部门",
"operationId": "systemCompanyDepartments",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/DepartmentSearchResponse"
}
}
},
"parameters": [
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/DepartmentSearchRequest"
}
}
],
"requestBody": {},
"tags": [
"department"
]
}
},
"v1/system/company_info": {
"get": {
"summary": "企业信息-详情",
"operationId": "systemCompanyInfo",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/SystemCompanyInfoResponse"
}
}
},
"requestBody": {},
"tags": [
"company"
]
},
"put": {
"summary": "企业信息-更新",
"operationId": "systemCompanyInfoUpdate",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/SystemCompanyUpdateResponse"
}
}
},
"parameters": [
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/SystemCompanyUpdateRequest"
}
}
],
"requestBody": {},
"tags": [
"company"
]
}
},
"v1/system/department": {
"post": {
"summary": "部门-保存",
... ... @@ -641,13 +713,48 @@
},
"name": {
"type": "string",
"description": " 名称"
"description": " 公司名称"
},
"logo": {
"type": "string",
"description": " 公司图标"
},
"unifiedSocialCreditCode": {
"type": "string",
"description": " 统一社会信用代码"
},
"contactName": {
"type": "string",
"description": " 联系人"
},
"contactPhone": {
"type": "string",
"description": " 联系人电话"
},
"address": {
"type": "string",
"description": " 惬意地址"
},
"remark": {
"type": "string",
"description": " 备注信息"
},
"businessLicenseImage": {
"type": "string",
"description": " 营业执照-照片地址"
}
},
"title": "Company",
"required": [
"id",
"name"
"name",
"logo",
"unifiedSocialCreditCode",
"contactName",
"contactPhone",
"address",
"remark",
"businessLicenseImage"
]
},
"CompanyRegisterRequest": {
... ... @@ -842,6 +949,11 @@
"departmentName": {
"type": "string",
"description": " 部门名称"
},
"countUserNumber": {
"type": "boolean",
"format": "boolean",
"description": " 统计用户数量"
}
},
"title": "DepartmentSearchRequest"
... ... @@ -913,6 +1025,11 @@
},
"description": " 部门列表"
},
"accountStatus": {
"type": "integer",
"format": "int32",
"description": " 账号状态 1.正常 2.已暂停"
},
"employeeType": {
"type": "string",
"description": " 人员类型 正式、实习、外包、劳务、顾问"
... ... @@ -948,6 +1065,7 @@
"code",
"phone",
"departments",
"accountStatus",
"employeeType",
"employedDate",
"superior",
... ... @@ -1188,6 +1306,46 @@
"name"
]
},
"SystemCompanyInfoRequest": {
"type": "object",
"title": "SystemCompanyInfoRequest"
},
"SystemCompanyInfoResponse": {
"type": "object",
"properties": {
"company": {
"$ref": "#/definitions/Company"
}
},
"title": "SystemCompanyInfoResponse",
"required": [
"company"
]
},
"SystemCompanyUpdateRequest": {
"type": "object",
"properties": {
"company": {
"$ref": "#/definitions/Company"
}
},
"title": "SystemCompanyUpdateRequest",
"required": [
"company"
]
},
"SystemCompanyUpdateResponse": {
"type": "object",
"properties": {
"company": {
"$ref": "#/definitions/Company"
}
},
"title": "SystemCompanyUpdateResponse",
"required": [
"company"
]
},
"User": {
"type": "object",
"properties": {
... ...
... ... @@ -111,6 +111,25 @@ func (repository *SysDepartmentRepository) FindOne(ctx context.Context, conn tra
return repository.ModelToDomainModel(m)
}
func (repository *SysDepartmentRepository) FindRootDepartmentByCompany(ctx context.Context, conn transaction.Conn, companyId int64) (*domain.SysDepartment, error) {
var (
err error
tx = conn.DB()
m = new(models.SysDepartment)
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Where("company_id = ?", companyId).Where("is_root = ?", true).First(m)
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, domain.ErrNotFound
}
return m, tx.Error
}
if _, err = repository.Query(queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *SysDepartmentRepository) FindOneUnscoped(ctx context.Context, conn transaction.Conn, id int64) (*domain.SysDepartment, error) {
var (
err error
... ...
... ... @@ -28,6 +28,7 @@ type SysDepartmentRepository interface {
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *SysDepartment) (*SysDepartment, error)
Delete(ctx context.Context, conn transaction.Conn, dm *SysDepartment) (*SysDepartment, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*SysDepartment, error)
FindRootDepartmentByCompany(ctx context.Context, conn transaction.Conn, companyId int64) (*SysDepartment, error)
FindOneUnscoped(ctx context.Context, conn transaction.Conn, id int64) (*SysDepartment, error)
Find(ctx context.Context, conn transaction.Conn, companyId int64, queryOptions map[string]interface{}) (int64, []*SysDepartment, error)
}
... ...
... ... @@ -11,7 +11,7 @@ import (
func ChatGPT(gptModelCode, key string, question string, channel chan string) (answer string, err error) {
config := openai.DefaultConfig(key)
config.BaseURL = "https://api.openai.com/v1" // "http://47.251.84.160:8080/v1" //
config.BaseURL = "http://47.251.84.160:8080/v1" // "https://api.openai.com/v1" //
c := openai.NewClientWithConfig(config)
ctx := context.Background()
... ... @@ -52,6 +52,5 @@ func ChatGPT(gptModelCode, key string, question string, channel chan string) (an
answer += content
channel <- content
}
close(channel)
return
}
... ...
... ... @@ -2,6 +2,7 @@ package ai
import (
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/json"
... ... @@ -23,17 +24,18 @@ import (
*/
var (
hostUrl = "wss://spark-api.xf-yun.com/v3.5/chat"
hostChatUrl = "wss://spark-api.xf-yun.com/v3.5/chat"
hostFileUploadUrl = "https://chatdoc.xfyun.cn/openapi/v1/file/upload"
hostChatDocumentUrl = "wss://chatdoc.xfyun.cn/openapi/chat"
)
func Spark(appid string, apiKey string, apiSecret string, question string, channel chan string) (answer string, err error) {
func ChatSpark(appid string, apiKey string, apiSecret string, question string, channel chan string) (answer string, err error) {
// fmt.Println(HmacWithShaTobase64("hmac-sha256", "hello\nhello", "hello"))
// st := time.Now()
d := websocket.Dialer{
HandshakeTimeout: 5 * time.Second,
}
//握手并建立websocket 连接
conn, resp, err := d.Dial(assembleAuthUrl1(hostUrl, apiKey, apiSecret), nil)
conn, resp, err := d.Dial(assembleAuthUrl(hostChatDocumentUrl, apiKey, apiSecret), nil)
if err != nil {
logx.Error(readResp(resp) + err.Error())
return
... ... @@ -43,7 +45,6 @@ func Spark(appid string, apiKey string, apiSecret string, question string, chann
}
go func() {
data := genParams1(appid, question)
conn.WriteJSON(data)
... ... @@ -92,7 +93,6 @@ func Spark(appid string, apiKey string, apiSecret string, question string, chann
break
}
}
close(channel)
return answer, nil
}
... ... @@ -125,13 +125,8 @@ func genParams1(appid, question string) map[string]interface{} { // 根据实际
return data // 根据实际情况修改返回的数据结构和字段名
}
type SparkUserMessage struct {
Role string `json:"role"`
Content string `json:"content"`
}
// 创建鉴权url apikey 即 hmac username
func assembleAuthUrl1(hosturl string, appKey, apiSecret string) string {
func assembleAuthUrl(hosturl string, appKey, apiSecret string) string {
ul, err := url.Parse(hosturl)
if err != nil {
fmt.Println(err)
... ... @@ -145,7 +140,7 @@ func assembleAuthUrl1(hosturl string, appKey, apiSecret string) string {
sgin := strings.Join(signString, "\n")
// fmt.Println(sgin)
//签名结果
sha := HmacWithShaTobase64("hmac-sha256", sgin, apiSecret)
sha := HmacWithSha256base64(sgin, apiSecret)
// fmt.Println(sha)
//构建请求参数 此时不需要urlencoding
authUrl := fmt.Sprintf(`api_key="%v", algorithm="hmac-sha256", headers="host date request-line", signature="%v"`, appKey, sha)
... ... @@ -161,13 +156,20 @@ func assembleAuthUrl1(hosturl string, appKey, apiSecret string) string {
return callurl
}
func HmacWithShaTobase64(algorithm, data, key string) string {
func HmacWithSha256base64(data, key string) string {
mac := hmac.New(sha256.New, []byte(key))
mac.Write([]byte(data))
encodeData := mac.Sum(nil)
return base64.StdEncoding.EncodeToString(encodeData)
}
func HmacWithSha1base64(data, key string) string {
mac := hmac.New(sha1.New, []byte(key))
mac.Write([]byte(data))
encodeData := mac.Sum(nil)
return base64.StdEncoding.EncodeToString(encodeData)
}
func readResp(resp *http.Response) string {
if resp == nil {
return ""
... ... @@ -179,6 +181,11 @@ func readResp(resp *http.Response) string {
return fmt.Sprintf("code=%d,body=%s", resp.StatusCode, string(b))
}
type SparkUserMessage struct {
Role string `json:"role"`
Content string `json:"content"`
}
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
... ...
package ai
import (
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
"github.com/zeromicro/go-zero/core/jsonx"
"github.com/zeromicro/go-zero/core/logx"
"time"
)
func ChatSparkDocument(appid string, apiKey string, apiSecret string, fileIds []string, question string, channel chan string) (answer string, err error) {
d := websocket.Dialer{
HandshakeTimeout: 5 * time.Second,
}
//握手并建立websocket 连接
ts := fmt.Sprintf("%d", time.Now().Unix())
conn, resp, err := d.Dial(hostChatDocumentUrl+"?"+"appId="+appid+"&timestamp="+ts+"&signature="+getSignature(appid, apiSecret, ts), nil)
if err != nil {
logx.Error(readResp(resp) + err.Error())
return
} else if resp.StatusCode != 101 {
logx.Error(readResp(resp) + err.Error())
return
}
go func() {
data := genDocumentChatParams(fileIds, question)
logx.Info(jsonx.MarshalToString(data))
conn.WriteJSON(data)
}()
//获取返回的数据
for {
var msg []byte
_, msg, err = conn.ReadMessage()
if err != nil {
logx.Error("read message error:", err)
break
}
var data SparkDocumentChatResponseMessage
err = json.Unmarshal(msg, &data)
if err != nil {
logx.Error("Error parsing JSON:", err)
return
}
logx.Info(string(msg))
// 异常时,错误信息作为回答
if data.Code != 0 {
channel <- data.Content
answer = data.Content
return
}
content := data.Content
channel <- content
if data.Status != 2 {
answer += content
} else {
answer += content
logx.Infof("收到最终结果 answer:%v", answer)
conn.Close()
break
}
}
return answer, nil
}
/*
若返回 fileRefer 为空,提示 "抱款,在文档中没有找到与提问相关的内容,请尝试换个问题问问吧。"表示提问未匹配到文档内容,可以降低chatExtends.wikiFilterScore以降低匹配阈值,也可以开启chatExtends.sparkWhenWithoutEmbedding用大模型兜底
*/
type SparkDocumentChatResponseMessage struct {
Code int `json:"code"` // 错误码 ,0 标识成功
Content string `json:"content"` // 回答内容
FileRefer string `json:"fileRefer"` // 文档引用,status=99 的时候返回;结构是个 Map,key=文件 id,value=引用的文段列表(对应 fileTrunks 的 index
Status int `json:"status"` // 会话状态,取值为[0,1,2,99];0 代表首次结果;1 代表中间结果;2 代表最后一个结果;99 代表引用的文档及文段
}
// 生成参数
func genDocumentChatParams(fileIds []string, question string) map[string]interface{} { // 根据实际情况修改返回的数据结构和字段名
messages := []Message{
{Role: "user", Content: question},
}
data := map[string]interface{}{ // 根据实际情况修改返回的数据结构和字段名
"chatExtends": map[string]interface{}{ // 根据实际情况修改返回的数据结构和字段名
"wikiPromptTpl": "请将以下内容作为已知信息:\n<wikicontent>\n请根据以上内容回答用户的问题。\n问题:<wikiquestion>\n回答:", // 根据实际情况修改返回的数据结构和字段名
"wikiFilterScore": 0.82,
"temperature": 0.5,
},
"fileIds": fileIds,
"messages": messages,
}
return data // 根据实际情况修改返回的数据结构和字段名
}
... ...
package ai
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/rest/httpc"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/tool"
"net/http"
"time"
)
func SparkUploadFile(appid string, appKey string, appSecret string, request SparkFileRequest) (id string, err error) {
var response *http.Response
request.FileType = "wiki"
request.Timestamp = fmt.Sprintf("%v", time.Now().Unix())
request.AppId = appid
request.Signature = getSignature(appid, appSecret, request.Timestamp)
if response, err = httpc.Do(context.Background(), http.MethodPost, hostFileUploadUrl, request); err != nil {
return "", err
}
var parseResponse SparkFileResponse
if err = httpc.Parse(response, &parseResponse); err != nil {
return "", err
}
if parseResponse.Code != 0 {
return "", fmt.Errorf("code:%v message:%v sid:%v", parseResponse.Code, parseResponse.Desc, parseResponse.Sid)
}
id = parseResponse.Data.FileID
return
}
type (
SparkFileRequest struct {
AppId string `header:"appId"`
Timestamp string `header:"timestamp"`
Signature string `header:"signature"`
Url string `form:"url"`
FileName string `form:"fileName"`
FileType string `form:"fileType"`
CallbackUrl string `form:"callbackUrl,optional"`
}
SparkFileResponse struct {
Flag bool `json:"flag,optional"`
Sid string `json:"sid,optional"`
Code int `json:"code,optional"`
Desc string `json:"desc,optional"`
Data struct {
FileID string `json:"fileId,optional"`
} `json:"data,optional"`
}
)
func getSignature(appId, secret string, ts string) string {
auth := tool.Md5ByString(fmt.Sprintf("%v%v", appId, ts))
return HmacWithSha1base64(auth, secret)
}
... ...