File size: 2,621 Bytes
7def60a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package localai

import (
	"github.com/gofiber/fiber/v2"
	"github.com/mudler/LocalAI/core/backend"
	"github.com/mudler/LocalAI/core/config"
	"github.com/mudler/LocalAI/core/schema"
	"github.com/mudler/LocalAI/pkg/model"
	"github.com/mudler/LocalAI/pkg/store"
)

func StoresSetEndpoint(sl *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
	return func(c *fiber.Ctx) error {
		input := new(schema.StoresSet)

		if err := c.BodyParser(input); err != nil {
			return err
		}

		sb, err := backend.StoreBackend(sl, appConfig, input.Store)
		if err != nil {
			return err
		}

		vals := make([][]byte, len(input.Values))
		for i, v := range input.Values {
			vals[i] = []byte(v)
		}

		err = store.SetCols(c.Context(), sb, input.Keys, vals)
		if err != nil {
			return err
		}

		return c.Send(nil)
	}
}

func StoresDeleteEndpoint(sl *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
	return func(c *fiber.Ctx) error {
		input := new(schema.StoresDelete)

		if err := c.BodyParser(input); err != nil {
			return err
		}

		sb, err := backend.StoreBackend(sl, appConfig, input.Store)
		if err != nil {
			return err
		}

		if err := store.DeleteCols(c.Context(), sb, input.Keys); err != nil {
			return err
		}

		return c.Send(nil)
	}
}

func StoresGetEndpoint(sl *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
	return func(c *fiber.Ctx) error {
		input := new(schema.StoresGet)

		if err := c.BodyParser(input); err != nil {
			return err
		}

		sb, err := backend.StoreBackend(sl, appConfig, input.Store)
		if err != nil {
			return err
		}

		keys, vals, err := store.GetCols(c.Context(), sb, input.Keys)
		if err != nil {
			return err
		}

		res := schema.StoresGetResponse{
			Keys:   keys,
			Values: make([]string, len(vals)),
		}

		for i, v := range vals {
			res.Values[i] = string(v)
		}

		return c.JSON(res)
	}
}

func StoresFindEndpoint(sl *model.ModelLoader, appConfig *config.ApplicationConfig) func(c *fiber.Ctx) error {
	return func(c *fiber.Ctx) error {
		input := new(schema.StoresFind)

		if err := c.BodyParser(input); err != nil {
			return err
		}

		sb, err := backend.StoreBackend(sl, appConfig, input.Store)
		if err != nil {
			return err
		}

		keys, vals, similarities, err := store.Find(c.Context(), sb, input.Key, input.Topk)
		if err != nil {
			return err
		}

		res := schema.StoresFindResponse{
			Keys:         keys,
			Values:       make([]string, len(vals)),
			Similarities: similarities,
		}

		for i, v := range vals {
			res.Values[i] = string(v)
		}

		return c.JSON(res)
	}
}