-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
187 lines (146 loc) · 4.91 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/gofiber/fiber/v2"
openai "github.com/sashabaranov/go-openai"
)
func (s *LLMService) RegisterRoutes(router fiber.Router) {
router.Post("/messages", s.chat)
router.Get("/messagesdb", s.getMessagesRelational)
router.Post("/messagesdb", s.insertMessageRelational)
router.Get("/productsdb", s.getProductsRelational)
router.Post("/productsdb", s.insertProductsRelational)
router.Delete("/productsdb/:id", s.deleteProduct)
}
var weekday time.Weekday
var weekdayStr string
var date string
func (s *LLMService) chat(c *fiber.Ctx) error {
message := new(Message)
if err := c.BodyParser(message); err != nil {
fmt.Printf("Bodyparsing error: %v\n", err)
return c.Status(fiber.StatusBadRequest).SendString(err.Error())
}
// chatHistory, err := chatHistory(c, s, message)
// if err != nil {
// fmt.Printf("chatHistory fetching resulted in error: %v", err)
// c.Status(fiber.StatusInternalServerError).SendString(err.Error())
// }
var messages []openai.ChatCompletionMessage
chatMessage := append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: message.Content,
})
weekday = time.Now().Weekday()
weekdayStr = weekday.String()
date = time.Now().Format("2006-01-02")
resp, err := s.llmClient.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: os.Getenv("OPENAI_MODEL_ID"),
Messages: chatMessage,
Functions: []openai.FunctionDefinition{getProductsAndDate},
},
)
if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
return c.SendString(err.Error())
}
var arguments Arguments
incommingArguments := ""
if resp.Choices[0].Message.Content == "" {
incommingArguments = resp.Choices[0].Message.FunctionCall.Arguments
} else {
incommingArguments = resp.Choices[0].Message.Content
}
// Save entries in Message DB to build a history -> Useful for medical scenario (not vape)
s.db.Create(&Message{Content: message.Content, Role: openai.ChatMessageRoleUser})
s.db.Create(&Message{Content: incommingArguments, Role: openai.ChatMessageRoleAssistant})
json.Unmarshal([]byte(incommingArguments), &arguments)
var product Products
result := s.db.Where("product = ? OR flavor = ? OR quantity = ?", arguments.Products[0].Item, arguments.Products[0].Flavor, arguments.Products[0].Quantity).First(&product)
if result.Error != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": result.Error,
})
}
fmt.Println(product)
return c.JSON(product)
}
func (s *LLMService) getMessagesRelational(c *fiber.Ctx) error {
var messages []Message
result := s.db.Find(&messages)
if result.Error != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": result.Error,
})
}
fmt.Println(result)
return c.JSON(messages)
}
func (s *LLMService) insertMessageRelational(c *fiber.Ctx) error {
message := new(Message)
if err := c.BodyParser(message); err != nil {
return c.Status(fiber.StatusBadRequest).SendString(err.Error())
}
resp, err := s.llmClient.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: os.Getenv("OPENAI_MODEL_ID"),
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: message.Content,
},
},
},
)
if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
return c.SendString(err.Error())
}
s.db.Create(&Message{Content: message.Content, Role: openai.ChatMessageRoleUser})
s.db.Create(&Message{Content: resp.Choices[0].Message.Content, Role: openai.ChatMessageRoleAssistant})
fmt.Println(resp.Choices[0].Message.Content)
return c.SendString(resp.Choices[0].Message.Content)
}
func (s *LLMService) getProductsRelational(c *fiber.Ctx) error {
var products []Products
result := s.db.Find(&products)
if result.Error != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": result.Error,
})
}
fmt.Println(result)
return c.JSON(products)
}
func (s *LLMService) insertProductsRelational(c *fiber.Ctx) error {
product := new(Products)
if err := c.BodyParser(product); err != nil {
return c.Status(fiber.StatusBadRequest).SendString(err.Error())
}
s.db.Create(&Products{Product: strings.ToLower(product.Product), Flavor: strings.ToLower(product.Flavor), Quantity: product.Quantity})
return c.SendString("Produto salvo com sucesso!")
}
func (s *LLMService) deleteProduct(c *fiber.Ctx) error {
id := c.Params("id")
result := s.db.Unscoped().Delete(&Products{}, id)
if result.Error != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": result.Error,
})
}
if result.RowsAffected > 0 {
fmt.Println("Record deleted successfully.")
return c.SendString("Record deleted successfully.")
} else {
fmt.Println("No record found with the provided ID.")
return c.SendString("No record found with the provided ID.")
}
}