JavaScript library for creating language models with next-token prediction capabilities including autocomplete, text completion, and AI-powered text generation.
The Text Prediction system provides core prediction capabilities for single tokens, token sequences, and multiple completion alternatives. It uses n-gram analysis combined with high-dimensional embeddings to generate contextually appropriate text completions.
Predicts the next single token based on input context using n-gram lookup and similarity analysis.
/**
* Predict the next single token based on input context
* @param {string} token - Input token or phrase to predict from
* @returns {TokenPredictionResult} Prediction with token and ranked alternatives
*/
getTokenPrediction(token);
/**
* Token prediction result structure
*/
interface TokenPredictionResult {
token: string; // Best predicted next token
rankedTokenList: string[]; // Alternative tokens ranked by likelihood
error?: { // Error information if prediction fails
message: string;
};
}Usage Examples:
// Simple token prediction
const prediction1 = model.getTokenPrediction('hello');
// Returns: { token: 'world', rankedTokenList: ['world', 'there', 'everyone', ...] }
// Phrase-based prediction
const prediction2 = model.getTokenPrediction('the weather is');
// Returns: { token: 'beautiful', rankedTokenList: ['beautiful', 'nice', 'sunny', ...] }
// Error handling
const prediction3 = model.getTokenPrediction('xyzunknowntoken');
// May return: { error: { message: 'Failed to look up n-gram.' }, token: '', rankedTokenList: [] }Predicts a sequence of multiple tokens by iteratively applying token prediction to build longer completions.
/**
* Predict a sequence of tokens extending the input
* @param {string} input - Input text to extend
* @param {number} [sequenceLength=2] - Number of tokens to predict in sequence
* @returns {SequencePredictionResult} Sequence completion with metadata
*/
getTokenSequencePrediction(input, sequenceLength);
/**
* Sequence prediction result structure
*/
interface SequencePredictionResult {
completion: string; // Complete predicted sequence
sequenceLength: number; // Number of tokens in sequence
token: string; // First predicted token
rankedTokenList: string[]; // Alternative first tokens ranked by likelihood
}Usage Examples:
// Short sequence prediction
const sequence1 = model.getTokenSequencePrediction('JavaScript is', 3);
// Returns: {
// completion: 'a programming language',
// sequenceLength: 3,
// token: 'a',
// rankedTokenList: ['a', 'an', 'the', ...]
// }
// Single token sequence (equivalent to getTokenPrediction but different format)
const sequence2 = model.getTokenSequencePrediction('hello', 1);
// Returns: {
// completion: 'world',
// sequenceLength: 1,
// token: 'world',
// rankedTokenList: ['world', 'there', ...]
// }
// Longer sequence
const sequence3 = model.getTokenSequencePrediction('The quick brown', 5);
// Returns: {
// completion: 'fox jumps over the',
// sequenceLength: 5,
// token: 'fox',
// rankedTokenList: ['fox', 'dog', 'cat', ...]
// }Generates multiple alternative completions for comprehensive text prediction scenarios, providing a top-k sampling approach.
/**
* Generate multiple completion alternatives with ranking
* @param {string} input - Input text to complete
* @returns {CompletionsResult} Multiple completions with ranking information
*/
getCompletions(input);
/**
* Multiple completions result structure
*/
interface CompletionsResult {
completion: string; // Primary/best completion
token: string; // First token of primary completion
rankedTokenList: string[]; // Alternative first tokens ranked by likelihood
completions: string[]; // Array of alternative full completions
}Usage Examples:
// Get multiple completion options
const completions = model.getCompletions('The sun');
// Returns: {
// completion: 'is shining brightly today',
// token: 'is',
// rankedTokenList: ['is', 'was', 'will', 'has', ...],
// completions: [
// 'is shining brightly today',
// 'was setting behind the mountains',
// 'will rise tomorrow morning',
// 'has been hidden by clouds',
// // ... more alternatives
// ]
// }
// Use for autocomplete suggestions
const suggestions = model.getCompletions('I need to');
console.log('Completion options:');
suggestions.completions.forEach((completion, index) => {
console.log(`${index + 1}. I need to ${completion}`);
});The prediction system respects several environment variables for customization:
/**
* Environment configuration affecting prediction behavior
*/
interface PredictionConfig {
RANKING_BATCH_SIZE: number; // Number of alternatives in rankedTokenList (default: 50)
MAX_RESPONSE_LENGTH: number; // Maximum sequence length for predictions (default: 240)
VARIANCE: number; // Prediction randomization level (default: 0)
}Configuration Examples:
# Increase number of alternatives returned
export RANKING_BATCH_SIZE=100
# Allow longer sequence predictions
export MAX_RESPONSE_LENGTH=500
# Add some randomization to predictions (experimental)
export VARIANCE=1These methods are available on the transformer instance but typically used internally:
/**
* Look up n-gram by token sequence
* @param {string} input - Space-separated token sequence
* @returns {Object} N-gram lookup result
*/
ngramSearch(input);
/**
* Look up embedding vector for token pair
* @param {string} prevToken - Previous token context
* @param {string} token - Current token
* @returns {number[]} Embedding vector or null vector
*/
embeddingSearch(prevToken, token);
/**
* Calculate vector similarity between tokens
* @param {string} prevToken - Previous token context
* @param {string} token - Reference token
* @returns {Object} Similar token with ranking data
*/
getSimilarToken(prevToken, token);The prediction system handles several error conditions gracefully:
Error Response Format:
{
error: { message: "Failed to look up n-gram." },
token: "",
rankedTokenList: []
}Install with Tessl CLI
npx tessl i tessl/npm-next-token-prediction