-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update for genai API changes + feedback
- Loading branch information
1 parent
4c2d892
commit 2f14dfe
Showing
3 changed files
with
121 additions
and
68 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,73 @@ | ||
using Microsoft.Extensions.AI; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.ML.OnnxRuntimeGenAI; | ||
|
||
/// <summary>Provides configuration options used when constructing a <see cref="ChatClient"/>.</summary> | ||
/// <remarks> | ||
/// Every model has different requirements for stop sequences and prompt formatting. For best results, | ||
/// the configuration should be tailored to the exact nature of the model being used. For example, | ||
/// when using a Phi3 model, a configuration like the following may be used: | ||
/// <code> | ||
/// static ChatClientConfiguration CreateForPhi3() => | ||
/// new(["<|system|>", "<|user|>", "<|assistant|>", "<|end|>"], | ||
/// (IEnumerable<ChatMessage> messages) => | ||
/// { | ||
/// StringBuilder prompt = new(); | ||
/// | ||
/// foreach (var message in messages) | ||
/// foreach (var content in message.Contents.OfType<TextContent>()) | ||
/// prompt.Append("<|").Append(message.Role.Value).Append("|>\n").Append(tc.Text).Append("<|end|>\n"); | ||
/// | ||
/// return prompt.Append("<|assistant|>\n").ToString(); | ||
/// }); | ||
/// </code> | ||
/// </remarks> | ||
public sealed class ChatClientConfiguration | ||
{ | ||
private string[] _stopSequences; | ||
private Func<IEnumerable<ChatMessage>, string> _promptFormatter; | ||
|
||
/// <summary>Initializes a new instance of the <see cref="ChatClientConfiguration"/> class.</summary> | ||
/// <param name="stopSequences">The stop sequences used by the model.</param> | ||
/// <param name="promptFormatter">The function to use to format a list of messages for input into the model.</param> | ||
/// <exception cref="ArgumentNullException"><paramref name="stopSequences"/> is null.</exception> | ||
/// <exception cref="ArgumentNullException"><paramref name="promptFormatter"/> is null.</exception> | ||
public ChatClientConfiguration( | ||
string[] stopSequences, | ||
Func<IEnumerable<ChatMessage>, string> promptFormatter) | ||
{ | ||
if (stopSequences is null) | ||
{ | ||
throw new ArgumentNullException(nameof(stopSequences)); | ||
} | ||
|
||
if (promptFormatter is null) | ||
{ | ||
throw new ArgumentNullException(nameof(promptFormatter)); | ||
} | ||
|
||
StopSequences = stopSequences; | ||
PromptFormatter = promptFormatter; | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets stop sequences to use during generation. | ||
/// </summary> | ||
/// <remarks> | ||
/// These will apply in addition to any stop sequences that are a part of the <see cref="ChatOptions.StopSequences"/>. | ||
/// </remarks> | ||
public string[] StopSequences | ||
{ | ||
get => _stopSequences; | ||
set => _stopSequences = value ?? throw new ArgumentNullException(nameof(value)); | ||
} | ||
|
||
/// <summary>Gets the function that creates a prompt string from the chat history.</summary> | ||
public Func<IEnumerable<ChatMessage>, string> PromptFormatter | ||
{ | ||
get => _promptFormatter; | ||
set => _promptFormatter = value ?? throw new ArgumentNullException(nameof(value)); | ||
} | ||
} |
This file contains 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