-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathChatContext.cs
More file actions
250 lines (214 loc) · 6.11 KB
/
ChatContext.cs
File metadata and controls
250 lines (214 loc) · 6.11 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
using MaIN.Domain.Configuration;
using MaIN.Domain.Entities;
using MaIN.Domain.Exceptions;
using MaIN.Domain.Models;
using MaIN.Services;
using MaIN.Services.Constants;
using MaIN.Services.Dtos;
using MaIN.Services.Services.Abstract;
using MaIN.Services.Services.Models;
using FileInfo = MaIN.Domain.Entities.FileInfo;
namespace MaIN.Core.Hub.Contexts;
public class ChatContext
{
private readonly IChatService _chatService;
private bool _preProcess;
private Chat _chat { get; set; }
private List<FileInfo> _files { get; set; } = [];
internal ChatContext(IChatService chatService)
{
_chatService = chatService;
_chat = new Chat
{
Name = "New Chat",
Id = Guid.NewGuid().ToString(),
Messages = new List<Message>(),
Model = string.Empty
};
_files = [];
}
internal ChatContext(IChatService chatService, Chat existingChat)
{
_chatService = chatService;
_chat = existingChat;
}
public ChatContext WithModel(string model)
{
_chat.Model = model;
return this;
}
public ChatContext WithInferenceParams(InferenceParams inferenceParams)
{
_chat.InterferenceParams = inferenceParams;
return this;
}
public ChatContext WithMemoryParams(MemoryParams memoryParams)
{
_chat.MemoryParams = memoryParams;
return this;
}
public ChatContext WithCustomModel(string model, string path, string? mmProject = null)
{
KnownModels.AddModel(model, path, mmProject);
_chat.Model = model;
return this;
}
public ChatContext WithBackend(BackendType backendType)
{
_chat.Backend = backendType;
return this;
}
public ChatContext WithMessage(string content)
{
var message = new Message
{
Role = "User",
Content = content,
Type = MessageType.LocalLLM,
Time = DateTime.Now
};
_chat.Messages.Add(message);
return this;
}
public ChatContext WithMessage(string content, byte[] image)
{
var message = new Message
{
Role = "User",
Content = content,
Type = MessageType.NotSet,
Time = DateTime.Now,
Image = image
};
_chat.Messages.Add(message);
return this;
}
public ChatContext WithSystemPrompt(string systemPrompt)
{
var message = new Message
{
Role = "System",
Content = systemPrompt,
Type = MessageType.NotSet,
Time = DateTime.Now
};
// Insert system message at the beginning
_chat.Messages.Insert(0, message);
return this;
}
public ChatContext WithFiles(List<FileStream> fileStreams, bool preProcess = false)
{
var files = fileStreams.Select(p => new FileInfo()
{
Name = Path.GetFileName(p.Name),
Path = null,
Extension = Path.GetExtension(p.Name),
StreamContent = p
}).ToList();
_preProcess = preProcess;
_files = files;
return this;
}
public ChatContext WithFiles(List<FileInfo> files, bool preProcess = false)
{
_files = files;
_preProcess = preProcess;
return this;
}
public ChatContext WithFiles(List<string> filePaths, bool preProcess = false)
{
var files = filePaths.Select(p => new FileInfo()
{
Name = Path.GetFileName(p),
Path = p,
Extension = Path.GetExtension(p)
}).ToList();
_preProcess = preProcess;
_files = files;
return this;
}
public ChatContext EnableVisual()
{
_chat.Visual = true;
return this;
}
public ChatContext DisableCache()
{
_chat.Properties.AddProperty(ServiceConstants.Properties.DisableCacheProperty);
return this;
}
public string GetChatId() => _chat.Id;
public async Task<ChatResult> CompleteAsync(
bool translate = false,
bool interactive = false,
Func<LLMTokenValue?, Task>? changeOfValue = null)
{
if (_chat.Messages.Count == 0)
{
throw new EmptyChatException(_chat.Id);
}
_chat.Messages.Last().Files = _files;
if(_preProcess)
{
_chat.Messages.Last().Properties.AddProperty(ServiceConstants.Properties.PreProcessProperty);
}
if (!await ChatExists(_chat.Id))
{
await _chatService.Create(_chat);
}
var result = await _chatService.Completions(_chat, translate, interactive, changeOfValue);
_files = [];
return result;
}
public async Task<Chat> GetCurrentChat()
{
if (_chat.Id == null)
{
throw new ChatNotInitializedException();
}
return await _chatService.GetById(_chat.Id);
}
public async Task<List<Chat>> GetAllChats()
{
return await _chatService.GetAll();
}
public async Task DeleteChat()
{
if (_chat.Id == null)
{
throw new ChatNotInitializedException();
}
await _chatService.Delete(_chat.Id);
}
private async Task<bool> ChatExists(string id)
{
try
{
await _chatService.GetById(id);
return true;
}
catch
{
return false;
}
}
// Static methods to create builder from existing chat
public async Task<ChatContext> FromExisting(string chatId)
{
var existingChat = await _chatService.GetById(chatId);
if (existingChat == null)
{
throw new ChatNotFoundException(chatId);
}
return new ChatContext(_chatService, existingChat);
}
public List<MessageShort> GetChatHistory()
{
return _chat.Messages.Select(x => new MessageShort()
{
Content = x.Content,
Role = x.Role,
Time = x.Time
}).ToList();
}
}