How to implment access token or other form of authorization
Is it possible to implement access tokens with this library? I'm basically looking for a way to implement authorization.
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
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!
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
With this setup, you can secure your Ollama server over the internet using an authorization token.