@@ -31,7 +31,11 @@ class OllamaAILLM {
3131 const headers = this . authToken
3232 ? { Authorization : `Bearer ${ this . authToken } ` }
3333 : { } ;
34- this . client = new Ollama ( { host : this . basePath , headers : headers } ) ;
34+ this . client = new Ollama ( {
35+ host : this . basePath ,
36+ headers : headers ,
37+ fetch : this . #applyFetch( ) ,
38+ } ) ;
3539 this . embedder = embedder ?? new NativeEmbedder ( ) ;
3640 this . defaultTemp = 0.7 ;
3741 this . #log(
@@ -55,6 +59,43 @@ class OllamaAILLM {
5559 ) ;
5660 }
5761
62+ /**
63+ * Apply a custom fetch function to the Ollama client.
64+ * This is useful when we want to bypass the default 5m timeout for global fetch
65+ * for machines which run responses very slowly.
66+ * @returns {Function } The custom fetch function.
67+ */
68+ #applyFetch( ) {
69+ try {
70+ if ( ! ( "OLLAMA_RESPONSE_TIMEOUT" in process . env ) ) return fetch ;
71+ const { Agent } = require ( "undici" ) ;
72+ const moment = require ( "moment" ) ;
73+ let timeout = process . env . OLLAMA_RESPONSE_TIMEOUT ;
74+
75+ if ( ! timeout || isNaN ( Number ( timeout ) ) || Number ( timeout ) <= 5 * 60_000 ) {
76+ this . #log(
77+ "Timeout option was not set, is not a number, or is less than 5 minutes in ms - falling back to default" ,
78+ { timeout }
79+ ) ;
80+ return fetch ;
81+ } else timeout = Number ( timeout ) ;
82+
83+ const noTimeoutFetch = ( input , init = { } ) => {
84+ return fetch ( input , {
85+ ...init ,
86+ dispatcher : new Agent ( { headersTimeout : timeout } ) ,
87+ } ) ;
88+ } ;
89+
90+ const humanDiff = moment . duration ( timeout ) . humanize ( ) ;
91+ this . #log( `Applying custom fetch w/timeout of ${ humanDiff } .` ) ;
92+ return noTimeoutFetch ;
93+ } catch ( error ) {
94+ this . #log( "Error applying custom fetch - using default fetch" , error ) ;
95+ return fetch ;
96+ }
97+ }
98+
5899 streamingEnabled ( ) {
59100 return "streamGetChatCompletion" in this ;
60101 }
0 commit comments