Repository navigation
Fix RawRepresentationFactory settings being overwritten in AsChatClient
#1442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
martindevans
merged 4 commits into
SciSharp:master
from
Laurianti:fix/chat-client-raw-representation
Sep 26, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
033004b
Fixed `RawRepresentationFactory` settings being overwritten in `AsCha…
Laurianti b486ee7
Kept a `MaxTokens` of -1 (unlimited) from `RawRepresentationFactory`
Laurianti 70b6044
Reused a `DefaultSamplingPipeline` from `RawRepresentationFactory` wh…
Laurianti 1f10708
Reverted reusing a `DefaultSamplingPipeline`: its native chain is cac…
Laurianti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| using System.Runtime.CompilerServices; | ||
| using LLama.Abstractions; | ||
| using LLama.Common; | ||
| using LLama.Native; | ||
| using LLama.Sampling; | ||
| using Microsoft.Extensions.AI; | ||
|
|
||
| namespace LLama.Unittest; | ||
|
|
||
| public sealed class LLamaExecutorChatClientTests | ||
| { | ||
| [Fact] | ||
| public async Task RawRepresentationSettingsAreKept() | ||
| { | ||
| var executor = new CapturingExecutor(); | ||
|
|
||
| await executor.AsChatClient().GetResponseAsync("hello", new ChatOptions | ||
| { | ||
| RawRepresentationFactory = _ => new InferenceParams | ||
| { | ||
| MaxTokens = 1024, | ||
| SamplingPipeline = new DefaultSamplingPipeline { Temperature = 0.1f, TopP = 0.5f, TopK = 5, Seed = 42 }, | ||
| }, | ||
| }); | ||
|
|
||
| var pipeline = Assert.IsType<DefaultSamplingPipeline>(executor.InferenceParams?.SamplingPipeline); | ||
| Assert.Equal(1024, executor.InferenceParams!.MaxTokens); | ||
| Assert.Equal(0.1f, pipeline.Temperature); | ||
| Assert.Equal(0.5f, pipeline.TopP); | ||
| Assert.Equal(5, pipeline.TopK); | ||
| Assert.Equal(42u, pipeline.Seed); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task RawRepresentationUnlimitedMaxTokensIsKept() | ||
| { | ||
| var executor = new CapturingExecutor(); | ||
|
|
||
| await executor.AsChatClient().GetResponseAsync("hello", new ChatOptions | ||
| { | ||
| RawRepresentationFactory = _ => new InferenceParams { MaxTokens = -1 }, | ||
| }); | ||
|
|
||
| Assert.Equal(-1, executor.InferenceParams!.MaxTokens); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ChatOptionsOverrideRawRepresentationSettings() | ||
| { | ||
| var executor = new CapturingExecutor(); | ||
|
|
||
| await executor.AsChatClient().GetResponseAsync("hello", new ChatOptions | ||
| { | ||
| MaxOutputTokens = 64, | ||
| Temperature = 0.9f, | ||
| TopP = 0.8f, | ||
| TopK = 20, | ||
| Seed = 7, | ||
| RawRepresentationFactory = _ => new InferenceParams | ||
| { | ||
| MaxTokens = 1024, | ||
| SamplingPipeline = new DefaultSamplingPipeline { Temperature = 0.1f, TopP = 0.5f, TopK = 5, Seed = 42 }, | ||
| }, | ||
| }); | ||
|
|
||
| var pipeline = Assert.IsType<DefaultSamplingPipeline>(executor.InferenceParams?.SamplingPipeline); | ||
| Assert.Equal(64, executor.InferenceParams!.MaxTokens); | ||
| Assert.Equal(0.9f, pipeline.Temperature); | ||
| Assert.Equal(0.8f, pipeline.TopP); | ||
| Assert.Equal(20, pipeline.TopK); | ||
| Assert.Equal(7u, pipeline.Seed); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task RawRepresentationSamplingPipelineIsKept() | ||
| { | ||
| var executor = new CapturingExecutor(); | ||
| var greedy = new GreedySamplingPipeline(); | ||
|
|
||
| await executor.AsChatClient().GetResponseAsync("hello", new ChatOptions | ||
| { | ||
| RawRepresentationFactory = _ => new InferenceParams { SamplingPipeline = greedy }, | ||
| }); | ||
|
|
||
| Assert.Same(greedy, executor.InferenceParams?.SamplingPipeline); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task MaxTokensDefaultsTo256WithoutALimit() | ||
| { | ||
| var executor = new CapturingExecutor(); | ||
|
|
||
| await executor.AsChatClient().GetResponseAsync("hello"); | ||
|
|
||
| Assert.Equal(256, executor.InferenceParams!.MaxTokens); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Records the inference parameters it is given, without a model. | ||
| /// </summary> | ||
| private sealed class CapturingExecutor : ILLamaExecutor | ||
| { | ||
| public IInferenceParams? InferenceParams { get; private set; } | ||
|
|
||
| public LLamaContext Context => throw new NotSupportedException(); | ||
| public bool IsMultiModal => false; | ||
| public MtmdWeights? ClipModel => null; | ||
| public List<SafeMtmdEmbed> Embeds { get; } = []; | ||
|
|
||
| public async IAsyncEnumerable<string> InferAsync(string text, IInferenceParams? inferenceParams = null, [EnumeratorCancellation] CancellationToken token = default) | ||
| { | ||
| InferenceParams = inferenceParams; | ||
| await Task.Yield(); | ||
| yield return "hi"; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.