Skip to content
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
7 changes: 7 additions & 0 deletions agentscope-distribution/agentscope-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,13 @@
<version>${project.version}</version>
</dependency>

<!-- AgentScope Ollama Spring Boot Starter -->
<dependency>
<groupId>io.agentscope</groupId>
<artifactId>agentscope-ollama-spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>

<!-- AgentScope Chat Completions Web Spring Boot Starter -->
<dependency>
<groupId>io.agentscope</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2024-2026 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
~
~ http://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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.agentscope</groupId>
<artifactId>agentscope-spring-boot-starters</artifactId>
<version>${revision}</version>
</parent>
<artifactId>agentscope-ollama-spring-boot-starter</artifactId>
<name>AgentScope Java Ollama - Spring Boot Starter</name>
<description>Spring Boot starter for AgentScope Java Ollama model integration</description>
<properties>
<maven.deploy.skip>false</maven.deploy.skip>
</properties>
<dependencies>
<dependency>
<groupId>io.agentscope</groupId>
<artifactId>agentscope-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>io.agentscope</groupId>
<artifactId>agentscope-extensions-model-ollama</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2024-2026 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
*
* http://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 io.agentscope.spring.boot.ollama;

import io.agentscope.core.model.Model;
import io.agentscope.extensions.model.ollama.OllamaChatModel;
import io.agentscope.spring.boot.AgentscopeAutoConfiguration;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
* Spring Boot auto-configuration for the Ollama model extension.
*/
@AutoConfiguration(before = AgentscopeAutoConfiguration.class)
@EnableConfigurationProperties(OllamaProperties.class)
@ConditionalOnClass(OllamaChatModel.class)
public class OllamaAutoConfiguration {

@Bean
@ConditionalOnProperty(prefix = "agentscope.model", name = "provider", havingValue = "ollama")
@ConditionalOnProperty(
prefix = "agentscope.ollama",
name = "enabled",
havingValue = "true",
matchIfMissing = true)
@ConditionalOnMissingBean(Model.class)
public OllamaChatModel ollamaChatModel(
OllamaProperties properties,
ObjectProvider<OllamaChatModelBuilderCustomizer> customizerObjectProvider) {
String modelName = trimToNull(properties.getModelName());
if (modelName == null) {
throw new IllegalStateException(
"agentscope.ollama.model-name must be configured when Ollama provider is"
+ " selected");
}
OllamaChatModel.Builder builder = OllamaChatModel.builder().modelName(modelName);
String baseUrl = trimToNull(properties.getBaseUrl());
if (baseUrl != null) {
builder.baseUrl(baseUrl);
}
customizerObjectProvider
.orderedStream()
.forEach(customizer -> customizer.customize(builder));
return builder.build();
}

private static String trimToNull(String value) {
if (value == null) {
return null;
}
String trimmed = value.trim();
return trimmed.isEmpty() ? null : trimmed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2024-2026 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
*
* http://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 io.agentscope.spring.boot.ollama;

import io.agentscope.extensions.model.ollama.OllamaChatModel;
import java.util.function.Consumer;

/**
* Customizer for {@link OllamaChatModel.Builder}.
*
* <p>Example usage:
*
* <pre>{@code
* @Bean
* public OllamaChatModelBuilderCustomizer ollamaChatModelBuilderCustomizer() {
* return builder -> builder.baseUrl("http://ollama.example.com:11434");
* }
* }</pre>
*/
@FunctionalInterface
public interface OllamaChatModelBuilderCustomizer extends Consumer<OllamaChatModel.Builder> {

/**
* Customize the {@link OllamaChatModel.Builder}.
*
* @param builder the builder to customize
*/
void customize(OllamaChatModel.Builder builder);

/**
* Accept and invoke the given builder.
*
* @param builder the builder to customize
*/
@Override
default void accept(OllamaChatModel.Builder builder) {
this.customize(builder);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2024-2026 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
*
* http://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 io.agentscope.spring.boot.ollama;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* Ollama provider specific settings.
*
* <p>Example configuration:
*
* <pre>{@code
* agentscope:
* model:
* provider: ollama
* ollama:
* enabled: true
* model-name: llama3
* # base-url: http://localhost:11434 # optional
* }</pre>
*/
@ConfigurationProperties(prefix = "agentscope.ollama")
public class OllamaProperties {

/**
* Whether Ollama model auto-configuration is enabled.
*/
private boolean enabled = true;

/**
* Ollama model name, for example {@code llama3}.
*/
private String modelName = "llama3";

/**
* Optional base URL for the Ollama server.
*/
private String baseUrl;

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public String getModelName() {
return modelName;
}

public void setModelName(String modelName) {
this.modelName = modelName;
}

public String getBaseUrl() {
return baseUrl;
}

public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Copyright 2024-2026 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
#
# http://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.
#
io.agentscope.spring.boot.ollama.OllamaAutoConfiguration
Loading
Loading