Skip to content

Commit 3924c20

Browse files
authored
Merge pull request #247 from milbk/main
Add RequestModelUnloadAsync
2 parents 083a835 + 7a4f047 commit 3924c20

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/OllamaSharp/OllamaApiClientExtensions.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,26 @@ public static Task<EmbedResponse> EmbedAsync(this IOllamaApiClient client, strin
9090
return client.GenerateAsync(request, cancellationToken);
9191
}
9292

93+
/// <summary>
94+
/// Send a request to /api/generate with keep_alive set to 0 to immediately unload a model from memory.
95+
/// </summary>
96+
/// <param name="client">The client used to execute the command.</param>
97+
/// <param name="model">The name of the model to unload.</param>
98+
/// <param name="cancellationToken">The token to cancel the operation with.</param>
99+
public static async Task RequestModelUnloadAsync(this IOllamaApiClient client, string model, CancellationToken cancellationToken = default)
100+
{
101+
var request = new GenerateRequest
102+
{
103+
Model = client.SelectedModel,
104+
Stream = false,
105+
KeepAlive = "0s"
106+
};
107+
await foreach (var _ in client.GenerateAsync(request, cancellationToken))
108+
{
109+
break;
110+
}
111+
}
112+
93113
/// <summary>
94114
/// Sends a request to the /api/show endpoint to show the information of a model.
95115
/// </summary>

0 commit comments

Comments
 (0)