-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.go
More file actions
253 lines (210 loc) · 6.4 KB
/
memory.go
File metadata and controls
253 lines (210 loc) · 6.4 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package ccatapi
import (
"fmt"
"net/http"
"net/url"
)
// memoryClient is a sub-client for the Memory API.
type memoryClient struct {
config clientConfig
}
// newMemoryClient creates a new Memory sub-client with the provided config.
func newMemoryClient(config clientConfig) *memoryClient {
client := &memoryClient{
config: config,
}
WithBaseURL(fmt.Sprintf("%s/%s", client.config.baseURL, "memory"))(&client.config)
return client
}
type RecallMemoriesResponse struct {
Query recallMemoriesResponseQuery `json:"query"`
Vectors recallMemoriesResponseVectors `json:"vectors"`
}
type recallMemoriesResponseQuery struct {
Text string `json:"text"`
Vector []float64 `json:"vector"`
}
type recallMemoriesResponseVectors struct {
Embedder string `json:"embedder"`
Collections recallMemoriesResponseVectorsCollections `json:"collections"`
}
type recallMemoriesResponseVectorsCollections struct {
Episodic []Memory `json:"episodic"`
Declarative []Memory `json:"declarative"`
Procedural []Memory `json:"procedural"`
}
type Memory struct {
PageContent string `json:"page_content"`
Metadata memoryMetadata `json:"metadata"`
Type string `json:"type"`
ID string `json:"id"`
Score float64 `json:"score"`
Vector []float64 `json:"vector"`
}
type memoryMetadata struct {
Source string `json:"source"`
When float64 `json:"when"`
}
// RecallMemories searches memories similar to given text.
func (client *memoryClient) RecallMemories(text string, k uint) (*RecallMemoriesResponse, error) {
queryParams := make(url.Values, 2)
queryParams.Set("text", text)
queryParams.Set("k", fmt.Sprint(k))
resp, err := doAPIRequest[any, RecallMemoriesResponse](
client.config,
http.MethodGet,
"recall",
queryParams,
nil,
)
if err != nil {
return nil, err
}
return resp, nil
}
// GetMemoryCollectionsResponse contains the response of a GetMemoryCollections call.
type GetMemoryCollectionsResponse struct {
// The available collections
Collections []MemoryCollection `json:"collections"`
}
// MemoryCollection contains the data about a single memory collection.
type MemoryCollection struct {
// The name of the collection
Name string `json:"name"`
// The number of vectors in the collection
VectorsCount uint `json:"vectors_count"`
}
// GetMemoryCollections returns all memories collections data.
func (client *memoryClient) GetMemoryCollections() (*GetMemoryCollectionsResponse, error) {
resp, err := doAPIRequest[any, GetMemoryCollectionsResponse](
client.config,
http.MethodGet,
"collections",
nil,
nil,
)
if err != nil {
return nil, err
}
return resp, nil
}
// WipeMemoryCollectionsResponse contains the response of a WipeMemoryCollections call.
type WipeMemoryCollectionsResponse struct {
Episodic bool `json:"episodic,omitempty"`
Declarative bool `json:"declarative,omitempty"`
Procedural bool `json:"procedural,omitempty"`
}
// WipeMemoryCollections wipes all memories collections data.
func (client *memoryClient) WipeMemoryCollections() (*WipeMemoryCollectionsResponse, error) {
resp, err := doAPIRequest[any, WipeMemoryCollectionsResponse](
client.config,
http.MethodDelete,
"collections",
nil,
nil,
)
if err != nil {
return nil, err
}
return resp, nil
}
// WipeMemoryCollection wipes all memories in a collection.
func (client *memoryClient) WipeMemoryCollection(id string) (*WipeMemoryCollectionsResponse, error) {
pathParams := fmt.Sprintf("collections/%s", id)
resp, err := doAPIRequest[any, WipeMemoryCollectionsResponse](
client.config,
http.MethodDelete,
pathParams,
nil,
nil,
)
if err != nil {
return nil, err
}
return resp, nil
}
// WipeMemoryCollectionPointResponse contains the response of a WipeMemoryCollectionPoint call.
type WipeMemoryCollectionPointResponse struct {
// The ID of the collected point
Deleted string `json:"deleted"`
}
// WipeMemoryCollectionPoint wipes a single memory in a collection.
func (client *memoryClient) WipeMemoryCollectionPoint(collectionID string, memoryID string) (*WipeMemoryCollectionsResponse, error) {
pathParams := fmt.Sprintf("collections/%s/points/%s", collectionID, memoryID)
resp, err := doAPIRequest[any, WipeMemoryCollectionsResponse](
client.config,
http.MethodDelete,
pathParams,
nil,
nil,
)
if err != nil {
return nil, err
}
return resp, nil
}
// WipeMemoryCollectionPointsByMetadata wipes all memories in a collection by metadata.
func (client *memoryClient) WipeMemoryCollectionPointsByMetadata(collectionID string, metadata map[string]any) (*WipeMemoryCollectionsResponse, error) {
pathParams := fmt.Sprintf("collections/%s/points", collectionID)
resp, err := doAPIRequest[map[string]any, WipeMemoryCollectionsResponse](
client.config,
http.MethodDelete,
pathParams,
nil,
&metadata,
)
if err != nil {
return nil, err
}
return resp, nil
}
type GetConversationHistoryResponse struct {
History []conversationMessage `json:"history"`
}
type conversationMessage struct {
Who string `json:"who"`
Message string `json:"message"`
Why conversationMessageWhy `json:"why"`
}
type conversationMessageWhy struct {
Input string `json:"input"`
IntermediateSteps []any `json:"intermediate_steps"`
Memory []conversationMessageMemoryData `json:"memory"`
}
type conversationMessageMemoryData struct {
Episodic []any `json:"episodic"`
Declarative []any `json:"declarative"`
Procedural []any `json:"procedural"`
}
// GetConversationHistory gets all conversation histories.
func (client *memoryClient) GetConversationHistory() (*GetConversationHistoryResponse, error) {
resp, err := doAPIRequest[any, GetConversationHistoryResponse](
client.config,
http.MethodGet,
"conversation_history",
nil,
nil,
)
if err != nil {
return nil, err
}
return resp, nil
}
// WipeConversationHistoryResponse contains the response of a WipeConversationHistory call.
type WipeConversationHistoryResponse struct {
Deleted bool `json:"deleted"`
}
// WipeConversationHistory wipes all conversation history.
func (client *memoryClient) WipeConversationHistory() (*WipeConversationHistoryResponse, error) {
resp, err := doAPIRequest[any, WipeConversationHistoryResponse](
client.config,
http.MethodDelete,
"conversation_history",
nil,
nil,
)
if err != nil {
return nil, err
}
return resp, nil
}