-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnamespace.go
137 lines (118 loc) · 6.08 KB
/
namespace.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
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
package vector
const deleteNamespacePath = "/delete-namespace"
const listNamespacesPath = "/list-namespaces"
type Namespace struct {
index *Index
ns string
}
// Namespace returns a new client associated with the given namespace.
func (ix *Index) Namespace(namespace string) (i *Namespace) {
return &Namespace{
index: ix,
ns: namespace,
}
}
// ListNamespaces returns the list of names of namespaces.
func (ix *Index) ListNamespaces() (namespaces []string, err error) {
data, err := ix.sendJson(listNamespacesPath, nil)
if err != nil {
return
}
namespaces, err = parseResponse[[]string](data)
return
}
// DeleteNamespace deletes the given namespace of index if it exists.
func (ns *Namespace) DeleteNamespace() error {
_, err := ns.index.sendBytes(buildPath(deleteNamespacePath, ns.ns), nil)
return err
}
// Upsert updates or inserts a vector to the namespace of the index.
// Additional metadata can also be provided while upserting the vector.
func (ns *Namespace) Upsert(u Upsert) (err error) {
return ns.index.upsertInternal(u, ns.ns)
}
// UpsertMany updates or inserts some vectors to the default namespace of the index.
// Additional metadata can also be provided for each vector.
func (ns *Namespace) UpsertMany(u []Upsert) (err error) {
return ns.index.upsertManyInternal(u, ns.ns)
}
// UpsertData updates or inserts a vector to the namespace of the index
// by converting given raw data to an embedding on the server.
// Additional metadata can also be provided while upserting the vector.
func (ns *Namespace) UpsertData(u UpsertData) (err error) {
return ns.index.upsertDataInternal(u, ns.ns)
}
// UpsertDataMany updates or inserts some vectors to the default namespace of the index
// by converting given raw data to an embedding on the server.
// Additional metadata can also be provided for each vector.
func (ns *Namespace) UpsertDataMany(u []UpsertData) (err error) {
return ns.index.upsertDataManyInternal(u, ns.ns)
}
// Fetch fetches one or more vectors in the namespace with the ids passed into f.
// If IncludeVectors is set to true, the vector values are also returned.
// If IncludeMetadata is set to true, any associated metadata of the vectors is also returned, if any.
func (ns *Namespace) Fetch(f Fetch) (vectors []Vector, err error) {
return ns.index.fetchInternal(f, ns.ns)
}
// QueryData returns the result of the query for the given data by converting it to an embedding on the server.
// When q.TopK is specified, the result will contain at most q.TopK many vectors.
// The returned list will contain vectors sorted in descending order of score,
// which correlates with the similarity of the vectors to the given query vector.
// When q.IncludeVectors is true, values of the vectors are also returned.
// When q.IncludeMetadata is true, metadata of the vectors are also returned, if any.
func (ns *Namespace) QueryData(q QueryData) (scores []VectorScore, err error) {
return ns.index.queryDataInternal(q, ns.ns)
}
// Query returns the result of the query for the given vector in the namespace.
// When q.TopK is specified, the result will contain at most q.TopK many vectors.
// The returned list will contain vectors sorted in descending order of score,
// which correlates with the similarity of the vectors to the given query vector.
// When q.IncludeVectors is true, values of the vectors are also returned.
// When q.IncludeMetadata is true, metadata of the vectors are also returned, if any.
func (ns *Namespace) Query(q Query) (scores []VectorScore, err error) {
return ns.index.queryInternal(q, ns.ns)
}
// Range returns a range of vectors, starting with r.Cursor (inclusive),
// until the end of the vectors in the index or until the given q.Limit.
// The initial cursor should be set to "0", and subsequent calls to
// Range might use the next cursor returned in the response.
// When r.IncludeVectors is true, values of the vectors are also returned.
// When r.IncludeMetadata is true, metadata of the vectors are also returned, if any.
func (ns *Namespace) Range(r Range) (vectors RangeVectors, err error) {
return ns.index.rangeInternal(r, ns.ns)
}
// Delete deletes the vector with the given id in the namespace and reports whether the vector is deleted.
// If a vector with the given id is not found, Delete returns false.
func (ns *Namespace) Delete(id string) (ok bool, err error) {
return ns.index.deleteInternal(id, ns.ns)
}
// DeleteMany deletes the vectors with the given ids in the namespace and reports how many of them are deleted.
func (ns *Namespace) DeleteMany(ids []string) (count int, err error) {
return ns.index.deleteManyInternal(ids, ns.ns)
}
// Reset deletes all the vectors in the namespace of the index and resets it to initial state.
func (ns *Namespace) Reset() (err error) {
return ns.index.resetInternal(ns.ns)
}
// Update updates a vector value, data, or metadata for the given id
// in the namespace and reports whether the vector is updated.
// If a vector with the given id is not found, Update returns false.
func (ns *Namespace) Update(u Update) (ok bool, err error) {
return ns.index.updateInternal(u, ns.ns)
}
// ResumableQuery starts a resumable query and returns the first page of the
// result of the query for the given vector in the default namespace.
// Then, next pages of the query results can be fetched over the returned handle.
// After all the needed pages of the results are fetched, it is recommended
// to close to handle to release the acquired resources.
func (ns *Namespace) ResumableQuery(q ResumableQuery) (scores []VectorScore, handle *ResumableQueryHandle, err error) {
return ns.index.resumableQueryInternal(q, ns.ns)
}
// ResumableQueryData starts a resumable query and returns the first page of the
// result of the query for the given text data in the default namespace.
// Then, next pages of the query results can be fetched over the returned handle.
// After all the needed pages of the results are fetched, it is recommended
// to close to handle to release the acquired resources.
func (ns *Namespace) ResumableQueryData(q ResumableQueryData) (scores []VectorScore, handle *ResumableQueryHandle, err error) {
return ns.index.resumableQueryDataInternal(q, ns.ns)
}