ollama-js icon indicating copy to clipboard operation
ollama-js copied to clipboard

Cors errors when using ollama/browser

Open POBGit opened this issue 2 months ago • 2 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?

POBGit avatar Apr 11 '24 19:04 POBGit

Thanks @POBGit your solution fixed my issue.

j2l avatar Apr 16 '24 15:04 j2l

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 🤔

monotykamary avatar May 04 '24 14:05 monotykamary