@@ -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
329342public class ChatRequestOptions
@@ -377,4 +390,4 @@ file class OpenAiModelsResponse
377390file abstract class OpenAiModel
378391{
379392 public string ? Id { get ; set ; }
380- }
393+ }
0 commit comments