-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathroutes.go
More file actions
516 lines (444 loc) · 17.8 KB
/
routes.go
File metadata and controls
516 lines (444 loc) · 17.8 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package main
import (
"crypto/subtle"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/labstack/echo/v4"
"github.com/mudler/localrecall/rag"
"github.com/mudler/xlog"
"github.com/sashabaranov/go-openai"
)
type collectionList map[string]*rag.PersistentKB
var collections = collectionList{}
// APIResponse represents a standardized API response
type APIResponse struct {
Success bool `json:"success"`
Message string `json:"message,omitempty"`
Data interface{} `json:"data,omitempty"`
Error *APIError `json:"error,omitempty"`
}
// APIError represents a detailed error response
type APIError struct {
Code string `json:"code"`
Message string `json:"message"`
Details string `json:"details,omitempty"`
}
// Error codes
const (
ErrCodeNotFound = "NOT_FOUND"
ErrCodeInvalidRequest = "INVALID_REQUEST"
ErrCodeInternalError = "INTERNAL_ERROR"
ErrCodeUnauthorized = "UNAUTHORIZED"
ErrCodeConflict = "CONFLICT"
)
func successResponse(message string, data interface{}) APIResponse {
return APIResponse{
Success: true,
Message: message,
Data: data,
}
}
func errorResponse(code string, message string, details string) APIResponse {
return APIResponse{
Success: false,
Error: &APIError{
Code: code,
Message: message,
Details: details,
},
}
}
func newVectorEngine(
vectorEngineType string,
llmClient *openai.Client,
apiURL, apiKey, collectionName, dbPath, embeddingModel string, maxChunkSize, chunkOverlap int) *rag.PersistentKB {
switch vectorEngineType {
case "chromem":
xlog.Info("Chromem collection", "collectionName", collectionName, "dbPath", dbPath)
return rag.NewPersistentChromeCollection(llmClient, collectionName, dbPath, fileAssets, embeddingModel, maxChunkSize, chunkOverlap)
case "localai":
xlog.Info("LocalAI collection", "collectionName", collectionName, "apiURL", apiURL)
return rag.NewPersistentLocalAICollection(llmClient, apiURL, apiKey, collectionName, dbPath, fileAssets, embeddingModel, maxChunkSize, chunkOverlap)
case "postgres":
databaseURL := os.Getenv("DATABASE_URL")
if databaseURL == "" {
xlog.Error("DATABASE_URL is required for PostgreSQL engine")
os.Exit(1)
}
xlog.Info("PostgreSQL collection", "collectionName", collectionName, "databaseURL", databaseURL)
return rag.NewPersistentPostgresCollection(llmClient, collectionName, dbPath, fileAssets, embeddingModel, maxChunkSize, chunkOverlap, databaseURL)
default:
xlog.Error("Unknown vector engine", "engine", vectorEngineType)
os.Exit(1)
}
return nil
}
// API routes for managing collections
func registerAPIRoutes(e *echo.Echo, openAIClient *openai.Client, maxChunkingSize, chunkOverlap int, apiKeys []string) {
// Load all collections
colls := rag.ListAllCollections(collectionDBPath)
for _, c := range colls {
collection := newVectorEngine(vectorEngine, openAIClient, openAIBaseURL, openAIKey, c, collectionDBPath, embeddingModel, maxChunkingSize, chunkOverlap)
collections[c] = collection
// Register the collection with the source manager
sourceManager.RegisterCollection(c, collection)
}
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, errorResponse(ErrCodeUnauthorized, "Unauthorized", "Invalid or missing API key"))
}
})
}
e.POST("/api/collections", createCollection(collections, openAIClient, embeddingModel, maxChunkingSize, chunkOverlap))
e.POST("/api/collections/:name/upload", uploadFile(collections, fileAssets))
e.GET("/api/collections", listCollections)
e.GET("/api/collections/:name/entries", listFiles(collections))
e.GET("/api/collections/:name/entries/:entry", getEntryContent(collections))
e.GET("/api/collections/:name/entries/:entry/raw", getEntryRawFile(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))
e.POST("/api/collections/:name/sources", registerExternalSource(collections))
e.DELETE("/api/collections/:name/sources", removeExternalSource(collections))
e.GET("/api/collections/:name/sources", listSources(collections))
}
// createCollection handles creating a new collection
func createCollection(collections collectionList, client *openai.Client, embeddingModel string, maxChunkingSize, chunkOverlap 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, errorResponse(ErrCodeInvalidRequest, "Invalid request", err.Error()))
}
collection := newVectorEngine(vectorEngine, client, openAIBaseURL, openAIKey, r.Name, collectionDBPath, embeddingModel, maxChunkingSize, chunkOverlap)
collections[r.Name] = collection
// Register the new collection with the source manager
sourceManager.RegisterCollection(r.Name, collection)
response := successResponse("Collection created successfully", map[string]interface{}{
"name": r.Name,
"created_at": time.Now().Format(time.RFC3339),
})
return c.JSON(http.StatusCreated, response)
}
}
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, errorResponse(ErrCodeNotFound, "Collection not found", fmt.Sprintf("Collection '%s' does not exist", name)))
}
type request struct {
Entry string `json:"entry"`
}
r := new(request)
if err := c.Bind(r); err != nil {
return c.JSON(http.StatusBadRequest, errorResponse(ErrCodeInvalidRequest, "Invalid request", err.Error()))
}
if err := collection.RemoveEntry(r.Entry); err != nil {
return c.JSON(http.StatusInternalServerError, errorResponse(ErrCodeInternalError, "Failed to remove entry", err.Error()))
}
remainingEntries := collection.ListDocuments()
response := successResponse("Entry deleted successfully", map[string]interface{}{
"deleted_entry": r.Entry,
"remaining_entries": remainingEntries,
"entry_count": len(remainingEntries),
})
return c.JSON(http.StatusOK, response)
}
}
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, errorResponse(ErrCodeNotFound, "Collection not found", fmt.Sprintf("Collection '%s' does not exist", name)))
}
if err := collection.Reset(); err != nil {
return c.JSON(http.StatusInternalServerError, errorResponse(ErrCodeInternalError, "Failed to reset collection", err.Error()))
}
delete(collections, name)
response := successResponse("Collection reset successfully", map[string]interface{}{
"collection": name,
"reset_at": time.Now().Format(time.RFC3339),
})
return c.JSON(http.StatusOK, response)
}
}
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, errorResponse(ErrCodeNotFound, "Collection not found", fmt.Sprintf("Collection '%s' does not exist", name)))
}
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, errorResponse(ErrCodeInvalidRequest, "Invalid request", err.Error()))
}
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, errorResponse(ErrCodeInternalError, "Failed to search collection", err.Error()))
}
response := successResponse("Search completed successfully", map[string]interface{}{
"query": r.Query,
"max_results": r.MaxResults,
"results": results,
"count": len(results),
})
return c.JSON(http.StatusOK, response)
}
}
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, errorResponse(ErrCodeNotFound, "Collection not found", fmt.Sprintf("Collection '%s' does not exist", name)))
}
keys := collection.ListDocuments()
// Return original filenames for backward compatibility
entries := make([]string, len(keys))
for i, k := range keys {
entries[i] = filepath.Base(k)
}
response := successResponse("Entries retrieved successfully", map[string]interface{}{
"collection": name,
"entries": entries,
"keys": keys,
"count": len(entries),
})
return c.JSON(http.StatusOK, response)
}
}
// getEntryContent returns the full content of the stored file (no chunk overlap) and the number of chunks it occupies.
func getEntryContent(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, errorResponse(ErrCodeNotFound, "Collection not found", fmt.Sprintf("Collection '%s' does not exist", name)))
}
entryParam := c.Param("entry")
entry, err := url.PathUnescape(entryParam)
if err != nil {
entry = entryParam
}
content, chunkCount, err := collection.GetEntryFileContent(entry)
if err != nil {
if strings.Contains(err.Error(), "entry not found") {
return c.JSON(http.StatusNotFound, errorResponse(ErrCodeNotFound, "Entry not found", fmt.Sprintf("Entry '%s' does not exist in collection '%s'", entry, name)))
}
if strings.Contains(err.Error(), "not implemented") || strings.Contains(err.Error(), "unsupported file type") {
return c.JSON(http.StatusNotImplemented, errorResponse(ErrCodeInternalError, "Not supported", err.Error()))
}
return c.JSON(http.StatusInternalServerError, errorResponse(ErrCodeInternalError, "Failed to get entry content", err.Error()))
}
response := successResponse("Entry content retrieved successfully", map[string]interface{}{
"collection": name,
"entry": entry,
"content": content,
"chunk_count": chunkCount,
})
return c.JSON(http.StatusOK, response)
}
}
// getEntryRawFile returns the original uploaded binary file.
func getEntryRawFile(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, errorResponse(ErrCodeNotFound, "Collection not found", fmt.Sprintf("Collection '%s' does not exist", name)))
}
entryParam := c.Param("entry")
entry, err := url.PathUnescape(entryParam)
if err != nil {
entry = entryParam
}
fpath, err := collection.GetEntryFilePath(entry)
if err != nil {
return c.JSON(http.StatusNotFound, errorResponse(ErrCodeNotFound, "Entry not found", fmt.Sprintf("Entry '%s' does not exist in collection '%s'", entry, name)))
}
return c.File(fpath)
}
}
// 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, errorResponse(ErrCodeNotFound, "Collection not found", fmt.Sprintf("Collection '%s' does not exist", name)))
}
file, err := c.FormFile("file")
if err != nil {
xlog.Error("Failed to read file", err)
return c.JSON(http.StatusBadRequest, errorResponse(ErrCodeInvalidRequest, "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, errorResponse(ErrCodeInvalidRequest, "Failed to open file", err.Error()))
}
defer f.Close()
// Write to a temp file; collection.Store() will copy it into the
// correct UUID subdirectory under the collection's asset dir.
tmpFile, err := os.CreateTemp("", "localrecall-upload-*-"+file.Filename)
if err != nil {
xlog.Error("Failed to create temp file", err)
return c.JSON(http.StatusInternalServerError, errorResponse(ErrCodeInternalError, "Failed to create temp file", err.Error()))
}
tmpPath := tmpFile.Name()
defer os.Remove(tmpPath)
_, err = io.Copy(tmpFile, f)
tmpFile.Close()
if err != nil {
xlog.Error("Failed to copy file", err)
return c.JSON(http.StatusInternalServerError, errorResponse(ErrCodeInternalError, "Failed to copy file", err.Error()))
}
// Rename the temp file so its base name matches the original filename,
// since collection.Store uses filepath.Base to derive the index key.
uploadPath := filepath.Join(filepath.Dir(tmpPath), file.Filename)
if err := os.Rename(tmpPath, uploadPath); err != nil {
xlog.Error("Failed to rename temp file", err)
return c.JSON(http.StatusInternalServerError, errorResponse(ErrCodeInternalError, "Failed to rename temp file", err.Error()))
}
defer os.Remove(uploadPath)
now := time.Now().Format(time.RFC3339)
// Save the file to disk
key, err := collection.Store(uploadPath, map[string]string{"created_at": now})
if err != nil {
xlog.Error("Failed to store file", err)
return c.JSON(http.StatusInternalServerError, errorResponse(ErrCodeInternalError, "Failed to store file", err.Error()))
}
response := successResponse("File uploaded successfully", map[string]interface{}{
"filename": file.Filename,
"collection": name,
"key": key,
"created_at": now,
})
return c.JSON(http.StatusOK, response)
}
}
// listCollections returns all collections
func listCollections(c echo.Context) error {
collectionsList := rag.ListAllCollections(collectionDBPath)
response := successResponse("Collections retrieved successfully", map[string]interface{}{
"collections": collectionsList,
"count": len(collectionsList),
})
return c.JSON(http.StatusOK, response)
}
// registerExternalSource handles registering an external source for a collection
func registerExternalSource(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, errorResponse(ErrCodeNotFound, "Collection not found", fmt.Sprintf("Collection '%s' does not exist", name)))
}
type request struct {
URL string `json:"url"`
UpdateInterval int `json:"update_interval"` // in minutes
}
r := new(request)
if err := c.Bind(r); err != nil {
return c.JSON(http.StatusBadRequest, errorResponse(ErrCodeInvalidRequest, "Invalid request", err.Error()))
}
if r.UpdateInterval < 1 {
r.UpdateInterval = 60 // default to 1 hour if not specified
}
// Register the collection with the source manager if not already registered
sourceManager.RegisterCollection(name, collection)
// Add the source to the manager
if err := sourceManager.AddSource(name, r.URL, time.Duration(r.UpdateInterval)*time.Minute); err != nil {
return c.JSON(http.StatusInternalServerError, errorResponse(ErrCodeInternalError, "Failed to register source", err.Error()))
}
response := successResponse("External source registered successfully", map[string]interface{}{
"collection": name,
"url": r.URL,
"update_interval": r.UpdateInterval,
})
return c.JSON(http.StatusOK, response)
}
}
// removeExternalSource handles removing an external source from a collection
func removeExternalSource(collections collectionList) func(c echo.Context) error {
return func(c echo.Context) error {
name := c.Param("name")
type request struct {
URL string `json:"url"`
}
r := new(request)
if err := c.Bind(r); err != nil {
return c.JSON(http.StatusBadRequest, errorResponse(ErrCodeInvalidRequest, "Invalid request", err.Error()))
}
if err := sourceManager.RemoveSource(name, r.URL); err != nil {
return c.JSON(http.StatusInternalServerError, errorResponse(ErrCodeInternalError, "Failed to remove source", err.Error()))
}
response := successResponse("External source removed successfully", map[string]interface{}{
"collection": name,
"url": r.URL,
})
return c.JSON(http.StatusOK, response)
}
}
// listSources handles listing external sources for a collection
func listSources(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, errorResponse(ErrCodeNotFound, "Collection not found", fmt.Sprintf("Collection '%s' does not exist", name)))
}
// Get sources from the collection
sources := collection.GetExternalSources()
// Convert sources to a more frontend-friendly format
sourcesList := []map[string]interface{}{}
for _, source := range sources {
sourcesList = append(sourcesList, map[string]interface{}{
"url": source.URL,
"update_interval": int(source.UpdateInterval.Minutes()),
"last_update": source.LastUpdate.Format(time.RFC3339),
})
}
response := successResponse("Sources retrieved successfully", map[string]interface{}{
"collection": name,
"sources": sourcesList,
"count": len(sourcesList),
})
return c.JSON(http.StatusOK, response)
}
}