Skip to content

Commit 4f2e694

Browse files
committed
TryAddOllamaOption enhanced with MaxOutputTokens, added new test that validates everything is covered
1 parent 08d9eb2 commit 4f2e694

File tree

4 files changed

+139
-34
lines changed

4 files changed

+139
-34
lines changed

Diff for: src/Constants/Application.cs

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ internal static class Application
7676
public const string Seed = "seed";
7777
public const string TfsZ = "tfs_z";
7878
public const string NumPredict = "num_predict";
79+
public const string MaxOutputTokens = "max_output_tokens";
7980
public const string TopK = "top_k";
8081
public const string TopP = "top_p";
8182
public const string MinP = "min_p";

Diff for: src/MicrosoftAi/AbstractionMapper.cs

+33-34
Original file line numberDiff line numberDiff line change
@@ -72,40 +72,39 @@ public static ChatRequest ToOllamaSharpChatRequest(IList<ChatMessage> chatMessag
7272
Tools = ToOllamaSharpTools(options?.Tools)
7373
};
7474

75-
if (options?.AdditionalProperties?.Any() ?? false)
76-
{
77-
TryAddOllamaOption<bool?>(options, OllamaOption.F16kv, v => request.Options.F16kv = (bool?)v);
78-
TryAddOllamaOption<float?>(options, OllamaOption.FrequencyPenalty, v => request.Options.FrequencyPenalty = (float?)v);
79-
TryAddOllamaOption<bool?>(options, OllamaOption.LogitsAll, v => request.Options.LogitsAll = (bool?)v);
80-
TryAddOllamaOption<bool?>(options, OllamaOption.LowVram, v => request.Options.LowVram = (bool?)v);
81-
TryAddOllamaOption<int?>(options, OllamaOption.MainGpu, v => request.Options.MainGpu = (int?)v);
82-
TryAddOllamaOption<float?>(options, OllamaOption.MinP, v => request.Options.MinP = (float?)v);
83-
TryAddOllamaOption<int?>(options, OllamaOption.MiroStat, v => request.Options.MiroStat = (int?)v);
84-
TryAddOllamaOption<float?>(options, OllamaOption.MiroStatEta, v => request.Options.MiroStatEta = (float?)v);
85-
TryAddOllamaOption<float?>(options, OllamaOption.MiroStatTau, v => request.Options.MiroStatTau = (float?)v);
86-
TryAddOllamaOption<bool?>(options, OllamaOption.Numa, v => request.Options.Numa = (bool?)v);
87-
TryAddOllamaOption<int?>(options, OllamaOption.NumBatch, v => request.Options.NumBatch = (int?)v);
88-
TryAddOllamaOption<int?>(options, OllamaOption.NumCtx, v => request.Options.NumCtx = (int?)v);
89-
TryAddOllamaOption<int?>(options, OllamaOption.NumGpu, v => request.Options.NumGpu = (int?)v);
90-
TryAddOllamaOption<int?>(options, OllamaOption.NumGqa, v => request.Options.NumGqa = (int?)v);
91-
TryAddOllamaOption<int?>(options, OllamaOption.NumKeep, v => request.Options.NumKeep = (int?)v);
92-
TryAddOllamaOption<int?>(options, OllamaOption.NumPredict, v => request.Options.NumPredict = (int?)v);
93-
TryAddOllamaOption<int?>(options, OllamaOption.NumThread, v => request.Options.NumThread = (int?)v);
94-
TryAddOllamaOption<bool?>(options, OllamaOption.PenalizeNewline, v => request.Options.PenalizeNewline = (bool?)v);
95-
TryAddOllamaOption<float?>(options, OllamaOption.PresencePenalty, v => request.Options.PresencePenalty = (float?)v);
96-
TryAddOllamaOption<int?>(options, OllamaOption.RepeatLastN, v => request.Options.RepeatLastN = (int?)v);
97-
TryAddOllamaOption<float?>(options, OllamaOption.RepeatPenalty, v => request.Options.RepeatPenalty = (float?)v);
98-
TryAddOllamaOption<int?>(options, OllamaOption.Seed, v => request.Options.Seed = (int?)v);
99-
TryAddOllamaOption<string[]?>(options, OllamaOption.Stop, v => request.Options.Stop = (v as IEnumerable<string>)?.ToArray());
100-
TryAddOllamaOption<float?>(options, OllamaOption.Temperature, v => request.Options.Temperature = (float?)v);
101-
TryAddOllamaOption<float?>(options, OllamaOption.TfsZ, v => request.Options.TfsZ = (float?)v);
102-
TryAddOllamaOption<int?>(options, OllamaOption.TopK, v => request.Options.TopK = (int?)v);
103-
TryAddOllamaOption<float?>(options, OllamaOption.TopP, v => request.Options.TopP = (float?)v);
104-
TryAddOllamaOption<float?>(options, OllamaOption.TypicalP, v => request.Options.TypicalP = (float?)v);
105-
TryAddOllamaOption<bool?>(options, OllamaOption.UseMlock, v => request.Options.UseMlock = (bool?)v);
106-
TryAddOllamaOption<bool?>(options, OllamaOption.UseMmap, v => request.Options.UseMmap = (bool?)v);
107-
TryAddOllamaOption<bool?>(options, OllamaOption.VocabOnly, v => request.Options.VocabOnly = (bool?)v);
108-
}
75+
if (!(options?.AdditionalProperties?.Any() ?? false)) return request;
76+
TryAddOllamaOption<bool?>(options, OllamaOption.F16kv, v => request.Options.F16kv = (bool?)v);
77+
TryAddOllamaOption<float?>(options, OllamaOption.FrequencyPenalty, v => request.Options.FrequencyPenalty = (float?)v);
78+
TryAddOllamaOption<bool?>(options, OllamaOption.LogitsAll, v => request.Options.LogitsAll = (bool?)v);
79+
TryAddOllamaOption<bool?>(options, OllamaOption.LowVram, v => request.Options.LowVram = (bool?)v);
80+
TryAddOllamaOption<int?>(options, OllamaOption.MainGpu, v => request.Options.MainGpu = (int?)v);
81+
TryAddOllamaOption<float?>(options, OllamaOption.MinP, v => request.Options.MinP = (float?)v);
82+
TryAddOllamaOption<int?>(options, OllamaOption.MiroStat, v => request.Options.MiroStat = (int?)v);
83+
TryAddOllamaOption<float?>(options, OllamaOption.MiroStatEta, v => request.Options.MiroStatEta = (float?)v);
84+
TryAddOllamaOption<float?>(options, OllamaOption.MiroStatTau, v => request.Options.MiroStatTau = (float?)v);
85+
TryAddOllamaOption<bool?>(options, OllamaOption.Numa, v => request.Options.Numa = (bool?)v);
86+
TryAddOllamaOption<int?>(options, OllamaOption.NumBatch, v => request.Options.NumBatch = (int?)v);
87+
TryAddOllamaOption<int?>(options, OllamaOption.NumCtx, v => request.Options.NumCtx = (int?)v);
88+
TryAddOllamaOption<int?>(options, OllamaOption.NumGpu, v => request.Options.NumGpu = (int?)v);
89+
TryAddOllamaOption<int?>(options, OllamaOption.NumGqa, v => request.Options.NumGqa = (int?)v);
90+
TryAddOllamaOption<int?>(options, OllamaOption.NumKeep, v => request.Options.NumKeep = (int?)v);
91+
TryAddOllamaOption<int?>(options, OllamaOption.NumPredict, v => request.Options.NumPredict = (int?)v);
92+
TryAddOllamaOption<int?>(options, OllamaOption.MaxOutputTokens, v => request.Options.NumPredict = (int?)v);
93+
TryAddOllamaOption<int?>(options, OllamaOption.NumThread, v => request.Options.NumThread = (int?)v);
94+
TryAddOllamaOption<bool?>(options, OllamaOption.PenalizeNewline, v => request.Options.PenalizeNewline = (bool?)v);
95+
TryAddOllamaOption<float?>(options, OllamaOption.PresencePenalty, v => request.Options.PresencePenalty = (float?)v);
96+
TryAddOllamaOption<int?>(options, OllamaOption.RepeatLastN, v => request.Options.RepeatLastN = (int?)v);
97+
TryAddOllamaOption<float?>(options, OllamaOption.RepeatPenalty, v => request.Options.RepeatPenalty = (float?)v);
98+
TryAddOllamaOption<int?>(options, OllamaOption.Seed, v => request.Options.Seed = (int?)v);
99+
TryAddOllamaOption<string[]?>(options, OllamaOption.Stop, v => request.Options.Stop = (v as IEnumerable<string>)?.ToArray());
100+
TryAddOllamaOption<float?>(options, OllamaOption.Temperature, v => request.Options.Temperature = (float?)v);
101+
TryAddOllamaOption<float?>(options, OllamaOption.TfsZ, v => request.Options.TfsZ = (float?)v);
102+
TryAddOllamaOption<int?>(options, OllamaOption.TopK, v => request.Options.TopK = (int?)v);
103+
TryAddOllamaOption<float?>(options, OllamaOption.TopP, v => request.Options.TopP = (float?)v);
104+
TryAddOllamaOption<float?>(options, OllamaOption.TypicalP, v => request.Options.TypicalP = (float?)v);
105+
TryAddOllamaOption<bool?>(options, OllamaOption.UseMlock, v => request.Options.UseMlock = (bool?)v);
106+
TryAddOllamaOption<bool?>(options, OllamaOption.UseMmap, v => request.Options.UseMmap = (bool?)v);
107+
TryAddOllamaOption<bool?>(options, OllamaOption.VocabOnly, v => request.Options.VocabOnly = (bool?)v);
109108

