Skip to content

Commit

Permalink
Merge pull request #163 from Joy-less/source-code-cleanups
Browse files Browse the repository at this point in the history
Source code cleanups
  • Loading branch information
awaescher authored Jan 8, 2025
2 parents fcba491 + fc60fd3 commit 44aa1b4
Show file tree
Hide file tree
Showing 18 changed files with 23 additions and 42 deletions.
3 changes: 3 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>

<!-- IDE0130: Namespace does not match folder structure -->
<NoWarn>IDE0130</NoWarn>
</PropertyGroup>
</Project>
1 change: 1 addition & 0 deletions demo/OllamaApiConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.6" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
<PackageReference Include="Spectre.Console.ImageSharp" Version="0.49.1" />
</ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions src/AsyncEnumerableExtensions/ChatResponseStreamAppender.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using OllamaSharp.Models.Chat;

namespace OllamaSharp.AsyncEnumerableExtensions;

/// <summary>
/// Appender to stream <see cref="System.Collections.Generic.IAsyncEnumerable{ChatResponseStream}"/> to
/// Appender to stream <see cref="IAsyncEnumerable{ChatResponseStream}"/> to
/// build up one single <see cref="ChatDoneResponseStream"/> object
/// </summary>
internal class ChatResponseStreamAppender : IAppender<ChatResponseStream?, ChatDoneResponseStream?>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using OllamaSharp.Models;

namespace OllamaSharp.AsyncEnumerableExtensions;

/// <summary>
/// Appender to stream <see cref="System.Collections.Generic.IAsyncEnumerable{GenerateDoneResponseStream}"/>
/// Appender to stream <see cref="IAsyncEnumerable{GenerateDoneResponseStream}"/>
/// to build up one single <see cref="GenerateDoneResponseStream"/> object
/// </summary>
internal class GenerateResponseStreamAppender : IAppender<GenerateResponseStream?, GenerateDoneResponseStream?>
Expand Down
5 changes: 0 additions & 5 deletions src/AsyncEnumerableExtensions/IAsyncEnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using OllamaSharp.AsyncEnumerableExtensions;
using OllamaSharp.Models;
using OllamaSharp.Models.Chat;

#pragma warning disable IDE0130 // Namespace does not match folder structure
namespace OllamaSharp;
#pragma warning restore IDE0130 // Namespace does not match folder structure

/// <summary>
/// Extension methods to stream IAsyncEnumerable to its end and return one single result value
Expand Down
3 changes: 2 additions & 1 deletion src/AsyncEnumerableExtensions/StringAppender.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Text;
using System.Collections.Generic;

namespace OllamaSharp.AsyncEnumerableExtensions;

/// <summary>
/// Appender to stream <see cref="System.Collections.Generic.IAsyncEnumerable{String}"/> to build up one single result string
/// Appender to stream <see cref="IAsyncEnumerable{String}"/> to build up one single result string
/// </summary>
internal class StringAppender : IAppender<string, string>
{
Expand Down
1 change: 0 additions & 1 deletion src/ByteArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ internal static class ByteArrayExtensions
/// <returns>A base-64 encoded string representation of the input byte sequence.</returns>
public static string ToBase64(this IEnumerable<byte> bytes) => Convert.ToBase64String(bytes.ToArray());


/// <summary>
/// Converts a collection of byte arrays to a collection of base64 strings.
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions src/Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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, imagesAsBase64: imagesAsBytes?.ToBase64() ?? [], cancellationToken: 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, tools: [], imagesAsBase64: imagesAsBase64, cancellationToken: cancellationToken);
=> SendAsync(message, tools: null, imagesAsBase64: imagesAsBase64, cancellationToken: cancellationToken);

/// <summary>
/// Sends a message to the currently selected model and streams its response
Expand Down Expand Up @@ -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, imagesAsBase64: imagesAsBytes?.ToBase64() ?? [], cancellationToken: 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, tools: [], imagesAsBase64: imagesAsBase64, cancellationToken: cancellationToken);
=> SendAsAsync(role, message, tools: null, imagesAsBase64: imagesAsBase64, cancellationToken: cancellationToken);

/// <summary>
/// Sends a message in a given role to the currently selected model and streams its response
Expand Down
4 changes: 0 additions & 4 deletions src/IOllamaApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using OllamaSharp.Models;
using OllamaSharp.Models.Chat;

Expand Down
2 changes: 1 addition & 1 deletion src/MicrosoftAi/AbstractionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private static Models.Chat.ChatRole ToOllamaSharpRole(Microsoft.Extensions.AI.Ch
"system" => Models.Chat.ChatRole.System,
"user" => Models.Chat.ChatRole.User,
"tool" => Models.Chat.ChatRole.Tool,
_ => new OllamaSharp.Models.Chat.ChatRole(role.Value),
_ => new Models.Chat.ChatRole(role.Value),
};
}

Expand Down
2 changes: 0 additions & 2 deletions src/MicrosoftAi/ChatOptionsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Microsoft.Extensions.AI;
using OllamaSharp.Models;

#pragma warning disable IDE0130 // Namespace does not match folder structure
namespace OllamaSharp;
#pragma warning restore IDE0130 // Namespace does not match folder structure

/// <summary>
/// Extension methods to stream IAsyncEnumerable to its end and return one single result value
Expand Down
15 changes: 5 additions & 10 deletions src/MicrosoftAi/IAsyncEnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.AI;

#pragma warning disable IDE0130 // Namespace does not match folder structure
namespace OllamaSharp;
#pragma warning restore IDE0130 // Namespace does not match folder structure

/// <summary>
/// Extension methods to stream IAsyncEnumerable to its end and return one single result value
/// </summary>
public static partial class IAsyncEnumerableExtensions
{
/// <summary>
/// Streams a given <see cref="IAsyncEnumerable{StreamingChatCompletionUpdate}" /> of response chunks to its end and builds one single <see cref="StreamingChatCompletionUpdate"/> out of them.
/// Streams a given <see cref="IAsyncEnumerable{StreamingChatCompletionUpdate}"/> of response chunks to its end and builds one single <see cref="StreamingChatCompletionUpdate"/> out of them.
/// </summary>
/// <param name="stream">The <see cref="IAsyncEnumerable{StreamingChatCompletionUpdate}" /> to stream</param>
/// <param name="itemCallback">An optional callback to additionally process every single item from the IAsyncEnumerable</param>
/// <returns>A single <see cref="StreamingChatCompletionUpdate"/> built up from every single IAsyncEnumerable item</returns>
/// <param name="stream">The <see cref="IAsyncEnumerable{StreamingChatCompletionUpdate}"/> to stream.</param>
/// <param name="itemCallback">An optional callback to additionally process every single item from the IAsyncEnumerable.</param>
/// <returns>A single <see cref="StreamingChatCompletionUpdate"/> built up from every single IAsyncEnumerable item.</returns>
public static Task<StreamingChatCompletionUpdate?> StreamToEndAsync(this IAsyncEnumerable<StreamingChatCompletionUpdate?> stream, Action<StreamingChatCompletionUpdate?>? itemCallback = null)
=> stream.StreamToEndAsync(new MicrosoftAi.StreamingChatCompletionUpdateAppender(), itemCallback);
}
}
2 changes: 1 addition & 1 deletion src/MicrosoftAi/StreamingChatCompletionUpdateAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace OllamaSharp.MicrosoftAi;

/// <summary>
/// Appender to stream <see cref="System.Collections.Generic.IAsyncEnumerable{StreamingChatCompletionUpdate}" />
/// Appender to stream <see cref="IAsyncEnumerable{StreamingChatCompletionUpdate}" />
/// to build up one consolidated <see cref="StreamingChatCompletionUpdate"/> object
/// </summary>
internal class StreamingChatCompletionUpdateAppender : IAppender<StreamingChatCompletionUpdate?, StreamingChatCompletionUpdate?>
Expand Down
1 change: 0 additions & 1 deletion src/MicrosoftAi/StreamingChatCompletionUpdateBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.AI;

Expand Down
4 changes: 1 addition & 3 deletions src/Models/PushModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
namespace OllamaSharp.Models;

/// <summary>
/// Upload a model to a model library. Requires registering for ollama.ai and
/// adding a public key first.
///
/// Upload a model to a model library. Requires registering for ollama.ai and adding a public key first.<br/>
/// <see href="https://github.com/ollama/ollama/blob/main/docs/api.md#push-a-model">Ollama API docs</see>
/// </summary>
public class PushModelRequest : OllamaRequest
Expand Down
4 changes: 1 addition & 3 deletions src/Models/ShowModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
namespace OllamaSharp.Models;

/// <summary>
/// Show information about a model including details, modelfile, template,
/// parameters, license, system prompt.
///
/// Show information about a model including details, modelfile, template, parameters, license, system prompt.<br/>
/// <see href="https://github.com/ollama/ollama/blob/main/docs/api.md#show-model-information">Ollama API docs</see>
/// </summary>
[JsonUnmappedMemberHandling(JsonUnmappedMemberHandling.Skip)]
Expand Down
2 changes: 1 addition & 1 deletion src/OllamaApiClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static Task<EmbedResponse> EmbedAsync(this IOllamaApiClient client, strin
Prompt = prompt,
Model = client.SelectedModel,
Stream = true,
Context = context?.Context ?? []
Context = context?.Context
};
return client.GenerateAsync(request, cancellationToken);
}
Expand Down
2 changes: 0 additions & 2 deletions test/FunctionalTests/ChatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ public class ChatTests
private readonly Uri _baseUri = new("http://localhost:11434");
private readonly string _model = "llama3.2:1b";

#pragma warning disable NUnit1032
private OllamaApiClient _client = null!;
private Chat _chat = null!;
#pragma warning restore NUnit1032

[SetUp]
public async Task Setup()
Expand Down

0 comments on commit 44aa1b4

Please sign in to comment.