Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warnings #191

Merged
merged 1 commit into from
Feb 21, 2025
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 src/OllamaSharp/Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public IAsyncEnumerable<string> SendAsync(string message, IEnumerable<object>? t
/// An <see cref="IAsyncEnumerable{T}"/> of strings representing the streamed response from the server.
/// </returns>
/// <example>
/// Example usage of the <see cref="SendAsAsync(string, string)"/> method:
/// Example usage of the <see cref="SendAsAsync(ChatRole, string, CancellationToken)"/> method:
/// <code>
/// var client = new OllamaApiClient("http://localhost:11434", "llama3.2-vision:latest");
/// var chat = new Chat(client);
Expand Down
6 changes: 3 additions & 3 deletions src/OllamaSharp/MicrosoftAi/AbstractionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace OllamaSharp.MicrosoftAi;
internal static class AbstractionMapper
{
/// <summary>
/// Maps a <see cref="ChatRequest"/> and <see cref="ChatDoneResponseStream"/> to a <see cref="ChatCompletion"/>.
/// Maps a <see cref="ChatRequest"/> and <see cref="ChatDoneResponseStream"/> to a <see cref="ChatResponse"/>.
/// </summary>
/// <param name="stream">The response stream with completion data.</param>
/// <param name="usedModel">The used model. This has to be a separate argument because there might be fallbacks from the calling method.</param>
Expand Down Expand Up @@ -137,7 +137,7 @@ private static void TryAddOllamaOption<T>(ChatOptions? microsoftChatOptions, Oll
/// <returns>An enumeration of <see cref="Tool"/> objects containing the converted data.</returns>
private static IEnumerable<object>? ToOllamaSharpTools(IEnumerable<AITool>? tools)
{
return tools?.Select(ToOllamaSharpTool).Where(t => t is not null);
return tools?.Select(ToOllamaSharpTool).Where(t => t is not null)!;
}

/// <summary>
Expand Down Expand Up @@ -313,7 +313,7 @@ private static Microsoft.Extensions.AI.ChatRole ToAbstractionRole(ChatRole? role
/// </summary>
/// <param name="response">The response stream to convert.</param>
/// <returns>A <see cref="ChatResponseUpdate"/> object containing the latest chat completion chunk.</returns>
public static ChatResponseUpdate ToStreamingChatCompletionUpdate(ChatResponseStream? response)
public static ChatResponseUpdate ToChatResponseUpdate(ChatResponseStream? response)
{
return new ChatResponseUpdate
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
namespace OllamaSharp.MicrosoftAi;

/// <summary>
/// Appender to stream <see cref="IAsyncEnumerable{StreamingChatCompletionUpdate}" />
/// Appender to stream <see cref="IAsyncEnumerable{ChatResponseUpdate}" />
/// to build up one consolidated <see cref="ChatResponseUpdate"/> object
/// </summary>
internal class StreamingChatCompletionUpdateAppender : IAppender<ChatResponseUpdate?, ChatResponseUpdate?>
internal class ChatResponseUpdateAppender : IAppender<ChatResponseUpdate?, ChatResponseUpdate?>
{
private readonly ChatResponseUpdateBuilder _messageBuilder = new();

/// <summary>
/// Appends a given <see cref="StreamingChatCompletionUpdate"/> item to build a single return object
/// Appends a given <see cref="ChatResponseUpdate"/> item to build a single return object
/// </summary>
/// <param name="item">The item to append</param>
public void Append(ChatResponseUpdate? item) => _messageBuilder.Append(item);

/// <summary>
/// Builds up one final, single <see cref="StreamingChatCompletionUpdate"/> object from the previously streamed items
/// Builds up one final, single <see cref="ChatResponseUpdate"/> object from the previously streamed items
/// </summary>
/// <returns>The completed, consolidated <see cref="StreamingChatCompletionUpdate"/> object</returns>
/// <returns>The completed, consolidated <see cref="ChatResponseUpdate"/> object</returns>
public ChatResponseUpdate? Complete() => _messageBuilder.Complete();
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public void Append(ChatResponseUpdate? update)
}

/// <summary>
/// Builds the final consolidated <see cref="StreamingChatCompletionUpdate"/> out of the streamed
/// Builds the final consolidated <see cref="ChatResponseUpdate"/> out of the streamed
/// updates that were appended before
/// </summary>
/// <returns>The final consolidated <see cref="StreamingChatCompletionUpdate"/> object</returns>
/// <returns>The final consolidated <see cref="ChatResponseUpdate"/> object</returns>
public ChatResponseUpdate? Complete()
{
if (_first is null)
Expand Down
6 changes: 3 additions & 3 deletions src/OllamaSharp/MicrosoftAi/IAsyncEnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace OllamaSharp;
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="MicrosoftAi.ChatResponseUpdateAppender"/> 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>
/// <returns>A single <see cref="MicrosoftAi.ChatResponseUpdateAppender"/> built up from every single IAsyncEnumerable item.</returns>
public static Task<ChatResponseUpdate?> StreamToEndAsync(this IAsyncEnumerable<ChatResponseUpdate?> stream, Action<ChatResponseUpdate?>? itemCallback = null)
=> stream.StreamToEndAsync(new MicrosoftAi.StreamingChatCompletionUpdateAppender(), itemCallback);
=> stream.StreamToEndAsync(new MicrosoftAi.ChatResponseUpdateAppender(), itemCallback);
}
5 changes: 2 additions & 3 deletions src/OllamaSharp/Models/Chat/MessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public Message ToMessage() =>
/// Console.WriteLine(string.Join(", ", resultMessage.Images)); // Output: example_image.png
/// </code>
/// </example>
public IReadOnlyCollection<string> Images { get; }
public IReadOnlyCollection<string>? Images { get; }

/// <summary>
/// Represents the collection of tool calls associated with a chat message.
Expand Down Expand Up @@ -176,8 +176,7 @@ public Message ToMessage() =>
/// Console.WriteLine(messageBuilder.ToolCalls.Count); // Output: 1
/// </code>
/// </example>
public IReadOnlyCollection<Message.ToolCall> ToolCalls { get; }

public IReadOnlyCollection<Message.ToolCall>? ToolCalls { get; }

/// <summary>
/// Indicates whether the current <see cref="MessageBuilder"/> instance contains any content, images, or tool calls.
Expand Down
6 changes: 3 additions & 3 deletions src/OllamaSharp/OllamaApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ private async Task EnsureSuccessStatusCodeAsync(HttpResponseMessage response)

#region IChatClient and IEmbeddingGenerator implementation

private ChatClientMetadata _chatClientMetadata;
private EmbeddingGeneratorMetadata _embeddingGeneratorMetadata;
private ChatClientMetadata? _chatClientMetadata;
private EmbeddingGeneratorMetadata? _embeddingGeneratorMetadata;

/// <inheritdoc/>
async Task<ChatResponse> IChatClient.GetResponseAsync(IList<ChatMessage> chatMessages, ChatOptions? options, CancellationToken cancellationToken)
Expand All @@ -409,7 +409,7 @@ async IAsyncEnumerable<ChatResponseUpdate> IChatClient.GetStreamingResponseAsync
{
var request = AbstractionMapper.ToOllamaSharpChatRequest(chatMessages, options, stream: true, OutgoingJsonSerializerOptions);
await foreach (var response in ChatAsync(request, cancellationToken).ConfigureAwait(false))
yield return AbstractionMapper.ToStreamingChatCompletionUpdate(response);
yield return AbstractionMapper.ToChatResponseUpdate(response);
}

/// <inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion src/OllamaSharp/OllamaSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\OllamaSharp.snk</AssemblyOriginatorKeyFile>
<NoWarn>IDE0065;IDE0055;IDE0011;S3881</NoWarn>
<NoWarn>$(NoWarn);IDE0065;IDE0055;IDE0011;S3881</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/SourceGenerators/OllamaToolGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ private static string GenerateToolClassCode(
using System.Text.Json.Serialization;
using System.Threading.Tasks;

#nullable enable

namespace {containingNamespace}
{{
/// <summary>
Expand Down
6 changes: 2 additions & 4 deletions test/AbstractionMapperTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Text.Json;
using System.Text.Json.Schema;
using FluentAssertions;
using Microsoft.Extensions.AI;
using NUnit.Framework;
Expand Down Expand Up @@ -721,7 +719,7 @@ public void Maps_Known_Properties()
}
}

public class ToStreamingChatCompletionUpdateMethod : AbstractionMapperTests
public class ToChatResponseUpdateMethod : AbstractionMapperTests
{
[Test]
public void Maps_Known_Properties()
Expand All @@ -738,7 +736,7 @@ public void Maps_Known_Properties()
Model = "llama3.1:8b"
};

var streamingChatCompletion = AbstractionMapper.ToStreamingChatCompletionUpdate(stream);
var streamingChatCompletion = AbstractionMapper.ToChatResponseUpdate(stream);

streamingChatCompletion.AdditionalProperties.Should().BeNull();
streamingChatCompletion.AuthorName.Should().BeNull();
Expand Down
2 changes: 1 addition & 1 deletion test/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<NoWarn>CS8604;CS8602</NoWarn>
<NoWarn>$(NoWarn);CS8604;CS8602</NoWarn>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<SignAssembly>True</SignAssembly>
Expand Down
Loading