From 9e11964dfea90bd00a5accdfb5c5de227b843bb3 Mon Sep 17 00:00:00 2001
From: Luna <34936608+Plootie@users.noreply.github.com>
Date: Fri, 6 Sep 2024 21:04:36 +0100
Subject: [PATCH 1/4] Add serializer options to fix character escapes
Add fix to address issue
---
src/OllamaApiClient.cs | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/src/OllamaApiClient.cs b/src/OllamaApiClient.cs
index deb5449..f31547c 100644
--- a/src/OllamaApiClient.cs
+++ b/src/OllamaApiClient.cs
@@ -4,6 +4,7 @@
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
+using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading;
@@ -21,6 +22,7 @@ namespace OllamaSharp;
public class OllamaApiClient : IOllamaApiClient
{
private readonly HttpClient _client;
+ private readonly JsonSerializerOptions _serializerOptions = new() { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
///
/// Gets the current configuration of the API client
@@ -88,7 +90,7 @@ public async Task DeleteModel(string model, CancellationToken cancellationToken
{
var request = new HttpRequestMessage(HttpMethod.Delete, "api/delete")
{
- Content = new StringContent(JsonSerializer.Serialize(new DeleteModelRequest { Model = model }), Encoding.UTF8, "application/json")
+ Content = new StringContent(JsonSerializer.Serialize(new DeleteModelRequest { Model = model }, _serializerOptions), Encoding.UTF8, "application/json")
};
using var response = await _client.SendAsync(request, cancellationToken);
@@ -151,7 +153,7 @@ public Task Embed(EmbedRequest request, CancellationToken cancell
{
var request = new HttpRequestMessage(HttpMethod.Post, "api/chat")
{
- Content = new StringContent(JsonSerializer.Serialize(chatRequest), Encoding.UTF8, "application/json")
+ Content = new StringContent(JsonSerializer.Serialize(chatRequest, _serializerOptions), Encoding.UTF8, "application/json")
};
var completion = chatRequest.Stream
@@ -186,7 +188,7 @@ public async Task GetVersion(CancellationToken cancellationToken = defa
{
var request = new HttpRequestMessage(HttpMethod.Post, "api/generate")
{
- Content = new StringContent(JsonSerializer.Serialize(generateRequest), Encoding.UTF8, "application/json")
+ Content = new StringContent(JsonSerializer.Serialize(generateRequest, _serializerOptions), Encoding.UTF8, "application/json")
};
var completion = generateRequest.Stream
@@ -214,7 +216,7 @@ private async Task GetAsync(string endpoint, CancellationT
private async Task PostAsync(string endpoint, TRequest request, CancellationToken cancellationToken)
{
- var content = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
+ var content = new StringContent(JsonSerializer.Serialize(request, _serializerOptions), Encoding.UTF8, "application/json");
var response = await _client.PostAsync(endpoint, content, cancellationToken);
await EnsureSuccessStatusCode(response);
@@ -222,7 +224,7 @@ private async Task PostAsync(string endpoint, TRequest request, Cancel
private async Task PostAsync(string endpoint, TRequest request, CancellationToken cancellationToken)
{
- var content = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");
+ var content = new StringContent(JsonSerializer.Serialize(request, _serializerOptions), Encoding.UTF8, "application/json");
var response = await _client.PostAsync(endpoint, content, cancellationToken);
await EnsureSuccessStatusCode(response);
@@ -236,7 +238,7 @@ private async Task PostAsync(string endpoint, TR
{
var request = new HttpRequestMessage(HttpMethod.Post, endpoint)
{
- Content = new StringContent(JsonSerializer.Serialize(requestModel), Encoding.UTF8, "application/json")
+ Content = new StringContent(JsonSerializer.Serialize(requestModel, _serializerOptions), Encoding.UTF8, "application/json")
};
using var response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
From b27f8dd4f18eccaa9bf575fd03ed2e0c99a4ad56 Mon Sep 17 00:00:00 2001
From: Luna <34936608+Plootie@users.noreply.github.com>
Date: Sat, 7 Sep 2024 00:16:16 +0100
Subject: [PATCH 2/4] Expose RequestOptions for Chat
ChatRequest holds support RequestOptions but there is no way to pass an instance into the chat requests currently (That I can see at least?)
---
src/Chat.cs | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/src/Chat.cs b/src/Chat.cs
index 125b9fc..e8a3edd 100644
--- a/src/Chat.cs
+++ b/src/Chat.cs
@@ -3,6 +3,7 @@
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
+using OllamaSharp.Models;
using OllamaSharp.Models.Chat;
namespace OllamaSharp;
@@ -26,6 +27,8 @@ public class Chat
/// Gets or sets the AI model to chat with
///
public string Model { get; set; }
+
+ public RequestOptions? Options { get; set; }
///
/// Creates a new chat instance
@@ -97,7 +100,8 @@ public async IAsyncEnumerable SendAs(ChatRole role, string message, IEnu
Messages = Messages,
Model = Model,
Stream = !hasTools, // cannot stream if tools should be used
- Tools = tools
+ Tools = tools,
+ Options = Options
};
var messageBuilder = new MessageBuilder();
From 7eb2d9a866ff88e0b8d073a1d21f759a3491b343 Mon Sep 17 00:00:00 2001
From: Luna <34936608+Plootie@users.noreply.github.com>
Date: Sat, 7 Sep 2024 00:16:16 +0100
Subject: [PATCH 3/4] Expose RequestOptions for Chat
ChatRequest holds support RequestOptions but there is no way to pass an instance into the chat requests currently (That I can see at least?)
---
src/Chat.cs | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/src/Chat.cs b/src/Chat.cs
index 125b9fc..e8a3edd 100644
--- a/src/Chat.cs
+++ b/src/Chat.cs
@@ -3,6 +3,7 @@
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
+using OllamaSharp.Models;
using OllamaSharp.Models.Chat;
namespace OllamaSharp;
@@ -26,6 +27,8 @@ public class Chat
/// Gets or sets the AI model to chat with
///
public string Model { get; set; }
+
+ public RequestOptions? Options { get; set; }
///
/// Creates a new chat instance
@@ -97,7 +100,8 @@ public async IAsyncEnumerable SendAs(ChatRole role, string message, IEnu
Messages = Messages,
Model = Model,
Stream = !hasTools, // cannot stream if tools should be used
- Tools = tools
+ Tools = tools,
+ Options = Options
};
var messageBuilder = new MessageBuilder();
From 76535190b644338bec2053a8ceb6c534a2eeded9 Mon Sep 17 00:00:00 2001
From: Luna <34936608+Plootie@users.noreply.github.com>
Date: Thu, 12 Sep 2024 00:31:28 +0100
Subject: [PATCH 4/4] Add Summary
---
src/Chat.cs | 3 +++
1 file changed, 3 insertions(+)
diff --git a/src/Chat.cs b/src/Chat.cs
index e8a3edd..af93abe 100644
--- a/src/Chat.cs
+++ b/src/Chat.cs
@@ -28,6 +28,9 @@ public class Chat
///
public string Model { get; set; }
+ ///
+ /// Gets or sets the RequestOptions to chat with
+ ///
public RequestOptions? Options { get; set; }
///