mirror of
https://github.com/mudler/LocalAI.git
synced 2025-05-28 14:35:00 +00:00
feat(stores): Vector store backend (#1795)
Add simple vector store backend Signed-off-by: Richard Palethorpe <io@richiejp.com>
This commit is contained in:
parent
4b1ee0c170
commit
643d85d2cc
30 changed files with 3250 additions and 441 deletions
15
examples/semantic-todo/README.md
Normal file
15
examples/semantic-todo/README.md
Normal file
|
@ -0,0 +1,15 @@
|
|||
This demonstrates the vector store backend in its simplest form.
|
||||
You can add tasks and then search/sort them using the TUI.
|
||||
|
||||
To build and run do
|
||||
|
||||
```bash
|
||||
$ go get .
|
||||
$ go run .
|
||||
```
|
||||
|
||||
A seperate LocaAI instance is required of course. For e.g.
|
||||
|
||||
```bash
|
||||
$ docker run -e DEBUG=true --rm -it -p 8080:8080 <LocalAI-image> bert-cpp
|
||||
```
|
18
examples/semantic-todo/go.mod
Normal file
18
examples/semantic-todo/go.mod
Normal file
|
@ -0,0 +1,18 @@
|
|||
module semantic-todo
|
||||
|
||||
go 1.21.6
|
||||
|
||||
require (
|
||||
github.com/gdamore/tcell/v2 v2.7.1
|
||||
github.com/rivo/tview v0.0.0-20240307173318-e804876934a1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/gdamore/encoding v1.0.0 // indirect
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
golang.org/x/sys v0.17.0 // indirect
|
||||
golang.org/x/term v0.17.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
)
|
50
examples/semantic-todo/go.sum
Normal file
50
examples/semantic-todo/go.sum
Normal file
|
@ -0,0 +1,50 @@
|
|||
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
|
||||
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
|
||||
github.com/gdamore/tcell/v2 v2.7.1 h1:TiCcmpWHiAU7F0rA2I3S2Y4mmLmO9KHxJ7E1QhYzQbc=
|
||||
github.com/gdamore/tcell/v2 v2.7.1/go.mod h1:dSXtXTSK0VsW1biw65DZLZ2NKr7j0qP/0J7ONmsraWg=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
||||
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/rivo/tview v0.0.0-20240307173318-e804876934a1 h1:bWLHTRekAy497pE7+nXSuzXwwFHI0XauRzz6roUvY+s=
|
||||
github.com/rivo/tview v0.0.0-20240307173318-e804876934a1/go.mod h1:02iFIz7K/A9jGCvrizLPvoqr4cEIx7q54RH5Qudkrss=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
|
||||
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
|
||||
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
352
examples/semantic-todo/main.go
Normal file
352
examples/semantic-todo/main.go
Normal file
|
@ -0,0 +1,352 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
const (
|
||||
localAI string = "http://localhost:8080"
|
||||
rootStatus string = "[::b]<space>[::-]: Add Task [::b]/[::-]: Search Task [::b]<C-c>[::-]: Exit"
|
||||
inputStatus string = "Press [::b]<enter>[::-] to submit the task, [::b]<esc>[::-] to cancel"
|
||||
)
|
||||
|
||||
type Task struct {
|
||||
Description string
|
||||
Similarity float32
|
||||
}
|
||||
|
||||
type AppState int
|
||||
|
||||
const (
|
||||
StateRoot AppState = iota
|
||||
StateInput
|
||||
StateSearch
|
||||
)
|
||||
|
||||
type App struct {
|
||||
state AppState
|
||||
tasks []Task
|
||||
app *tview.Application
|
||||
flex *tview.Flex
|
||||
table *tview.Table
|
||||
}
|
||||
|
||||
func NewApp() *App {
|
||||
return &App{
|
||||
state: StateRoot,
|
||||
tasks: []Task{
|
||||
{Description: "Take the dog for a walk (after I get a dog)"},
|
||||
{Description: "Go to the toilet"},
|
||||
{Description: "Allow TODOs to be marked completed or removed"},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func getEmbeddings(description string) ([]float32, error) {
|
||||
// Define the request payload
|
||||
payload := map[string]interface{}{
|
||||
"model": "bert-cpp-minilm-v6",
|
||||
"input": description,
|
||||
}
|
||||
|
||||
// Marshal the payload into JSON
|
||||
jsonPayload, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Make the HTTP request to the local OpenAI embeddings API
|
||||
resp, err := http.Post(localAI+"/embeddings", "application/json", bytes.NewBuffer(jsonPayload))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Check if the request was successful
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("request to embeddings API failed with status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// Parse the response body
|
||||
var result struct {
|
||||
Data []struct {
|
||||
Embedding []float32 `json:"embedding"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Return the embedding
|
||||
if len(result.Data) > 0 {
|
||||
return result.Data[0].Embedding, nil
|
||||
}
|
||||
return nil, errors.New("no embedding received from API")
|
||||
}
|
||||
|
||||
type StoresSet struct {
|
||||
Store string `json:"store,omitempty" yaml:"store,omitempty"`
|
||||
|
||||
Keys [][]float32 `json:"keys" yaml:"keys"`
|
||||
Values []string `json:"values" yaml:"values"`
|
||||
}
|
||||
|
||||
func postTasksToExternalService(tasks []Task) error {
|
||||
keys := make([][]float32, 0, len(tasks))
|
||||
// Get the embeddings for the task description
|
||||
for _, task := range tasks {
|
||||
embedding, err := getEmbeddings(task.Description)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
keys = append(keys, embedding)
|
||||
}
|
||||
|
||||
values := make([]string, 0, len(tasks))
|
||||
for _, task := range tasks {
|
||||
values = append(values, task.Description)
|
||||
}
|
||||
|
||||
// Construct the StoresSet object
|
||||
storesSet := StoresSet{
|
||||
Store: "tasks_store", // Assuming you have a specific store name
|
||||
Keys: keys,
|
||||
Values: values,
|
||||
}
|
||||
|
||||
// Marshal the StoresSet object into JSON
|
||||
jsonData, err := json.Marshal(storesSet)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Make the HTTP POST request to the external service
|
||||
resp, err := http.Post(localAI+"/stores/set", "application/json", bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Check if the request was successful
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
// read resp body into string
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("store request failed with status code: %d: %s", resp.StatusCode, body)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type StoresFind struct {
|
||||
Store string `json:"store,omitempty" yaml:"store,omitempty"`
|
||||
|
||||
Key []float32 `json:"key" yaml:"key"`
|
||||
Topk int `json:"topk" yaml:"topk"`
|
||||
}
|
||||
|
||||
type StoresFindResponse struct {
|
||||
Keys [][]float32 `json:"keys" yaml:"keys"`
|
||||
Values []string `json:"values" yaml:"values"`
|
||||
Similarities []float32 `json:"similarities" yaml:"similarities"`
|
||||
}
|
||||
|
||||
func findSimilarTexts(inputText string, topk int) (StoresFindResponse, error) {
|
||||
// Initialize an empty response object
|
||||
response := StoresFindResponse{}
|
||||
|
||||
// Get the embedding for the input text
|
||||
embedding, err := getEmbeddings(inputText)
|
||||
if err != nil {
|
||||
return response, err
|
||||
}
|
||||
|
||||
// Construct the StoresFind object
|
||||
storesFind := StoresFind{
|
||||
Store: "tasks_store", // Assuming you have a specific store name
|
||||
Key: embedding,
|
||||
Topk: topk,
|
||||
}
|
||||
|
||||
// Marshal the StoresFind object into JSON
|
||||
jsonData, err := json.Marshal(storesFind)
|
||||
if err != nil {
|
||||
return response, err
|
||||
}
|
||||
|
||||
// Make the HTTP POST request to the external service's /stores/find endpoint
|
||||
resp, err := http.Post(localAI+"/stores/find", "application/json", bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return response, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Check if the request was successful
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return response, fmt.Errorf("request to /stores/find failed with status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// Parse the response body to retrieve similar texts and similarities
|
||||
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
|
||||
return response, err
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (app *App) updateUI() {
|
||||
// Clear the flex layout
|
||||
app.flex.Clear()
|
||||
app.flex.SetDirection(tview.FlexColumn)
|
||||
app.flex.AddItem(nil, 0, 1, false)
|
||||
|
||||
midCol := tview.NewFlex()
|
||||
midCol.SetDirection(tview.FlexRow)
|
||||
midCol.AddItem(nil, 0, 1, false)
|
||||
|
||||
// Create a new table.
|
||||
app.table.Clear()
|
||||
app.table.SetBorders(true)
|
||||
|
||||
// Set table headers
|
||||
app.table.SetCell(0, 0, tview.NewTableCell("Description").SetAlign(tview.AlignLeft).SetExpansion(1).SetAttributes(tcell.AttrBold))
|
||||
app.table.SetCell(0, 1, tview.NewTableCell("Similarity").SetAlign(tview.AlignCenter).SetExpansion(0).SetAttributes(tcell.AttrBold))
|
||||
|
||||
// Add the tasks to the table.
|
||||
for i, task := range app.tasks {
|
||||
row := i + 1
|
||||
app.table.SetCell(row, 0, tview.NewTableCell(task.Description))
|
||||
app.table.SetCell(row, 1, tview.NewTableCell(fmt.Sprintf("%.2f", task.Similarity)))
|
||||
}
|
||||
|
||||
if app.state == StateInput {
|
||||
inputField := tview.NewInputField()
|
||||
inputField.
|
||||
SetLabel("New Task: ").
|
||||
SetFieldWidth(0).
|
||||
SetDoneFunc(func(key tcell.Key) {
|
||||
if key == tcell.KeyEnter {
|
||||
task := Task{Description: inputField.GetText()}
|
||||
app.tasks = append(app.tasks, task)
|
||||
app.state = StateRoot
|
||||
postTasksToExternalService([]Task{task})
|
||||
}
|
||||
app.updateUI()
|
||||
})
|
||||
midCol.AddItem(inputField, 3, 2, true)
|
||||
app.app.SetFocus(inputField)
|
||||
} else if app.state == StateSearch {
|
||||
searchField := tview.NewInputField()
|
||||
searchField.SetLabel("Search: ").
|
||||
SetFieldWidth(0).
|
||||
SetDoneFunc(func(key tcell.Key) {
|
||||
if key == tcell.KeyEnter {
|
||||
similar, err := findSimilarTexts(searchField.GetText(), 100)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
app.tasks = make([]Task, len(similar.Keys))
|
||||
for i, v := range similar.Values {
|
||||
app.tasks[i] = Task{Description: v, Similarity: similar.Similarities[i]}
|
||||
}
|
||||
}
|
||||
app.updateUI()
|
||||
})
|
||||
midCol.AddItem(searchField, 3, 2, true)
|
||||
app.app.SetFocus(searchField)
|
||||
} else {
|
||||
midCol.AddItem(nil, 3, 1, false)
|
||||
}
|
||||
|
||||
midCol.AddItem(app.table, 0, 2, true)
|
||||
|
||||
// Add the status bar to the flex layout
|
||||
statusBar := tview.NewTextView().
|
||||
SetText(rootStatus).
|
||||
SetDynamicColors(true).
|
||||
SetTextAlign(tview.AlignCenter)
|
||||
if app.state == StateInput {
|
||||
statusBar.SetText(inputStatus)
|
||||
}
|
||||
midCol.AddItem(statusBar, 1, 1, false)
|
||||
midCol.AddItem(nil, 0, 1, false)
|
||||
|
||||
app.flex.AddItem(midCol, 0, 10, true)
|
||||
app.flex.AddItem(nil, 0, 1, false)
|
||||
|
||||
// Set the flex as the root element
|
||||
app.app.SetRoot(app.flex, true)
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := NewApp()
|
||||
tApp := tview.NewApplication()
|
||||
flex := tview.NewFlex().SetDirection(tview.FlexRow)
|
||||
table := tview.NewTable()
|
||||
|
||||
app.app = tApp
|
||||
app.flex = flex
|
||||
app.table = table
|
||||
|
||||
app.updateUI() // Initial UI setup
|
||||
|
||||
app.app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
||||
switch app.state {
|
||||
case StateRoot:
|
||||
// Handle key events when in the root state
|
||||
switch event.Key() {
|
||||
case tcell.KeyRune:
|
||||
switch event.Rune() {
|
||||
case ' ':
|
||||
app.state = StateInput
|
||||
app.updateUI()
|
||||
return nil // Event is handled
|
||||
case '/':
|
||||
app.state = StateSearch
|
||||
app.updateUI()
|
||||
return nil // Event is handled
|
||||
}
|
||||
}
|
||||
|
||||
case StateInput:
|
||||
// Handle key events when in the input state
|
||||
if event.Key() == tcell.KeyEsc {
|
||||
// Exit input state without adding a task
|
||||
app.state = StateRoot
|
||||
app.updateUI()
|
||||
return nil // Event is handled
|
||||
}
|
||||
|
||||
case StateSearch:
|
||||
// Handle key events when in the search state
|
||||
if event.Key() == tcell.KeyEsc {
|
||||
// Exit search state
|
||||
app.state = StateRoot
|
||||
app.updateUI()
|
||||
return nil // Event is handled
|
||||
}
|
||||
}
|
||||
|
||||
// Return the event for further processing by tview
|
||||
return event
|
||||
})
|
||||
|
||||
if err := postTasksToExternalService(app.tasks); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Start the application
|
||||
if err := app.app.Run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue