-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathroutes.go
246 lines (203 loc) · 7.17 KB
/
routes.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"crypto/subtle"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/labstack/echo/v4"
"github.com/mudler/localrecall/pkg/xlog"
"github.com/mudler/localrecall/rag"
"github.com/sashabaranov/go-openai"
)
type collectionList map[string]*rag.PersistentKB
var collections = collectionList{}
func newVectorEngine(
vectorEngineType string,
llmClient *openai.Client,
apiURL, apiKey, collectionName, dbPath, embeddingModel string, maxChunkSize int) *rag.PersistentKB {
switch vectorEngineType {
case "chromem":
return rag.NewPersistentChromeCollection(llmClient, collectionName, dbPath, fileAssets, embeddingModel, maxChunkSize)
case "localai":
return rag.NewPersistentLocalAICollection(llmClient, apiURL, apiKey, collectionName, dbPath, fileAssets, embeddingModel, maxChunkSize)
default:
xlog.Error("Unknown vector engine")
os.Exit(1)
}
return nil
}
// API routes for managing collections
func registerAPIRoutes(e *echo.Echo, openAIClient *openai.Client, maxChunkingSize int, apiKeys []string) {
// Load all collections
colls := rag.ListAllCollections(collectionDBPath)
for _, c := range colls {
collections[c] = newVectorEngine(vectorEngine, openAIClient, openAIBaseURL, openAIKey, c, collectionDBPath, embeddingModel, maxChunkingSize)
}
if len(apiKeys) > 0 {
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
apiKey := c.Request().Header.Get("Authorization")
apiKey = strings.TrimPrefix(apiKey, "Bearer ")
if len(apiKeys) == 0 {
return next(c)
}
for _, validKey := range apiKeys {
if subtle.ConstantTimeCompare([]byte(apiKey), []byte(validKey)) == 1 {
return next(c)
}
}
return c.JSON(http.StatusUnauthorized, errorMessage("Unauthorized"))
}
})
}
e.POST("/api/collections", createCollection(collections, openAIClient, embeddingModel, maxChunkingSize))
e.POST("/api/collections/:name/upload", uploadFile(collections, fileAssets))
e.GET("/api/collections", listCollections)
e.GET("/api/collections/:name/entries", listFiles(collections))
e.POST("/api/collections/:name/search", search(collections))
e.POST("/api/collections/:name/reset", reset(collections))
e.DELETE("/api/collections/:name/entry/delete", deleteEntryFromCollection(collections))
}
// createCollection handles creating a new collection
func createCollection(collections collectionList, client *openai.Client, embeddingModel string, maxChunkingSize int) func(c echo.Context) error {
return func(c echo.Context) error {
type request struct {
Name string `json:"name"`
}
r := new(request)
if err := c.Bind(r); err != nil {
return c.JSON(http.StatusBadRequest, errorMessage("Invalid request"))
}
collections[r.Name] = newVectorEngine(vectorEngine, client, openAIBaseURL, openAIKey, r.Name, collectionDBPath, embeddingModel, maxChunkingSize)
return c.JSON(http.StatusCreated, collections[r.Name])
}
}
func deleteEntryFromCollection(collections collectionList) func(c echo.Context) error {
return func(c echo.Context) error {
name := c.Param("name")
collection, exists := collections[name]
if !exists {
return c.JSON(http.StatusNotFound, errorMessage("Collection not found"))
}
type request struct {
Entry string `json:"entry"`
}
r := new(request)
if err := c.Bind(r); err != nil {
return c.JSON(http.StatusBadRequest, errorMessage("Invalid request"))
}
if err := collection.RemoveEntry(r.Entry); err != nil {
return c.JSON(http.StatusInternalServerError, errorMessage("Failed to remove entry: "+err.Error()))
}
return c.JSON(http.StatusOK, collection.ListDocuments())
}
}
func reset(collections collectionList) func(c echo.Context) error {
return func(c echo.Context) error {
name := c.Param("name")
collection, exists := collections[name]
if !exists {
return c.JSON(http.StatusNotFound, errorMessage("Collection not found"))
}
if err := collection.Reset(); err != nil {
return c.JSON(http.StatusInternalServerError, errorMessage("Failed to reset collection: "+err.Error()))
}
delete(collections, name)
return nil
}
}
func search(collections collectionList) func(c echo.Context) error {
return func(c echo.Context) error {
name := c.Param("name")
collection, exists := collections[name]
if !exists {
return c.JSON(http.StatusNotFound, errorMessage("Collection not found"))
}
type request struct {
Query string `json:"query"`
MaxResults int `json:"max_results"`
}
r := new(request)
if err := c.Bind(r); err != nil {
return c.JSON(http.StatusBadRequest, errorMessage("Invalid request"))
}
fmt.Println(r)
if r.MaxResults == 0 {
if len(collection.ListDocuments()) >= 5 {
r.MaxResults = 5
} else {
r.MaxResults = 1
}
}
results, err := collection.Search(r.Query, r.MaxResults)
if err != nil {
return c.JSON(http.StatusInternalServerError, errorMessage("Failed to search collection: "+err.Error()))
}
return c.JSON(http.StatusOK, results)
}
}
func errorMessage(message string) map[string]string {
return map[string]string{"error": message}
}
func listFiles(collections collectionList) func(c echo.Context) error {
return func(c echo.Context) error {
name := c.Param("name")
collection, exists := collections[name]
if !exists {
return c.JSON(http.StatusNotFound, errorMessage("Collection not found"))
}
return c.JSON(http.StatusOK, collection.ListDocuments())
}
}
// uploadFile handles uploading files to a collection
func uploadFile(collections collectionList, fileAssets string) func(c echo.Context) error {
return func(c echo.Context) error {
name := c.Param("name")
collection, exists := collections[name]
if !exists {
xlog.Error("Collection not found")
return c.JSON(http.StatusNotFound, errorMessage("Collection not found"))
}
file, err := c.FormFile("file")
if err != nil {
xlog.Error("Failed to read file", err)
return c.JSON(http.StatusBadRequest, errorMessage("Failed to read file: "+err.Error()))
}
f, err := file.Open()
if err != nil {
xlog.Error("Failed to open file", err)
return c.JSON(http.StatusBadRequest, errorMessage("Failed to open file: "+err.Error()))
}
defer f.Close()
filePath := filepath.Join(fileAssets, file.Filename)
out, err := os.Create(filePath)
if err != nil {
xlog.Error("Failed to create file", err)
return c.JSON(http.StatusInternalServerError, errorMessage("Failed to create file "+err.Error()))
}
defer out.Close()
_, err = io.Copy(out, f)
if err != nil {
xlog.Error("Failed to copy file", err)
return c.JSON(http.StatusInternalServerError, errorMessage("Failed to copy file: "+err.Error()))
}
if collection.EntryExists(file.Filename) {
xlog.Info("Entry already exists")
return c.JSON(http.StatusBadRequest, errorMessage("Entry already exists"))
}
// Save the file to disk
err = collection.Store(filePath)
if err != nil {
xlog.Error("Failed to store file", err)
return c.JSON(http.StatusInternalServerError, errorMessage("Failed to store file: "+err.Error()))
}
return c.JSON(http.StatusOK, collection)
}
}
// listCollections returns all collections
func listCollections(c echo.Context) error {
return c.JSON(http.StatusOK, rag.ListAllCollections(collectionDBPath))
}