Skip to content
Closed
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
16 changes: 14 additions & 2 deletions TransformersSharp/MEAI/ChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public class TextGenerationPipelineChatClient : IChatClient
{
public TextGenerationPipeline TextGenerationPipeline { get; private set; }

Check warning on line 8 in TransformersSharp/MEAI/ChatClient.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'TextGenerationPipeline' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 8 in TransformersSharp/MEAI/ChatClient.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'TextGenerationPipeline' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

public static TextGenerationPipelineChatClient FromModel(string model, TorchDtype? torchDtype = null, string? device = null, bool trustRemoteCode = false)
{
Expand All @@ -24,12 +24,24 @@
{
return Task.Run(() =>
{
var result = TextGenerationPipeline.Generate(messages.Select(
var input = messages.Select(
message => new Dictionary<string, string>
{
{ "role", message.Role.Value },
{ "content", message.Text }
}).ToList(),
}).ToList();

if (options?.Instructions is { } instructions)
{
input.Add(new()
{
["role"] = ChatRole.System.Value,
["content"] = instructions
});
}

var result = TextGenerationPipeline.Generate(
input,
maxNewTokens: options?.MaxOutputTokens,
topk: options?.TopK,
topp: options?.TopP,
Expand All @@ -48,7 +60,7 @@
serviceType == typeof(TextGenerationPipeline) ? this.TextGenerationPipeline :
null;

public async IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)

Check warning on line 63 in TransformersSharp/MEAI/ChatClient.cs

View workflow job for this annotation

GitHub Actions / build

Async-iterator 'TextGenerationPipelineChatClient.GetStreamingResponseAsync(IEnumerable<ChatMessage>, ChatOptions?, CancellationToken)' has one or more parameters of type 'CancellationToken' but none of them is decorated with the 'EnumeratorCancellation' attribute, so the cancellation token parameter from the generated 'IAsyncEnumerable<>.GetAsyncEnumerator' will be unconsumed

Check warning on line 63 in TransformersSharp/MEAI/ChatClient.cs

View workflow job for this annotation

GitHub Actions / build

Async-iterator 'TextGenerationPipelineChatClient.GetStreamingResponseAsync(IEnumerable<ChatMessage>, ChatOptions?, CancellationToken)' has one or more parameters of type 'CancellationToken' but none of them is decorated with the 'EnumeratorCancellation' attribute, so the cancellation token parameter from the generated 'IAsyncEnumerable<>.GetAsyncEnumerator' will be unconsumed
{
var response = await GetResponseAsync(messages, options, cancellationToken).ConfigureAwait(false);
foreach (var update in response.ToChatResponseUpdates())
Expand Down
4 changes: 2 additions & 2 deletions TransformersSharp/TransformersSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

<ItemGroup>
<PackageReference Include="CSnakes.Runtime" Version="1.0.33" />
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.5.0" />
<PackageReference Include="Microsoft.ML.Tokenizers" Version="2.0.0-preview.1.25127.4" />
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.9.0" />
<PackageReference Include="Microsoft.ML.Tokenizers" Version="2.0.0-preview.25373.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading