Skip to content

Commit 9af468d

Browse files
committed
feat(ollama): Support Olama API key authentication
1 parent 920e6f4 commit 9af468d

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

auto-configurations/models/spring-ai-autoconfigure-model-ollama/src/main/java/org/springframework/ai/model/ollama/autoconfigure/OllamaApiAutoConfiguration.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.boot.restclient.autoconfigure.RestClientAutoConfiguration;
2727
import org.springframework.boot.webclient.autoconfigure.WebClientAutoConfiguration;
2828
import org.springframework.context.annotation.Bean;
29+
import org.springframework.util.StringUtils;
2930
import org.springframework.web.client.ResponseErrorHandler;
3031
import org.springframework.web.client.RestClient;
3132
import org.springframework.web.reactive.function.client.WebClient;
@@ -56,10 +57,21 @@ PropertiesOllamaConnectionDetails ollamaConnectionDetails(OllamaConnectionProper
5657
public OllamaApi ollamaApi(OllamaConnectionDetails connectionDetails,
5758
ObjectProvider<RestClient.Builder> restClientBuilderProvider,
5859
ObjectProvider<WebClient.Builder> webClientBuilderProvider, ResponseErrorHandler responseErrorHandler) {
60+
61+
RestClient.Builder restClientBuilder = restClientBuilderProvider.getIfAvailable(RestClient::builder);
62+
WebClient.Builder webClientBuilder = webClientBuilderProvider.getIfAvailable(WebClient::builder);
63+
64+
// Add Authorization header if API key is provided
65+
if (StringUtils.hasText(connectionDetails.getApiKey())) {
66+
String bearerToken = "Bearer " + connectionDetails.getApiKey();
67+
restClientBuilder = restClientBuilder.defaultHeader("Authorization", bearerToken);
68+
webClientBuilder = webClientBuilder.defaultHeader("Authorization", bearerToken);
69+
}
70+
5971
return OllamaApi.builder()
6072
.baseUrl(connectionDetails.getBaseUrl())
61-
.restClientBuilder(restClientBuilderProvider.getIfAvailable(RestClient::builder))
62-
.webClientBuilder(webClientBuilderProvider.getIfAvailable(WebClient::builder))
73+
.restClientBuilder(restClientBuilder)
74+
.webClientBuilder(webClientBuilder)
6375
.responseErrorHandler(responseErrorHandler)
6476
.build();
6577
}
@@ -77,6 +89,11 @@ public String getBaseUrl() {
7789
return this.properties.getBaseUrl();
7890
}
7991

92+
@Override
93+
public String getApiKey() {
94+
return this.properties.getApiKey();
95+
}
96+
8097
}
8198

8299
}

auto-configurations/models/spring-ai-autoconfigure-model-ollama/src/main/java/org/springframework/ai/model/ollama/autoconfigure/OllamaConnectionDetails.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,12 @@ public interface OllamaConnectionDetails extends ConnectionDetails {
2727

2828
String getBaseUrl();
2929

30+
/**
31+
* Returns the API key for authenticating requests.
32+
* @return the API key, or null if no authentication is required
33+
*/
34+
default String getApiKey() {
35+
return null;
36+
}
37+
3038
}

auto-configurations/models/spring-ai-autoconfigure-model-ollama/src/main/java/org/springframework/ai/model/ollama/autoconfigure/OllamaConnectionProperties.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ public class OllamaConnectionProperties {
3434
*/
3535
private String baseUrl = "http://localhost:11434";
3636

37+
/**
38+
* API key for authenticating requests to Ollama API server.
39+
* If provided, it will be sent as a Bearer token in the Authorization header.
40+
*/
41+
private String apiKey;
42+
3743
public String getBaseUrl() {
3844
return this.baseUrl;
3945
}
@@ -42,4 +48,12 @@ public void setBaseUrl(String baseUrl) {
4248
this.baseUrl = baseUrl;
4349
}
4450

51+
public String getApiKey() {
52+
return this.apiKey;
53+
}
54+
55+
public void setApiKey(String apiKey) {
56+
this.apiKey = apiKey;
57+
}
58+
4559
}

0 commit comments

Comments
 (0)