Skip to content

Commit

Permalink
refactor: Renamed Encoders to ModelToEncoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed May 19, 2024
1 parent 93e1125 commit 39b1550
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ We will be happy to accept any PR.

### Usage
```csharp
using Tiktoken.Encodings;
using Tiktoken;

var encoder = Encoders.ForModel("gpt-4o"); // or explicitly new Encoder(new O200KBase())
var encoder = ModelToEncoder.For("gpt-4o"); // or explicitly using new Encoder(new O200KBase())
var tokens = encoder.Encode("hello world"); // [15339, 1917]
var text = encoder.Decode(tokens); // hello world
var numberOfTokens = encoder.CountTokens(text); // 2
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</ItemGroup>

<PropertyGroup Label="Nuget">
<Version>2.0.1</Version>
<Version>2.0.2</Version>
<Description>The fastest tokenizer for GPT-3.5 and GPT-4 inspired by Tiktoken.</Description>
<PackageTags>chatgpt;openai;tiktoken;tokens;gpt-4;gpt-3.5-turbo;cl100k_base;p50k_base</PackageTags>
<GeneratePackageOnBuild Condition=" '$(Configuration)' == 'Release' ">true</GeneratePackageOnBuild>
Expand Down
31 changes: 31 additions & 0 deletions src/libs/Tiktoken/ModelToEncoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Tiktoken;

/// <summary>
///
/// </summary>
public static class ModelToEncoder
{
/// <summary>
/// Returns encoder by model name.
/// </summary>
/// <param name="modelName">gpt-3.5-turbo</param>
/// <returns></returns>
public static Encoder For(string modelName)
{
return new Encoder(ModelToEncoding.For(modelName));
}

/// <summary>
/// Returns encoder by model name or null.
/// </summary>
/// <param name="modelName">gpt-3.5-turbo</param>
/// <returns></returns>
public static Encoder? TryFor(string modelName)
{
var encoding = ModelToEncoding.TryFor(modelName);

return encoding == null
? null
: new Encoder(encoding);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,9 @@ namespace Tiktoken;
/// <summary>
///
/// </summary>
public static class Encoders
public static class ModelToEncoding
{
/// <summary>
/// Returns encoder by model name.
/// </summary>
/// <param name="modelName">gpt-3.5-turbo</param>
/// <returns></returns>
public static Encoder ForModel(string modelName)
{
return new Encoder(GetEncodingByModel(modelName));
}

/// <summary>
/// Returns encoder by model name or null.
/// </summary>
/// <param name="modelName">gpt-3.5-turbo</param>
/// <returns></returns>
public static Encoder? TryForModel(string modelName)
{
var encoding = TryGetEncodingByModel(modelName);

return encoding == null
? null
: new Encoder(encoding);
}

private static Dictionary<string, Encoding> ModelToEncoding { get; } = new()
private static Dictionary<string, Encoding> Dictionary { get; } = new()
{
// chat
{ "gpt-4o", new O200KBase() },
Expand All @@ -51,9 +27,9 @@ public static Encoder ForModel(string modelName)
/// <param name="modelName">gpt-4 gpt-3.5-turbo ...</param>
/// <exception cref="ArgumentException"></exception>
/// <returns></returns>
public static Encoding? TryGetEncodingByModel(string modelName)
public static Encoding? TryFor(string modelName)
{
return ModelToEncoding
return Dictionary
.FirstOrDefault(a => modelName.StartsWith(a.Key, StringComparison.Ordinal)).Value;
}

Expand All @@ -63,9 +39,9 @@ public static Encoder ForModel(string modelName)
/// <param name="modelName">gpt-4 gpt-3.5-turbo ...</param>
/// <exception cref="ArgumentException"></exception>
/// <returns></returns>
public static Encoding GetEncodingByModel(string modelName)
public static Encoding For(string modelName)
{
return TryGetEncodingByModel(modelName) ??
return TryFor(modelName) ??
throw new ArgumentException($"Model name {modelName} is not supported.");
}
}

1 comment on commit 39b1550

@Xan-Kun
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic! Even with "For" and "TryFor"! Someone know what they are doing 😉
Thx again.

Please sign in to comment.