diff --git a/01-chat-models/chat-models-ollama/.gitignore b/01-chat-models/chat-models-ollama/.gitignore deleted file mode 100644 index c2065bc..0000000 --- a/01-chat-models/chat-models-ollama/.gitignore +++ /dev/null @@ -1,37 +0,0 @@ -HELP.md -.gradle -build/ -!gradle/wrapper/gradle-wrapper.jar -!**/src/main/**/build/ -!**/src/test/**/build/ - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr -out/ -!**/src/main/**/out/ -!**/src/test/**/out/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ diff --git a/01-chat-models/chat-models-openai/.gitignore b/01-chat-models/chat-models-openai/.gitignore deleted file mode 100644 index c2065bc..0000000 --- a/01-chat-models/chat-models-openai/.gitignore +++ /dev/null @@ -1,37 +0,0 @@ -HELP.md -.gradle -build/ -!gradle/wrapper/gradle-wrapper.jar -!**/src/main/**/build/ -!**/src/test/**/build/ - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr -out/ -!**/src/main/**/out/ -!**/src/test/**/out/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ diff --git a/02-prompts/prompts-basics-ollama/.gitignore b/02-prompts/prompts-basics-ollama/.gitignore deleted file mode 100644 index c2065bc..0000000 --- a/02-prompts/prompts-basics-ollama/.gitignore +++ /dev/null @@ -1,37 +0,0 @@ -HELP.md -.gradle -build/ -!gradle/wrapper/gradle-wrapper.jar -!**/src/main/**/build/ -!**/src/test/**/build/ - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr -out/ -!**/src/main/**/out/ -!**/src/test/**/out/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ diff --git a/02-prompts/prompts-basics-openai/.gitignore b/02-prompts/prompts-basics-openai/.gitignore deleted file mode 100644 index c2065bc..0000000 --- a/02-prompts/prompts-basics-openai/.gitignore +++ /dev/null @@ -1,37 +0,0 @@ -HELP.md -.gradle -build/ -!gradle/wrapper/gradle-wrapper.jar -!**/src/main/**/build/ -!**/src/test/**/build/ - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr -out/ -!**/src/main/**/out/ -!**/src/test/**/out/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ diff --git a/02-prompts/prompts-messages-ollama/.gitignore b/02-prompts/prompts-messages-ollama/.gitignore deleted file mode 100644 index c2065bc..0000000 --- a/02-prompts/prompts-messages-ollama/.gitignore +++ /dev/null @@ -1,37 +0,0 @@ -HELP.md -.gradle -build/ -!gradle/wrapper/gradle-wrapper.jar -!**/src/main/**/build/ -!**/src/test/**/build/ - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr -out/ -!**/src/main/**/out/ -!**/src/test/**/out/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ diff --git a/02-prompts/prompts-messages-openai/.gitignore b/02-prompts/prompts-messages-openai/.gitignore deleted file mode 100644 index c2065bc..0000000 --- a/02-prompts/prompts-messages-openai/.gitignore +++ /dev/null @@ -1,37 +0,0 @@ -HELP.md -.gradle -build/ -!gradle/wrapper/gradle-wrapper.jar -!**/src/main/**/build/ -!**/src/test/**/build/ - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr -out/ -!**/src/main/**/out/ -!**/src/test/**/out/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ diff --git a/02-prompts/prompts-templates-ollama/.gitignore b/02-prompts/prompts-templates-ollama/.gitignore deleted file mode 100644 index c2065bc..0000000 --- a/02-prompts/prompts-templates-ollama/.gitignore +++ /dev/null @@ -1,37 +0,0 @@ -HELP.md -.gradle -build/ -!gradle/wrapper/gradle-wrapper.jar -!**/src/main/**/build/ -!**/src/test/**/build/ - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr -out/ -!**/src/main/**/out/ -!**/src/test/**/out/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ diff --git a/02-prompts/prompts-templates-openai/.gitignore b/02-prompts/prompts-templates-openai/.gitignore deleted file mode 100644 index c2065bc..0000000 --- a/02-prompts/prompts-templates-openai/.gitignore +++ /dev/null @@ -1,37 +0,0 @@ -HELP.md -.gradle -build/ -!gradle/wrapper/gradle-wrapper.jar -!**/src/main/**/build/ -!**/src/test/**/build/ - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr -out/ -!**/src/main/**/out/ -!**/src/test/**/out/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ diff --git a/03-output-parsers/output-parsers-ollama/.gitignore b/03-output-parsers/output-parsers-ollama/.gitignore deleted file mode 100644 index c2065bc..0000000 --- a/03-output-parsers/output-parsers-ollama/.gitignore +++ /dev/null @@ -1,37 +0,0 @@ -HELP.md -.gradle -build/ -!gradle/wrapper/gradle-wrapper.jar -!**/src/main/**/build/ -!**/src/test/**/build/ - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr -out/ -!**/src/main/**/out/ -!**/src/test/**/out/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ diff --git a/03-output-parsers/output-parsers-openai/.gitignore b/03-output-parsers/output-parsers-openai/.gitignore deleted file mode 100644 index c2065bc..0000000 --- a/03-output-parsers/output-parsers-openai/.gitignore +++ /dev/null @@ -1,37 +0,0 @@ -HELP.md -.gradle -build/ -!gradle/wrapper/gradle-wrapper.jar -!**/src/main/**/build/ -!**/src/test/**/build/ - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr -out/ -!**/src/main/**/out/ -!**/src/test/**/out/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ diff --git a/04-embedding-models/embedding-models-ollama/README.md b/04-embedding-models/embedding-models-ollama/README.md new file mode 100644 index 0000000..042a1ba --- /dev/null +++ b/04-embedding-models/embedding-models-ollama/README.md @@ -0,0 +1,9 @@ +# Embedding Models: Ollama + +```shell +./gradlew bootTestRun +``` + +```shell +http :8080/ai/embed +``` diff --git a/04-embedding-models/embedding-models-ollama/build.gradle b/04-embedding-models/embedding-models-ollama/build.gradle new file mode 100644 index 0000000..a0ede90 --- /dev/null +++ b/04-embedding-models/embedding-models-ollama/build.gradle @@ -0,0 +1,34 @@ +plugins { + id 'java' + id 'org.springframework.boot' + id 'io.spring.dependency-management' +} + +group = 'com.thomasvitale' +version = '0.0.1-SNAPSHOT' + +java { + sourceCompatibility = '21' +} + +repositories { + mavenCentral() + maven { url 'https://repo.spring.io/milestone' } + maven { url 'https://repo.spring.io/snapshot' } +} + +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-web' + + implementation "org.springframework.ai:spring-ai-ollama-spring-boot-starter:${springAiVersion}" + + testAndDevelopmentOnly 'org.springframework.boot:spring-boot-devtools' + + testImplementation 'org.springframework.boot:spring-boot-starter-test' + testImplementation 'org.springframework.boot:spring-boot-testcontainers' + testImplementation 'org.testcontainers:junit-jupiter' +} + +tasks.named('test') { + useJUnitPlatform() +} diff --git a/04-embedding-models/embedding-models-ollama/src/main/java/com/thomasvitale/ai/spring/EmbeddingController.java b/04-embedding-models/embedding-models-ollama/src/main/java/com/thomasvitale/ai/spring/EmbeddingController.java new file mode 100644 index 0000000..1276062 --- /dev/null +++ b/04-embedding-models/embedding-models-ollama/src/main/java/com/thomasvitale/ai/spring/EmbeddingController.java @@ -0,0 +1,23 @@ +package com.thomasvitale.ai.spring; + +import org.springframework.ai.embedding.EmbeddingClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +class EmbeddingController { + + private final EmbeddingClient embeddingClient; + + EmbeddingController(EmbeddingClient embeddingClient) { + this.embeddingClient = embeddingClient; + } + + @GetMapping("/ai/embed") + String embed(@RequestParam(defaultValue = "And Gandalf yelled: 'You shall not pass!'") String message) { + var embeddings = embeddingClient.embed(message); + return "Size of the embedding vector: " + embeddings.size(); + } + +} diff --git a/04-embedding-models/embedding-models-ollama/src/main/java/com/thomasvitale/ai/spring/EmbeddingModelsOllamaApplication.java b/04-embedding-models/embedding-models-ollama/src/main/java/com/thomasvitale/ai/spring/EmbeddingModelsOllamaApplication.java new file mode 100644 index 0000000..85823ed --- /dev/null +++ b/04-embedding-models/embedding-models-ollama/src/main/java/com/thomasvitale/ai/spring/EmbeddingModelsOllamaApplication.java @@ -0,0 +1,13 @@ +package com.thomasvitale.ai.spring; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class EmbeddingModelsOllamaApplication { + + public static void main(String[] args) { + SpringApplication.run(EmbeddingModelsOllamaApplication.class, args); + } + +} diff --git a/04-embedding-models/embedding-models-ollama/src/main/resources/application.yml b/04-embedding-models/embedding-models-ollama/src/main/resources/application.yml new file mode 100644 index 0000000..8f06a76 --- /dev/null +++ b/04-embedding-models/embedding-models-ollama/src/main/resources/application.yml @@ -0,0 +1,8 @@ +spring: + ai: + ollama: + embedding: + model: llama2 + threads: + virtual: + enabled: true diff --git a/04-embedding-models/embedding-models-ollama/src/test/java/com/thomasvitale/ai/spring/EmbeddingModelsOllamaApplicationTests.java b/04-embedding-models/embedding-models-ollama/src/test/java/com/thomasvitale/ai/spring/EmbeddingModelsOllamaApplicationTests.java new file mode 100644 index 0000000..5d25f38 --- /dev/null +++ b/04-embedding-models/embedding-models-ollama/src/test/java/com/thomasvitale/ai/spring/EmbeddingModelsOllamaApplicationTests.java @@ -0,0 +1,13 @@ +package com.thomasvitale.ai.spring; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class EmbeddingModelsOllamaApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/04-embedding-models/embedding-models-ollama/src/test/java/com/thomasvitale/ai/spring/TestEmbeddingModelsOllamaApplication.java b/04-embedding-models/embedding-models-ollama/src/test/java/com/thomasvitale/ai/spring/TestEmbeddingModelsOllamaApplication.java new file mode 100644 index 0000000..ee3bfab --- /dev/null +++ b/04-embedding-models/embedding-models-ollama/src/test/java/com/thomasvitale/ai/spring/TestEmbeddingModelsOllamaApplication.java @@ -0,0 +1,29 @@ +package com.thomasvitale.ai.spring; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.devtools.restart.RestartScope; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Scope; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.testcontainers.containers.GenericContainer; + +@TestConfiguration(proxyBeanMethods = false) +public class TestEmbeddingModelsOllamaApplication { + + @Bean + @RestartScope + @Scope("singleton") // needed because of https://github.com/spring-projects/spring-boot/issues/35786 + GenericContainer ollama(DynamicPropertyRegistry properties) { + var ollama = new GenericContainer<>("ghcr.io/thomasvitale/ollama-llama2") + .withExposedPorts(11434); + properties.add("spring.ai.ollama.base-url", + () -> "http://%s:%s".formatted(ollama.getHost(), ollama.getMappedPort(11434))); + return ollama; + } + + public static void main(String[] args) { + SpringApplication.from(EmbeddingModelsOllamaApplication::main).with(TestEmbeddingModelsOllamaApplication.class).run(args); + } + +} diff --git a/04-embedding-models/embedding-models-openai/.gitignore b/04-embedding-models/embedding-models-openai/.gitignore deleted file mode 100644 index c2065bc..0000000 --- a/04-embedding-models/embedding-models-openai/.gitignore +++ /dev/null @@ -1,37 +0,0 @@ -HELP.md -.gradle -build/ -!gradle/wrapper/gradle-wrapper.jar -!**/src/main/**/build/ -!**/src/test/**/build/ - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr -out/ -!**/src/main/**/out/ -!**/src/test/**/out/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ diff --git a/04-embedding-models/embedding-models-openai/README.md b/04-embedding-models/embedding-models-openai/README.md new file mode 100644 index 0000000..fad86dc --- /dev/null +++ b/04-embedding-models/embedding-models-openai/README.md @@ -0,0 +1,9 @@ +# Embedding Models: OpenAI + +```shell +./gradlew bootTestRun +``` + +```shell +http :8080/ai/embed +``` diff --git a/04-embedding-models/embedding-models-openai/build.gradle b/04-embedding-models/embedding-models-openai/build.gradle index f434c31..bfb9516 100644 --- a/04-embedding-models/embedding-models-openai/build.gradle +++ b/04-embedding-models/embedding-models-openai/build.gradle @@ -25,6 +25,8 @@ dependencies { testAndDevelopmentOnly 'org.springframework.boot:spring-boot-devtools' testImplementation 'org.springframework.boot:spring-boot-starter-test' + testImplementation 'org.springframework.boot:spring-boot-testcontainers' + testImplementation 'org.testcontainers:junit-jupiter' } tasks.named('test') { diff --git a/04-embedding-models/embedding-models-openai/src/test/java/com/thomasvitale/ai/spring/TestEmbeddingModelsOpenAiApplication.java b/04-embedding-models/embedding-models-openai/src/test/java/com/thomasvitale/ai/spring/TestEmbeddingModelsOpenAiApplication.java new file mode 100644 index 0000000..9140ca5 --- /dev/null +++ b/04-embedding-models/embedding-models-openai/src/test/java/com/thomasvitale/ai/spring/TestEmbeddingModelsOpenAiApplication.java @@ -0,0 +1,13 @@ +package com.thomasvitale.ai.spring; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.test.context.TestConfiguration; + +@TestConfiguration(proxyBeanMethods = false) +public class TestEmbeddingModelsOpenAiApplication { + + public static void main(String[] args) { + SpringApplication.from(EmbeddingModelsOpenAiApplication::main).with(TestEmbeddingModelsOpenAiApplication.class).run(args); + } + +} diff --git a/settings.gradle b/settings.gradle index 9399055..1166a63 100644 --- a/settings.gradle +++ b/settings.gradle @@ -13,4 +13,5 @@ include '02-prompts:prompts-templates-openai' include '03-output-parsers:output-parsers-ollama' include '03-output-parsers:output-parsers-openai' +include '04-embedding-models:embedding-models-ollama' include '04-embedding-models:embedding-models-openai'