forked from SciSharp/BotSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMongoRepository.Log.cs
More file actions
412 lines (368 loc) · 14.8 KB
/
Copy pathMongoRepository.Log.cs
File metadata and controls
412 lines (368 loc) · 14.8 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Repositories.Models;
using MongoDB.Driver;
using System.Text.Json;
namespace BotSharp.Plugin.MongoStorage.Repository;
public partial class MongoRepository
{
#region LLM Completion Log
public async Task SaveLlmCompletionLog(LlmCompletionLog log)
{
if (log == null) return;
var conversationId = log.ConversationId.IfNullOrEmptyAs(Guid.NewGuid().ToString());
var messageId = log.MessageId.IfNullOrEmptyAs(Guid.NewGuid().ToString());
var data = new LlmCompletionLogDocument
{
Id = Guid.NewGuid().ToString(),
ConversationId = conversationId,
MessageId = messageId,
AgentId = log.AgentId,
Prompt = log.Prompt,
Response = log.Response,
CreatedTime = log.CreatedTime
};
await _dc.LlmCompletionLogs.InsertOneAsync(data);
}
#endregion
#region Conversation Content Log
public async Task SaveConversationContentLog(ContentLogOutputModel log)
{
if (log == null) return;
var filter = Builders<ConversationDocument>.Filter.Eq(x => x.Id, log.ConversationId);
var found = await _dc.Conversations.Find(filter).FirstOrDefaultAsync();
if (found == null) return;
var logDoc = new ConversationContentLogDocument
{
ConversationId = log.ConversationId,
MessageId = log.MessageId,
Name = log.Name,
AgentId = log.AgentId,
Role = log.Role,
Source = log.Source,
Content = log.Content,
CreatedTime = log.CreatedTime
};
await _dc.ContentLogs.InsertOneAsync(logDoc);
}
public async Task<DateTimePagination<ContentLogOutputModel>> GetConversationContentLogs(string conversationId, ConversationLogFilter filter)
{
var builder = Builders<ConversationContentLogDocument>.Filter;
var logFilters = new List<FilterDefinition<ConversationContentLogDocument>>
{
builder.Eq(x => x.ConversationId, conversationId),
builder.Lt(x => x.CreatedTime, filter.StartTime)
};
var logSortDef = Builders<ConversationContentLogDocument>.Sort.Descending(x => x.CreatedTime);
var docs = await _dc.ContentLogs.Find(builder.And(logFilters)).Sort(logSortDef).Limit(filter.Size).ToListAsync();
var logs = docs.Select(x => new ContentLogOutputModel
{
ConversationId = x.ConversationId,
MessageId = x.MessageId,
Name = x.Name,
AgentId = x.AgentId,
Role = x.Role,
Source = x.Source,
Content = x.Content,
CreatedTime = x.CreatedTime
}).ToList();
logs.Reverse();
return new DateTimePagination<ContentLogOutputModel>
{
Items = logs,
Count = logs.Count,
NextTime = logs.FirstOrDefault()?.CreatedTime
};
}
#endregion
#region Conversation State Log
public async Task SaveConversationStateLog(ConversationStateLogModel log)
{
if (log == null) return;
var filter = Builders<ConversationDocument>.Filter.Eq(x => x.Id, log.ConversationId);
var found = await _dc.Conversations.Find(filter).FirstOrDefaultAsync();
if (found == null) return;
var logDoc = new ConversationStateLogDocument
{
ConversationId = log.ConversationId,
AgentId= log.AgentId,
MessageId = log.MessageId,
States = log.States,
CreatedTime = log.CreatedTime
};
await _dc.StateLogs.InsertOneAsync(logDoc);
}
public async Task<DateTimePagination<ConversationStateLogModel>> GetConversationStateLogs(string conversationId, ConversationLogFilter filter)
{
var builder = Builders<ConversationStateLogDocument>.Filter;
var logFilters = new List<FilterDefinition<ConversationStateLogDocument>>
{
builder.Eq(x => x.ConversationId, conversationId),
builder.Lt(x => x.CreatedTime, filter.StartTime)
};
var logSortDef = Builders<ConversationStateLogDocument>.Sort.Descending(x => x.CreatedTime);
var docs = await _dc.StateLogs.Find(builder.And(logFilters)).Sort(logSortDef).Limit(filter.Size).ToListAsync();
var logs = docs.Select(x => new ConversationStateLogModel
{
ConversationId = x.ConversationId,
AgentId = x.AgentId,
MessageId = x.MessageId,
States = x.States,
CreatedTime = x.CreatedTime
}).ToList();
logs.Reverse();
return new DateTimePagination<ConversationStateLogModel>
{
Items = logs,
Count = logs.Count,
NextTime = logs.FirstOrDefault()?.CreatedTime
};
}
#endregion
#region Log Cleanup
public async Task<int> DeleteOldConversationLogs(int retentionDays, int batchSize)
{
if (retentionDays <= 0) return 0;
var threshold = DateTime.UtcNow.AddDays(-retentionDays);
var contentLogFilter = Builders<ConversationContentLogDocument>.Filter.Lt(x => x.CreatedTime, threshold);
var stateLogFilter = Builders<ConversationStateLogDocument>.Filter.Lt(x => x.CreatedTime, threshold);
var contentDocsToDelete = await _dc.ContentLogs.Find(contentLogFilter).Limit(batchSize).Project(x => x.Id).ToListAsync();
long contentDeletedCount = 0;
if (contentDocsToDelete.Any())
{
var deleteFilter = Builders<ConversationContentLogDocument>.Filter.In(x => x.Id, contentDocsToDelete);
var contentDeleted = await _dc.ContentLogs.DeleteManyAsync(deleteFilter);
contentDeletedCount = contentDeleted.DeletedCount;
}
var stateDocsToDelete = await _dc.StateLogs.Find(stateLogFilter).Limit(batchSize).Project(x => x.Id).ToListAsync();
long stateDeletedCount = 0;
if (stateDocsToDelete.Any())
{
var deleteFilter = Builders<ConversationStateLogDocument>.Filter.In(x => x.Id, stateDocsToDelete);
var stateDeleted = await _dc.StateLogs.DeleteManyAsync(deleteFilter);
stateDeletedCount = stateDeleted.DeletedCount;
}
return (int)(contentDeletedCount + stateDeletedCount);
}
#endregion
#region Instruction Log
public async Task<bool> SaveInstructionLogs(IEnumerable<InstructionLogModel> logs)
{
if (logs.IsNullOrEmpty())
{
return false;
}
var docs = new List<InstructionLogDocument>();
foreach (var log in logs)
{
var doc = InstructionLogDocument.ToMongoModel(log);
foreach (var pair in log.States)
{
try
{
var jsonStr = JsonSerializer.Serialize(new
{
Data = JsonDocument.Parse(pair.Value),
StringfyData = pair.Value
}, _botSharpOptions.JsonSerializerOptions);
var json = BsonDocument.Parse(jsonStr);
doc.States[pair.Key] = json;
}
catch
{
var jsonStr = JsonSerializer.Serialize(new
{
Data = pair.Value,
StringfyData = pair.Value
}, _botSharpOptions.JsonSerializerOptions);
var json = BsonDocument.Parse(jsonStr);
doc.States[pair.Key] = json;
}
}
docs.Add(doc);
}
await _dc.InstructionLogs.InsertManyAsync(docs);
return true;
}
public async Task<bool> UpdateInstructionLogStates(UpdateInstructionLogStatesModel updateInstructionStates)
{
if (string.IsNullOrWhiteSpace(updateInstructionStates?.LogId)
|| updateInstructionStates?.States?.Any() != true)
{
return false;
}
var id = updateInstructionStates?.LogId;
var logDoc = await _dc.InstructionLogs.Find(p => p.Id == id).FirstOrDefaultAsync();
if (logDoc == null)
{
return false;
}
foreach (var pair in updateInstructionStates.States)
{
var key = updateInstructionStates.StateKeyPrefix + pair.Key;
try
{
var jsonStr = JsonSerializer.Serialize(new
{
Data = JsonDocument.Parse(pair.Value),
StringfyData = pair.Value
}, _botSharpOptions.JsonSerializerOptions);
var json = BsonDocument.Parse(jsonStr);
logDoc.States[key] = json;
}
catch
{
var jsonStr = JsonSerializer.Serialize(new
{
Data = pair.Value,
StringfyData = pair.Value
}, _botSharpOptions.JsonSerializerOptions);
var json = BsonDocument.Parse(jsonStr);
logDoc.States[key] = json;
}
}
await _dc.InstructionLogs.ReplaceOneAsync(p => p.Id == id, logDoc);
return true;
}
public async Task<PagedItems<InstructionLogModel>> GetInstructionLogs(InstructLogFilter filter)
{
if (filter == null)
{
filter = InstructLogFilter.Empty();
}
var logBuilder = Builders<InstructionLogDocument>.Filter;
var logFilters = new List<FilterDefinition<InstructionLogDocument>>() { logBuilder.Empty };
// Filter logs
if (!filter.AgentIds.IsNullOrEmpty())
{
logFilters.Add(logBuilder.In(x => x.AgentId, filter.AgentIds));
}
if (!filter.Providers.IsNullOrEmpty())
{
logFilters.Add(logBuilder.In(x => x.Provider, filter.Providers));
}
if (!filter.Models.IsNullOrEmpty())
{
logFilters.Add(logBuilder.In(x => x.Model, filter.Models));
}
if (!filter.TemplateNames.IsNullOrEmpty())
{
logFilters.Add(logBuilder.In(x => x.TemplateName, filter.TemplateNames));
}
if (!string.IsNullOrEmpty(filter.SimilarTemplateName))
{
logFilters.Add(logBuilder.Regex(x => x.TemplateName, new BsonRegularExpression(filter.SimilarTemplateName, "i")));
}
if (filter.StartTime.HasValue)
{
logFilters.Add(logBuilder.Gte(x => x.CreatedTime, filter.StartTime.Value));
}
if (filter.EndTime.HasValue)
{
logFilters.Add(logBuilder.Lte(x => x.CreatedTime, filter.EndTime.Value));
}
// Filter states
if (filter != null && !filter.States.IsNullOrEmpty())
{
foreach (var pair in filter.States)
{
if (string.IsNullOrWhiteSpace(pair.Key)) continue;
// Format key
var keys = pair.Key.Split(".").ToList();
keys.Insert(1, "data");
keys.Insert(0, "States");
var formattedKey = string.Join(".", keys);
if (string.IsNullOrWhiteSpace(pair.Value))
{
logFilters.Add(logBuilder.Exists(formattedKey));
}
else if (bool.TryParse(pair.Value, out var boolValue))
{
logFilters.Add(logBuilder.Eq(formattedKey, boolValue));
}
else if (int.TryParse(pair.Value, out var intValue))
{
logFilters.Add(logBuilder.Eq(formattedKey, intValue));
}
else if (decimal.TryParse(pair.Value, out var decimalValue))
{
logFilters.Add(logBuilder.Eq(formattedKey, decimalValue));
}
else if (float.TryParse(pair.Value, out var floatValue))
{
logFilters.Add(logBuilder.Eq(formattedKey, floatValue));
}
else if (double.TryParse(pair.Value, out var doubleValue))
{
logFilters.Add(logBuilder.Eq(formattedKey, doubleValue));
}
else
{
logFilters.Add(logBuilder.Eq(formattedKey, pair.Value));
}
}
}
var filterDef = logBuilder.And(logFilters);
var sortDef = Builders<InstructionLogDocument>.Sort.Descending(x => x.CreatedTime);
var docsTask = _dc.InstructionLogs.FindAsync(filterDef, options: new()
{
Sort = sortDef,
Skip = filter.Offset,
Limit = filter.Size
});
var countTask = _dc.InstructionLogs.CountDocumentsAsync(filterDef);
await Task.WhenAll([docsTask, countTask]);
var docs = docsTask.Result.ToList();
var count = countTask.Result;
var logs = docs.Select(x =>
{
var log = InstructionLogDocument.ToDomainModel(x);
log.States = x.States.ToDictionary(p => p.Key, p =>
{
var jsonStr = p.Value.ToJson();
var jsonDoc = JsonDocument.Parse(jsonStr);
var data = jsonDoc.RootElement.GetProperty("data");
return data.ValueKind != JsonValueKind.Null ? data.ToString() : null;
});
return log;
}).ToList();
return new PagedItems<InstructionLogModel>
{
Items = logs,
Count = count
};
}
public async Task<List<string>> GetInstructionLogSearchKeys(InstructLogKeysFilter filter)
{
var builder = Builders<InstructionLogDocument>.Filter;
var sortDef = Builders<InstructionLogDocument>.Sort.Descending(x => x.CreatedTime);
var filters = new List<FilterDefinition<InstructionLogDocument>>()
{
builder.Exists(x => x.States),
builder.Ne(x => x.States, [])
};
if (!filter.AgentIds.IsNullOrEmpty())
{
filters.Add(builder.In(x => x.AgentId, filter.AgentIds));
}
if (!filter.UserIds.IsNullOrEmpty())
{
filters.Add(builder.In(x => x.UserId, filter.UserIds));
}
if (filter.StartTime.HasValue)
{
filters.Add(builder.Gte(x => x.CreatedTime, filter.StartTime.Value));
}
if (filter.EndTime.HasValue)
{
filters.Add(builder.Lte(x => x.CreatedTime, filter.EndTime.Value));
}
var convDocs = await _dc.InstructionLogs.Find(builder.And(filters))
.Sort(sortDef)
.Limit(filter.LogLimit)
.ToListAsync();
var keys = convDocs.SelectMany(x => x.States.Select(x => x.Key)).Distinct().ToList();
return keys;
}
#endregion
}