Skip to content
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

added Ollama starter #3

Merged
merged 1 commit into from
Jan 29, 2024
Merged
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
80 changes: 80 additions & 0 deletions langchain4j-ollama-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>dev.langchain4j</groupId>
<artifactId>langchain4j-spring</artifactId>
<version>0.26.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>langchain4j-ollama-spring-boot-starter</artifactId>
<name>LangChain4j Spring Boot starter for Ollama</name>
<packaging>jar</packaging>

<dependencies>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-ollama</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>

<!-- should be listed before spring-boot-configuration-processor -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<!-- needed to generate automatic metadata about available config properties -->
<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>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<licenses>
<license>
<name>Apache-2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package dev.langchain4j.ollama.spring;

import dev.langchain4j.model.ollama.*;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import static dev.langchain4j.ollama.spring.Properties.PREFIX;

@AutoConfiguration
@EnableConfigurationProperties(Properties.class)
public class AutoConfig {

@Bean
@ConditionalOnProperty(PREFIX + ".chat-model.base-url")
OllamaChatModel ollamaChatModel(Properties properties) {
ChatModelProperties chatModelProperties = properties.getChatModel();
return OllamaChatModel.builder()
.baseUrl(chatModelProperties.getBaseUrl())
.modelName(chatModelProperties.getModelName())
.temperature(chatModelProperties.getTemperature())
.topK(chatModelProperties.getTopK())
.topP(chatModelProperties.getTopP())
.repeatPenalty(chatModelProperties.getRepeatPenalty())
.seed(chatModelProperties.getSeed())
.numPredict(chatModelProperties.getNumPredict())
.stop(chatModelProperties.getStop())
.format(chatModelProperties.getFormat())
.timeout(chatModelProperties.getTimeout())
.maxRetries(chatModelProperties.getMaxRetries())
.build();
}

@Bean
@ConditionalOnProperty(PREFIX + ".streaming-chat-model.base-url")
OllamaStreamingChatModel ollamaStreamingChatModel(Properties properties) {
ChatModelProperties chatModelProperties = properties.getStreamingChatModel();
return OllamaStreamingChatModel.builder()
.baseUrl(chatModelProperties.getBaseUrl())
.modelName(chatModelProperties.getModelName())
.temperature(chatModelProperties.getTemperature())
.topK(chatModelProperties.getTopK())
.topP(chatModelProperties.getTopP())
.repeatPenalty(chatModelProperties.getRepeatPenalty())
.seed(chatModelProperties.getSeed())
.numPredict(chatModelProperties.getNumPredict())
.stop(chatModelProperties.getStop())
.format(chatModelProperties.getFormat())
.timeout(chatModelProperties.getTimeout())
.build();
}

@Bean
@ConditionalOnProperty(PREFIX + ".language-model.base-url")
OllamaLanguageModel ollamaLanguageModel(Properties properties) {
LanguageModelProperties languageModelProperties = properties.getLanguageModel();
return OllamaLanguageModel.builder()
.baseUrl(languageModelProperties.getBaseUrl())
.modelName(languageModelProperties.getModelName())
.temperature(languageModelProperties.getTemperature())
.topK(languageModelProperties.getTopK())
.topP(languageModelProperties.getTopP())
.repeatPenalty(languageModelProperties.getRepeatPenalty())
.seed(languageModelProperties.getSeed())
.numPredict(languageModelProperties.getNumPredict())
.stop(languageModelProperties.getStop())
.format(languageModelProperties.getFormat())
.timeout(languageModelProperties.getTimeout())
.maxRetries(languageModelProperties.getMaxRetries())
.build();
}

@Bean
@ConditionalOnProperty(PREFIX + ".streaming-language-model.base-url")
OllamaStreamingLanguageModel ollamaStreamingLanguageModel(Properties properties) {
LanguageModelProperties languageModelProperties = properties.getStreamingLanguageModel();
return OllamaStreamingLanguageModel.builder()
.baseUrl(languageModelProperties.getBaseUrl())
.modelName(languageModelProperties.getModelName())
.temperature(languageModelProperties.getTemperature())
.topK(languageModelProperties.getTopK())
.topP(languageModelProperties.getTopP())
.repeatPenalty(languageModelProperties.getRepeatPenalty())
.seed(languageModelProperties.getSeed())
.numPredict(languageModelProperties.getNumPredict())
.stop(languageModelProperties.getStop())
.format(languageModelProperties.getFormat())
.timeout(languageModelProperties.getTimeout())
.build();
}

@Bean
@ConditionalOnProperty(PREFIX + ".embedding-model.base-url")
OllamaEmbeddingModel ollamaEmbeddingModel(Properties properties) {
EmbeddingModelProperties embeddingModelProperties = properties.getEmbeddingModel();
return OllamaEmbeddingModel.builder()
.baseUrl(embeddingModelProperties.getBaseUrl())
.modelName(embeddingModelProperties.getModelName())
.timeout(embeddingModelProperties.getTimeout())
.maxRetries(embeddingModelProperties.getMaxRetries())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dev.langchain4j.ollama.spring;

import lombok.Getter;
import lombok.Setter;

import java.time.Duration;
import java.util.List;

@Getter
@Setter
class ChatModelProperties {

String baseUrl;
String modelName;
Double temperature;
Integer topK;
Double topP;
Double repeatPenalty;
Integer seed;
Integer numPredict;
List<String> stop;
String format;
Duration timeout;
Integer maxRetries;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.langchain4j.ollama.spring;

import lombok.Getter;
import lombok.Setter;

import java.time.Duration;

@Getter
@Setter
class EmbeddingModelProperties {

String baseUrl;
String modelName;
Duration timeout;
Integer maxRetries;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dev.langchain4j.ollama.spring;

import lombok.Getter;
import lombok.Setter;

import java.time.Duration;
import java.util.List;

@Getter
@Setter
class LanguageModelProperties {

String baseUrl;
String modelName;
Double temperature;
Integer topK;
Double topP;
Double repeatPenalty;
Integer seed;
Integer numPredict;
List<String> stop;
String format;
Duration timeout;
Integer maxRetries;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.langchain4j.ollama.spring;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

@Getter
@Setter
@ConfigurationProperties(prefix = Properties.PREFIX)
public class Properties {

static final String PREFIX = "langchain4j.ollama";

@NestedConfigurationProperty
ChatModelProperties chatModel;

@NestedConfigurationProperty
ChatModelProperties streamingChatModel;

@NestedConfigurationProperty
LanguageModelProperties languageModel;

@NestedConfigurationProperty
LanguageModelProperties streamingLanguageModel;

@NestedConfigurationProperty
EmbeddingModelProperties embeddingModel;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=dev.langchain4j.ollama.spring.AutoConfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dev.langchain4j.ollama.spring.AutoConfig
Loading
Loading