110109
return request;
111110
}

Diff for: src/Models/OllamaOption.cs

+6
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ public class OllamaOption(string name)
115115
/// (Default: 128, -1 = infinite generation, -2 = fill context)
116116
/// </summary>
117117
public static OllamaOption NumPredict { get; } = new(Application.NumPredict);
118+
119+
/// <summary>
120+
/// The number of tokens to generate in the output.
121+
/// (Default: -1, infinite generation)
122+
/// </summary>
123+
public static OllamaOption MaxOutputTokens { get; } = new(Application.MaxOutputTokens);
118124

119125
/// <summary>
120126
/// Sets the number of threads to use during computation. By default,

Diff for: test/AbstractionMapperTests.cs

+99
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,105 @@ public void Maps_Messages_With_Tools()
250250
tool.Type.Should().Be("function");
251251
}
252252

253+
[Test]
254+
public void Maps_All_Options_With_AdditionalProperties()
255+
{
256+
// Arrange
257+
List<ChatMessage> chatMessages = [];
258+
259+
var options = new ChatOptions
260+
{
261+
AdditionalProperties = new AdditionalPropertiesDictionary()
262+
{
263+
// Boolean options
264+
[OllamaOption.F16kv.Name] = true,
265+
[OllamaOption.LogitsAll.Name] = true,
266+
[OllamaOption.LowVram.Name] = true,
267+
[OllamaOption.Numa.Name] = true,
268+
[OllamaOption.PenalizeNewline.Name] = true,
269+
[OllamaOption.UseMlock.Name] = true,
270+
[OllamaOption.UseMmap.Name] = true,
271+
[OllamaOption.VocabOnly.Name] = true,
272+
273+
// Float options
274+
[OllamaOption.FrequencyPenalty.Name] = 0.5f,
275+
[OllamaOption.MinP.Name] = 0.1f,
276+
[OllamaOption.MiroStatEta.Name] = 0.1f,
277+
[OllamaOption.MiroStatTau.Name] = 0.2f,
278+
[OllamaOption.PresencePenalty.Name] = 0.3f,
279+
[OllamaOption.RepeatPenalty.Name] = 0.4f,
280+
[OllamaOption.Temperature.Name] = 0.7f,
281+
[OllamaOption.TfsZ.Name] = 0.8f,
282+
[OllamaOption.TopP.Name] = 0.9f,
283+
[OllamaOption.TypicalP.Name] = 0.95f,
284+
285+
// Integer options
286+
[OllamaOption.MainGpu.Name] = 0,
287+
[OllamaOption.MiroStat.Name] = 1,
288+
[OllamaOption.NumBatch.Name] = 512,
289+
[OllamaOption.NumCtx.Name] = 4096,
290+
[OllamaOption.NumGpu.Name] = 1,
291+
[OllamaOption.NumGqa.Name] = 8,
292+
[OllamaOption.NumKeep.Name] = 64,
293+
[OllamaOption.NumPredict.Name] = 1024,
294+
[OllamaOption.MaxOutputTokens.Name] = 2048,
295+
[OllamaOption.NumThread.Name] = 8,
296+
[OllamaOption.RepeatLastN.Name] = 64,
297+
[OllamaOption.Seed.Name] = 42,
298+
[OllamaOption.TopK.Name] = 40,
299+
300+
// String array options
301+
[OllamaOption.Stop.Name] = new[] { "stop1", "stop2" }
302+
}
303+
};
304+
305+
// Act
306+
var chatRequest = AbstractionMapper.ToOllamaSharpChatRequest(chatMessages, options, stream: true, JsonSerializerOptions.Default);
307+
308+
// Assert
309+
chatRequest.Options.Should().NotBeNull();
310+
311+
// Boolean assertions
312+
chatRequest.Options!.F16kv.Should().BeTrue();
313+
chatRequest.Options!.LogitsAll.Should().BeTrue();
314+
chatRequest.Options!.LowVram.Should().BeTrue();
315+
chatRequest.Options!.Numa.Should().BeTrue();
316+
chatRequest.Options!.PenalizeNewline.Should().BeTrue();
317+
chatRequest.Options!.UseMlock.Should().BeTrue();
318+
chatRequest.Options!.UseMmap.Should().BeTrue();
319+
chatRequest.Options!.VocabOnly.Should().BeTrue();
320+
321+
// Float assertions
322+
chatRequest.Options!.FrequencyPenalty.Should().Be(0.5f);
323+
chatRequest.Options!.MinP.Should().Be(0.1f);
324+
chatRequest.Options!.MiroStatEta.Should().Be(0.1f);
325+
chatRequest.Options!.MiroStatTau.Should().Be(0.2f);
326+
chatRequest.Options!.PresencePenalty.Should().Be(0.3f);
327+
chatRequest.Options!.RepeatPenalty.Should().Be(0.4f);
328+
chatRequest.Options!.Temperature.Should().Be(0.7f);
329+
chatRequest.Options!.TfsZ.Should().Be(0.8f);
330+
chatRequest.Options!.TopP.Should().Be(0.9f);
331+
chatRequest.Options!.TypicalP.Should().Be(0.95f);
332+
333+
// Integer assertions
334+
chatRequest.Options!.MainGpu.Should().Be(0);
335+
chatRequest.Options!.MiroStat.Should().Be(1);
336+
chatRequest.Options!.NumBatch.Should().Be(512);
337+
chatRequest.Options!.NumCtx.Should().Be(4096);
338+
chatRequest.Options!.NumGpu.Should().Be(1);
339+
chatRequest.Options!.NumGqa.Should().Be(8);
340+
chatRequest.Options!.NumKeep.Should().Be(64);
341+
chatRequest.Options!.NumPredict.Should().Be(2048);
342+
chatRequest.Options!.NumThread.Should().Be(8);
343+
chatRequest.Options!.RepeatLastN.Should().Be(64);
344+
chatRequest.Options!.Seed.Should().Be(42);
345+
chatRequest.Options!.TopK.Should().Be(40);
346+
347+
// String array assertions
348+
chatRequest.Options!.Stop.Should().NotBeNull();
349+
chatRequest.Options!.Stop.Should().BeEquivalentTo("stop1", "stop2");
350+
}
351+
253352
[TestCaseSource(nameof(StopSequencesTestData))]
254353
public void Maps_Messages_With_IEnumerable_StopSequences(object? enumerable)
255354
{

0 commit comments

Comments
 (0)