ollama / ollama-js

Ollama JavaScript library
https://ollama.com
MIT License
2.27k stars 178 forks source link

enable ollama-js use in an environment without whatwg #125

Closed BruceMacD closed 3 months ago

BruceMacD commented 3 months ago

When trying to vendor ollama-js into a basic JavaScript project I hit an issue that this import of whatwg-fetch not being simple to resolve. This change allows for setting the fetch library used by Ollama in an environment where whatwg-fetch is not present.

Example:

async function initOllama() {
  try {
    const ollamaModule = await import('/public/vendor/ollama/dist/browser.mjs');
    // Create an instance of Ollama with a custom fetch implementation
    const ollama = new ollamaModule.Ollama({
      fetch: window.fetch
    });
    return ollama;
  } catch (error) {
    console.error('Error initializing Ollama:', error);
    throw error;
  }
}