Skip to content

Commit

Permalink
Merge pull request #20 from gen-mind/develop
Browse files Browse the repository at this point in the history
develop
  • Loading branch information
apaladiychuk authored Apr 15, 2024
2 parents b763b85 + a525abb commit d567734
Show file tree
Hide file tree
Showing 48 changed files with 2,915 additions and 411 deletions.
863 changes: 792 additions & 71 deletions backend/api/docs/docs.go

Large diffs are not rendered by default.

863 changes: 792 additions & 71 deletions backend/api/docs/swagger.json

Large diffs are not rendered by default.

517 changes: 489 additions & 28 deletions backend/api/docs/swagger.yaml

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion backend/api/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ func (h *AuthHandler) SignIn(c *gin.Context) error {
return nil
}

func (h *AuthHandler) RefreshToken(c *gin.Context) error {
//buf, err := json.Marshal(parameters.OAuthParam{Action: oauth.LoginState})
//if err != nil {
// return utils.Internal.Wrap(err, "can not marshal payload")
//}
//state := base64.URLEncoding.EncodeToString(buf)
//url, err := h.oauthClient.RefreshToken(c.Request.Context(), state)
//if err != nil {
// return err
//}
//c.Redirect(http.StatusFound, url)
return nil
}

func (h *AuthHandler) Callback(c *gin.Context) error {
code := c.Query(oauth.CodeNameGoogle)

Expand All @@ -89,7 +103,7 @@ func (h *AuthHandler) Callback(c *gin.Context) error {
var user *model.User
switch state.Action {
case oauth.LoginState:
user, err = h.authBL.Login(c.Request.Context(), response.Email)
user, err = h.authBL.QuickLogin(c.Request.Context(), response)
case oauth.SignUpState:
user, err = h.authBL.SignUp(c.Request.Context(), response)
case oauth.InviteState:
Expand Down
26 changes: 26 additions & 0 deletions backend/api/handler/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (h *ChatHandler) Mount(route *gin.Engine, authMiddleware gin.HandlerFunc) {
handler.GET("/get-chat-session/:id", server.HandlerErrorFuncAuth(h.GetByID))
handler.POST("/create-chat-session", server.HandlerErrorFuncAuth(h.CreateSession))
handler.POST("/send-message", server.HandlerErrorFuncAuth(h.SendMessage))
handler.POST("/message/feedback", server.HandlerErrorFuncAuth(h.MessageFeedback))
}

// GetSessions return list of chat sessions for current user
Expand Down Expand Up @@ -121,3 +122,28 @@ func (h *ChatHandler) SendMessage(c *gin.Context, identity *security.Identity) e
})
return nil
}

// MessageFeedback add feedback to message
// @Summary add feedback to message
// @Description add feedback to message
// @Tags Chat
// @ID chat_message_feedback
// @Param payload body parameters.MessageFeedbackParam true "send message parameters"
// @Produce json
// @Security ApiKeyAuth
// @Success 200 {object} model.ChatMessageFeedback
// @Router /chats/message/feedback [post]
func (h *ChatHandler) MessageFeedback(c *gin.Context, identity *security.Identity) error {
var param parameters.MessageFeedbackParam
if err := c.BindJSON(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong payload")
}
if err := param.Validate(); err != nil {
return utils.InvalidInput.Wrap(err, err.Error())
}
feedback, err := h.chatBL.FeedbackMessage(c, identity.User, param.ID, param.Vote == parameters.MessageFeedbackUpvote)
if err != nil {
return err
}
return server.JsonResult(c, http.StatusOK, feedback)
}
72 changes: 72 additions & 0 deletions backend/api/handler/document.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package handler

import (
"cognix.ch/api/v2/core/bll"
"cognix.ch/api/v2/core/parameters"
"cognix.ch/api/v2/core/security"
"cognix.ch/api/v2/core/server"
"fmt"
"github.com/gin-gonic/gin"
"mime/multipart"
"net/http"
"sync"
)

type DocumentHandler struct {
documentBL bll.DocumentBL
}

func NewDocumentHandler(documentBL bll.DocumentBL) *DocumentHandler {
return &DocumentHandler{documentBL: documentBL}
}

func (h *DocumentHandler) Mount(router *gin.Engine, authMiddleware gin.HandlerFunc) {
handler := router.Group("/manage/documents").Use(authMiddleware)
handler.GET("/", server.HandlerErrorFuncAuth(h.GetAll))
handler.POST("/upload", server.HandlerErrorFuncAuth(h.Upload))
}

func (h *DocumentHandler) GetAll(c *gin.Context, identity *security.Identity) error {
return nil
}

