|
| 1 | +// @ts-ignore deno_core environment |
| 2 | +const core = globalThis.Deno.core; |
| 3 | + |
| 4 | +export type LLMRunInput = { |
| 5 | + /** |
| 6 | + * Stream response from model. Applies only for LLMs like `mistral` (default: false) |
| 7 | + */ |
| 8 | + stream?: boolean; |
| 9 | + |
| 10 | + /** |
| 11 | + * Automatically abort the request to the model after specified time (in seconds). Applies only for LLMs like `mistral` (default: 60) |
| 12 | + */ |
| 13 | + timeout?: number; |
| 14 | + |
| 15 | + prompt: string; |
| 16 | + |
| 17 | + signal?: AbortSignal; |
| 18 | +}; |
| 19 | + |
| 20 | +export interface ILLMProviderOptions { |
| 21 | + inferenceAPIHost: string; |
| 22 | + model: string; |
| 23 | +} |
| 24 | + |
| 25 | +export interface ILLMProvider { |
| 26 | + // TODO:(kallebysantos) remove 'any' |
| 27 | + getStream(prompt: string, signal: AbortSignal): Promise<AsyncIterable<any>>; |
| 28 | + getText(prompt: string, signal: AbortSignal): Promise<any>; |
| 29 | +} |
| 30 | + |
| 31 | +export class LLMSession { |
| 32 | + #inner: ILLMProvider; |
| 33 | + |
| 34 | + constructor(provider: ILLMProvider) { |
| 35 | + this.#inner = provider; |
| 36 | + } |
| 37 | + |
| 38 | + static fromProvider(name: string, opts: ILLMProviderOptions) { |
| 39 | + const ProviderType = providers[name]; |
| 40 | + if (!ProviderType) throw new Error('invalid provider'); |
| 41 | + |
| 42 | + const provider = new ProviderType(opts); |
| 43 | + |
| 44 | + return new LLMSession(provider); |
| 45 | + } |
| 46 | + |
| 47 | + run( |
| 48 | + opts: LLMRunInput, |
| 49 | + ): Promise<AsyncIterable<any>> | Promise<any> { |
| 50 | + const isStream = opts.stream ?? false; |
| 51 | + |
| 52 | + const timeoutSeconds = typeof opts.timeout === 'number' ? opts.timeout : 60; |
| 53 | + const timeoutMs = timeoutSeconds * 1000; |
| 54 | + |
| 55 | + const timeoutSignal = AbortSignal.timeout(timeoutMs); |
| 56 | + const abortSignals = [opts.signal, timeoutSignal] |
| 57 | + .filter((it) => it instanceof AbortSignal); |
| 58 | + const signal = AbortSignal.any(abortSignals); |
| 59 | + |
| 60 | + if (isStream) { |
| 61 | + return this.#inner.getStream(opts.prompt, signal); |
| 62 | + } |
| 63 | + |
| 64 | + return this.#inner.getText(opts.prompt, signal); |
| 65 | + } |
| 66 | +} |
| 67 | + |
0 commit comments