Skip to content

Commit

Permalink
Merge pull request #168 from JerrettDavis/more-code-cleanup
Browse files Browse the repository at this point in the history
Moved the rest of the model JsonPropertyNames to constants for consistency
  • Loading branch information
awaescher authored Jan 10, 2025
2 parents 9a43aaa + 43746c7 commit 83578f9
Show file tree
Hide file tree
Showing 16 changed files with 226 additions and 150 deletions.
2 changes: 1 addition & 1 deletion src/Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public IAsyncEnumerable<string> SendAsync(string message, IEnumerable<Tool>? too
/// An <see cref="IAsyncEnumerable{T}"/> of strings representing the streamed response from the server.
/// </returns>
/// <example>
/// Example usage of the <see cref="SendAsAsync"/> method:
/// Example usage of the <see cref="SendAsAsync(string, string)"/> method:
/// <code>
/// var client = new OllamaApiClient("http://localhost:11434", "llama3.2-vision:latest");
/// var chat = new Chat(client);
Expand Down
77 changes: 72 additions & 5 deletions src/Constants/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace OllamaSharp.Constants;
internal static class Application
{
public const string Ollama = "ollama";

public const string KeepAlive = "keep_alive";
public const string Truncate = "truncate";
public const string LoadDuration = "load_duration";
Expand All @@ -16,22 +17,88 @@ internal static class Application
public const string EvalCount = "eval_count";
public const string Context = "context";
public const string Done = "done";
public const string DoneReason = "done_reason";
public const string Response = "response";
public const string CreatedAt = "created_at";
public const string Model = "model";

public const string Models = "models";
public const string Name = "name";
public const string Size = "size";
public const string Format = "format";
public const string Images = "images";
public const string Template = "template";
public const string ModifiedAt = "modified_at";
public const string ModelFile = "modelfile";
public const string Path = "path";
public const string Stream = "stream";
public const string Quantize = "quantize";
public const string Status = "status";
public const string Input = "input";
public const string Prompt = "prompt";
public const string Suffix = "suffix";
public const string Options = "options";
public const string Source = "source";
public const string Destination = "destination";
public const string Assistant = "assistant";
public const string System = "system";
public const string User = "user";
public const string Tool = "tool";

public const string Length = "length";
public const string Stop = "stop";

public const string Object = "object";
public const string Function = "function";

public const string Json = "json";

public const string NotApplicable = "n/a";
public const string Raw = "raw";
public const string Digest = "digest";
public const string Details = "details";
public const string ParentModel = "parent_model";
public const string Family = "family";
public const string Families = "families";
public const string QuantizationLevel = "quantization_level";
public const string SizeVram = "size_vram";
public const string ExpiresAt = "expires_at";
public const string MiroStat = "mirostat";
public const string MiroStatEta = "mirostat_eta";
public const string MiroStatTau = "mirostat_tau";
public const string NumCtx = "num_ctx";
public const string NumGqa = "num_gqa";
public const string NumGpu = "num_gpu";
public const string MainGpu = "main_gpu";
public const string NumBatch = "num_batch";
public const string NumThread = "num_thread";
public const string NumKeep = "num_keep";
public const string RepeatLastN = "repeat_last_n";
public const string RepeatPenalty = "repeat_penalty";
public const string PresencePenalty = "presence_penalty";
public const string FrequencyPenalty = "frequency_penalty";
public const string Temperature = "temperature";
public const string Seed = "seed";
public const string TfsZ = "tfs_z";
public const string NumPredict = "num_predict";
public const string TopK = "top_k";
public const string TopP = "top_p";
public const string MinP = "min_p";
public const string TypicalP = "typical_p";
public const string PenalizeNewline = "penalize_newline";
public const string UseMmap = "use_mmap";
public const string UseMlock = "use_mlock";
public const string LowVram = "low_vram";
public const string F16kv = "f16_kv";
public const string LogitsAll = "logits_all";
public const string VocabOnly = "vocab_only";
public const string Numa = "numa";
public const string License = "license";
public const string Parameters = "parameters";
public const string ModelInfo = "model_info";
public const string Projector = "projector_info";
public const string GeneralArchitecture = "general.architecture";
public const string GeneralFileType = "general.file_type";
public const string GeneralParameterCount = "general.parameter_count";
public const string GeneralQuantizationVersion = "general.quantization_version";
public const string Insecure = "insecure";
public const string Total = "total";
public const string Completed = "completed";
public const string Embeddings = "embeddings";
public const string ParameterSize = "parameter_size";
}
5 changes: 2 additions & 3 deletions src/MicrosoftAi/AbstractionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static ChatRequest ToOllamaSharpChatRequest(IList<ChatMessage> chatMessag
Format = Equals(options?.ResponseFormat, ChatResponseFormat.Json) ? Application.Json : null,
KeepAlive = null,
Messages = ToOllamaSharpMessages(chatMessages, serializerOptions),
Model = options?.ModelId ?? "", // will be set OllamaApiClient.SelectedModel if not set
Model = options?.ModelId ?? string.Empty, // will be set OllamaApiClient.SelectedModel if not set
Options = new RequestOptions
{
FrequencyPenalty = options?.FrequencyPenalty,
Expand Down Expand Up @@ -95,8 +95,7 @@ public static ChatRequest ToOllamaSharpChatRequest(IList<ChatMessage> chatMessag
TryAddOllamaOption<int?>(options, OllamaOption.RepeatLastN, v => request.Options.RepeatLastN = (int?)v);
TryAddOllamaOption<float?>(options, OllamaOption.RepeatPenalty, v => request.Options.RepeatPenalty = (float?)v);
TryAddOllamaOption<int?>(options, OllamaOption.Seed, v => request.Options.Seed = (int?)v);
TryAddOllamaOption<string[]?>(options, OllamaOption.Stop,
v => request.Options.Stop = (v as IEnumerable<string>)?.ToArray());
TryAddOllamaOption<string[]?>(options, OllamaOption.Stop, v => request.Options.Stop = (v as IEnumerable<string>)?.ToArray());
TryAddOllamaOption<float?>(options, OllamaOption.Temperature, v => request.Options.Temperature = (float?)v);
TryAddOllamaOption<float?>(options, OllamaOption.TfsZ, v => request.Options.TfsZ = (float?)v);
TryAddOllamaOption<int?>(options, OllamaOption.TopK, v => request.Options.TopK = (int?)v);
Expand Down
6 changes: 3 additions & 3 deletions src/Models/Chat/ChatDoneResponseStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ChatDoneResponseStream : ChatResponseStream
/// <summary>
/// The number of tokens in the prompt
/// </summary>
[JsonPropertyName("prompt_eval_count")]
[JsonPropertyName(Application.PromptEvalCount)]
public int PromptEvalCount { get; set; }

/// <summary>
Expand All @@ -35,7 +35,7 @@ public class ChatDoneResponseStream : ChatResponseStream
/// <summary>
/// The number of tokens in the response
/// </summary>
[JsonPropertyName("eval_count")]
[JsonPropertyName(Application.EvalCount)]
public int EvalCount { get; set; }

/// <summary>
Expand All @@ -47,6 +47,6 @@ public class ChatDoneResponseStream : ChatResponseStream
/// <summary>
/// The reason for the completion of the chat
/// </summary>
[JsonPropertyName("done_reason")]
[JsonPropertyName(Application.DoneReason)]
public string? DoneReason { get; set; }
}
5 changes: 3 additions & 2 deletions src/Models/CopyModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using OllamaSharp.Constants;

namespace OllamaSharp.Models;

Expand All @@ -11,12 +12,12 @@ public class CopyModelRequest : OllamaRequest
/// <summary>
/// The source model name
/// </summary>
[JsonPropertyName("source")]
[JsonPropertyName(Application.Source)]
public string Source { get; set; } = null!;

/// <summary>
/// The destination model name
/// </summary>
[JsonPropertyName("destination")]
[JsonPropertyName(Application.Destination)]
public string Destination { get; set; } = null!;
}
13 changes: 7 additions & 6 deletions src/Models/CreateModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using OllamaSharp.Constants;

namespace OllamaSharp.Models;

Expand All @@ -18,32 +19,32 @@ public class CreateModelRequest : OllamaRequest
/// <summary>
/// Name of the model to create
/// </summary>
[JsonPropertyName("model")]
[JsonPropertyName(Application.Model)]
public string? Model { get; set; }

/// <summary>
/// Contents of the Modelfile
/// See https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md
/// </summary>
[JsonPropertyName("modelfile")]
[JsonPropertyName(Application.ModelFile)]
public string ModelFileContent { get; set; } = null!;

/// <summary>
/// Path to the Modelfile (optional)
/// </summary>
[JsonPropertyName("path")]
[JsonPropertyName(Application.Path)]
public string? Path { get; set; }

/// <summary>
/// If false the response will be returned as a single response object, rather than a stream of objects (optional)
/// </summary>
[JsonPropertyName("stream")]
[JsonPropertyName(Application.Stream)]
public bool Stream { get; set; }

/// <summary>
/// Set the quantization level for quantize model when importing (e.g. q4_0, optional)
/// </summary>
[JsonPropertyName("quantize")]
[JsonPropertyName(Application.Quantize)]
public string? Quantize { get; set; }
}

Expand All @@ -55,6 +56,6 @@ public class CreateModelResponse
/// <summary>
/// Represents the status of a model creation.
/// </summary>
[JsonPropertyName("status")]
[JsonPropertyName(Application.Status)]
public string Status { get; set; } = null!;
}
3 changes: 2 additions & 1 deletion src/Models/DeleteModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using OllamaSharp.Constants;