func (h *DocumentHandler) Upload(c *gin.Context, identity *security.Identity) error {
form, _ := c.MultipartForm()
files := form.File["upload[]"]

wg := sync.WaitGroup{}
result := make([]*parameters.DocumentUploadResponse, len(files))
for i, f := range files {
wg.Add(1)
go func(idx int, file multipart.FileHeader) {
defer wg.Done()
fileReader, err := file.Open()
contentType := file.Header.Get("Content-Type")
if err != nil {
result[idx] = &parameters.DocumentUploadResponse{
FileName: file.Filename,
Error: fmt.Sprintf("open file : %s", err.Error()),
Document: nil,
}
return
}
defer fileReader.Close()
document, err := h.documentBL.UploadDocument(c.Request.Context(), identity.User, file.Filename, contentType, fileReader)
if err != nil {
result[idx] = &parameters.DocumentUploadResponse{
FileName: file.Filename,
Error: fmt.Sprintf("upload document : %s", err.Error()),
Document: nil,
}
return
}
result[idx] = &parameters.DocumentUploadResponse{
FileName: file.Filename,
Error: "",
Document: document,
}
}(i, *f)
}
wg.Wait()
return server.JsonResult(c, http.StatusOK, result)
}
41 changes: 37 additions & 4 deletions backend/api/handler/persona.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewPersonaHandler(personaBL bll.PersonaBL) *PersonaHandler {
return &PersonaHandler{personaBL: personaBL}
}
func (h *PersonaHandler) Mount(route *gin.Engine, authMiddleware gin.HandlerFunc) {
handler := route.Group("/manage/persona")
handler := route.Group("/manage/personas")
handler.Use(authMiddleware)
handler.GET("/", server.HandlerErrorFuncAuth(h.GetAll))
handler.GET("/:id", server.HandlerErrorFuncAuth(h.GetByID))
Expand All @@ -35,7 +35,7 @@ func (h *PersonaHandler) Mount(route *gin.Engine, authMiddleware gin.HandlerFunc
// @Produce json
// @Security ApiKeyAuth
// @Success 200 {array} model.Persona
// @Router /manage/persona [get]
// @Router /manage/personas [get]
func (h *PersonaHandler) GetAll(c *gin.Context, identity *security.Identity) error {
personas, err := h.personaBL.GetAll(c.Request.Context(), identity.User)
if err != nil {
Expand All @@ -54,7 +54,7 @@ func (h *PersonaHandler) GetAll(c *gin.Context, identity *security.Identity) err
// @Produce json
// @Security ApiKeyAuth
// @Success 200 {object} model.Persona
// @Router /manage/embedding_models/{id} [get]
// @Router /manage/personas/{id} [get]
func (h *PersonaHandler) GetByID(c *gin.Context, identity *security.Identity) error {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id == 0 {
Expand All @@ -67,6 +67,16 @@ func (h *PersonaHandler) GetByID(c *gin.Context, identity *security.Identity) er
return server.JsonResult(c, http.StatusOK, persona)
}

// Create create persona
// @Summary create persona
// @Description create persona
// @Tags Persona
// @ID persona_create
// @Param id body parameters.PersonaParam true "persona payload"
// @Produce json
// @Security ApiKeyAuth
// @Success 201 {object} model.Persona
// @Router /manage/personas [post]
func (h *PersonaHandler) Create(c *gin.Context, identity *security.Identity) error {
var param parameters.PersonaParam
if err := c.ShouldBindJSON(&param); err != nil {
Expand All @@ -79,6 +89,29 @@ func (h *PersonaHandler) Create(c *gin.Context, identity *security.Identity) err
return server.JsonResult(c, http.StatusCreated, persona)
}

// Update update persona
// @Summary update persona
// @Description update persona
// @Tags Persona
// @ID persona_update
// @Param id path int true "persona id"
// @Param id body parameters.PersonaParam true "persona payload"
// @Produce json
// @Security ApiKeyAuth
// @Success 200 {object} model.Persona
// @Router /manage/personas/{id} [put]
func (h *PersonaHandler) Update(c *gin.Context, identity *security.Identity) error {
return server.JsonResult(c, http.StatusOK, "not implemented yet")
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id == 0 {
return utils.InvalidInput.New("id should be presented")
}
var param parameters.PersonaParam
if err = c.ShouldBindJSON(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong payload")
}
persona, err := h.personaBL.Update(c.Request.Context(), id, identity.User, &param)
if err != nil {
return err
}
return server.JsonResult(c, http.StatusOK, persona)
}
112 changes: 112 additions & 0 deletions backend/api/handler/tenant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package handler

import (
"cognix.ch/api/v2/core/bll"
"cognix.ch/api/v2/core/model"
"cognix.ch/api/v2/core/parameters"
"cognix.ch/api/v2/core/security"
"cognix.ch/api/v2/core/server"
"cognix.ch/api/v2/core/utils"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"net/http"
)

// TenantHandler handles authentication endpoints
type TenantHandler struct {
tenantBL bll.TenantBL
}

func NewTenantHandler(TenantBL bll.TenantBL) *TenantHandler {
return &TenantHandler{
tenantBL: TenantBL,
}
}

func (h *TenantHandler) Mount(route *gin.Engine, TenantMiddleware gin.HandlerFunc) {
handler := route.Group("/tenant")
handler.Use(TenantMiddleware)
handler.GET("/users", server.HandlerErrorFuncAuth(h.GetUserList))
handler.POST("/users", server.HandlerErrorFuncAuth(h.AddUser))
handler.PUT("/users/:id", server.HandlerErrorFuncAuth(h.EditUser))
}

// GetUserList return list of users
// @Summary return list of users
// @Description return list of users
// @Tags Tenant
// @ID tenant_get_users
// @Produce json
// @Security ApiKeyAuth
// @Success 200 {array} model.User
// @Router /tenant/users [get]
func (h *TenantHandler) GetUserList(c *gin.Context, identity *security.Identity) error {
users, err := h.tenantBL.GetUsers(c.Request.Context(), identity.User)
if err != nil {
return err
}
return server.JsonResult(c, http.StatusOK, users)
}

// AddUser add new user
// @Summary add new user
// @Description add new user
// @Tags Tenant
// @ID tenant_add_user
// @Param params body parameters.AddUserParam true "create user parameter"
// @Produce json
// @Security ApiKeyAuth
// @Success 200 {object} model.User
// @Router /tenant/users [post]
func (h *TenantHandler) AddUser(c *gin.Context, identity *security.Identity) error {
if !identity.User.HasRoles(model.RoleAdmin, model.RoleSuperAdmin) {
return utils.ErrorPermission.New("do not have permission")
}

var param parameters.AddUserParam
if err := c.ShouldBind(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong parameter")
}
if err := param.Validate(); err != nil {
return utils.InvalidInput.New(err.Error())
}
user, err := h.tenantBL.AddUser(c.Request.Context(), identity.User, param.Email, param.Role)
if err != nil {
return err
}
return server.JsonResult(c, http.StatusOK, user)
}

// EditUser edit user
// @Summary edit user
// @Description edit user
// @Tags Tenant
// @ID tenant_edit_user
// @Param id path string true "user id"
// @Param params body parameters.EditUserParam true "edit user parameter"
// @Produce json
// @Security ApiKeyAuth
// @Success 200 {object} model.User
// @Router /tenant/users/{id} [put]
func (h *TenantHandler) EditUser(c *gin.Context, identity *security.Identity) error {
if !identity.User.HasRoles(model.RoleAdmin, model.RoleSuperAdmin) {
return utils.ErrorPermission.New("do not have permission")
}
id := c.Param("id")
userID, err := uuid.Parse(id)
if err != nil {
return utils.ErrorPermission.Wrap(err, "wrong id parameter")
}
var param parameters.EditUserParam
if err := c.ShouldBind(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong parameter")
}
if err := param.Validate(); err != nil {
return utils.InvalidInput.New(err.Error())
}
user, err := h.tenantBL.UpdateUser(c.Request.Context(), identity.User, userID, param.Role)
if err != nil {
return err
}
return server.JsonResult(c, http.StatusOK, user)
}
3 changes: 3 additions & 0 deletions backend/api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type MountParams struct {
ChatHandler *handler.ChatHandler
PersonaHandler *handler.PersonaHandler
EmbeddingModelHandler *handler.EmbeddingModelHandler
TenantHandler *handler.TenantHandler
DocumentHandler *handler.DocumentHandler
DocumentSetHandler *handler.DocumentSetHandler
}

type Config struct {
Expand Down
7 changes: 7 additions & 0 deletions backend/api/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
var Module = fx.Options(
repository.DatabaseModule,
bll.BLLModule,
storage.MinioModule,
fx.Provide(ReadConfig,
NewRouter,
newGoogleOauthProvider,
Expand All @@ -35,6 +36,9 @@ var Module = fx.Options(
handler.NewPersonaHandler,
handler.NewChatHandler,
handler.NewEmbeddingModelHandler,
handler.NewTenantHandler,
handler.NewDocumentHandler,
handler.NewDocumentSetHandler,
),
fx.Invoke(
MountRoute,
Expand All @@ -50,6 +54,9 @@ func MountRoute(param MountParams) error {
param.ChatHandler.Mount(param.Router, param.AuthMiddleware.RequireAuth)
param.PersonaHandler.Mount(param.Router, param.AuthMiddleware.RequireAuth)
param.EmbeddingModelHandler.Mount(param.Router, param.AuthMiddleware.RequireAuth)
param.TenantHandler.Mount(param.Router, param.AuthMiddleware.RequireAuth)
param.DocumentHandler.Mount(param.Router, param.AuthMiddleware.RequireAuth)
param.DocumentSetHandler.Mount(param.Router, param.AuthMiddleware.RequireAuth)
return nil
}

Expand Down
7 changes: 7 additions & 0 deletions backend/core/ai/open-ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ func (o *openAIClient) Request(ctx context.Context, message string) (*Response,
Messages: []openai.ChatCompletionMessage{userMessage},
},
)
o.client.CreateEmbeddings(ctx, &openai.EmbeddingRequest{
Input: nil,
Model: "",
User: "",
EncodingFormat: "",
Dimensions: 0,
})
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit d567734

Please sign in to comment.