Skip to content

Commit

Permalink
feature: artikel AI (#82)
Browse files Browse the repository at this point in the history
feature: artikel AI
  • Loading branch information
masnann authored Dec 4, 2023
2 parents 432c5ed + ae2ff8e commit b813c2c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions module/feature/chatbot/dto/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ type ChatRequest struct {
Text string `json:"text" form:"text"`
CreatedAt time.Time
}
type GenerateArtikelAiRequest struct {
Text string `json:"text" form:"text"`
}
17 changes: 17 additions & 0 deletions module/feature/chatbot/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handler
import (
"github.com/capstone-kelompok-7/backend-disappear/module/entities"
"github.com/capstone-kelompok-7/backend-disappear/module/feature/chatbot"
"github.com/capstone-kelompok-7/backend-disappear/module/feature/chatbot/dto"
"github.com/capstone-kelompok-7/backend-disappear/utils/response"
"github.com/labstack/echo/v4"
)
Expand Down Expand Up @@ -58,3 +59,19 @@ func (h *ChatbotHandler) GetChatByIdUser() echo.HandlerFunc {
return response.SendSuccessResponse(c, "Berhasil mendapatkan chat by id User", chat)
}
}

func (h *ChatbotHandler) GenerateArtikelAi() echo.HandlerFunc {
return func(c echo.Context) error {
judulRequest := new(dto.GenerateArtikelAiRequest)
if err := c.Bind(judulRequest); err != nil {
return response.SendBadRequestResponse(c, "Format input yang Anda masukkan tidak sesuai.")
}

chat, err := h.service.GenerateArtikelAi(judulRequest.Text)
if err != nil {
return response.SendStatusInternalServerResponse(c, "Gagal generate artikel: "+err.Error())
}

return response.SendSuccessResponse(c, "Berhasil mendapatkan jawaban", chat)
}
}
2 changes: 2 additions & 0 deletions module/feature/chatbot/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ type ServicChatbotInterface interface {
CreateQuestion(chat entities.ChatModel) error
CreateAnswer(chat entities.ChatModel) (string, error)
GetAnswerFromAi(chat []openai.ChatCompletionMessage, ctx context.Context) (openai.ChatCompletionResponse, error)
GenerateArtikelAi(judul string) (string, error)
}

type HandlerChatbotInterface interface {
GetChatByIdUser() echo.HandlerFunc
CreateQuestion() echo.HandlerFunc
CreateAnswer() echo.HandlerFunc
GenerateArtikelAi() echo.HandlerFunc
}
40 changes: 40 additions & 0 deletions module/feature/chatbot/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,43 @@ func (s *ChatbotService) GetChatByIdUser(id string) ([]entities.ChatModel, error
}
return res, nil
}

func (s *ChatbotService) GenerateArtikelAi(judul string) (string, error) {
ctx := context.Background()
chat := []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: "Kamu Adalah Chatbot Bertema Lingkungan",
},
{
Role: openai.ChatMessageRoleUser,
Content: "Hi Bisa Bantu Saya Menjawab Pertanyaan Tentang Lingkungan",
},
}

if judul != "" {
chat = append(chat, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: fmt.Sprintf("Buatlah Artikel Dengan Judul %s", judul),
})
}

resp, err := s.GetAnswerFromAi(chat, ctx)
if err != nil {
logrus.Error("Can't Get Answer From Ai: ", err.Error())
}

if s.debug {
fmt.Printf(
"ID: %s. Created: %d. Model: %s. Choices: %v.\n",
resp.ID, resp.Created, resp.Model, resp.Choices,
)
}

answer := openai.ChatCompletionMessage{
Role: resp.Choices[0].Message.Role,
Content: resp.Choices[0].Message.Content,
}

return answer.Content, nil
}
1 change: 1 addition & 0 deletions routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func RouteChatbot(e *echo.Echo, h chatbot.HandlerChatbotInterface, jwtService ut
chatbotGroup.POST("/question", h.CreateQuestion(), middlewares.AuthMiddleware(jwtService, userService))
chatbotGroup.POST("/answer", h.CreateAnswer(), middlewares.AuthMiddleware(jwtService, userService))
chatbotGroup.GET("/:id", h.GetChatByIdUser(), middlewares.AuthMiddleware(jwtService, userService))
chatbotGroup.POST("/generate", h.GenerateArtikelAi(), middlewares.AuthMiddleware(jwtService, userService))
}

func RouteDashboard(e *echo.Echo, h dashboard.HandlerDashboardInterface, jwtService utils.JWTInterface, userService users.ServiceUserInterface) {
Expand Down

0 comments on commit b813c2c

Please sign in to comment.