From 65f6ed7b3f4be56278b3346e9d0c1e2998c68db3 Mon Sep 17 00:00:00 2001 From: Milkey Tan <24996957+mili-tan@users.noreply.github.com> Date: Tue, 4 Jun 2024 19:14:10 +0800 Subject: [PATCH 1/2] Add Quantize --- src/Models/CreateModel.cs | 72 ++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/src/Models/CreateModel.cs b/src/Models/CreateModel.cs index 4b1c1e0..5ddc891 100644 --- a/src/Models/CreateModel.cs +++ b/src/Models/CreateModel.cs @@ -2,40 +2,48 @@ namespace OllamaSharp.Models { - /// - /// https://github.com/jmorganca/ollama/blob/main/docs/api.md#create-a-model - /// - public class CreateModelRequest - { - /// - /// Name of the model to create - /// - [JsonPropertyName("name")] - public string Name { get; set; } + /// + /// https://github.com/jmorganca/ollama/blob/main/docs/api.md#create-a-model + /// - /// - /// Contents of the Modelfile - /// See https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md - /// - [JsonPropertyName("modelfile")] - public string ModelFileContent { get; set; } + [JsonUnmappedMemberHandling(JsonUnmappedMemberHandling.Skip)] + public class CreateModelRequest + { + /// + /// Name of the model to create + /// + [JsonPropertyName("name")] + public string Name { get; set; } - /// - /// Path to the Modelfile (optional) - /// - [JsonPropertyName("path")] - public string Path { get; set; } + /// + /// Contents of the Modelfile + /// See https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md + /// + [JsonPropertyName("modelfile")] + public string ModelFileContent { get; set; } - /// - /// If false the response will be returned as a single response object, rather than a stream of objects (optional) - /// - [JsonPropertyName("stream")] - public bool Stream { get; set; } - } + /// + /// Path to the Modelfile (optional) + /// + [JsonPropertyName("path")] + public string Path { get; set; } - public class CreateStatus - { - [JsonPropertyName("status")] - public string Status { get; set; } - } + /// + /// If false the response will be returned as a single response object, rather than a stream of objects (optional) + /// + [JsonPropertyName("stream")] + public bool Stream { get; set; } + + /// + /// Set the quantization level for quantize model when importing (e.g. q4_0, optional) + /// + [JsonPropertyName("quantize")] + public string? Quantize { get; set; } + } + + public class CreateStatus + { + [JsonPropertyName("status")] + public string Status { get; set; } + } } \ No newline at end of file From 2ca79d3bdabf39157c3a71c61dd4949062b92546 Mon Sep 17 00:00:00 2001 From: Milkey Tan <24996957+mili-tan@users.noreply.github.com> Date: Tue, 4 Jun 2024 20:14:39 +0800 Subject: [PATCH 2/2] Add Model, mark Name as obsolete --- src/Models/CreateModel.cs | 14 +++++++++++--- src/Models/DeleteModel.cs | 23 +++++++++++++++-------- src/Models/PullModel.cs | 24 ++++++++++++++---------- src/Models/PushModel.cs | 22 +++++++++++++++------- src/Models/ShowModel.cs | 32 +++++++++++++++++++++----------- src/OllamaApiClient.cs | 4 ++-- src/OllamaApiClientExtensions.cs | 10 +++++----- 7 files changed, 83 insertions(+), 46 deletions(-) diff --git a/src/Models/CreateModel.cs b/src/Models/CreateModel.cs index 5ddc891..9cf3289 100644 --- a/src/Models/CreateModel.cs +++ b/src/Models/CreateModel.cs @@ -1,19 +1,27 @@ -using System.Text.Json.Serialization; +using System; +using System.Text.Json.Serialization; namespace OllamaSharp.Models { /// /// https://github.com/jmorganca/ollama/blob/main/docs/api.md#create-a-model /// - + [JsonUnmappedMemberHandling(JsonUnmappedMemberHandling.Skip)] public class CreateModelRequest { /// /// Name of the model to create /// + [JsonPropertyName("model")] + public string? Model { get; set; } + + /// + /// Name of the model to create(Obsolete) + /// + [Obsolete("Name is deprecated, see Model")] [JsonPropertyName("name")] - public string Name { get; set; } + public string? Name { get; set; } /// /// Contents of the Modelfile diff --git a/src/Models/DeleteModel.cs b/src/Models/DeleteModel.cs index 9b35dd5..079c291 100644 --- a/src/Models/DeleteModel.cs +++ b/src/Models/DeleteModel.cs @@ -1,13 +1,20 @@ -using System.Text.Json.Serialization; +using System; +using System.Text.Json.Serialization; namespace OllamaSharp.Models { - /// - /// https://github.com/jmorganca/ollama/blob/main/docs/api.md#delete-a-model - /// - public class DeleteModelRequest + /// + /// https://github.com/jmorganca/ollama/blob/main/docs/api.md#delete-a-model + /// + + [JsonUnmappedMemberHandling(JsonUnmappedMemberHandling.Skip)] + public class DeleteModelRequest { - [JsonPropertyName("name")] - public string Name { get; set; } - } + [Obsolete("Name is deprecated, see Model")] + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("model")] + public string? Model { get; set; } + } } \ No newline at end of file diff --git a/src/Models/PullModel.cs b/src/Models/PullModel.cs index 9922c37..edaf188 100644 --- a/src/Models/PullModel.cs +++ b/src/Models/PullModel.cs @@ -1,18 +1,22 @@ -using System.Text.Json.Serialization; +using System; +using System.Text.Json.Serialization; namespace OllamaSharp.Models { - /// - /// https://github.com/jmorganca/ollama/blob/main/docs/api.md#pull-a-model - /// - public class PullModelRequest + /// + /// https://github.com/jmorganca/ollama/blob/main/docs/api.md#pull-a-model + /// + + [Obsolete("Name is deprecated, see Model")] + public class PullModelRequest { - [JsonPropertyName("name")] - public string Name { get; set; } + [Obsolete("Name is deprecated, see Model")] + [JsonPropertyName("name")] + public string? Name { get; set; } - [JsonPropertyName("insecure")] - public bool Insecure { get; set; } - } + [JsonPropertyName("model")] + public string? Model { get; set; } + } public class PullStatus { diff --git a/src/Models/PushModel.cs b/src/Models/PushModel.cs index e951d66..032213d 100644 --- a/src/Models/PushModel.cs +++ b/src/Models/PushModel.cs @@ -1,4 +1,5 @@ -using System.Text.Json.Serialization; +using System; +using System.Text.Json.Serialization; namespace OllamaSharp.Models { @@ -7,13 +8,20 @@ namespace OllamaSharp.Models /// public class PushRequest { - /// - /// Name of the model to push in the form of /: - /// - [JsonPropertyName("name")] - public string Name { get; set; } + /// + /// Name of the model to push in the form of /: (Obsolete) + /// + [Obsolete("Name is deprecated, see Model")] + [JsonPropertyName("name")] + public string? Name { get; set; } - [JsonPropertyName("insecure")] + /// + /// Name of the model to push in the form of /: + /// + [JsonPropertyName("model")] + public string? Model { get; set; } + + [JsonPropertyName("insecure")] public bool Insecure { get; set; } [JsonPropertyName("stream")] diff --git a/src/Models/ShowModel.cs b/src/Models/ShowModel.cs index 04604ff..6ce5b44 100644 --- a/src/Models/ShowModel.cs +++ b/src/Models/ShowModel.cs @@ -1,19 +1,29 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Text.Json.Serialization; namespace OllamaSharp.Models { - /// - /// https://github.com/jmorganca/ollama/blob/main/docs/api.md#show-model-information - /// - public class ShowModelRequest + /// + /// https://github.com/jmorganca/ollama/blob/main/docs/api.md#show-model-information + /// + + [JsonUnmappedMemberHandling(JsonUnmappedMemberHandling.Skip)] + public class ShowModelRequest { - /// - /// The name of the model to show - /// - [JsonPropertyName("name")] - public string Name { get; set; } - } + /// + /// The name of the model to show + /// + [JsonPropertyName("model")] + public string? Model { get; set; } + + /// + /// The name of the model to show(Obsolete) + /// + [Obsolete("Name is deprecated, see Model")] + [JsonPropertyName("name")] + public string? Name { get; set; } + } public class ShowModelResponse { diff --git a/src/OllamaApiClient.cs b/src/OllamaApiClient.cs index a87d151..944d449 100644 --- a/src/OllamaApiClient.cs +++ b/src/OllamaApiClient.cs @@ -59,7 +59,7 @@ public async Task DeleteModel(string model, CancellationToken cancellationToken { var request = new HttpRequestMessage(HttpMethod.Delete, "api/delete") { - Content = new StringContent(JsonSerializer.Serialize(new DeleteModelRequest { Name = model }), Encoding.UTF8, "application/json") + Content = new StringContent(JsonSerializer.Serialize(new DeleteModelRequest { Model = model }), Encoding.UTF8, "application/json") }; using var response = await _client.SendAsync(request, cancellationToken); @@ -81,7 +81,7 @@ public async Task> ListRunningModels(CancellationToken public async Task ShowModelInformation(string model, CancellationToken cancellationToken = default) { - return await PostAsync("api/show", new ShowModelRequest { Name = model }, cancellationToken); + return await PostAsync("api/show", new ShowModelRequest { Model = model }, cancellationToken); } public async Task CopyModel(CopyModelRequest request, CancellationToken cancellationToken = default) diff --git a/src/OllamaApiClientExtensions.cs b/src/OllamaApiClientExtensions.cs index 9dce3e4..d8a7ecd 100644 --- a/src/OllamaApiClientExtensions.cs +++ b/src/OllamaApiClientExtensions.cs @@ -103,7 +103,7 @@ public static async Task CreateModel(this IOllamaApiClient client, string name, /// The token to cancel the operation with public static async Task CreateModel(this IOllamaApiClient client, string name, string modelFileContent, IResponseStreamer streamer, CancellationToken cancellationToken = default) { - await client.CreateModel(new CreateModelRequest { Name = name, ModelFileContent = modelFileContent, Stream = true }, streamer, cancellationToken); + await client.CreateModel(new CreateModelRequest { Model = name, ModelFileContent = modelFileContent, Stream = true }, streamer, cancellationToken); } /// @@ -124,7 +124,7 @@ public static async Task CreateModel(this IOllamaApiClient client, string name, { await client.CreateModel(new CreateModelRequest { - Name = name, + Model = name, ModelFileContent = modelFileContent, Path = path, Stream = true @@ -147,7 +147,7 @@ await client.CreateModel(new CreateModelRequest /// The token to cancel the operation with public static async Task CreateModel(this IOllamaApiClient client, string name, string modelFileContent, string path, IResponseStreamer streamer, CancellationToken cancellationToken = default) { - await client.CreateModel(new CreateModelRequest { Name = name, ModelFileContent = modelFileContent, Path = path, Stream = true }, streamer, cancellationToken); + await client.CreateModel(new CreateModelRequest { Model = name, ModelFileContent = modelFileContent, Path = path, Stream = true }, streamer, cancellationToken); } /// @@ -175,7 +175,7 @@ public static async Task PullModel(this IOllamaApiClient client, string model, A /// The token to cancel the operation with public static async Task PullModel(this IOllamaApiClient client, string model, IResponseStreamer streamer, CancellationToken cancellationToken = default) { - await client.PullModel(new PullModelRequest { Name = model }, streamer, cancellationToken); + await client.PullModel(new PullModelRequest { Model = model }, streamer, cancellationToken); } /// @@ -203,7 +203,7 @@ public static async Task PushModel(this IOllamaApiClient client, string name, Ac /// The token to cancel the operation with public static async Task PushModel(this IOllamaApiClient client, string name, IResponseStreamer streamer, CancellationToken cancellationToken = default) { - await client.PushModel(new PushRequest { Name = name, Stream = true }, streamer, cancellationToken); + await client.PushModel(new PushRequest { Model = name, Stream = true }, streamer, cancellationToken); } ///