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

How to implment access token or other form of authorization

Open PhilipAmadasun opened this issue 1 year ago • 3 comments

Is it possible to implement access tokens with this library? I'm basically looking for a way to implement authorization.

PhilipAmadasun avatar Jun 12 '24 03:06 PhilipAmadasun

I was trying to do that using headers to pass the API Key: client = Client(host='https://localhost:11434',headers=["x-token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"])

But after that I get a different error: File "C:\Python312\Lib\site-packages\ollama_client.py", line 67, in init super().init(httpx.Client, host, **kwargs) File "C:\Python312\Lib\site-packages\ollama_client.py", line 52, in init headers['Content-Type'] = 'application/json' ~~~~~~~^^^^^^^^^^^^^^^^ TypeError: list indices must be integers or slices, not str

ajrodrigues avatar Sep 26 '24 15:09 ajrodrigues

I was wondering the same thing and was able to get it work using the example below:

import ollama
import os

url = "https://<Ollama URL>"
api_key = os.getenv("OLLAMA_API_KEY")
model_name = "llama3.2"
headers = {
    "Authorization": f"Bearer {api_key}"
}

client = ollama.Client(host=url, headers=headers)

response = client.generate(model=model_name, prompt="Why is the sky blue?")

print(response)

I hope this helps someone in the future!

westbrook-ai avatar Feb 24 '25 15:02 westbrook-ai

Many thanks to @westbrook-ai for sharing that code snippet! 🙏

If someone wants to implement this Authorization feature with a Cloudflare Tunnel, you can point it to a local reverse proxy. For example, you can use something like this reverse-proxy

# .env file
LISTEN_PORT=8011        # Port for the reverse proxy application (Python script)
BACKEND_URL="http://localhost:11434"  # Your Ollama server
BEARER_TOKEN="my-secret-token"        # Authorization token
TIMEOUT=60             # Response timeout in seconds

Image

With this setup, you can secure your Ollama server over the internet using an authorization token.

ramibch avatar Nov 05 '25 15:11 ramibch