Skip to content

Ollama: Support custom Ollama API paths #3470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* @author Eddú Meléndez
* @author Thomas Vitale
* @author Ilayaperumal Gopinathan
* @author lambochen
* @since 0.8.0
*/
@AutoConfiguration
Expand All @@ -53,6 +54,13 @@ public OllamaApi ollamaApi(OllamaConnectionDetails connectionDetails,
ObjectProvider<WebClient.Builder> webClientBuilderProvider) {
return OllamaApi.builder()
.baseUrl(connectionDetails.getBaseUrl())
.chatPath(connectionDetails.getChatPath())
.embedPath(connectionDetails.getEmbedPath())
.listModelsPath(connectionDetails.getListModelsPath())
.showModelPath(connectionDetails.getShowModelPath())
.copyModelPath(connectionDetails.getCopyModelPath())
.deleteModelPath(connectionDetails.getDeleteModelPath())
.pullModelPath(connectionDetails.getPullModelPath())
.restClientBuilder(restClientBuilderProvider.getIfAvailable(RestClient::builder))
.webClientBuilder(webClientBuilderProvider.getIfAvailable(WebClient::builder))
.build();
Expand All @@ -71,6 +79,41 @@ public String getBaseUrl() {
return this.properties.getBaseUrl();
}

@Override
public String getChatPath() {
return this.properties.getChatPath();
}

@Override
public String getEmbedPath() {
return this.properties.getEmbedPath();
}

@Override
public String getListModelsPath() {
return this.properties.getListModelsPath();
}

@Override
public String getShowModelPath() {
return this.properties.getShowModelPath();
}

@Override
public String getCopyModelPath() {
return this.properties.getCopyModelPath();
}

@Override
public String getDeleteModelPath() {
return this.properties.getDeleteModelPath();
}

@Override
public String getPullModelPath() {
return this.properties.getPullModelPath();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,24 @@
* Connection details for an Ollama service.
*
* @author Eddú Meléndez
* @author lambochen
*/
public interface OllamaConnectionDetails extends ConnectionDetails {

String getBaseUrl();

String getChatPath();

String getEmbedPath();

String getListModelsPath();

String getShowModelPath();

String getCopyModelPath();

String getDeleteModelPath();

String getPullModelPath();

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

package org.springframework.ai.model.ollama.autoconfigure;

import org.springframework.ai.ollama.api.common.OllamaApiConstants;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* Ollama connection autoconfiguration properties.
*
* @author Christian Tzolov
* @author lambochen
* @since 0.8.0
*/
@ConfigurationProperties(OllamaConnectionProperties.CONFIG_PREFIX)
Expand All @@ -32,7 +34,42 @@ public class OllamaConnectionProperties {
/**
* Base URL where Ollama API server is running.
*/
private String baseUrl = "http://localhost:11434";
private String baseUrl = OllamaApiConstants.DEFAULT_BASE_URL;

/**
* The path of the chat endpoint.
*/
private String chatPath = OllamaApiConstants.DEFAULT_CHAT_PATH;

/**
* The path of the embed endpoint.
*/
private String embedPath = OllamaApiConstants.DEFAULT_EMBED_PATH;

/**
* The path of the list models endpoint.
*/
private String listModelsPath = OllamaApiConstants.DEFAULT_LIST_MODELS_PATH;

/**
* The path of the show model endpoint.
*/
private String showModelPath = OllamaApiConstants.DEFAULT_SHOW_MODEL_PATH;

/**
* The path of the copy model endpoint.
*/
private String copyModelPath = OllamaApiConstants.DEFAULT_COPY_MODEL_PATH;

/**
* The path of the delete model endpoint.
*/
private String deleteModelPath = OllamaApiConstants.DEFAULT_DELETE_MODEL_PATH;

/**
* The path of the pull model endpoint.
*/
private String pullModelPath = OllamaApiConstants.DEFAULT_PULL_MODEL_PATH;

public String getBaseUrl() {
return this.baseUrl;
Expand All @@ -42,4 +79,60 @@ public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}

public String getChatPath() {
return chatPath;
}

public void setChatPath(String chatPath) {
this.chatPath = chatPath;
}

public String getEmbedPath() {
return embedPath;
}

public void setEmbedPath(String embedPath) {
this.embedPath = embedPath;
}

public String getListModelsPath() {
return listModelsPath;
}

public void setListModelsPath(String listModelsPath) {
this.listModelsPath = listModelsPath;
}

public String getShowModelPath() {
return showModelPath;
}

public void setShowModelPath(String showModelPath) {
this.showModelPath = showModelPath;
}

public String getCopyModelPath() {
return copyModelPath;
}

public void setCopyModelPath(String copyModelPath) {
this.copyModelPath = copyModelPath;
}

public String getDeleteModelPath() {
return deleteModelPath;
}

public void setDeleteModelPath(String deleteModelPath) {
this.deleteModelPath = deleteModelPath;
}

public String getPullModelPath() {
return pullModelPath;
}

public void setPullModelPath(String pullModelPath) {
this.pullModelPath = pullModelPath;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.model.ollama.autoconfigure;

import org.junit.jupiter.api.Test;
import org.springframework.ai.ollama.api.OllamaApi;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

/**
* @author lambochen
*/
public class OllamaApiBuilderTests {

@Test
void fullBuilder() {
OllamaApi ollamaApi = OllamaApi.builder()
.baseUrl("http://localhost:11434")
.chatPath("/api/chat")
.embedPath("/api/embeddings")
.listModelsPath("/api/tags")
.showModelPath("/api/show")
.copyModelPath("/api/copy")
.deleteModelPath("/api/delete")
.pullModelPath("/api/pull")
.build();

assertThat(ollamaApi).isNotNull();
}

@Test
void defaultBuilder() {
OllamaApi ollamaApi = OllamaApi.builder().build();
assertThat(ollamaApi).isNotNull();
}

@Test
void invalidPath() {
assertThatThrownBy(() -> OllamaApi.builder().chatPath("").build()).isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> OllamaApi.builder().embedPath("").build())
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> OllamaApi.builder().listModelsPath("").build())
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> OllamaApi.builder().showModelPath("").build())
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> OllamaApi.builder().copyModelPath("").build())
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> OllamaApi.builder().deleteModelPath("").build())
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> OllamaApi.builder().pullModelPath("").build())
.isInstanceOf(IllegalArgumentException.class);

assertThatThrownBy(() -> OllamaApi.builder().chatPath(null).build())
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> OllamaApi.builder().embedPath(null).build())
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> OllamaApi.builder().listModelsPath(null).build())
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> OllamaApi.builder().showModelPath(null).build())
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> OllamaApi.builder().copyModelPath(null).build())
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> OllamaApi.builder().deleteModelPath(null).build())
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> OllamaApi.builder().pullModelPath(null).build())
.isInstanceOf(IllegalArgumentException.class);
}

}
Loading