ollama / ollama-js

Ollama JavaScript library

Home Page:https://ollama.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cors errors when using ollama/browser

POBGit opened this issue · comments

I'm trying to use ollama-js/browser in my application writing in TS. I'm running this app on my computer, with the ollama server and apache. I created a vhost, so I can access my app at this url http://dev.my-app.local/, which points to 127.0.0.1. I bundled my code with webpack.

The problem is that I'm trying to run this simple code, but I have CORS errors (204 error code) :

import {Ollama} from 'ollama/dist/browser.mjs'

async function run() {
    const ollama = new Ollama({host: 'http://localhost:11434'})

    const response = await ollama.generate({
        model: 'mistral',
        prompt: "Why is the sky blue?",
        stream: true
    });

    console.log(response);
}

run();

I read in the official ollama docs that I need to set up environment variables OLLAMA_HOST=* and OLLAMA_ORIGINS=*, but it does not fix my issue.

I then tried with this simpler code, and I don't have CORS errors.

const request = fetch(URL, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        model: 'mistral',
        prompt: "Why is the sky blue?",
    }),
});

Any idea?

Thanks @POBGit your solution fixed my issue.

launchctl setenv OLLAMA_HOST "*"   
launchctl setenv OLLAMA_HOST "0.0.0.0"

Running these commands and restarting Ollama seems to work fine on my end on my M2 mac 🤔

I believe this should be fixed now with most recent Ollama release, it needed to allow more headers cross-origin. If that isn't the case please let me know.