changes
This commit is contained in:
+42
-7
@@ -1,12 +1,47 @@
|
|||||||
import { Ollama } from 'ollama';
|
import { Ollama } from 'ollama';
|
||||||
|
|
||||||
// Initialize Ollama with the host from environment variables
|
// Singleton holder
|
||||||
// defaulting to localhost if not specified.
|
let ollamaInstance: Ollama | null = null;
|
||||||
// This ensures that OLLAMA_HOST is respected.
|
|
||||||
const host = process.env.OLLAMA_HOST || 'http://127.0.0.1:11434';
|
|
||||||
|
|
||||||
console.log(`Initializing Ollama client with host: ${host}`);
|
function getOllamaInstance(): Ollama {
|
||||||
|
if (!ollamaInstance) {
|
||||||
|
// DEBUGGING: Log the environment variable explicitly on first use
|
||||||
|
const envHost = process.env.OLLAMA_HOST;
|
||||||
|
console.log('----------------------------------------');
|
||||||
|
console.log('[lib/ollama.ts] Initializing Ollama instance (Lazy)');
|
||||||
|
console.log(`[lib/ollama.ts] process.env.OLLAMA_HOST: '${envHost}'`);
|
||||||
|
console.log(`[lib/ollama.ts] NODE_ENV: '${process.env.NODE_ENV}'`);
|
||||||
|
console.log('----------------------------------------');
|
||||||
|
|
||||||
const ollama = new Ollama({ host });
|
// Initialize Ollama with the host from environment variables
|
||||||
|
// defaulting to localhost if not specified.
|
||||||
|
const host = envHost || 'http://127.0.0.1:11434';
|
||||||
|
|
||||||
export default ollama;
|
if (!envHost) {
|
||||||
|
console.warn(
|
||||||
|
'[lib/ollama.ts] WARNING: OLLAMA_HOST is not set. Defaulting to 127.0.0.1:11434. If your Ollama instance is on another machine, this will fail.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`[lib/ollama.ts] Final Host used for Ollama: ${host}`);
|
||||||
|
ollamaInstance = new Ollama({ host });
|
||||||
|
}
|
||||||
|
return ollamaInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Proxy to ensure we initialize lazily when any property is accessed
|
||||||
|
const ollamaProxy = new Proxy({} as Ollama, {
|
||||||
|
get(_target, prop) {
|
||||||
|
const instance = getOllamaInstance();
|
||||||
|
// @ts-ignore
|
||||||
|
const value = instance[prop];
|
||||||
|
|
||||||
|
// Bind functions to the instance to ensure 'this' context is correct
|
||||||
|
if (typeof value === 'function') {
|
||||||
|
return value.bind(instance);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default ollamaProxy;
|
||||||
|
|||||||
Reference in New Issue
Block a user