1
1
import * as utils from './utils.js'
2
- import { AbortableAsyncIterator , parseJSON , post } from './utils.js'
2
+ import { AbortableAsyncIterator , parseJSON } from './utils.js'
3
3
import 'whatwg-fetch'
4
4
5
5
import type {
@@ -34,15 +34,14 @@ export class Ollama {
34
34
constructor ( config ?: Partial < Config > ) {
35
35
this . config = {
36
36
host : '' ,
37
+ headers : config ?. headers
37
38
}
39
+
38
40
if ( ! config ?. proxy ) {
39
41
this . config . host = utils . formatHost ( config ?. host ?? 'http://127.0.0.1:11434' )
40
42
}
41
43
42
- this . fetch = fetch
43
- if ( config ?. fetch != null ) {
44
- this . fetch = config . fetch
45
- }
44
+ this . fetch = config ?. fetch ?? fetch
46
45
}
47
46
48
47
// Abort any ongoing streamed requests to Ollama
@@ -72,7 +71,7 @@ export class Ollama {
72
71
const host = `${ this . config . host } /api/${ endpoint } `
73
72
if ( request . stream ) {
74
73
const abortController = new AbortController ( )
75
- const response = await post ( this . fetch , host , request , {
74
+ const response = await utils . post ( this . fetch , host , request , {
76
75
signal : abortController . signal ,
77
76
headers : this . config . headers
78
77
} )
@@ -236,9 +235,12 @@ async encodeImage(image: Uint8Array | string): Promise<string> {
236
235
* @returns {Promise<StatusResponse> } - The response object.
237
236
*/
238
237
async delete ( request : DeleteRequest ) : Promise < StatusResponse > {
239
- await utils . del ( this . fetch , `${ this . config . host } /api/delete` , {
240
- name : request . model ,
241
- } )
238
+ await utils . del (
239
+ this . fetch ,
240
+ `${ this . config . host } /api/delete` ,
241
+ { name : request . model } ,
242
+ { headers : this . config . headers }
243
+ )
242
244
return { status : 'success' }
243
245
}
244
246
@@ -249,7 +251,9 @@ async encodeImage(image: Uint8Array | string): Promise<string> {
249
251
* @returns {Promise<StatusResponse> } - The response object.
250
252
*/
251
253
async copy ( request : CopyRequest ) : Promise < StatusResponse > {
252
- await utils . post ( this . fetch , `${ this . config . host } /api/copy` , { ...request } )
254
+ await utils . post ( this . fetch , `${ this . config . host } /api/copy` , { ...request } , {
255
+ headers : this . config . headers
256
+ } )
253
257
return { status : 'success' }
254
258
}
255
259
@@ -259,7 +263,9 @@ async encodeImage(image: Uint8Array | string): Promise<string> {
259
263
* @throws {Error } - If the response body is missing.
260
264
*/
261
265
async list ( ) : Promise < ListResponse > {
262
- const response = await utils . get ( this . fetch , `${ this . config . host } /api/tags` )
266
+ const response = await utils . get ( this . fetch , `${ this . config . host } /api/tags` , {
267
+ headers : this . config . headers
268
+ } )
263
269
return ( await response . json ( ) ) as ListResponse
264
270
}
265
271
@@ -271,6 +277,8 @@ async encodeImage(image: Uint8Array | string): Promise<string> {
271
277
async show ( request : ShowRequest ) : Promise < ShowResponse > {
272
278
const response = await utils . post ( this . fetch , `${ this . config . host } /api/show` , {
273
279
...request ,
280
+ } , {
281
+ headers : this . config . headers
274
282
} )
275
283
return ( await response . json ( ) ) as ShowResponse
276
284
}
@@ -283,6 +291,8 @@ async encodeImage(image: Uint8Array | string): Promise<string> {
283
291
async embed ( request : EmbedRequest ) : Promise < EmbedResponse > {
284
292
const response = await utils . post ( this . fetch , `${ this . config . host } /api/embed` , {
285
293
...request ,
294
+ } , {
295
+ headers : this . config . headers
286
296
} )
287
297
return ( await response . json ( ) ) as EmbedResponse
288
298
}
@@ -295,6 +305,8 @@ async encodeImage(image: Uint8Array | string): Promise<string> {
295
305
async embeddings ( request : EmbeddingsRequest ) : Promise < EmbeddingsResponse > {
296
306
const response = await utils . post ( this . fetch , `${ this . config . host } /api/embeddings` , {
297
307
...request ,
308
+ } , {
309
+ headers : this . config . headers
298
310
} )
299
311
return ( await response . json ( ) ) as EmbeddingsResponse
300
312
}
@@ -305,7 +317,9 @@ async encodeImage(image: Uint8Array | string): Promise<string> {
305
317
* @throws {Error } - If the response body is missing.
306
318
*/
307
319
async ps ( ) : Promise < ListResponse > {
308
- const response = await utils . get ( this . fetch , `${ this . config . host } /api/ps` )
320
+ const response = await utils . get ( this . fetch , `${ this . config . host } /api/ps` , {
321
+ headers : this . config . headers
322
+ } )
309
323
return ( await response . json ( ) ) as ListResponse
310
324
}
311
325
}
0 commit comments