Skip to content

Commit 1838447

Browse files
committed
feat: creating llm session abstraction
- LLM Section is a wrapper to handle LLM inference based on the selected provider
1 parent 492db16 commit 1838447

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

ext/ai/js/llm/llm_session.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+

ext/ai/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ deno_core::extension!(
5555
"util/event_stream_parser.mjs",
5656
"util/event_source_stream.mjs",
5757
"onnxruntime/onnx.js",
58-
"onnxruntime/cache_adapter.js"
58+
"onnxruntime/cache_adapter.js",
59+
"llm/llm_session.ts",
5960
]
6061
);
6162

0 commit comments

Comments
 (0)