namespace OllamaSharp.Models;

Expand All @@ -13,6 +14,6 @@ public class DeleteModelRequest : OllamaRequest
/// <summary>
/// The name of the model to delete
/// </summary>
[JsonPropertyName("model")]
[JsonPropertyName(Application.Model)]
public string? Model { get; set; }
}
14 changes: 7 additions & 7 deletions src/Models/Embed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,35 @@ public class EmbedRequest : OllamaRequest
/// <summary>
/// The name of the model to generate embeddings from
/// </summary>
[JsonPropertyName("model")]
[JsonPropertyName(Application.Model)]
public string Model { get; set; } = null!;

/// <summary>
/// The text to generate embeddings for
/// </summary>
[JsonPropertyName("input")]
[JsonPropertyName(Application.Input)]
public List<string> Input { get; set; } = null!;

/// <summary>
/// Additional model parameters listed in the documentation for the Modelfile
/// such as temperature.
/// </summary>
[JsonPropertyName("options")]
[JsonPropertyName(Application.Options)]
public RequestOptions? Options { get; set; }

/// <summary>
/// Gets or sets the KeepAlive property, which decides how long a given
/// model should stay loaded.
/// </summary>
[JsonPropertyName("keep_alive")]
[JsonPropertyName(Application.KeepAlive)]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public long? KeepAlive { get; set; }

/// <summary>
/// Truncates the end of each input to fit within context length.
/// Returns error if false and context length is exceeded. Defaults to true
/// </summary>
[JsonPropertyName("truncate")]
[JsonPropertyName(Application.Truncate)]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public bool? Truncate { get; set; }
}
Expand All @@ -54,7 +54,7 @@ public class EmbedResponse
/// <summary>
/// An array of embeddings for the input text
/// </summary>
[JsonPropertyName("embeddings")]
[JsonPropertyName(Application.Embeddings)]
public List<float[]> Embeddings { get; set; } = null!;

/// <summary>
Expand All @@ -72,6 +72,6 @@ public class EmbedResponse
/// <summary>
/// The number of tokens in the input text
/// </summary>
[JsonPropertyName("prompt_eval_count")]
[JsonPropertyName(Application.PromptEvalCount)]
public int? PromptEvalCount { get; set; }
}
Loading

0 comments on commit 83578f9

Please sign in to comment.