UNPKG

anylang

Version:

A translator's kit that uses the free APIs of Google Translate, Yandex, Bing, ChatGPT, and other LLMs

43 lines (42 loc) 1.61 kB
import { TranslatorInstanceMembers } from '../Translator'; import { LLMFetcher } from '.'; export type PromptGenerator = (texts: string[], from: string, to: string) => string; export type LLMTranslatorRetryOptions = { /** * Maximum number of retry attempts after a failed request */ retryLimit?: number; /** * Delay before first retry in ms; increases exponentially up to maxRetryTimeout */ retryTimeout?: number; /** * Maximum delay before the next retry */ maxRetryTimeout?: number; /** * An exponential multiplier used to increase the delay between retry attempts. * With each attempt, the delay grows based on this factor. */ retryBackoffFactor?: number; }; export declare const getPrompt: (text: string[], from: string, to: string) => string; export declare class LLMTranslator implements TranslatorInstanceMembers { private readonly llm; private readonly config; constructor(llm: LLMFetcher, options?: { getPrompt?: PromptGenerator; retryOptions?: LLMTranslatorRetryOptions; }); translate(text: string, from: string, to: string): Promise<string>; translateBatch(text: string[], from: string, to: string): Promise<string[]>; getLengthLimit(): number; getRequestsTimeout(): number; checkLimitExceeding(text: string | string[]): number; /** * Calculates retry delays: starts with retryTimeout, * then increases exponentially (retryTimeout * factor^n) up to maxRetryTimeout (default: 4000). * Default retryBackoffFactor: 1.5 */ private waitRetryDelay; }