Skip to content

Commit

Permalink
refactor: cleaning up magic string and moving over to constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
JerrettDavis committed Jan 9, 2025
1 parent ae443e8 commit ce3b8b9
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 59 deletions.
4 changes: 0 additions & 4 deletions src/ByteArrayExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace OllamaSharp;

/// <summary>
Expand Down
28 changes: 28 additions & 0 deletions src/Constants/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,32 @@ 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";
public const string TotalDuration = "total_duration";
public const string PromptEvalDuration = "prompt_eval_duration";
public const string PromptEvalCount = "prompt_eval_count";
public const string EvalDuration = "eval_duration";
public const string EvalCount = "eval_count";
public const string Context = "context";
public const string Done = "done";
public const string Response = "response";
public const string CreatedAt = "created_at";
public const string Model = "model";

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";
}
59 changes: 29 additions & 30 deletions src/MicrosoftAi/AbstractionMapper.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.Extensions.AI;
using OllamaSharp.Constants;
using OllamaSharp.Models;
using OllamaSharp.Models.Chat;
using ChatRole = OllamaSharp.Models.Chat.ChatRole;

namespace OllamaSharp.MicrosoftAi;

Expand Down Expand Up @@ -53,7 +52,7 @@ public static ChatRequest ToOllamaSharpChatRequest(IList<ChatMessage> chatMessag
{
var request = new ChatRequest
{
Format = Equals(options?.ResponseFormat, ChatResponseFormat.Json) ? "json" : null,
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
Expand Down Expand Up @@ -167,17 +166,17 @@ private static Tool ToOllamaSharpTool(AIFunctionMetadata functionMetadata)
Name = functionMetadata.Name,
Parameters = new Parameters
{
Properties = functionMetadata.Parameters.ToDictionary(p => p.Name, p => new Models.Chat.Property
Properties = functionMetadata.Parameters.ToDictionary(p => p.Name, p => new Property
{
Description = p.Description,
Enum = GetPossibleValues(p.Schema as JsonObject),
Type = ToFunctionTypeString(p.Schema as JsonObject)
}),
Required = functionMetadata.Parameters.Where(p => p.IsRequired).Select(p => p.Name),
Type = "object"
Type = Application.Object
}
},
Type = "function"
Type = Application.Function
};
}

Expand Down Expand Up @@ -238,7 +237,7 @@ private static IEnumerable<Message> ToOllamaSharpMessages(IList<ChatMessage> cha
CallId = frc.CallId,
Result = jsonResult,
}, serializerOptions),
Role = Models.Chat.ChatRole.Tool,
Role = ChatRole.Tool,
};
}
}
Expand Down Expand Up @@ -282,15 +281,15 @@ private static Message.ToolCall ToOllamaSharpToolCall(FunctionCallContent functi
/// </summary>
/// <param name="role">The chat role to map.</param>
/// <returns>A <see cref="OllamaSharp.Models.Chat.ChatRole"/> object containing the mapped role.</returns>
private static Models.Chat.ChatRole ToOllamaSharpRole(Microsoft.Extensions.AI.ChatRole role)
private static ChatRole ToOllamaSharpRole(Microsoft.Extensions.AI.ChatRole role)
{
return role.Value switch
{
"assistant" => Models.Chat.ChatRole.Assistant,
"system" => Models.Chat.ChatRole.System,
"user" => Models.Chat.ChatRole.User,
"tool" => Models.Chat.ChatRole.Tool,
_ => new Models.Chat.ChatRole(role.Value),
Application.Assistant => ChatRole.Assistant,
Application.System => ChatRole.System,
Application.User => ChatRole.User,
Application.Tool => ChatRole.Tool,
_ => new ChatRole(role.Value),
};
}

Expand All @@ -299,17 +298,17 @@ private static Models.Chat.ChatRole ToOllamaSharpRole(Microsoft.Extensions.AI.Ch
/// </summary>
/// <param name="role">The chat role to map.</param>
/// <returns>A <see cref="Microsoft.Extensions.AI.ChatRole"/> object containing the mapped role.</returns>
private static Microsoft.Extensions.AI.ChatRole ToAbstractionRole(OllamaSharp.Models.Chat.ChatRole? role)
private static Microsoft.Extensions.AI.ChatRole ToAbstractionRole(ChatRole? role)
{
if (role is null)
return new Microsoft.Extensions.AI.ChatRole("unknown");

return role.ToString() switch
{
"assistant" => Microsoft.Extensions.AI.ChatRole.Assistant,
"system" => Microsoft.Extensions.AI.ChatRole.System,
"user" => Microsoft.Extensions.AI.ChatRole.User,
"tool" => Microsoft.Extensions.AI.ChatRole.Tool,
Application.Assistant => Microsoft.Extensions.AI.ChatRole.Assistant,
Application.System => Microsoft.Extensions.AI.ChatRole.System,
Application.User => Microsoft.Extensions.AI.ChatRole.User,
Application.Tool => Microsoft.Extensions.AI.ChatRole.Tool,
_ => new Microsoft.Extensions.AI.ChatRole(role.ToString()!),
};
}
Expand Down Expand Up @@ -352,7 +351,7 @@ public static ChatMessage ToChatMessage(Message message)
if (toolCall.Function is { } function)
{
var id = Guid.NewGuid().ToString().Substring(0, 8);
contents.Add(new FunctionCallContent(id, function.Name ?? "n/a", function.Arguments) { RawRepresentation = toolCall });
contents.Add(new FunctionCallContent(id, function.Name ?? Application.NotApplicable, function.Arguments) { RawRepresentation = toolCall });
}
}
}
Expand All @@ -376,10 +375,10 @@ private static AdditionalPropertiesDictionary ParseOllamaChatResponseProps(ChatD

return new AdditionalPropertiesDictionary
{
["load_duration"] = TimeSpan.FromMilliseconds(response.LoadDuration / NANOSECONDS_PER_MILLISECOND),
["total_duration"] = TimeSpan.FromMilliseconds(response.TotalDuration / NANOSECONDS_PER_MILLISECOND),
["prompt_eval_duration"] = TimeSpan.FromMilliseconds(response.PromptEvalDuration / NANOSECONDS_PER_MILLISECOND),
["eval_duration"] = TimeSpan.FromMilliseconds(response.EvalDuration / NANOSECONDS_PER_MILLISECOND)
[Application.LoadDuration] = TimeSpan.FromMilliseconds(response.LoadDuration / NANOSECONDS_PER_MILLISECOND),
[Application.TotalDuration] = TimeSpan.FromMilliseconds(response.TotalDuration / NANOSECONDS_PER_MILLISECOND),
[Application.PromptEvalDuration] = TimeSpan.FromMilliseconds(response.PromptEvalDuration / NANOSECONDS_PER_MILLISECOND),
[Application.EvalDuration] = TimeSpan.FromMilliseconds(response.EvalDuration / NANOSECONDS_PER_MILLISECOND)
};
}

