Skip to content

Commit d864270

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

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

ollama/_client.py

Lines changed: 15 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,13 @@ def web_search(self, queries: Sequence[str], max_results: int = 3) -> WebSearchR
638641
639642
Returns:
640643
WebSearchResponse with the search results
644+
Raises:
645+
ValueError: If OLLAMA_API_KEY environment variable is not set
641646
"""
647+
api_key = os.getenv('OLLAMA_API_KEY')
648+
if not api_key:
649+
raise ValueError('OLLAMA_API_KEY environment variable is required for web search')
650+
642651
return self._request(
643652
WebSearchResponse,
644653
'POST',
@@ -658,7 +667,13 @@ def web_crawl(self, urls: Sequence[str]) -> WebCrawlResponse:
658667
659668
Returns:
660669
WebCrawlResponse with the crawl results
670+
Raises:
671+
ValueError: If OLLAMA_API_KEY environment variable is not set
661672
"""
673+
api_key = os.getenv('OLLAMA_API_KEY')
674+
if not api_key:
675+
raise ValueError('OLLAMA_API_KEY environment variable is required for web fetch')
676+
662677
return self._request(
663678
WebCrawlResponse,
664679
'POST',

0 commit comments

Comments
 (0)