-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from langchain4j/ollama_starter
added Ollama starter
- Loading branch information
Showing
10 changed files
with
457 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
104 changes: 104 additions & 0 deletions
104
...4j-ollama-spring-boot-starter/src/main/java/dev/langchain4j/ollama/spring/AutoConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...-spring-boot-starter/src/main/java/dev/langchain4j/ollama/spring/ChatModelProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
16 changes: 16 additions & 0 deletions
16
...ng-boot-starter/src/main/java/dev/langchain4j/ollama/spring/EmbeddingModelProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
25 changes: 25 additions & 0 deletions
25
...ing-boot-starter/src/main/java/dev/langchain4j/ollama/spring/LanguageModelProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
29 changes: 29 additions & 0 deletions
29
...4j-ollama-spring-boot-starter/src/main/java/dev/langchain4j/ollama/spring/Properties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
1 change: 1 addition & 0 deletions
1
langchain4j-ollama-spring-boot-starter/src/main/resources/META-INF/spring.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=dev.langchain4j.ollama.spring.AutoConfig |
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dev.langchain4j.ollama.spring.AutoConfig |
Oops, something went wrong.