Skip to content

Commit 563f05b

Browse files
committed
client: load OLLAMA_API_KEY on init
1 parent b22c5fd commit 563f05b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

ollama/_client.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def __init__(
9494
`kwargs` are passed to the httpx client.
9595
"""
9696

97+
api_key = os.getenv('OLLAMA_API_KEY', None)
98+
9799
self._client = client(
98100
base_url=_parse_host(host or os.getenv('OLLAMA_HOST')),
99101
follow_redirects=follow_redirects,
@@ -106,6 +108,7 @@ def __init__(
106108
'Content-Type': 'application/json',
107109
'Accept': 'application/json',
108110
'User-Agent': f'ollama-python/{__version__} ({platform.machine()} {platform.system().lower()}) Python/{platform.python_version()}',
111+
'Authorization': f'Bearer {api_key}' if api_key else "",
109112
}.items()
110113
},
111114
**kwargs,
@@ -638,7 +641,14 @@ def web_search(self, queries: Sequence[str], max_results: int = 3) -> WebSearchR
638641
639642
Returns:
640643
WebSearchResponse with the search results
644+
645+
Raises:
646+
ValueError: If OLLAMA_API_KEY environment variable is not set
641647
"""
648+
api_key = os.getenv('OLLAMA_API_KEY')
649+
if not api_key:
650+
raise ValueError('OLLAMA_API_KEY environment variable is required for web search')
651+
642652
return self._request(
643653
WebSearchResponse,
644654
'POST',
@@ -658,7 +668,14 @@ def web_crawl(self, urls: Sequence[str]) -> WebCrawlResponse:
658668
659669
Returns:
660670
WebCrawlResponse with the crawl results
671+
672+
Raises:
673+
ValueError: If OLLAMA_API_KEY environment variable is not set
661674
"""
675+
api_key = os.getenv('OLLAMA_API_KEY')
676+
if not api_key:
677+
raise ValueError('OLLAMA_API_KEY environment variable is required for web fetch')
678+
662679
return self._request(
663680
WebCrawlResponse,
664681
'POST',

0 commit comments

Comments
 (0)