Expand All @@ -394,8 +393,8 @@ private static AdditionalPropertiesDictionary ParseOllamaEmbedResponseProps(Embe

return new AdditionalPropertiesDictionary
{
["load_duration"] = TimeSpan.FromMilliseconds((response.LoadDuration ?? 0) / NANOSECONDS_PER_MILLISECOND),
["total_duration"] = TimeSpan.FromMilliseconds((response.TotalDuration ?? 0) / NANOSECONDS_PER_MILLISECOND)
[Application.LoadDuration] = TimeSpan.FromMilliseconds((response.LoadDuration ?? 0) / NANOSECONDS_PER_MILLISECOND),
[Application.TotalDuration] = TimeSpan.FromMilliseconds((response.TotalDuration ?? 0) / NANOSECONDS_PER_MILLISECOND)
};
}

Expand All @@ -409,8 +408,8 @@ private static AdditionalPropertiesDictionary ParseOllamaEmbedResponseProps(Embe
return ollamaDoneReason switch
{
null => null,
"length" => ChatFinishReason.Length,
"stop" => ChatFinishReason.Stop,
Application.Length => ChatFinishReason.Length,
Application.Stop => ChatFinishReason.Stop,
_ => new ChatFinishReason(ollamaDoneReason),
};
}
Expand Down Expand Up @@ -451,10 +450,10 @@ public static EmbedRequest ToOllamaEmbedRequest(IEnumerable<string> values, Embe

if (options?.AdditionalProperties is { } requestProps)
{
if (requestProps.TryGetValue("keep_alive", out long keepAlive))
if (requestProps.TryGetValue(Application.KeepAlive, out long keepAlive))
request.KeepAlive = keepAlive;

if (requestProps.TryGetValue("truncate", out bool truncate))
if (requestProps.TryGetValue(Application.Truncate, out bool truncate))
request.Truncate = truncate;
}

Expand Down
9 changes: 5 additions & 4 deletions src/Models/Chat/ChatDoneResponseStream.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.Chat;

Expand All @@ -10,13 +11,13 @@ public class ChatDoneResponseStream : ChatResponseStream
/// <summary>
/// The time spent generating the response
/// </summary>
[JsonPropertyName("total_duration")]
[JsonPropertyName(Application.TotalDuration)]
public long TotalDuration { get; set; }

/// <summary>
/// The time spent in nanoseconds loading the model
/// </summary>
[JsonPropertyName("load_duration")]
[JsonPropertyName(Application.LoadDuration)]
public long LoadDuration { get; set; }

/// <summary>
Expand All @@ -28,7 +29,7 @@ public class ChatDoneResponseStream : ChatResponseStream
/// <summary>
/// The time spent in nanoseconds evaluating the prompt
/// </summary>
[JsonPropertyName("prompt_eval_duration")]
[JsonPropertyName(Application.PromptEvalDuration)]
public long PromptEvalDuration { get; set; }

/// <summary>
Expand All @@ -40,7 +41,7 @@ public class ChatDoneResponseStream : ChatResponseStream
/// <summary>
/// The time in nanoseconds spent generating the response
/// </summary>
[JsonPropertyName("eval_duration")]
[JsonPropertyName(Application.EvalDuration)]
public long EvalDuration { get; set; }

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Models/Embed.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
using OllamaSharp.Constants;

namespace OllamaSharp.Models;

Expand Down Expand Up @@ -60,13 +60,13 @@ public class EmbedResponse
/// <summary>
/// The time spent generating the response
/// </summary>
[JsonPropertyName("total_duration")]
[JsonPropertyName(Application.TotalDuration)]
public long? TotalDuration { get; set; }

/// <summary>
/// The time spent in nanoseconds loading the model
/// </summary>
[JsonPropertyName("load_duration")]
[JsonPropertyName(Application.LoadDuration)]
public long? LoadDuration { get; set; }

/// <summary>
Expand Down
24 changes: 13 additions & 11 deletions src/Models/Generate.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Net.Mime;
using System.Text.Json.Serialization;
using OllamaSharp.Constants;

namespace OllamaSharp.Models;

Expand Down Expand Up @@ -108,13 +110,13 @@ public class GenerateResponseStream
/// <summary>
/// The model that generated the response
/// </summary>
[JsonPropertyName("model")]
[JsonPropertyName(Application.Model)]
public string Model { get; set; } = null!;

/// <summary>
/// Gets or sets the time the response was generated.
/// </summary>
[JsonPropertyName("created_at")]
[JsonPropertyName(Application.CreatedAt)]
public string? CreatedAtString
{
get => _createdAtString;
Expand Down Expand Up @@ -142,13 +144,13 @@ public DateTimeOffset? CreatedAt
/// <summary>
/// The response generated by the model
/// </summary>
[JsonPropertyName("response")]
[JsonPropertyName(Application.Response)]
public string Response { get; set; } = null!;

/// <summary>
/// Whether the response is complete
/// </summary>
[JsonPropertyName("done")]
[JsonPropertyName(Application.Done)]
public bool Done { get; set; }
}

Expand All @@ -161,42 +163,42 @@ public class GenerateDoneResponseStream : GenerateResponseStream
/// An encoding of the conversation used in this response, this can be
/// sent in the next request to keep a conversational memory
/// </summary>
[JsonPropertyName("context")]
[JsonPropertyName(Application.Context)]
public long[] Context { get; set; } = null!;

/// <summary>
/// The time spent generating the response
/// </summary>
[JsonPropertyName("total_duration")]
[JsonPropertyName(Application.TotalDuration)]
public long TotalDuration { get; set; }

/// <summary>
/// The time spent in nanoseconds loading the model
/// </summary>
[JsonPropertyName("load_duration")]
[JsonPropertyName(Application.LoadDuration)]
public long LoadDuration { get; set; }

/// <summary>
/// The number of tokens in the prompt
/// </summary>
[JsonPropertyName("prompt_eval_count")]
[JsonPropertyName(Application.PromptEvalCount)]
public int PromptEvalCount { get; set; }

/// <summary>
/// The time spent in nanoseconds evaluating the prompt
/// </summary>
[JsonPropertyName("prompt_eval_duration")]
[JsonPropertyName(Application.PromptEvalDuration)]
public long PromptEvalDuration { get; set; }

/// <summary>
/// The number of tokens in the response
/// </summary>
[JsonPropertyName("eval_count")]
[JsonPropertyName(Application.EvalCount)]
public int EvalCount { get; set; }

/// <summary>
/// The time in nanoseconds spent generating the response
/// </summary>
[JsonPropertyName("eval_duration")]
[JsonPropertyName(Application.EvalDuration)]
public long EvalDuration { get; set; }
}
3 changes: 0 additions & 3 deletions src/OllamaApiClientExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using OllamaSharp.Models;

