Skip to content

Commit

Permalink
Merge pull request #161 from awaescher/hotfix/159
Browse files Browse the repository at this point in the history
Fix SendAsync/SendAsAsync overloads introduced in #159
  • Loading branch information
awaescher authored Jan 3, 2025
2 parents f2d35e6 + 3eac2bd commit f38890d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public Chat(IOllamaApiClient client, string systemPrompt = "")
/// </code>
/// </example>
public IAsyncEnumerable<string> SendAsync(string message, CancellationToken cancellationToken = default)
=> SendAsync(message, tools: null, imagesAsBase64: null, cancellationToken);
=> SendAsync(message, tools: null, imagesAsBase64: null, format: null, cancellationToken: cancellationToken);

/// <summary>
/// Sends a message to the currently selected model and streams its response
Expand All @@ -127,7 +127,7 @@ public IAsyncEnumerable<string> SendAsync(string message, CancellationToken canc
/// </code>
/// </example>
public IAsyncEnumerable<string> SendAsync(string message, IEnumerable<IEnumerable<byte>>? imagesAsBytes, CancellationToken cancellationToken = default)
=> SendAsync(message, imagesAsBytes?.ToBase64() ?? [], cancellationToken);
=> SendAsync(message, imagesAsBase64: imagesAsBytes?.ToBase64() ?? [], cancellationToken: cancellationToken);

/// <summary>
/// Sends a message to the currently selected model and streams its response
Expand Down Expand Up @@ -155,7 +155,7 @@ public IAsyncEnumerable<string> SendAsync(string message, IEnumerable<IEnumerabl
/// </code>
/// </example>
public IAsyncEnumerable<string> SendAsync(string message, IEnumerable<string>? imagesAsBase64, CancellationToken cancellationToken = default)
=> SendAsync(message, [], imagesAsBase64, cancellationToken);
=> SendAsync(message, tools: [], imagesAsBase64: imagesAsBase64, cancellationToken: cancellationToken);

/// <summary>
/// Sends a message to the currently selected model and streams its response
Expand All @@ -166,7 +166,7 @@ public IAsyncEnumerable<string> SendAsync(string message, IEnumerable<string>? i
/// <param name="format">Currently accepts "json" or JsonSchema or null.</param>
/// <param name="cancellationToken">The token to cancel the operation with</param>
public IAsyncEnumerable<string> SendAsync(string message, IEnumerable<Tool>? tools, IEnumerable<string>? imagesAsBase64 = null, object? format = null, CancellationToken cancellationToken = default)
=> SendAsAsync(ChatRole.User, message, tools, imagesAsBase64, format, cancellationToken);
=> SendAsAsync(ChatRole.User, message, tools: tools, imagesAsBase64: imagesAsBase64, format: format, cancellationToken: cancellationToken);

/// <summary>
/// Sends a message in a given role to the currently selected model and streams its response
Expand All @@ -175,7 +175,7 @@ public IAsyncEnumerable<string> SendAsync(string message, IEnumerable<Tool>? too
/// <param name="message">The message to send</param>
/// <param name="cancellationToken">The token to cancel the operation with</param>
public IAsyncEnumerable<string> SendAsAsync(ChatRole role, string message, CancellationToken cancellationToken = default)
=> SendAsAsync(role, message, tools: null, imagesAsBase64: null, cancellationToken);
=> SendAsAsync(role, message, tools: null, imagesAsBase64: null, cancellationToken: cancellationToken);

/// <summary>
/// Sends a message in a given role to the currently selected model and streams its response
Expand All @@ -185,7 +185,7 @@ public IAsyncEnumerable<string> SendAsAsync(ChatRole role, string message, Cance
/// <param name="imagesAsBytes">Images in byte representation to send to the model</param>
/// <param name="cancellationToken">The token to cancel the operation with</param>
public IAsyncEnumerable<string> SendAsAsync(ChatRole role, string message, IEnumerable<IEnumerable<byte>>? imagesAsBytes, CancellationToken cancellationToken = default)
=> SendAsAsync(role, message, imagesAsBytes?.ToBase64() ?? [], cancellationToken);
=> SendAsAsync(role, message, imagesAsBase64: imagesAsBytes?.ToBase64() ?? [], cancellationToken: cancellationToken);

/// <summary>
/// Sends a message in a given role to the currently selected model and streams its response
Expand All @@ -195,7 +195,7 @@ public IAsyncEnumerable<string> SendAsAsync(ChatRole role, string message, IEnum
/// <param name="imagesAsBase64">Base64 encoded images to send to the model</param>
/// <param name="cancellationToken">The token to cancel the operation with</param>
public IAsyncEnumerable<string> SendAsAsync(ChatRole role, string message, IEnumerable<string>? imagesAsBase64, CancellationToken cancellationToken = default)
=> SendAsAsync(role, message, [], imagesAsBase64, cancellationToken);
=> SendAsAsync(role, message, tools: [], imagesAsBase64: imagesAsBase64, cancellationToken: cancellationToken);

/// <summary>
/// Sends a message in a given role to the currently selected model and streams its response
Expand All @@ -208,6 +208,9 @@ public IAsyncEnumerable<string> SendAsAsync(ChatRole role, string message, IEnum
/// <param name="cancellationToken">The token to cancel the operation with</param>
public async IAsyncEnumerable<string> SendAsAsync(ChatRole role, string message, IEnumerable<Tool>? tools, IEnumerable<string>? imagesAsBase64 = null, object? format = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
if (format is CancellationToken)
throw new NotSupportedException($"Argument \"{nameof(format)}\" cannot be of type {nameof(CancellationToken)}. Make sure you use the correct method overload of {nameof(Chat)}{nameof(SendAsync)}() or {nameof(Chat)}{nameof(SendAsAsync)}().");

Messages.Add(new Message(role, message, imagesAsBase64?.ToArray()));

var hasTools = tools?.Any() ?? false;
Expand Down
12 changes: 12 additions & 0 deletions test/Tests.v3.ncrunchproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<ProjectConfiguration>
<Settings>
<IgnoredTests>
<FixtureTestSelector>
<FixtureName>Tests.FunctionalTests.ChatTests</FixtureName>
</FixtureTestSelector>
<FixtureTestSelector>
<FixtureName>Tests.FunctionalTests.OllamaApiClientTests</FixtureName>
</FixtureTestSelector>
</IgnoredTests>
</Settings>
</ProjectConfiguration>

0 comments on commit f38890d

Please sign in to comment.