Skip to content

Commit

Permalink
Merge pull request #36 from milbk/main
Browse files Browse the repository at this point in the history
Add ListRunningModels for "/api/ps" endpoint
  • Loading branch information
awaescher authored Jun 2, 2024
2 parents 23ee0ea + 31a7612 commit 60b5fb8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
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

0 comments on commit 60b5fb8

Please sign in to comment.