Skip to content

Commit

Permalink
Merge pull request #159 from Joy-less/fix-155
Browse files Browse the repository at this point in the history
Add support for Json Schema in Chat
  • Loading branch information
awaescher authored Jan 3, 2025
2 parents 2771cb0 + b70ecdc commit 6b477e2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ public IAsyncEnumerable<string> SendAsync(string message, IEnumerable<string>? i
/// <param name="message">The message to send</param>
/// <param name="tools">Tools that the model can make use of, see https://ollama.com/blog/tool-support. By using tools, response streaming is automatically turned off</param>
/// <param name="imagesAsBase64">Base64 encoded images to send to the model</param>
/// <param name="format">Currently accepts "json" or JsonSchema or null.</param>
/// <param name="cancellationToken">The token to cancel the operation with</param>
public IAsyncEnumerable<string> SendAsync(string message, IReadOnlyCollection<Tool>? tools, IEnumerable<string>? imagesAsBase64 = default, CancellationToken cancellationToken = default)
=> SendAsAsync(ChatRole.User, message, tools, imagesAsBase64, cancellationToken);
public IAsyncEnumerable<string> SendAsync(string message, IEnumerable<Tool>? tools, IEnumerable<string>? imagesAsBase64 = null, object? format = null, CancellationToken cancellationToken = default)
=> SendAsAsync(ChatRole.User, message, tools, imagesAsBase64, format, cancellationToken);

/// <summary>
/// Sends a message in a given role to the currently selected model and streams its response
Expand Down Expand Up @@ -203,8 +204,9 @@ public IAsyncEnumerable<string> SendAsAsync(ChatRole role, string message, IEnum
/// <param name="message">The message to send</param>
/// <param name="tools">Tools that the model can make use of, see https://ollama.com/blog/tool-support. By using tools, response streaming is automatically turned off</param>
/// <param name="imagesAsBase64">Base64 encoded images to send to the model</param>
/// <param name="format">Currently accepts "json" or JsonSchema or null.</param>
/// <param name="cancellationToken">The token to cancel the operation with</param>
public async IAsyncEnumerable<string> SendAsAsync(ChatRole role, string message, IReadOnlyCollection<Tool>? tools, IEnumerable<string>? imagesAsBase64 = default, [EnumeratorCancellation] CancellationToken cancellationToken = default)
public async IAsyncEnumerable<string> SendAsAsync(ChatRole role, string message, IEnumerable<Tool>? tools, IEnumerable<string>? imagesAsBase64 = null, object? format = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
Messages.Add(new Message(role, message, imagesAsBase64?.ToArray()));

Expand All @@ -216,6 +218,7 @@ public async IAsyncEnumerable<string> SendAsAsync(ChatRole role, string message,
Model = Model,
Stream = !hasTools, // cannot stream if tools should be used
Tools = tools,
Format = format,
Options = Options
};

Expand Down
2 changes: 1 addition & 1 deletion src/Models/Chat/ChatRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class ChatRequest : OllamaRequest
public string? KeepAlive { get; set; }

/// <summary>
/// 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.
/// </summary>
[JsonPropertyName("format")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Generate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public class GenerateRequest : OllamaRequest
public string? KeepAlive { get; set; }

/// <summary>
/// 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.
/// </summary>
[JsonPropertyName("format")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
Expand Down

0 comments on commit 6b477e2

Please sign in to comment.