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
5 changes: 4 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ dotnet_diagnostic.CA2201.severity = warning # Exception type System.Exception is

dotnet_diagnostic.IDE0001.severity = warning # Simplify name
# dotnet_diagnostic.IDE0005.severity = warning # Remove unnecessary using directives
# dotnet_diagnostic.IDE0009.severity = warning # Add this or Me qualification
dotnet_diagnostic.IDE0011.severity = warning # Add braces
dotnet_diagnostic.IDE0018.severity = warning # Inline variable declaration
dotnet_diagnostic.IDE0032.severity = warning # Use auto-implemented property
Expand All @@ -158,6 +157,10 @@ dotnet_diagnostic.IDE0082.severity = warning # Convert typeof to nameof
dotnet_diagnostic.IDE0161.severity = warning # Use file-scoped namespace

# Suppressed diagnostics
dotnet_diagnostic.IDE0009.severity = none # Add this or Me qualification
dotnet_diagnostic.IDE0017.severity = none # Add this or Me qualification
dotnet_diagnostic.IDE0090.severity = none # Simplify new expression

dotnet_diagnostic.CA1002.severity = none # Change 'List<string>' in '...' to use 'Collection<T>' ...
dotnet_diagnostic.CA1032.severity = none # We're using RCS1194 which seems to cover more ctors
dotnet_diagnostic.CA1034.severity = none # Do not nest type. Alternatively, change its accessibility so that it is not externally visible
Expand Down
380 changes: 380 additions & 0 deletions examples/.editorconfig

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/CoffeeShop/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public CoffeeShopApp()

public override async Task ProcessInputAsync(string input, CancellationToken cancelToken)
{
Cart cart = await _translator.TranslateAsync(input);
Cart cart = await _translator.TranslateAsync(input, cancelToken);

Console.WriteLine("##YOUR ORDER");
string json = Json.Stringify(cart);
Expand Down
2 changes: 1 addition & 1 deletion examples/Sentiment/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public SentimentApp()

public override async Task ProcessInputAsync(string input, CancellationToken cancelToken)
{
SentimentResponse response = await _translator.TranslateAsync(input);
SentimentResponse response = await _translator.TranslateAsync(input, cancelToken);
Console.WriteLine($"The sentiment is {response.Sentiment}");
}

Expand Down
2 changes: 2 additions & 0 deletions examples/typechat.examplesLib/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ public Config()
/// Configuration for OpenAI language models
/// </summary>
public OpenAIConfig OpenAI => _openAI;

/// <summary>
/// Configuration for OpenAI embeddings models
/// </summary>
public OpenAIConfig? OpenAIEmbeddings => _openAIEmbeddings;

public bool HasOpenAI => (_openAI != null);

public bool HasOpenAIEmbeddings => (_openAIEmbeddings != null);
}
17 changes: 10 additions & 7 deletions examples/typechat.examplesLib/ConsoleApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ public ConsoleApp()


public string? ConsolePrompt { get; set; } = ">";

public IList<string> StopStrings => _stopStrings;

public string CommentPrefix { get; set; } = "#";

public string CommandPrefix { get; set; } = "@";

public async Task RunAsync(string consolePrompt, string? inputFilePath = null)
Expand All @@ -40,7 +43,7 @@ public async Task RunAsync(CancellationToken cancelToken = default)
string? input = await ReadLineAsync(cancelToken).ConfigureAwait(false);
input = input.Trim();
if (!string.IsNullOrEmpty(input) &&
!await EvalInput(input, cancelToken).ConfigureAwait(false))
!await EvalInputAsync(input, cancelToken).ConfigureAwait(false))
{
break;
}
Expand All @@ -63,22 +66,22 @@ public async Task RunBatchAsync(string batchFilePath, CancellationToken cancelTo

Console.Write(ConsolePrompt);
Console.WriteLine(line);
await EvalInput(line, cancelToken).ConfigureAwait(false);
await EvalInputAsync(line, cancelToken).ConfigureAwait(false);
}
}

