You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_hnsw.go 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package object
  15. import (
  16. "encoding/json"
  17. "io/ioutil"
  18. "sync"
  19. "github.com/casibase/casibase/util"
  20. "github.com/casibase/go-hnsw"
  21. )
  22. type HnswSearchProvider struct{}
  23. func NewHnswSearchProvider() (*HnswSearchProvider, error) {
  24. return &HnswSearchProvider{}, nil
  25. }
  26. func (p *HnswSearchProvider) Search(qVector []float32) ([]Vector, error) {
  27. return Index.Search(qVector)
  28. }
  29. var Index *HNSWIndex
  30. const (
  31. M = 64
  32. efConstruction = 400
  33. )
  34. type HNSWIndex struct {
  35. Hnsw *hnsw.Hnsw `json:"-"`
  36. Lock sync.RWMutex `json:"-"`
  37. Id uint32 `json:"id,omitempty"`
  38. IdToStr map[uint32]string `json:"id_to_str,omitempty"`
  39. StrToId map[string]uint32 `json:"str_to_id,omitempty"`
  40. }
  41. func InitHNSW() {
  42. Index = &HNSWIndex{}
  43. err := Index.load()
  44. if err != nil {
  45. Index.IdToStr = make(map[uint32]string)
  46. Index.StrToId = make(map[string]uint32)
  47. Index.Hnsw = hnsw.New(M, efConstruction, make([]float32, 128))
  48. }
  49. }
  50. func (h *HNSWIndex) Add(name string, vector []float32) error {
  51. h.Lock.Lock()
  52. h.Id++
  53. id := h.Id
  54. h.IdToStr[id] = name
  55. h.StrToId[name] = id
  56. h.Lock.Unlock()
  57. h.Hnsw.Grow(int(id + 1))
  58. h.Hnsw.Add(vector, id)
  59. return h.save()
  60. }
  61. func (h *HNSWIndex) Search(vector []float32) ([]Vector, error) {
  62. result := h.Hnsw.Search(vector, 100, 4)
  63. item := result.Pop()
  64. owner, name := util.GetOwnerAndNameFromId(h.IdToStr[item.ID])
  65. v, err := getVector(owner, name)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return []Vector{*v}, nil
  70. }
  71. func (h *HNSWIndex) save() error {
  72. h.Lock.RLock()
  73. defer h.Lock.RUnlock()
  74. data, err := json.Marshal(h)
  75. if err != nil {
  76. return err
  77. }
  78. err = ioutil.WriteFile("./hnsw", data, 0o644)
  79. if err != nil {
  80. return err
  81. }
  82. err = h.Hnsw.Save("./index")
  83. if err != nil {
  84. return err
  85. }
  86. return nil
  87. }
  88. func (h *HNSWIndex) load() error {
  89. h.Lock.Lock()
  90. defer h.Lock.Unlock()
  91. data, err := ioutil.ReadFile("./hnsw")
  92. if err != nil {
  93. return err
  94. }
  95. err = json.Unmarshal(data, h)
  96. if err != nil {
  97. return err
  98. }
  99. h.Hnsw, _, err = hnsw.Load("./index")
  100. if err != nil {
  101. return err
  102. }
  103. return nil
  104. }