@juspay/neurolink
Version:
The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002
154 lines (153 loc) • 7.16 kB
TypeScript
/**
* LLM rewrite of a transcript: spelling, punctuation, dictionary terms and
* transliteration, for English, any Indian language, or a mix of the two.
*
* The rewrite is a convenience, never a risk to the words:
* - on timeout (default 12 s) or error the input text is kept;
* - a rewrite that drops more than `maxDropRatio` (default 0.25) of the words
* is discarded — a model that summarises or truncates must not cost words;
* - the model's answer is the last non-empty line(s), because some models
* echo a preamble ("Here is the corrected sentence:") before it;
* - `maxTokens` is always an integer: a float (`60 + 1.5 × chars` unrounded)
* was once rejected with an HTTP 400.
*
* The prompt's hardest rule is "never translate": asked to "correct" a
* Hindi-English sentence, models readily return an English translation, which
* reads well and loses what was said.
*
* @module voice/correction/rewrite
*/
import type { STTCorrectionDeps, STTCorrectionOptions, STTDictionaryEntry, STTRewriteLinesOutcome, STTRewriteOutcome, STTWordPair } from "../../types/index.js";
/**
* The rewrite's provider/model/timeout settings, whatever shape `rewrite`
* took: `false` and `"auto"` carry none, an object carries its own.
*/
export declare function rewriteSettings(rewrite: STTCorrectionOptions["rewrite"]): {
provider?: string;
model?: string;
timeoutMs?: number;
} | undefined;
/**
* Whether the rewrite is wanted for a text, under the "auto" rule: it runs
* when there is something for it to do and is skipped otherwise, which is
* what makes an already clean English sentence cost nothing. `"always"`
* (object form with `mode: "always"`) runs it every time; `false` never.
*/
export declare function rewriteWanted(text: string, options: STTCorrectionOptions | undefined, signals?: {
/** The rewrite has a second transcript to reconcile. */
hasSecondOpinion?: boolean;
/** The dictionary changed something (a term was applied). */
termsApplied?: number;
/** Detected language of the speech. */
language?: string;
}): {
wanted: boolean;
reason: string;
};
/**
* The script setting in effect. Unset, Indian languages (and speech whose
* language is unknown, as before) are written in Latin letters; any other
* known language keeps its own script.
*/
export declare function resolveTransliterate(options: Pick<STTCorrectionOptions, "transliterate"> | undefined, language: string | undefined): "latin" | "native";
/** Default per-batch timeout for `rewriteLines`: a batch carries up to 25 lines. */
export declare const DEFAULT_REWRITE_LINES_TIMEOUT_MS = 60000;
/** Default share of words a rewrite may drop before it is discarded. */
export declare const DEFAULT_MAX_DROP_RATIO = 0.25;
/** Lines per request in `rewriteLines`. */
export declare const REWRITE_LINES_BATCH = 25;
/**
* Output budget: ~1.5 tokens per input character plus a fixed allowance,
* capped. Generous on purpose — transliterated Indic text takes more tokens
* than its source script — and rounded, because a float is a 400 on at least
* one OpenAI-compatible server.
*/
export declare function rewriteMaxTokens(inputChars: number): number;
/** Words for the drop guard: whitespace tokens that hold a letter or a digit. */
export declare function countWords(text: string): number;
/**
* System prompt for the rewrite. `lines: true` is the batched variant for long
* transcripts: numbered lines in, the same numbered lines out.
*/
export declare function buildRewriteSystemPrompt(options?: {
transliterate?: "latin" | "native";
punctuate?: boolean;
dictionary?: readonly STTDictionaryEntry[];
lines?: boolean;
cleanup?: "verbatim" | "clean";
/** Detected language of the speech; a known language outside India gets the general prompt. */
language?: string;
}): string;
/**
* User message for the rewrite. With a second opinion the two transcripts are
* labelled; `literal` lists aliases the guard decided were meant literally, so
* the rewrite does not turn them back into dictionary terms.
*/
export declare function buildRewriteUserPrompt(primary: string, secondOpinion?: string, extras?: {
labels?: {
primary?: string;
secondary?: string;
};
literal?: readonly string[];
/** Earlier corrected utterances, oldest first: context the model must not echo. */
previous?: readonly string[];
/** The two transcripts aligned word by word (Indic reading against English reading). */
pairs?: readonly STTWordPair[];
}): string;
/**
* The word alignment of two transcripts as a prompt block: same-sounding
* words side by side ("मीटिंग ≈ meeting"), and runs where only one engine
* heard something, so the rewrite decides word by word which reading holds
* the speaker's word in which language.
*/
export declare function alignmentBlock(pairs: readonly STTWordPair[] | undefined): string | undefined;
/**
* Word pairs for a reconciliation: the Indic-script transcript aligned
* against the Latin-script one, whichever order they came in; `undefined`
* when both are in the same script (nothing to line up across scripts).
*/
export declare function reconcilePairs(primary: string, secondOpinion: string | undefined): {
pairs: STTWordPair[];
englishShare: number;
} | undefined;
/**
* The model's answer: the last `lineCount` non-empty lines, since a preamble,
* if any, comes first. An echoed "Transcript:" label is stripped.
*/
export declare function extractRewrite(content: string, lineCount?: number): string;
/**
* Rewrite one transcript (a sentence, an utterance, or a short multi-line
* text). `primary` is the leading text — already dictionary-applied — and the
* one the drop guard measures against. Never throws.
*/
export declare function rewriteTranscript(input: {
primary: string;
secondOpinion?: string;
dictionary?: readonly STTDictionaryEntry[];
options?: STTCorrectionOptions;
labels?: {
primary?: string;
secondary?: string;
};
literal?: readonly string[];
/** Detected language of the speech. */
language?: string;
/** Earlier corrected utterances, oldest first. */
previous?: readonly string[];
/** The two transcripts aligned word by word (see `reconcilePairs`). */
pairs?: readonly STTWordPair[];
onPartial?: (text: string) => void;
}, rewrite: STTCorrectionDeps["rewrite"]): Promise<STTRewriteOutcome>;
/**
* Batched rewrite for long transcripts (file transcription, diarized turns):
* 25 numbered lines per request, the same numbers back. A failed or timed-out
* batch keeps its lines; a line missing from the output, empty, or shorter by
* more than `maxDropRatio` of its words keeps its input. Blank input lines
* are never sent and come back as they were. Never throws.
*/
export declare function rewriteLines(lines: readonly string[], rewrite: STTCorrectionDeps["rewrite"], options?: {
dictionary?: readonly STTDictionaryEntry[];
correction?: STTCorrectionOptions;
/** Detected language of the recording. */
language?: string;
}): Promise<STTRewriteLinesOutcome>;