/// <summary>
/// Return false if should exit
/// </summary>
async Task<bool> EvalInput(string input, CancellationToken cancelToken)
async Task<bool> EvalInputAsync(string input, CancellationToken cancelToken)
{
try
{
if (input.StartsWith(CommandPrefix))
{
return await EvalCommand(input, cancelToken).ConfigureAwait(false);
return await EvalCommandAsync(input, cancelToken).ConfigureAwait(false);
}
return await EvalLine(input, cancelToken).ConfigureAwait(false);
return await EvalLineAsync(input, cancelToken).ConfigureAwait(false);
}
catch (Exception ex)
{
Expand All @@ -87,7 +90,7 @@ async Task<bool> EvalInput(string input, CancellationToken cancelToken)
return true;
}

async Task<bool> EvalLine(string input, CancellationToken cancelToken)
async Task<bool> EvalLineAsync(string input, CancellationToken cancelToken)
{
if (IsStop(input))
{
Expand All @@ -97,7 +100,7 @@ async Task<bool> EvalLine(string input, CancellationToken cancelToken)
return true;
}

async Task<bool> EvalCommand(string input, CancellationToken cancelToken)
async Task<bool> EvalCommandAsync(string input, CancellationToken cancelToken)
{
// Process a command
List<string> parts = CommandLineStringSplitter.Instance.Split(input).ToList();
Expand Down
22 changes: 17 additions & 5 deletions examples/typechat.examplesLib/Dialog/VectorizedMessageList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,15 @@ public int MaxContextMatches
}
}

public IEnumerable<Message> All() => _messageList.All();
public IAsyncEnumerable<Message> AllAsync(CancellationToken cancelToken) => _messageList.AllAsync(cancelToken);
public IEnumerable<Message> All()
{
return _messageList.All();
}

public IAsyncEnumerable<Message> AllAsync(CancellationToken cancelToken)
{
return _messageList.AllAsync(cancelToken);
}

public void Append(Message message)
{
Expand Down Expand Up @@ -101,11 +108,16 @@ public async IAsyncEnumerable<IPromptSection> GetContextAsync(string request, [E
/// Return newest messages
/// </summary>
/// <returns>an enumerable of messages</returns>
public IEnumerable<Message> Newest() => _messageList.Newest();

public IEnumerable<Message> Newest()
{
return _messageList.Newest();
}
/// <summary>
/// Return newest messages
/// </summary>
/// <returns>an async enumerable of messages</returns>
public IAsyncEnumerable<Message> NewestAsync(CancellationToken cancelToken) => _messageList.NewestAsync(cancelToken);
public IAsyncEnumerable<Message> NewestAsync(CancellationToken cancelToken)
{
return _messageList.NewestAsync(cancelToken);
}
}
1 change: 1 addition & 0 deletions examples/typechat.examplesLib/PluginProgramTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public PluginProgramTranslator(IKernel kernel, ModelInfo model)
_pluginSchema
);
}

/// <summary>
/// Translator being used
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions examples/typechat.examplesLib/PluginProgramValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ protected override void VisitFunction(FunctionCall functionCall)
// Verify function exists
var name = PluginFunctionName.Parse(functionCall.Name);
FunctionView typeInfo = _typeInfo[name];

// Verify that parameter counts etc match
ValidateArgs(functionCall, typeInfo, functionCall.Args);

// Continue visiting to handle any nested function calls
base.VisitFunction(functionCall);
return;
Expand All @@ -55,22 +57,26 @@ void ValidateArgs(FunctionCall call, FunctionView typeInfo, Expression[] args)
{
// Verify arg counts
CheckArgCount(call, typeInfo, args);

// Verify arg types
TypeCheckArgs(call, typeInfo.Parameters, args);
}

int GetRequiredArgCount(IList<ParameterView> parameters)
{
int requiredCount = 0;

for (int i = 0; i < parameters.Count; ++i)
{
if (IsOptional(parameters[i]))
{
// Optional parameters follow required ones
break;
}

requiredCount++;
}

return requiredCount;
}

Expand All @@ -79,10 +85,12 @@ void CheckArgCount(FunctionCall call, FunctionView typeInfo, Expression[] args)
// Just checks if the right number of parameters were supplied
int requiredArgCount = (typeInfo.Parameters != null) ? GetRequiredArgCount(typeInfo.Parameters) : 0;
int actualCount = (args != null) ? args.Length : 0;

if (actualCount < requiredArgCount)
{
ProgramException.ThrowArgCountMismatch(call, requiredArgCount, actualCount);
}

int totalArgCount = (typeInfo.Parameters != null) ? typeInfo.Parameters.Count : 0;
if (actualCount > totalArgCount)
{
Expand All @@ -93,6 +101,7 @@ void CheckArgCount(FunctionCall call, FunctionView typeInfo, Expression[] args)
void TypeCheckArgs(FunctionCall call, IList<ParameterView> parameters, Expression[] args)
{
Debug.Assert(args.Length <= parameters.Count);

for (int i = 0; i < args.Length; ++i)
{
ParameterViewType expectedType = parameters[i].Type;
Expand Down
9 changes: 9 additions & 0 deletions examples/typechat.examplesLib/VectorTextIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public async Task AddAsync(T item, string textRepresentation, CancellationToken
ArgumentVerify.ThrowIfNullOrEmpty(textRepresentation, nameof(textRepresentation));

var embedding = await GetNormalizedEmbeddingAsync(textRepresentation, cancelToken).ConfigureAwait(false);

_list.Add(item, embedding);
}

Expand All @@ -84,15 +85,18 @@ public async Task AddAsync(T[] items, string[] textRepresentations, Cancellation
{
ArgumentVerify.ThrowIfNull(items, nameof(items));
ArgumentVerify.ThrowIfNull(textRepresentations, nameof(textRepresentations));

if (items.Length != textRepresentations.Length)
{
throw new ArgumentException("items and their representations must of the same length");
}

Embedding[] embeddings = await GetNormalizedEmbeddingAsync(textRepresentations, cancelToken).ConfigureAwait(false);
if (embeddings.Length != items.Length)
{
throw new InvalidOperationException($"Embedding length {embeddings.Length} does not match items length {items.Length}");
}

for (int i = 0; i < items.Length; ++i)
{
_list.Add(items[i], embeddings[i]);
Expand All @@ -108,6 +112,7 @@ public async Task AddAsync(T[] items, string[] textRepresentations, Cancellation
public async Task<T> NearestAsync(string text, CancellationToken cancelToken = default)
{
var embedding = await GetNormalizedEmbeddingAsync(text, cancelToken).ConfigureAwait(false);

return _list.Nearest(embedding, EmbeddingDistance.Dot);
}

Expand All @@ -121,23 +126,27 @@ public async Task<T> NearestAsync(string text, CancellationToken cancelToken = d
public async Task<List<T>> NearestAsync(string text, int maxMatches, CancellationToken cancelToken = default)
{
var embedding = await GetNormalizedEmbeddingAsync(text, cancelToken).ConfigureAwait(false);

return _list.Nearest(embedding, maxMatches, EmbeddingDistance.Dot).ToList();
}

async Task<Embedding> GetNormalizedEmbeddingAsync(string text, CancellationToken cancelToken)
{
var embedding = await _model.GenerateEmbeddingAsync(text, cancelToken).ConfigureAwait(false);
embedding.NormalizeInPlace();

return embedding;
}

async Task<Embedding[]> GetNormalizedEmbeddingAsync(string[] texts, CancellationToken cancelToken)
{
var embeddings = await _model.GenerateEmbeddingsAsync(texts, cancelToken).ConfigureAwait(false);

for (int i = 0; i < embeddings.Length; ++i)
{
embeddings[i].NormalizeInPlace();
}

return embeddings;
}
}
2 changes: 2 additions & 0 deletions src/typechat/ConstraintsValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ string ToErrorString(List<ValidationResult> validationResults)
{
sb.AppendLine(result.ErrorMessage);
}

return sb.ToString();
}
}
Expand Down Expand Up @@ -72,6 +73,7 @@ string ToErrorString(List<ValidationResult> validationResults)
{
sb.AppendLine(result.ErrorMessage);
}

return sb.ToString();
}
}
1 change: 1 addition & 0 deletions src/typechat/LanguageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public LanguageModel(OpenAIConfig config, ModelInfo? model = null, HttpClient? c
/// Information about the language model
/// </summary>
public ModelInfo ModelInfo => _model;

/// <summary>
/// Get a completion for the given prompt
/// </summary>
Expand Down
Loading