UPDATE: Chatbot module
All checks were successful
Build and Release / release (push) Successful in 2m13s
All checks were successful
Build and Release / release (push) Successful in 2m13s
This commit is contained in:
60
internal/controllers/chatbotController.go
Normal file
60
internal/controllers/chatbotController.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"history-api/internal/dtos/request"
|
||||
"history-api/internal/dtos/response"
|
||||
"history-api/internal/services"
|
||||
"history-api/pkg/validator"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
type ChatbotController struct {
|
||||
chatbotService services.ChatbotService
|
||||
}
|
||||
|
||||
func NewChatbotController(chatbotService services.ChatbotService) *ChatbotController {
|
||||
return &ChatbotController{
|
||||
chatbotService: chatbotService,
|
||||
}
|
||||
}
|
||||
|
||||
// Chat godoc
|
||||
// @Summary Ask the AI chatbot
|
||||
// @Description Ask a history question based on project context or global knowledge using RAG
|
||||
// @Tags Chatbot
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param request body request.ChatbotDto true "Chatbot query"
|
||||
// @Success 200 {object} response.CommonResponse{data=string} "Successful response with AI answer"
|
||||
// @Failure 400 {object} response.CommonResponse "Invalid request"
|
||||
// @Failure 500 {object} response.CommonResponse "Internal server error"
|
||||
// @Router /chatbot/chat [post]
|
||||
func (cx *ChatbotController) Chat(c fiber.Ctx) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
dto := &request.ChatbotDto{}
|
||||
if err := validator.ValidateBodyDto(c, dto); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Errors: err,
|
||||
})
|
||||
}
|
||||
|
||||
answer, err := cx.chatbotService.Chat(ctx, dto.ProjectID, dto.Question)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
|
||||
Status: true,
|
||||
Data: answer,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user