Skip to content

Commit a462b2d

Browse files
author
Piotr Stachaczynski
committed
Merge branch '43-include-grammar-in-chat-agent-contexts' of https://github.com/wisedev-code/MaIN.NET into 43-include-grammar-in-chat-agent-contexts
2 parents 0f1cd32 + bc6d1fe commit a462b2d

3 files changed

Lines changed: 22 additions & 9 deletions

File tree

src/MaIN.InferPage/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
settings.BackendType = BackendType.OpenAi;
8282
});
8383
}
84-
if (Utils.Gemini)
84+
else if (Utils.Gemini)
8585
{
8686
builder.Services.AddMaIN(builder.Configuration, settings =>
8787
{

src/MaIN.InferPage/Utils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public static class Utils
66
{
77
public static string? Model = "gemma2:2b";
88
public static bool Visual => VisualModels.Contains(Model);
9-
private static readonly string[] VisualModels = ["FLUX.1_Shnell", "dall-e-3"];
9+
private static readonly string[] VisualModels = ["FLUX.1_Shnell", "FLUX.1", "dall-e-3", "dall-e", "imagen", "imagen-3"]; //user might type different names
1010
public static bool OpenAi { get; set; }
1111
public static bool Gemini { get; set; }
1212
public static string? Path { get; set; }

src/MaIN.Services/Services/LLMService/OpenAiCompatibleService.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ public abstract class OpenAiCompatibleService(
2525
ILogger<OpenAiCompatibleService>? logger = null)
2626
: ILLMService
2727
{
28-
private readonly INotificationService _notificationService = notificationService ?? throw new ArgumentNullException(nameof(notificationService));
29-
private readonly IHttpClientFactory _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
28+
private readonly INotificationService _notificationService =
29+
notificationService ?? throw new ArgumentNullException(nameof(notificationService));
30+
31+
private readonly IHttpClientFactory _httpClientFactory =
32+
httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
33+
3034
private static readonly ConcurrentDictionary<string, List<ChatMessage>> SessionCache = new();
31-
private static readonly JsonSerializerOptions DefaultJsonSerializerOptions = new() { PropertyNameCaseInsensitive = true };
35+
36+
private static readonly JsonSerializerOptions DefaultJsonSerializerOptions =
37+
new() { PropertyNameCaseInsensitive = true };
3238

3339
protected abstract string GetApiKey();
3440
protected abstract void ValidateApiKey();
@@ -190,7 +196,14 @@ private async Task ProcessStreamingChatAsync(
190196
var requestBody = new
191197
{
192198
model = chat.Model,
193-
messages = conversation.Select(m => new { role = m.Role, content = m.Content }).ToArray(),
199+
messages = conversation.Select(m => new
200+
{
201+
role = m.Role,
202+
content = chat.InterferenceParams.Grammar != null
203+
//I know that this is a bit ugly, but hey, it works
204+
? $"{m.Content} | Respond only using the following grammar format: \n{chat.InterferenceParams.Grammar}\n. Do not add explanations, code tags, or any extra content."
205+
: m.Content
206+
}).ToArray(),
194207
stream = true
195208
};
196209

@@ -277,7 +290,8 @@ private async Task ProcessNonStreamingChatAsync(
277290
response.EnsureSuccessStatusCode();
278291

279292
var responseJson = await response.Content.ReadAsStringAsync(cancellationToken);
280-
var chatResponse = JsonSerializer.Deserialize<ChatCompletionResponse>(responseJson, DefaultJsonSerializerOptions);
293+
var chatResponse =
294+
JsonSerializer.Deserialize<ChatCompletionResponse>(responseJson, DefaultJsonSerializerOptions);
281295
var responseContent = chatResponse?.Choices?.FirstOrDefault()?.Message?.Content;
282296

283297
if (responseContent != null)
@@ -323,7 +337,6 @@ private static async Task InvokeTokenCallbackAsync(Func<LLMTokenValue, Task>? ca
323337
await callback.Invoke(token);
324338
}
325339
}
326-
327340
}
328341

329342
public class ChatRequestOptions
@@ -377,4 +390,4 @@ file class OpenAiModelsResponse
377390
file abstract class OpenAiModel
378391
{
379392
public string? Id { get; set; }
380-
}
393+
}

0 commit comments

Comments
 (0)