ollama / ollama-js

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

Added optional headers to Ollama initialisation #138

Closed ronneldavis closed 2 months ago

ronneldavis commented 2 months ago

This continues on the work done by @kpotter-m2 from this issue: https://github.com/ollama/ollama-js/pull/70

Usage example:

import {Ollama} from "ollama";

const myHeaders = new Headers();
myHeaders.append("Authorization", "basic");

const ollama = new Ollama({
  headers: myHeaders
}):
ronneldavis commented 2 months ago

@BruceMacD is this good enough to be merged?

felixdrp commented 1 month ago

how do you use the headers? I'm using the version 0.5.9

Does it needs something like? ollama-js/src /browser.ts

  constructor(config?: Partial<Config>) {
    this.config = {
      host: '',
    }
    if (!config?.proxy) {
      this.config.host = utils.formatHost(config?.host ?? 'http://127.0.0.1:11434')
    }

+    if (!config?.headers {
+     this.config.headers = config.headers
+   }

    this.fetch = fetch
    if (config?.fetch != null) {
      this.fetch = config.fetch
    }
  }
NeuroWhAI commented 1 month ago

headers option is still not working.

NeuroWhAI commented 1 month ago

@felixdrp Here's my solution.

  return new Ollama({
    host: env.OLLAMA_URL,
    fetch: (
      input: string | URL | globalThis.Request,
      init?: RequestInit,
    ): Promise<Response> => {
      if (!init) {
        init = {};
      }
      if (!init.headers) {
        init.headers = {};
      }
      init.headers['Authorization'] = `Bearer ${env.OLLAMA_KEY}`;
      return fetch(input, init);
    },
  });