-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_index_query.go
61 lines (45 loc) · 1.07 KB
/
server_index_query.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
package api
import (
"encoding/json"
"net/http"
"github.com/adrianliechti/llama/pkg/index"
"github.com/adrianliechti/llama/pkg/to"
"github.com/go-chi/chi/v5"
)
func (s *Server) handleIndexQuery(w http.ResponseWriter, r *http.Request) {
i, err := s.Index(chi.URLParam(r, "index"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var query Query
if err := json.NewDecoder(r.Body).Decode(&query); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if len(query.Text) == 0 {
writeError(w, http.StatusBadRequest, nil)
return
}
options := &index.QueryOptions{
Limit: query.Limit,
Distance: query.Distance,
}
result, err := i.Query(r.Context(), query.Text, options)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
results := make([]Result, 0)
for _, r := range result {
results = append(results, Result{
Document: Document{
ID: r.ID,
Content: r.Content,
Metadata: r.Metadata,
},
Distance: to.Ptr(r.Distance),
})
}
writeJson(w, results)
}