44// -------------------------------------------------------------------------
55
66import { FoundryLocalManager } from '../src/index.js' ;
7- import path from 'path' ;
87
98async function main ( ) {
10- let modelToLoad : any = null ;
11-
129 try {
1310 // Initialize the Foundry Local SDK
1411 console . log ( 'Initializing Foundry Local SDK...' ) ;
1512
13+ // NOTE: You must update libraryPath to point to your built DLL if not in standard location
1614 const manager = FoundryLocalManager . create ( {
17- appName : 'FoundryLocalAudioExample' ,
15+ appName : 'FoundryLocalExample' ,
16+ serviceEndpoint : 'http://localhost:5000' ,
1817 logLevel : 'info'
1918 } ) ;
2019 console . log ( '✓ SDK initialized successfully' ) ;
2120
21+ const availableEps = manager . discoverEps ( ) ;
22+ console . log ( `\nAvailable execution providers: ${ availableEps . map ( ( ep ) => ep . name ) . join ( ', ' ) } ` ) ;
23+
24+ console . log ( '\nDownloading and registering execution providers...' ) ;
25+ const downloadResult = await manager . downloadAndRegisterEps ( ) ;
26+ if ( downloadResult . success ) {
27+ console . log ( '✓ All execution providers registered successfully' ) ;
28+ } else {
29+ console . log ( `⚠️ Some execution providers failed to download and/or register: ${ downloadResult . failedEps . join ( ', ' ) } ` ) ;
30+ }
31+
2232 // Explore available models
2333 console . log ( '\nFetching available models...' ) ;
2434 const catalog = manager . catalog ;
@@ -30,45 +40,44 @@ async function main() {
3040 console . log ( ` - ${ model . alias } (variants: ${ variants } )` ) ;
3141 }
3242
33- const modelAlias = 'whisper-medium' ;
43+ // Explore cached models
44+ console . log ( '\nFetching cached models...' ) ;
45+ const cachedModels = await catalog . getCachedModels ( ) ;
46+ console . log ( `Found ${ cachedModels . length } cached models:` ) ;
47+ for ( const cachedModel of cachedModels ) {
48+ console . log ( ` - ${ cachedModel . alias } ` ) ;
49+ }
50+
51+ const modelAlias = 'MODEL_ALIAS' ; // Replace with a valid model alias from the list above
3452
35- // Get the Whisper model
53+ // Load the model first
3654 console . log ( `\nLoading model ${ modelAlias } ...` ) ;
37- modelToLoad = await catalog . getModel ( modelAlias ) ;
55+ const modelToLoad = await catalog . getModel ( modelAlias ) ;
3856 if ( ! modelToLoad ) {
3957 throw new Error ( `Model ${ modelAlias } not found` ) ;
4058 }
4159
42- // Download if not cached
43- if ( ! modelToLoad . isCached ) {
44- console . log ( 'Downloading model...' ) ;
45- await modelToLoad . download ( ( progress : number ) => {
46- process . stdout . write ( `\rDownload: ${ progress . toFixed ( 1 ) } %` ) ;
47- } ) ;
48- console . log ( ) ;
49- }
50-
5160 await modelToLoad . load ( ) ;
5261 console . log ( '✓ Model loaded' ) ;
5362
54- // Create audio client
55- console . log ( '\nCreating audio client...' ) ;
56- const audioClient = modelToLoad . createAudioClient ( ) ;
63+ // Create chat client
64+ console . log ( '\nCreating chat client...' ) ;
65+ const chatClient = modelToLoad . createChatClient ( ) ;
5766
58- // Configure settings
59- audioClient . settings . language = 'en' ;
60- // audioClient .settings.temperature = 0.0; // deterministic results
67+ // Configure chat settings
68+ chatClient . settings . temperature = 0.7 ;
69+ chatClient . settings . maxTokens = 800 ;
6170
62- console . log ( '✓ Audio client created' ) ;
71+ console . log ( '✓ Chat client created' ) ;
6372
64- // Audio file path — update this to point to your audio file
65- const audioFilePath = path . join ( process . cwd ( ) , '..' , 'testdata' , 'Recording.mp3' ) ;
73+ // Example chat completion
74+ console . log ( '\nTesting chat completion...' ) ;
75+ const completion = await chatClient . completeChat ( [
76+ { role : 'user' , content : 'What is the capital of France?' }
77+ ] ) ;
6678
67- // Example: Standard transcription
68- console . log ( '\nTesting standard transcription...' ) ;
69- const result = await audioClient . transcribe ( audioFilePath ) ;
70- console . log ( '\nTranscription result:' ) ;
71- console . log ( result . text ) ;
79+ console . log ( '\nChat completion result:' ) ;
80+ console . log ( completion . choices [ 0 ] ?. message ?. content ) ;
7281
7382 // Example streaming completion
7483 console . log ( '\nTesting streaming completion...' ) ;
@@ -82,27 +91,33 @@ async function main() {
8291 }
8392 console . log ( '\n' ) ;
8493
85- // Unload the model
86- console . log ( 'Unloading model...' ) ;
87- await modelToLoad . unload ( ) ;
88- console . log ( `✓ Model unloaded` ) ;
94+ // Model management example
95+ const model = await catalog . getModel ( modelAlias ) ;
96+ if ( model ) {
97+ console . log ( '\nModel management example:' ) ;
98+ // Already loaded above, but let's check status
99+ console . log ( `Checking model: ${ model . id } ` ) ;
100+ console . log ( `✓ Model loaded: ${ await model . isLoaded ( ) } ` ) ;
101+
102+ // Unload when done
103+ console . log ( 'Unloading model...' ) ;
104+ await model . unload ( ) ;
105+ console . log ( `✓ Model loaded: ${ await model . isLoaded ( ) } ` ) ;
106+ }
89107
90- console . log ( '\n✓ Audio transcription example completed successfully' ) ;
108+ console . log ( '\n✓ Example completed successfully' ) ;
91109
92110 } catch ( error ) {
93111 console . log ( 'Error running example:' , error ) ;
112+ // Print stack trace if available
94113 if ( error instanceof Error && error . stack ) {
95114 console . log ( error . stack ) ;
96115 }
97- // Best-effort cleanup
98- if ( modelToLoad ) {
99- try { await modelToLoad . unload ( ) ; } catch { /* ignore */ }
100- }
101116 process . exit ( 1 ) ;
102117 }
103118}
104119
105120// Run the example
106121main ( ) . catch ( console . error ) ;
107122
108- export { main } ;
123+ export { main } ;
0 commit comments