Skip to content

Commit

Permalink
Fix match model to encoding error.
Browse files Browse the repository at this point in the history
  • Loading branch information
aiqinxuancai committed May 14, 2024
1 parent c07d9d2 commit 598b72e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
3 changes: 2 additions & 1 deletion TiktokenSharp.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ static void Main(string[] args)

static void GPT4o()
{
TikToken tikToken = TikToken.GetEncoding("o200k_base");
TikToken tikToken = TikToken.EncodingForModel("gpt-4o");
//TikToken tikToken = TikToken.GetEncoding("o200k_base");
var i = tikToken.Encode("hello world");
var d = tikToken.Decode(i);

Expand Down
19 changes: 11 additions & 8 deletions TiktokenSharp/Services/EncodingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,23 @@ public static EncodingManager Instance
/// <returns></returns>
public EncodingSettingModel GetEncodingSetting(string modelOrEncodingName)
{
var encodingName = MODEL_TO_ENCODING.FirstOrDefault(a => modelOrEncodingName.StartsWith(a.Key)).Value;
var encodingName = "";

if (string.IsNullOrEmpty(encodingName))

if (MODEL_TO_ENCODING.Any(a => a.Value == modelOrEncodingName))
{
//modelOrEncodingName is encoding name?
encodingName = modelOrEncodingName;
}

if (MODEL_TO_ENCODING.ContainsKey(modelOrEncodingName))
{
encodingName = MODEL_TO_ENCODING.FirstOrDefault(a => a.Key.StartsWith(modelOrEncodingName)).Value;
encodingName = MODEL_TO_ENCODING[modelOrEncodingName];
}

if (string.IsNullOrEmpty(encodingName))
{
if (MODEL_TO_ENCODING.Any(a => a.Value == modelOrEncodingName))
{
//modelOrEncodingName is encoding name
encodingName = modelOrEncodingName;
}
encodingName = MODEL_TO_ENCODING.FirstOrDefault(a => a.Key.StartsWith(modelOrEncodingName)).Value; //MODEL_TO_ENCODING.FirstOrDefault(a => modelOrEncodingName.StartsWith(a.Key)).Value;
}

return GetEncoding(encodingName);
Expand Down
2 changes: 1 addition & 1 deletion TiktokenSharp/TiktokenSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<RepositoryUrl>https://github.com/aiqinxuancai/TiktokenSharp</RepositoryUrl>
<PackageTags>chatgpt;openai</PackageTags>
<Title>TiktokenSharp</Title>
<VersionPrefix Condition=" '$(VersionPrefix)' == '' ">1.1.3</VersionPrefix>
<VersionPrefix Condition=" '$(VersionPrefix)' == '' ">1.1.4</VersionPrefix>
<Authors>aiqinxuancai</Authors>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
Expand Down

0 comments on commit 598b72e

Please sign in to comment.