-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
43 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,37 @@ | ||
// Import the function to parse model identifiers | ||
import { parseModelIdentifier } from "./models" | ||
|
||
export interface TokenEncoders { | ||
encode: TokenEncoder | ||
decode: TokenDecoder | ||
} | ||
|
||
/** | ||
* Resolves the appropriate token encoder based on the given model ID. | ||
* @param modelId - The identifier for the model to resolve the encoder for. | ||
* @returns A Promise that resolves to a TokenEncoder function. | ||
*/ | ||
export async function resolveTokenEncoder( | ||
modelId: string | ||
): Promise<TokenEncoder> { | ||
): Promise<TokenEncoders> { | ||
// Parse the model identifier to extract the model information | ||
const { model } = parseModelIdentifier(modelId) | ||
const module = model // Assign model to module for dynamic import path | ||
|
||
const options = { disallowedSpecial: new Set<string>() } | ||
try { | ||
// Attempt to dynamically import the encoder module for the specified model | ||
const mod = await import(`gpt-tokenizer/model/${module}`) | ||
return (line) => mod.encode(line, options) // Return the encoder function | ||
const { encode, decode } = await import(`gpt-tokenizer/model/${module}`) | ||
return Object.freeze<TokenEncoders>({ | ||
encode: (line) => encode(line, options), // Return the default encoder function | ||
decode, | ||
}) | ||
} catch (e) { | ||
// If the specific model encoder is not found, default to gpt-4o encoder | ||
const { encode } = await import("gpt-tokenizer") | ||
return (line) => encode(line, options) // Return the default encoder function | ||
const { encode, decode } = await import("gpt-tokenizer") | ||
return Object.freeze<TokenEncoders>({ | ||
encode: (line) => encode(line, options), // Return the default encoder function | ||
decode, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters