From 59633f09627d06c62dd53c89ee095a9dc53cc039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20W=C3=A4scher?= Date: Fri, 24 Jul 2026 11:24:31 +0200 Subject: [PATCH] Fix model selection when unloading a model (#386) --- src/OllamaSharp/OllamaApiClientExtensions.cs | 4 +-- test/OllamaApiClientTests.cs | 31 +++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/OllamaSharp/OllamaApiClientExtensions.cs b/src/OllamaSharp/OllamaApiClientExtensions.cs index 4ad6170..5a97f3d 100644 --- a/src/OllamaSharp/OllamaApiClientExtensions.cs +++ b/src/OllamaSharp/OllamaApiClientExtensions.cs @@ -102,7 +102,7 @@ public static async Task RequestModelUnloadAsync(this IOllamaApiClient client, s { var request = new GenerateRequest { - Model = client.SelectedModel, + Model = model, Stream = false, KeepAlive = "0s" }; @@ -131,4 +131,4 @@ public static Task ShowModelAsync(this IOllamaApiClient clien /// A task that represents the asynchronous push operation. public static Task PushBlobAsync(this IOllamaApiClient client, byte[] bytes, CancellationToken cancellationToken = default) => client.PushBlobAsync($"sha256:{BitConverter.ToString(SHA256.Create().ComputeHash(bytes)).Replace("-", string.Empty).ToLower()}", bytes, cancellationToken); -} \ No newline at end of file +} diff --git a/test/OllamaApiClientTests.cs b/test/OllamaApiClientTests.cs index 608fab5..0967e4c 100644 --- a/test/OllamaApiClientTests.cs +++ b/test/OllamaApiClientTests.cs @@ -297,6 +297,35 @@ public async Task Returns_Streamed_Responses_At_Once() } } + /// + /// Contains tests for the RequestModelUnload extension method. + /// + public class RequestModelUnloadMethod : OllamaApiClientTests + { + /// + /// Verifies that the explicitly requested model is unloaded instead of the selected model. + /// + [Test, NonParallelizable] + public async Task Uses_Requested_Model() + { + _client.SelectedModel = "selected-model"; + _response = new HttpResponseMessage + { + StatusCode = HttpStatusCode.OK, + Content = new StringContent("""{"model":"requested-model","response":"","done":true}""") + }; + + await _client.RequestModelUnloadAsync("requested-model", CancellationToken.None); + + _requestContent.ShouldNotBeNull(); + using var requestJson = JsonDocument.Parse(_requestContent); + var root = requestJson.RootElement; + root.GetProperty("model").GetString().ShouldBe("requested-model"); + root.GetProperty("stream").GetBoolean().ShouldBeFalse(); + root.GetProperty("keep_alive").GetString().ShouldBe("0s"); + } + } + /// /// Contains tests for the Complete method. /// @@ -838,4 +867,4 @@ public static async Task FinishChatStreamResponse(this StreamWriter writer, stri #pragma warning restore CS0618 #pragma warning restore CS8602 // Dereference of a possibly null reference. -#pragma warning restore CS8604 // Possible null reference argument. \ No newline at end of file +#pragma warning restore CS8604 // Possible null reference argument.