namespace OllamaSharp;
Expand Down
9 changes: 5 additions & 4 deletions test/AbstractionMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.AI;
using NUnit.Framework;
using OllamaSharp;
using OllamaSharp.Constants;
using OllamaSharp.MicrosoftAi;
using OllamaSharp.Models;
using OllamaSharp.Models.Chat;
Expand Down Expand Up @@ -562,10 +563,10 @@ public void Maps_Known_Properties()
var chatCompletion = AbstractionMapper.ToChatCompletion(stream, usedModel: null);

chatCompletion.AdditionalProperties.Should().NotBeNull();
chatCompletion.AdditionalProperties["eval_duration"].Should().Be(TimeSpan.FromSeconds(2.222222222));
chatCompletion.AdditionalProperties["load_duration"].Should().Be(TimeSpan.FromSeconds(3.333333333));
chatCompletion.AdditionalProperties["total_duration"].Should().Be(TimeSpan.FromSeconds(6.666666666));
chatCompletion.AdditionalProperties["prompt_eval_duration"].Should().Be(TimeSpan.FromSeconds(5.555555555));
chatCompletion.AdditionalProperties[Application.EvalDuration].Should().Be(TimeSpan.FromSeconds(2.222222222));
chatCompletion.AdditionalProperties[Application.LoadDuration].Should().Be(TimeSpan.FromSeconds(3.333333333));
chatCompletion.AdditionalProperties[Application.TotalDuration].Should().Be(TimeSpan.FromSeconds(6.666666666));
chatCompletion.AdditionalProperties[Application.PromptEvalDuration].Should().Be(TimeSpan.FromSeconds(5.555555555));
chatCompletion.Choices.Should().HaveCount(1);
chatCompletion.Choices.Single().Text.Should().Be("Hi.");
chatCompletion.CompletionId.Should().Be(ollamaCreatedStamp);
Expand Down

0 comments on commit ce3b8b9

Please sign in to comment.