Issue with calling a local Ollama API from Chrome Extension

1.6k views Asked by At

I need your help. I'm currently developing a Chrome Extension, that will trigger a API call when clicking a button. The api that I want to call is a local instance of Ollama (https://ollama.ai/), that provides multiple API's.

When calling the api from my chrome extensions, I get an 403-Forbidden Error.

Function that I call:

function generateCodeSuggestionFromOllama(prompt) {
    const apiUrl = `http://localhost:11434/api/generate`;
    const body = {
        "model": "codellama",
        "prompt": prompt,
        "stream": false,
    };
    fetch(apiUrl, {
        method: 'POST',
        body: JSON.stringify(body),
    })
        .then(response => {
            console.error(response);
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            return response.text();  // change this line
        })
        .then(data => {
            // Check if the response is empty
            if (!data) {
                throw new Error('Empty response from server');
            }
            // Parse the JSON data
            const jsonData = JSON.parse(data);
            console.log(jsonData);
            const response = jsonData.response;
            document.getElementById("codeSuggestion").innerHTML = response;
        })
}

manifest.json

{
  "manifest_version": 3,
  "name": "Codegenerator",
  "description": "A Codegenerator",
  "version": "1.0",
  "permissions": [
    "tabs",
    "activeTab",
    "http://localhost:11434/*",
    "http://127.0.0.1:11434/*",
    "<all_urls>",
    "scripting"
  ],
  "host_permissions": ["http://localhost:11434/*", "http://127.0.0.1:11434/*"],
  "action": {
    "default_popup": "index.html",
    "default_icon": "logo.png"
  }
}

I think I defined all required permissions correctly, but I'm not sure. My Ollama Service runs on Port 11434.

My guess is, that I have to configure the Ollama service to allow requests from the outside, but I also don't know how to do it.

A maybe useful Documentation (https://github.com/ollama/ollama/blob/main/docs/faq.md#how-do-i-configure-ollama-server)

Any guess, what I should do?

Thanks in advance ;)

2

There are 2 answers

0
DevChris On BEST ANSWER

So I solved the issue with setting the OLLAMA_ORIGIN to allow requests from chrome-extension origin, which is not allowed from the default settings. I started the ollama service with following command:

OLLAMA_ORIGINS=chrome-extension://* ollama serve

But I also changed the host_permission like @wOxxOm recommended in the manifest.json file.

Hope it helps for people who are facing the same issue :)

1
R0CKSTAR On

Update the host permissions section of manifest.json.

"host_permissions": [
    "http://localhost/*"
]