From b70ecdc19c3a81275e2bed4f57324f5d027b5517 Mon Sep 17 00:00:00 2001 From: Joyless <65855333+Joy-less@users.noreply.github.com> Date: Tue, 31 Dec 2024 04:05:56 +0000 Subject: [PATCH] Add support for Json Schema in Chat --- src/Chat.cs | 9 ++++++--- src/Models/Chat/ChatRequest.cs | 2 +- src/Models/Generate.cs | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Chat.cs b/src/Chat.cs index 65c18e5..73336d2 100644 --- a/src/Chat.cs +++ b/src/Chat.cs @@ -163,9 +163,10 @@ public IAsyncEnumerable SendAsync(string message, IEnumerable? i /// The message to send /// Tools that the model can make use of, see https://ollama.com/blog/tool-support. By using tools, response streaming is automatically turned off /// Base64 encoded images to send to the model + /// Currently accepts "json" or JsonSchema or null. /// The token to cancel the operation with - public IAsyncEnumerable SendAsync(string message, IReadOnlyCollection? tools, IEnumerable? imagesAsBase64 = default, CancellationToken cancellationToken = default) - => SendAsAsync(ChatRole.User, message, tools, imagesAsBase64, cancellationToken); + public IAsyncEnumerable SendAsync(string message, IEnumerable? tools, IEnumerable? imagesAsBase64 = null, object? format = null, CancellationToken cancellationToken = default) + => SendAsAsync(ChatRole.User, message, tools, imagesAsBase64, format, cancellationToken); /// /// Sends a message in a given role to the currently selected model and streams its response @@ -203,8 +204,9 @@ public IAsyncEnumerable SendAsAsync(ChatRole role, string message, IEnum /// The message to send /// Tools that the model can make use of, see https://ollama.com/blog/tool-support. By using tools, response streaming is automatically turned off /// Base64 encoded images to send to the model + /// Currently accepts "json" or JsonSchema or null. /// The token to cancel the operation with - public async IAsyncEnumerable SendAsAsync(ChatRole role, string message, IReadOnlyCollection? tools, IEnumerable? imagesAsBase64 = default, [EnumeratorCancellation] CancellationToken cancellationToken = default) + public async IAsyncEnumerable SendAsAsync(ChatRole role, string message, IEnumerable? tools, IEnumerable? imagesAsBase64 = null, object? format = null, [EnumeratorCancellation] CancellationToken cancellationToken = default) { Messages.Add(new Message(role, message, imagesAsBase64?.ToArray())); @@ -216,6 +218,7 @@ public async IAsyncEnumerable SendAsAsync(ChatRole role, string message, Model = Model, Stream = !hasTools, // cannot stream if tools should be used Tools = tools, + Format = format, Options = Options }; diff --git a/src/Models/Chat/ChatRequest.cs b/src/Models/Chat/ChatRequest.cs index eb0fd41..c653cd3 100644 --- a/src/Models/Chat/ChatRequest.cs +++ b/src/Models/Chat/ChatRequest.cs @@ -43,7 +43,7 @@ public class ChatRequest : OllamaRequest public string? KeepAlive { get; set; } /// - /// Gets or sets the format to return a response in. Currently accepts "json" and JsonSchema or null. + /// Gets or sets the format to return a response in. Currently accepts "json" or JsonSchema or null. /// [JsonPropertyName("format")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] diff --git a/src/Models/Generate.cs b/src/Models/Generate.cs index f42b8e2..368ee94 100644 --- a/src/Models/Generate.cs +++ b/src/Models/Generate.cs @@ -76,7 +76,7 @@ public class GenerateRequest : OllamaRequest public string? KeepAlive { get; set; } /// - /// Gets or sets the format to return a response in. Currently accepts "json" and JsonSchema or null. + /// Gets or sets the format to return a response in. Currently accepts "json" or JsonSchema or null. /// [JsonPropertyName("format")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]