First attempt at spring boot autoconfiguration

This commit is contained in:
Sebastiaan de Schaetzen 2024-08-20 20:25:25 +02:00
parent e051ca803c
commit 3ccc0dd265
20 changed files with 247 additions and 25 deletions

View File

@ -1,34 +1,11 @@
plugins {
id 'java'
id 'io.freefair.lombok' version '8.10'
id 'maven-publish'
}
group = 'be.seeseemelk'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation 'io.github.ollama4j:ollama4j:1.0.82'
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.hamcrest:hamcrest:3.0'
}
test {
useJUnitPlatform()
}
version = '0.1-SNAPSHOT'
publishing {
publications {
library(MavenPublication) {
from components.java
}
}
repositories {
maven {
name = "Gitea"

View File

@ -0,0 +1,34 @@
plugins {
id 'java'
id 'io.freefair.lombok' version '8.10'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'io.github.ollama4j:ollama4j:1.0.82'
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.hamcrest:hamcrest:3.0'
}
test {
useJUnitPlatform()
}
java {
withSourcesJar()
withJavadocJar()
}
publishing {
publications {
libllamascript(MavenPublication) {
from components.java
artifactId 'libllamascript'
}
}
}

View File

@ -1,2 +1,4 @@
rootProject.name = 'libllamascript'
rootProject.name = 'llamascript'
include('libllamascript')
include('spring-llamascript-starter')

View File

@ -0,0 +1,40 @@
plugins {
id 'java'
id 'io.freefair.lombok' version '8.10'
id 'org.springframework.boot' version '3.3.2'
}
apply plugin: 'io.spring.dependency-management'
repositories {
mavenCentral()
}
dependencies {
implementation project(':libllamascript')
implementation 'org.springframework.boot:spring-boot'
implementation 'org.springframework.boot:spring-boot-autoconfigure'
implementation 'org.springframework.boot:spring-boot-configuration-processor'
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.hamcrest:hamcrest:3.0'
}
test {
useJUnitPlatform()
}
java {
withSourcesJar()
withJavadocJar()
}
publishing {
publications {
springLlamascriptStarter(MavenPublication) {
from components.java
artifactId 'spring-llamascript-starter'
}
}
}

View File

@ -0,0 +1,31 @@
package be.seeseemelk.llamascript.autoconfigure;
import be.seeseemelk.llamascript.LLamaScriptFactory;
import be.seeseemelk.llamascript.LlamaScript;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(LlamaScriptProperties.class)
public class LlamaScriptAutoConfiguration {
@Autowired
private LlamaScriptProperties properties;
@Bean
@ConditionalOnMissingBean
public LLamaScriptFactory lLamaScriptFactory() {
var factory = new LLamaScriptFactory();
factory.setHost(properties.getHost());
factory.setModel(properties.getModel());
return factory;
}
@Bean
@ConditionalOnMissingBean
public LlamaScript llamaScript(LLamaScriptFactory factory) {
return factory.build();
}
}

View File

@ -0,0 +1,13 @@
package be.seeseemelk.llamascript.autoconfigure;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Getter
@Setter
@ConfigurationProperties(prefix = "llamascript")
public class LlamaScriptProperties {
private String host = "http://localhost:11434";
private String model = "llama3.1:8b";
}

View File

@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
be.seeseemelk.llamascript.autoconfigure.LlamaScriptAutoConfiguration

View File

@ -0,0 +1,12 @@
package be.seeseemelk.llamascript;
import org.junit.jupiter.api.BeforeEach;
public class AbstractLlamaTest {
protected LlamaScript llama;
@BeforeEach
void setUp() {
llama = new LLamaScriptFactory().build();
}
}

View File

@ -0,0 +1,26 @@
package be.seeseemelk.llamascript;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
public class BooleanTests extends AbstractLlamaTest {
@ParameterizedTest
@ValueSource(ints = {0, 1, 2, 3, 4, 5})
void isEven(int number) {
boolean isEven = llama.evalBool("Return true if the number is even", number);
assertThat(isEven, equalTo(number % 2 == 0));
}
@Test
void isABuilding() {
boolean isBuilding = llama.evalBool("Return true if the argument is a building or skyscraper", "Dalai Lama");
assertThat(isBuilding, equalTo(false));
isBuilding = llama.evalBool("Return true if the argument is a building or skyscraper", "Burj Khalifa");
assertThat(isBuilding, equalTo(true));
}
}

View File

@ -0,0 +1,41 @@
package be.seeseemelk.llamascript;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
public class IntegerTests extends AbstractLlamaTest {
@Test
void canIncrementNumber() {
int value = llama.evalInt("Increment the number", 4);
assertThat(value, equalTo(5));
}
@Test
void canMultiply() {
int value = llama.evalInt("Multiply the numbers", 2, 3);
assertThat(value, equalTo(2*3));
}
@Test
void canMax() {
int value = llama.evalInt("Select the largest number", -2, 8);
assertThat(value, equalTo(8));
}
@Test
void canDoWeirdStuff() {
int value = llama.evalInt("Select the number with the most '5's in it.", 12, 5, 1023978, 158525);
assertThat(value, equalTo(158525));
}
@Test
void givePositivity() {
int value = llama.evalInt("If the string is something positive, return 1. Else, return 0.", "I like rainbows");
assertThat(value, equalTo(1));
value = llama.evalInt("If the string is something positive, return 1. Else, return 0.", "Death to all");
assertThat(value, equalTo(0));
}
}

View File

@ -0,0 +1,44 @@
package be.seeseemelk.llamascript;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
public class StringTests extends AbstractLlamaTest {
@Test
void canUppercase() {
String value = llama.evalString("Return the string in uppercase", "Cat");
assertThat(value, equalTo("CAT"));
}
@Test
void canLowercase() {
String value = llama.evalString("Return the string in lowercase", "Cat");
assertThat(value, equalTo("cat"));
}
@Test
void canInvert() {
String value = llama.evalString("Return the string backwards", "Cat");
assertThat(value, equalTo("taC"));
}
@Test
void canSelectLongest() {
String value = llama.evalString("Return the longest string", "Cat", "Dog", "Horse");
assertThat(value, equalTo("Horse"));
}
@Test
void canDoWeirdStuff() {
String value = llama.evalString("Return the string that does not fit", "Cat", "Dog", "Horse", "Bicycle");
assertThat(value, equalTo("Bicycle"));
}
@Test
void canDoReallyWeirdStuff() {
String value = llama.evalString("Sort the letters alphabetically", "horse");
assertThat(value, equalTo("ehors"));
}
}