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

Add ListRunningModels for "/api/ps" endpoint #36

Merged
merged 2 commits into from
Jun 2, 2024
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
26 changes: 16 additions & 10 deletions src/IOllamaApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,22 @@ public interface IOllamaApiClient
/// <param name="cancellationToken">The token to cancel the operation with</param>
Task<IEnumerable<Model>> ListLocalModels(CancellationToken cancellationToken = default);

/// <summary>
/// Sends a request to the /api/pull endpoint to pull a new model
/// </summary>
/// <param name="request">The request parameters</param>
/// <param name="streamer">
/// The streamer that receives status updates as they are streamed by the Ollama endpoint.
/// Can be used to update the user interface while the operation is running.
/// </param>
/// <param name="cancellationToken">The token to cancel the operation with</param>
Task PullModel(PullModelRequest request, IResponseStreamer<PullStatus> streamer, CancellationToken cancellationToken = default);
/// <summary>
/// Sends a request to the /api/ps endpoint to get the running models
/// </summary>
/// <param name="cancellationToken">The token to cancel the operation with</param>
Task<IEnumerable<RunningModel>> ListRunningModels(CancellationToken cancellationToken = default);

/// <summary>
/// Sends a request to the /api/pull endpoint to pull a new model
/// </summary>
/// <param name="request">The request parameters</param>
/// <param name="streamer">
/// The streamer that receives status updates as they are streamed by the Ollama endpoint.
/// Can be used to update the user interface while the operation is running.
/// </param>
/// <param name="cancellationToken">The token to cancel the operation with</param>
Task PullModel(PullModelRequest request, IResponseStreamer<PullStatus> streamer, CancellationToken cancellationToken = default);

/// <summary>
/// Sends a request to the /api/push endpoint to push a new model
Expand Down
33 changes: 33 additions & 0 deletions src/Models/ListRunningModels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#nullable enable
using System;
using System.Diagnostics;
using System.Text.Json.Serialization;

namespace OllamaSharp.Models
{

[JsonUnmappedMemberHandling(JsonUnmappedMemberHandling.Skip)]
public class ListRunningModelsResponse
{
[JsonPropertyName("models")] public RunningModel[] RunningModels { get; set; }
}

[JsonUnmappedMemberHandling(JsonUnmappedMemberHandling.Skip)]
[DebuggerDisplay("{Name}")]
public class RunningModel
{
[JsonPropertyName("name")] public string? Name { get; set; }

[JsonPropertyName("modified_at")] public DateTime? ModifiedAt { get; set; }

[JsonPropertyName("size")] public long? Size { get; set; }

[JsonPropertyName("size_vram")] public long? SizeVRAM { get; set; }

[JsonPropertyName("digest")] public string? Digest { get; set; }

[JsonPropertyName("details")] public Details? Details { get; set; }

[JsonPropertyName("expires_at")] public DateTime? ExpiresAt { get; set; }
}
}
9 changes: 8 additions & 1 deletion src/OllamaApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,14 @@ public async Task<IEnumerable<Model>> ListLocalModels(CancellationToken cancella
return data.Models;
}

public async Task<ShowModelResponse> ShowModelInformation(string model, CancellationToken cancellationToken = default)
public async Task<IEnumerable<RunningModel>> ListRunningModels(CancellationToken cancellationToken = default)
{
var data = await GetAsync<ListRunningModelsResponse>("api/ps", cancellationToken);
return data.RunningModels;
}


public async Task<ShowModelResponse> ShowModelInformation(string model, CancellationToken cancellationToken = default)
{
return await PostAsync<ShowModelRequest, ShowModelResponse>("api/show", new ShowModelRequest { Name = model }, cancellationToken);
}
Expand Down
6 changes: 5 additions & 1 deletion test/TestOllamaApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ public Task<IEnumerable<Model>> ListLocalModels(CancellationToken cancellationTo
{
throw new NotImplementedException();
}
public Task<IEnumerable<RunningModel>> ListRunningModels(CancellationToken cancellationToken)
{
throw new NotImplementedException();
}

public Task PullModel(PullModelRequest request, IResponseStreamer<PullStatus> streamer, CancellationToken cancellationToken)
public Task PullModel(PullModelRequest request, IResponseStreamer<PullStatus> streamer, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
Expand Down
Loading