Skip to content

Commit

Permalink
Refactor InnerLengthChatMessage in ILengthFunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRamosEs committed Feb 13, 2024
1 parent 5d429f5 commit aae8b0a
Showing 1 changed file with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public interface ILengthFunctions : AI.Abstractions.ILengthFunctions
/// <param name="lengthFunction">A function to calculate the length of a string.</param>
/// <returns>The total length for the chat message.</returns>
public static int LengthChatMessage(string content, AuthorRole authorRole, Func<string, int> lengthFunction)
=> InnerLengthChatMessage(content, null, authorRole, (s, _) => lengthFunction(s));
=> InnerLengthChatMessage(content, authorRole, lengthFunction);

/// <summary>
/// Calculates the length of a chat message with the specified content, encoding and author role, using a provided length function with encoding.
Expand All @@ -51,20 +51,19 @@ public static int LengthChatMessage(string content, AuthorRole authorRole, Func<
/// <param name="lengthFunctionWithEncoding">A function to calculate the length of a string with encoding.</param>
/// <returns>The total length for the chat message.</returns>
public static int LengthChatMessageWithEncoding(string content, string encoding, AuthorRole authorRole, Func<string, string, int> lengthFunctionWithEncoding)
=> InnerLengthChatMessage(content, encoding, authorRole, lengthFunctionWithEncoding);
=> InnerLengthChatMessage(content, authorRole, s => lengthFunctionWithEncoding(encoding, s));

/// <summary>
/// Internal method to calculate the length of a chat message with the specified content, encoding and author role, using a provided length function with encoding.
/// </summary>
/// <param name="content">The content of the chat message.</param>
/// <param name="encoding">The name of the GptEncoding.</param>
/// <param name="authorRole">The <see cref="AuthorRole"/> of the message.</param>
/// <param name="lengthFunction">A function to calculate the length of a string.</param>
/// <returns>The total length for the chat message.</returns>
private static int InnerLengthChatMessage(string content, string encoding, AuthorRole authorRole, Func<string, string, int> lengthFunction)
private static int InnerLengthChatMessage(string content, AuthorRole authorRole, Func<string, int> lengthFunction)
{
var tokenCount = authorRole == AuthorRole.System ? lengthFunction(encoding, "\n") : 0;
return tokenCount + lengthFunction(encoding, $"role:{authorRole.Label}") + lengthFunction(encoding, $"content:{content}");
var tokenCount = authorRole == AuthorRole.System ? lengthFunction("\n") : 0;
return tokenCount + lengthFunction($"role:{authorRole.Label}") + lengthFunction($"content:{content}");
}

/// <summary>
Expand Down

0 comments on commit aae8b0a

Please sign in to comment.