@@ -73,18 +73,20 @@ export class GPTScript {
73
73
74
74
75
75
private ready : boolean
76
+ private opts : GlobalOpts
76
77
77
78
constructor ( opts ?: GlobalOpts ) {
79
+ this . opts = opts || { }
78
80
this . ready = false
79
81
GPTScript . instanceCount ++
80
82
if ( ! GPTScript . serverURL ) {
81
83
GPTScript . serverURL = "http://" + ( process . env . GPTSCRIPT_URL || "127.0.0.1:0" )
82
84
}
83
85
if ( GPTScript . instanceCount === 1 && process . env . GPTSCRIPT_DISABLE_SERVER !== "true" ) {
84
86
let env = process . env
85
- if ( opts && opts . Env ) {
87
+ if ( this . opts . Env ) {
86
88
env = { }
87
- for ( const v of opts . Env ) {
89
+ for ( const v of this . opts . Env ) {
88
90
const equalIndex = v . indexOf ( "=" )
89
91
if ( equalIndex === - 1 ) {
90
92
env [ v ] = ""
@@ -94,7 +96,7 @@ export class GPTScript {
94
96
}
95
97
}
96
98
97
- globalOptsToEnv ( env , opts )
99
+ globalOptsToEnv ( env , this . opts )
98
100
process . on ( "exit" , ( code ) => {
99
101
if ( GPTScript . serverProcess ) {
100
102
GPTScript . serverProcess . stdin ?. end ( )
@@ -133,20 +135,30 @@ export class GPTScript {
133
135
return this . runBasicCommand ( "list-tools" )
134
136
}
135
137
136
- listModels ( ) : Promise < string > {
137
- return this . runBasicCommand ( "list-models" )
138
+ listModels ( providers ?: string [ ] , credentialOverrides ?: string [ ] ) : Promise < string > {
139
+ if ( this . opts . DefaultModelProvider ) {
140
+ if ( ! providers ) {
141
+ providers = [ ]
142
+ }
143
+ providers . push ( this . opts . DefaultModelProvider )
144
+ }
145
+ return this . runBasicCommand ( "list-models" , {
146
+ "providers" : providers ,
147
+ "env" : this . opts . Env ,
148
+ "credentialOverrides" : credentialOverrides
149
+ } )
138
150
}
139
151
140
152
version ( ) : Promise < string > {
141
153
return this . runBasicCommand ( "version" )
142
154
}
143
155
144
- async runBasicCommand ( cmd : string ) : Promise < string > {
156
+ async runBasicCommand ( cmd : string , body ?: any ) : Promise < string > {
145
157
if ( ! this . ready ) {
146
158
this . ready = await this . testGPTScriptURL ( 20 )
147
159
}
148
160
const r = new RunSubcommand ( cmd , "" , { } , GPTScript . serverURL )
149
- r . requestNoStream ( null )
161
+ r . requestNoStream ( body )
150
162
return r . text ( )
151
163
}
152
164
@@ -161,7 +173,8 @@ export class GPTScript {
161
173
if ( ! this . ready ) {
162
174
this . ready = await this . testGPTScriptURL ( 20 )
163
175
}
164
- return ( new Run ( "run" , toolName , opts , GPTScript . serverURL ) ) . nextChat ( opts . input )
176
+
177
+ return ( new Run ( "run" , toolName , { ...this . opts , ...opts } , GPTScript . serverURL ) ) . nextChat ( opts . input )
165
178
}
166
179
167
180
/**
@@ -176,7 +189,7 @@ export class GPTScript {
176
189
this . ready = await this . testGPTScriptURL ( 20 )
177
190
}
178
191
179
- return ( new Run ( "evaluate" , tool , opts , GPTScript . serverURL ) ) . nextChat ( opts . input )
192
+ return ( new Run ( "evaluate" , tool , { ... this . opts , ... opts } , GPTScript . serverURL ) ) . nextChat ( opts . input )
180
193
}
181
194
182
195
async parse ( fileName : string , disableCache ?: boolean ) : Promise < Block [ ] > {
@@ -265,7 +278,7 @@ export class GPTScript {
265
278
disableCache ?: boolean ,
266
279
subTool ?: string
267
280
) : Promise < LoadResponse > {
268
- return this . _load ( { file : fileName , disableCache, subTool } ) ;
281
+ return this . _load ( { file : fileName , disableCache, subTool} )
269
282
}
270
283
271
284
/**
@@ -281,7 +294,7 @@ export class GPTScript {
281
294
disableCache ?: boolean ,
282
295
subTool ?: string
283
296
) : Promise < LoadResponse > {
284
- return this . _load ( { content, disableCache, subTool } ) ;
297
+ return this . _load ( { content, disableCache, subTool} )
285
298
}
286
299
287
300
/**
@@ -297,7 +310,7 @@ export class GPTScript {
297
310
disableCache ?: boolean ,
298
311
subTool ?: string
299
312
) : Promise < LoadResponse > {
300
- return this . _load ( { toolDefs, disableCache, subTool } ) ;
313
+ return this . _load ( { toolDefs, disableCache, subTool} )
301
314
}
302
315
303
316
/**
@@ -308,12 +321,12 @@ export class GPTScript {
308
321
*/
309
322
private async _load ( payload : any ) : Promise < LoadResponse > {
310
323
if ( ! this . ready ) {
311
- this . ready = await this . testGPTScriptURL ( 20 ) ;
324
+ this . ready = await this . testGPTScriptURL ( 20 )
312
325
}
313
- const r : Run = new RunSubcommand ( "load" , payload . toolDefs || [ ] , { } , GPTScript . serverURL ) ;
326
+ const r : Run = new RunSubcommand ( "load" , payload . toolDefs || [ ] , { } , GPTScript . serverURL )
314
327
315
- r . request ( payload ) ;
316
- return ( await r . json ( ) ) as LoadResponse ;
328
+ r . request ( payload )
329
+ return ( await r . json ( ) ) as LoadResponse
317
330
}
318
331
319
332
private async testGPTScriptURL ( count : number ) : Promise < boolean > {
@@ -511,12 +524,16 @@ export class Run {
511
524
512
525
const options = this . requestOptions ( this . gptscriptURL , this . requestPath , tool ) as any
513
526
if ( tool ) {
514
- options . body = { ...tool , ...this . opts }
527
+ options . body = JSON . stringify ( { ...tool , ...this . opts } )
515
528
}
516
529
const req = new Request ( this . gptscriptURL + "/" + this . requestPath , options )
517
530
518
531
this . promise = new Promise < string > ( async ( resolve , reject ) => {
519
- fetch ( req ) . then ( resp => resp . json ( ) ) . then ( res => resolve ( res . stdout ) ) . catch ( e => {
532
+ fetch ( req ) . then ( resp => {
533
+ return resp . json ( )
534
+ } ) . then ( res => {
535
+ resolve ( res . stdout )
536
+ } ) . catch ( e => {
520
537
reject ( e )
521
538
} )
522
539
} )
0 commit comments