Skip to content
Merged
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
2 changes: 1 addition & 1 deletion docs/samples/Microsoft.ML.GenAI.Samples/MEAI/Llama3_1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static async Task RunAsync(string weightFolder, string checkPointName = "
""";
var chatMessage = new ChatMessage(ChatRole.User, task);

await foreach (var response in client.CompleteStreamingAsync([chatMessage]))
await foreach (var response in client.GetStreamingResponseAsync([chatMessage]))
{
Console.Write(response.Text);
}
Expand Down
2 changes: 1 addition & 1 deletion docs/samples/Microsoft.ML.GenAI.Samples/MEAI/Phi3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static async Task RunAsync(string weightFolder)
""";
var chatMessage = new ChatMessage(ChatRole.User, task);

await foreach (var response in client.CompleteStreamingAsync([chatMessage]))
await foreach (var response in client.GetStreamingResponseAsync([chatMessage]))
{
Console.Write(response.Text);
}
Expand Down
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<MicrosoftDotNetInteractiveVersion>1.0.0-beta.24375.2</MicrosoftDotNetInteractiveVersion>
<MicrosoftMLOnnxRuntimeVersion>1.18.1</MicrosoftMLOnnxRuntimeVersion>
<MlNetMklDepsVersion>0.0.0.12</MlNetMklDepsVersion>
<MicrosoftExtensionsAIVersion>9.0.1-preview.1.24570.5</MicrosoftExtensionsAIVersion>
<MicrosoftExtensionsAIVersion>9.3.0-preview.1.25114.11</MicrosoftExtensionsAIVersion>
<!-- runtime.native.System.Data.SqlClient.sni is not updated by dependency flow as it is not produced live anymore. -->
<RuntimeNativeSystemDataSqlClientSniVersion>4.4.0</RuntimeNativeSystemDataSqlClientSniVersion>
<!--
Expand Down
17 changes: 9 additions & 8 deletions src/Microsoft.ML.GenAI.Core/CausalLMPipelineChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@ public abstract class CausalLMPipelineChatClient<TTokenizer, TCausalLMModel> : I
{
private readonly ICausalLMPipeline<TTokenizer, TCausalLMModel> _pipeline;
private readonly IMEAIChatTemplateBuilder _chatTemplateBuilder;
private readonly ChatClientMetadata _metadata;

public CausalLMPipelineChatClient(
ICausalLMPipeline<TTokenizer, TCausalLMModel> pipeline,
IMEAIChatTemplateBuilder chatTemplateBuilder,
ChatClientMetadata? metadata = null)
{
var classNameWithType = $"{nameof(CausalLMPipelineChatClient<TTokenizer, TCausalLMModel>)}<{typeof(TTokenizer).Name}, {typeof(TCausalLMModel).Name}>";
Metadata ??= new ChatClientMetadata(providerName: classNameWithType, modelId: typeof(TCausalLMModel).Name);
_metadata = new ChatClientMetadata(providerName: classNameWithType, modelId: typeof(TCausalLMModel).Name);
_chatTemplateBuilder = chatTemplateBuilder;
_pipeline = pipeline;
}

public ChatClientMetadata Metadata { get; }

public virtual Task<ChatCompletion> CompleteAsync(IList<ChatMessage> chatMessages, ChatOptions? options = null, CancellationToken cancellationToken = default)
public virtual Task<ChatResponse> GetResponseAsync(IList<ChatMessage> chatMessages, ChatOptions? options = null, CancellationToken cancellationToken = default)
{
var prompt = _chatTemplateBuilder.BuildPrompt(chatMessages, options);
var stopSequences = options?.StopSequences ?? Array.Empty<string>();
Expand All @@ -46,15 +45,15 @@ public virtual Task<ChatCompletion> CompleteAsync(IList<ChatMessage> chatMessage
stopSequences: stopSequences.ToArray()) ?? throw new InvalidOperationException("Failed to generate a reply.");

var chatMessage = new ChatMessage(ChatRole.Assistant, output);
return Task.FromResult(new ChatCompletion([chatMessage])
return Task.FromResult(new ChatResponse([chatMessage])
{
CreatedAt = DateTime.UtcNow,
FinishReason = ChatFinishReason.Stop,
});
}

#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public virtual async IAsyncEnumerable<StreamingChatCompletionUpdate> CompleteStreamingAsync(
public virtual async IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
IList<ChatMessage> chatMessages,
ChatOptions? options = null,
Expand All @@ -69,7 +68,7 @@ public virtual async IAsyncEnumerable<StreamingChatCompletionUpdate> CompleteStr
temperature: options?.Temperature ?? 0.7f,
stopSequences: stopSequences.ToArray()))
{
yield return new StreamingChatCompletionUpdate
yield return new ChatResponseUpdate
{
Role = ChatRole.Assistant,
Text = output,
Expand All @@ -83,6 +82,8 @@ public virtual void Dispose()
}

public virtual object? GetService(Type serviceType, object? serviceKey = null) =>
serviceKey is null && serviceType is not null && serviceType.IsAssignableFrom(GetType()) ? this :
serviceKey is not null ? null :
serviceType == typeof(ChatClientMetadata) ? _metadata :
serviceType.IsAssignableFrom(GetType()) ? this :
null;
}
8 changes: 4 additions & 4 deletions src/Microsoft.ML.GenAI.LLaMA/Llama3CausalLMChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Llama3CausalLMChatClient(
{
}

public override Task<ChatCompletion> CompleteAsync(
public override Task<ChatResponse> GetResponseAsync(
IList<ChatMessage> chatMessages,
ChatOptions? options = null,
CancellationToken cancellationToken = default)
Expand All @@ -40,10 +40,10 @@ public override Task<ChatCompletion> CompleteAsync(
options.StopSequences = new List<string> { _eotToken };
}

return base.CompleteAsync(chatMessages, options, cancellationToken);
return base.GetResponseAsync(chatMessages, options, cancellationToken);
}

public override IAsyncEnumerable<StreamingChatCompletionUpdate> CompleteStreamingAsync(
public override IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(
IList<ChatMessage> chatMessages,
ChatOptions? options = null,
CancellationToken cancellationToken = default)
Expand All @@ -52,6 +52,6 @@ public override IAsyncEnumerable<StreamingChatCompletionUpdate> CompleteStreamin
options.StopSequences ??= [];
options.StopSequences.Add(_eotToken);

return base.CompleteStreamingAsync(chatMessages, options, cancellationToken);
return base.GetStreamingResponseAsync(chatMessages, options, cancellationToken);
}
}
8 changes: 4 additions & 4 deletions src/Microsoft.ML.GenAI.Phi/Phi3/Phi3CausalLMChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Phi3CausalLMChatClient(
{
}

public override Task<ChatCompletion> CompleteAsync(
public override Task<ChatResponse> GetResponseAsync(
IList<ChatMessage> chatMessages,
ChatOptions? options = null,
CancellationToken cancellationToken = default)
Expand All @@ -45,10 +45,10 @@ public override Task<ChatCompletion> CompleteAsync(
options.StopSequences = [_eotToken];
}

return base.CompleteAsync(chatMessages, options, cancellationToken);
return base.GetResponseAsync(chatMessages, options, cancellationToken);
}

public override IAsyncEnumerable<StreamingChatCompletionUpdate> CompleteStreamingAsync(
public override IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(
IList<ChatMessage> chatMessages,
ChatOptions? options = null,
CancellationToken cancellationToken = default)
Expand All @@ -57,6 +57,6 @@ public override IAsyncEnumerable<StreamingChatCompletionUpdate> CompleteStreamin
options.StopSequences ??= [];
options.StopSequences.Add(_eotToken);

return base.CompleteStreamingAsync(chatMessages, options, cancellationToken);
return base.GetStreamingResponseAsync(chatMessages, options, cancellationToken);
}
}
Loading