Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/csharp/OnnxRuntimeGenAIChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ generator.ConversationId is null ||
}

/// <inheritdoc/>
/// <remarks>
/// A <see cref="Generator"/> service is available only while a generator is cached between requests.
/// The returned generator is owned by this client and must not be disposed, retained across requests,
/// or used concurrently with a request.
/// </remarks>
object? IChatClient.GetService(Type serviceType, object? serviceKey)
{
if (serviceType is null)
Expand All @@ -259,6 +264,7 @@ generator.ConversationId is null ||
serviceType == typeof(Model) ? _model :
serviceType == typeof(Tokenizer) ? _tokenizer :
serviceType == typeof(Config) ? _config :
serviceType == typeof(Generator) ? Volatile.Read(ref _cachedGenerator)?.Generator :
serviceType?.IsInstanceOfType(this) is true ? this :
null;
}
Expand Down Expand Up @@ -418,4 +424,4 @@ private sealed class YieldAwaiter : INotifyCompletion
public void UnsafeOnCompleted(Action continuation) => Task.Run(continuation);
public void GetResult() { }
}
}
}
25 changes: 25 additions & 0 deletions test/csharp/TestOnnxRuntimeGenAIAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,31 @@ public async Task TestChatClient()
Assert.NotEmpty(completion.Text);
}

[Fact(DisplayName = "TestChatClientGetServiceGenerator")]
public async Task TestChatClientGetServiceGenerator()
{
OnnxRuntimeGenAIChatClientOptions options = new()
{
EnableCaching = true,
PromptFormatter = static (messages, _) => string.Concat(messages.Select(message => message.Text)),
};

using var client = new OnnxRuntimeGenAIChatClient(_tinyRandomGpt2ModelPath, options);
IChatClient chatClient = client;

Assert.Null(chatClient.GetService<Generator>(null));

await client.GetResponseAsync("H", new()
{
MaxOutputTokens = 1,
Temperature = 0f,
});

var generator = Assert.IsType<Generator>(chatClient.GetService<Generator>(null));
Assert.Same(generator, chatClient.GetService<Generator>(null));
Assert.Null(chatClient.GetService<Generator>(new object()));
}

[IgnoreOnModelAbsenceFact(DisplayName = "TestTokenizerBatchEncodeDecode")]
public void TestTokenizerBatchEncodeDecode()
{
Expand Down