-
Notifications
You must be signed in to change notification settings - Fork 510
Expand file tree
/
Copy pathMtmdInteractiveModeExecute.cs
More file actions
275 lines (233 loc) 路 11.6 KB
/
Copy pathMtmdInteractiveModeExecute.cs
File metadata and controls
275 lines (233 loc) 路 11.6 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
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using LLama;
using LLama.Common;
using LLama.Exceptions;
using Spectre.Console;
using LLama.Native;
using LLama.Sampling;
namespace LLama.Examples.Examples
{
// This example shows how to chat with Mtmd model with audio, image and text as input.
// It uses the interactive executor to inference.
public class MtmdInteractiveModeExecute
{
public static async Task Run()
{
string multiModalProj = UserSettings.GetMMProjPath();
string modelPath = UserSettings.GetModelPath();
const int maxTokens = 4096;
string? prompt = await File.ReadAllTextAsync("Assets/chat-with-bob.json");
var parameters = new ModelParams(modelPath);
var mtmdParameters = MtmdContextParams.Default();
mtmdParameters.UseGpu = false;
using var model = await LLamaWeights.LoadFromFileAsync(parameters);
using var context = model.CreateContext(parameters);
// Mtmd Init
using var clipModel = await MtmdWeights.LoadFromFileAsync(multiModalProj, model, mtmdParameters );
var mediaMarker = mtmdParameters.MediaMarker ?? NativeApi.MtmdDefaultMarker() ?? "<media>";
var supportsVision = clipModel.SupportsVision;
var supportsAudio = clipModel.SupportsAudio;
var ex = new InteractiveExecutor(context, clipModel);
var chatHistory = new ChatHistory();
var isFirstTurn = true;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("The executor has been enabled. In this example the maximum tokens is set to {0} and the context size is {1}.", maxTokens, parameters.ContextSize );
Console.WriteLine("Model: {0}", modelPath);
Console.WriteLine("MMProj: {0}", multiModalProj);
Console.WriteLine("Supported modalities: vision={0} | audio={1} | video=no", supportsVision ? "yes" : "no", supportsAudio ? "yes" : "no");
if (supportsVision)
Console.WriteLine("To send an image, enter its filename in double braces, like this {{c:/image.jpg}}.");
if (supportsAudio)
Console.WriteLine("To send audio, enter its filename in double braces, like this {{c:/audio.wav}}.");
Console.WriteLine("Video inputs are not supported (format would be {{c:/video.mp4}}).");
Console.WriteLine("Commands: /exit (return to main menu) | /clear (reset the chat history and KV cache).");
Console.WriteLine("Press Ctrl+c to return to main menu.");
void ResetConversation()
{
context.NativeHandle.MemoryClear();
foreach (var embed in ex.Embeds)
embed.Dispose();
ex.Embeds.Clear();
clipModel.ClearMedia();
chatHistory.Messages.Clear();
ex = new InteractiveExecutor(context, clipModel);
Console.WriteLine("User:");
}
var inferenceParams = new InferenceParams
{
SamplingPipeline = new DefaultSamplingPipeline
{
Temperature = 0.1f
},
AntiPrompts = new List<string> { "User:" },
MaxTokens = maxTokens
};
do
{
if (!isFirstTurn)
{
Console.ForegroundColor = ConsoleColor.Green;
prompt = Console.ReadLine();
Console.WriteLine();
if (prompt == null || prompt.Equals("/exit", StringComparison.OrdinalIgnoreCase))
break;
if (prompt.Equals("/clear", StringComparison.OrdinalIgnoreCase))
{
ResetConversation();
continue;
}
}
else
{
isFirstTurn = false;
}
var userPrompt = prompt ?? string.Empty;
// Evaluate if we have media
//
var mediaMatches = Regex.Matches(userPrompt, @"\{\{(.*?)\}\}").Select(m => m.Value);
var mediaCount = mediaMatches.Count();
var hasMedia = mediaCount > 0;
if (hasMedia)
{
var mediaPathsWithCurlyBraces = Regex.Matches(userPrompt, @"\{\{(.*?)\}\}").Select(m => m.Value);
var mediaPaths = Regex.Matches(userPrompt, @"\{\{(.*?)\}\}").Select(m => m.Groups[1].Value).ToList();
var embeds = new List<SafeMtmdEmbed>();
var imageList = new List<byte[]>();
var audioList = new List<string>();
var imageExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
".png",
".jpg",
".jpeg",
".bmp",
".gif",
".webp"
};
var audioExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
".wav",
".mp3",
".flac",
".ogg",
".m4a",
".aac",
".opus"
};
var videoExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
".mp4",
".mkv",
".mov",
".avi",
".webm"
};
try
{
foreach (var mediaPath in mediaPaths)
{
var extension = Path.GetExtension(mediaPath);
var isImage = !string.IsNullOrEmpty(extension) && imageExtensions.Contains(extension);
var isAudio = !string.IsNullOrEmpty(extension) && audioExtensions.Contains(extension);
var isVideo = !string.IsNullOrEmpty(extension) && videoExtensions.Contains(extension);
if (isVideo)
throw new NotSupportedException("Video inputs are not supported by MTMD.");
if (isImage && !supportsVision)
throw new InvalidOperationException("This model does not support vision inputs.");
if (isAudio && !supportsAudio)
throw new InvalidOperationException("This model does not support audio inputs.");
if (isImage)
{
// Keep the raw image data so the caller can reuse or inspect the images later.
imageList.Add(File.ReadAllBytes(mediaPath));
}
else if (isAudio)
{
audioList.Add(mediaPath);
}
var embed = clipModel.LoadMedia(mediaPath);
embeds.Add(embed);
}
}
catch (Exception exception) when (exception is IOException or UnauthorizedAccessException or RuntimeError or NotSupportedException or InvalidOperationException)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(
$"Could not load your {(mediaCount == 1 ? "media" : "medias")}:");
Console.Write($"{exception.Message}");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Please try again.");
clipModel.ClearMedia();
break;
}
// Replace placeholders with media markers (one marker per image)
foreach (var path in mediaPathsWithCurlyBraces)
{
userPrompt = userPrompt.Replace(path, mediaMarker, StringComparison.Ordinal);
}
if (imageList.Count > 0)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Here are the images, that are sent to the chat model in addition to your message.");
Console.WriteLine();
foreach (var consoleImage in imageList.Select(image => new CanvasImage(image.ToArray())))
{
consoleImage.MaxWidth = 50;
AnsiConsole.Write(consoleImage);
}
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("The images were scaled down for the console only, the model gets full versions.");
Console.WriteLine();
}
if (audioList.Count > 0)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"Loaded audio files: {string.Join(", ", audioList)}");
Console.WriteLine();
}
// Initialize Images in executor
//
ex.Embeds.Clear();
foreach (var embed in embeds)
ex.Embeds.Add(embed);
}
var formattedPrompt = BuildChatDelta(model, chatHistory, userPrompt);
Console.ForegroundColor = ConsoleColor.White;
var responseBuilder = new StringBuilder();
await foreach (var text in ex.InferAsync(formattedPrompt, inferenceParams))
{
Console.Write(text);
responseBuilder.Append(text);
}
Console.Write(" ");
chatHistory.AddMessage(AuthorRole.Assistant, responseBuilder.ToString());
}
while(true);
}
private static string BuildChatDelta(LLamaWeights model, ChatHistory history, string userContent)
{
var pastPrompt = FormatChatHistory(model, history, addAssistant: false);
history.AddMessage(AuthorRole.User, userContent);
var fullPrompt = FormatChatHistory(model, history, addAssistant: true);
if (!fullPrompt.StartsWith(pastPrompt, StringComparison.Ordinal))
return fullPrompt;
var delta = fullPrompt[pastPrompt.Length..];
if (pastPrompt.Length > 0 && pastPrompt.EndsWith('\n') && (delta.Length == 0 || delta[0] != '\n'))
delta = "\n" + delta;
return delta;
}
private static string FormatChatHistory(LLamaWeights model, ChatHistory history, bool addAssistant)
{
var template = new LLamaTemplate(model.NativeHandle)
{
AddAssistant = addAssistant,
};
foreach (var message in history.Messages)
template.Add(message.AuthorRole.ToString().ToLowerInvariant(), message.Content);
return LLamaTemplate.Encoding.GetString(template.Apply());
}
}
}