From 4c09e192421d7e3e79cbd509c8bb67179e7daacf Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Wed, 27 Mar 2024 19:27:17 +0000 Subject: [PATCH 1/7] feat : convert from openai to ollama model --- llm-rag-with-springai/pom.xml | 11 ++++++----- .../TestLlmRagWithSpringAiApplication.java | 13 +++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/llm-rag-with-springai/pom.xml b/llm-rag-with-springai/pom.xml index 4a332ee..91dc2b8 100644 --- a/llm-rag-with-springai/pom.xml +++ b/llm-rag-with-springai/pom.xml @@ -28,7 +28,7 @@ org.springframework.ai - spring-ai-openai-spring-boot-starter + spring-ai-ollama-spring-boot-starter org.springframework.ai @@ -38,10 +38,6 @@ org.springframework.ai spring-ai-pdf-document-reader - - org.springframework.retry - spring-retry - org.springframework spring-aspects @@ -77,6 +73,11 @@ postgresql test + + org.testcontainers + ollama + test + diff --git a/llm-rag-with-springai/src/test/java/com/learning/ai/llmragwithspringai/TestLlmRagWithSpringAiApplication.java b/llm-rag-with-springai/src/test/java/com/learning/ai/llmragwithspringai/TestLlmRagWithSpringAiApplication.java index 764965c..e160060 100644 --- a/llm-rag-with-springai/src/test/java/com/learning/ai/llmragwithspringai/TestLlmRagWithSpringAiApplication.java +++ b/llm-rag-with-springai/src/test/java/com/learning/ai/llmragwithspringai/TestLlmRagWithSpringAiApplication.java @@ -1,10 +1,14 @@ package com.learning.ai.llmragwithspringai; +import java.io.IOException; + import org.springframework.boot.SpringApplication; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.testcontainers.service.connection.ServiceConnection; import org.springframework.context.annotation.Bean; +import org.springframework.test.context.DynamicPropertyRegistry; import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.ollama.OllamaContainer; import org.testcontainers.utility.DockerImageName; @TestConfiguration(proxyBeanMethods = false) @@ -16,6 +20,15 @@ PostgreSQLContainer pgvectorContainer() { return new PostgreSQLContainer<>(DockerImageName.parse("pgvector/pgvector:pg16")); } + @Bean + OllamaContainer ollama(DynamicPropertyRegistry properties) throws UnsupportedOperationException, IOException, InterruptedException { + OllamaContainer ollama = new OllamaContainer("ollama/ollama"); + ollama.start(); + ollama.execInContainer("ollama", "pull", "mistral"); + properties.add("spring.ai.ollama.base-url", ollama::getEndpoint); + return ollama; + } + public static void main(String[] args) { SpringApplication.from(LlmRagWithSpringAiApplication::main).with(TestLlmRagWithSpringAiApplication.class).run(args); } From 9d1ed61842b5ba9691cd2a70e5ff473d3d073989 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Wed, 27 Mar 2024 19:32:22 +0000 Subject: [PATCH 2/7] feat : using substitute image --- .../src/main/resources/application.properties | 7 ++----- .../TestLlmRagWithSpringAiApplication.java | 4 +--- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/llm-rag-with-springai/src/main/resources/application.properties b/llm-rag-with-springai/src/main/resources/application.properties index 06ad57f..a36dff1 100644 --- a/llm-rag-with-springai/src/main/resources/application.properties +++ b/llm-rag-with-springai/src/main/resources/application.properties @@ -2,10 +2,7 @@ spring.application.name=llm-rag-with-springai spring.threads.virtual.enabled=true -spring.ai.openai.api-key=demo -spring.ai.openai.base-url=http://langchain4j.dev/demo/openai -spring.ai.openai.chat.options.model=gpt-3.5-turbo -spring.ai.openai.chat.options.temperature=0.7 -spring.ai.openai.chat.options.responseFormat=json_object +spring.ai.ollama.chat.options.model=llama2 +spring.ai.ollama.chat.options.responseFormat=json_object #spring.ai.openai.image.model=dall-e-3 diff --git a/llm-rag-with-springai/src/test/java/com/learning/ai/llmragwithspringai/TestLlmRagWithSpringAiApplication.java b/llm-rag-with-springai/src/test/java/com/learning/ai/llmragwithspringai/TestLlmRagWithSpringAiApplication.java index e160060..048e409 100644 --- a/llm-rag-with-springai/src/test/java/com/learning/ai/llmragwithspringai/TestLlmRagWithSpringAiApplication.java +++ b/llm-rag-with-springai/src/test/java/com/learning/ai/llmragwithspringai/TestLlmRagWithSpringAiApplication.java @@ -22,9 +22,7 @@ PostgreSQLContainer pgvectorContainer() { @Bean OllamaContainer ollama(DynamicPropertyRegistry properties) throws UnsupportedOperationException, IOException, InterruptedException { - OllamaContainer ollama = new OllamaContainer("ollama/ollama"); - ollama.start(); - ollama.execInContainer("ollama", "pull", "mistral"); + OllamaContainer ollama = new OllamaContainer(DockerImageName.parse("ghcr.io/thomasvitale/ollama-llama2").asCompatibleSubstituteFor("ollama/ollama")); properties.add("spring.ai.ollama.base-url", ollama::getEndpoint); return ollama; } From 7f5825e117056de42463b7f5c0dd16e68fcb13fa Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Thu, 28 Mar 2024 01:22:41 +0530 Subject: [PATCH 3/7] using correct model for embeddings --- llm-rag-with-springai/src/main/resources/application.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llm-rag-with-springai/src/main/resources/application.properties b/llm-rag-with-springai/src/main/resources/application.properties index a36dff1..4c0f194 100644 --- a/llm-rag-with-springai/src/main/resources/application.properties +++ b/llm-rag-with-springai/src/main/resources/application.properties @@ -5,4 +5,4 @@ spring.threads.virtual.enabled=true spring.ai.ollama.chat.options.model=llama2 spring.ai.ollama.chat.options.responseFormat=json_object -#spring.ai.openai.image.model=dall-e-3 +spring.ai.ollama.embedding.options.model=llama2 From 493bf50deb8ece825d7d6ab246a9e88c7646e8cb Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Thu, 28 Mar 2024 01:34:08 +0530 Subject: [PATCH 4/7] adds pgvector dimensions --- llm-rag-with-springai/src/main/resources/application.properties | 2 ++ 1 file changed, 2 insertions(+) diff --git a/llm-rag-with-springai/src/main/resources/application.properties b/llm-rag-with-springai/src/main/resources/application.properties index 4c0f194..d577876 100644 --- a/llm-rag-with-springai/src/main/resources/application.properties +++ b/llm-rag-with-springai/src/main/resources/application.properties @@ -6,3 +6,5 @@ spring.ai.ollama.chat.options.model=llama2 spring.ai.ollama.chat.options.responseFormat=json_object spring.ai.ollama.embedding.options.model=llama2 + +spring.ai.vectorstore.pgvector.dimensions=1536 From 788b538e01c2269f6d246d126584186b75313881 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Thu, 28 Mar 2024 07:27:31 +0530 Subject: [PATCH 5/7] fix issue with dimensions --- llm-rag-with-springai/src/main/resources/application.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llm-rag-with-springai/src/main/resources/application.properties b/llm-rag-with-springai/src/main/resources/application.properties index d577876..520cf10 100644 --- a/llm-rag-with-springai/src/main/resources/application.properties +++ b/llm-rag-with-springai/src/main/resources/application.properties @@ -7,4 +7,4 @@ spring.ai.ollama.chat.options.responseFormat=json_object spring.ai.ollama.embedding.options.model=llama2 -spring.ai.vectorstore.pgvector.dimensions=1536 +spring.ai.vectorstore.pgvector.dimensions=4096 From eb40dbfe03d541c71a4d3d6b1e08886ec96618d5 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Thu, 28 Mar 2024 07:36:33 +0530 Subject: [PATCH 6/7] Update application.properties --- llm-rag-with-springai/src/main/resources/application.properties | 2 ++ 1 file changed, 2 insertions(+) diff --git a/llm-rag-with-springai/src/main/resources/application.properties b/llm-rag-with-springai/src/main/resources/application.properties index 520cf10..54724d1 100644 --- a/llm-rag-with-springai/src/main/resources/application.properties +++ b/llm-rag-with-springai/src/main/resources/application.properties @@ -8,3 +8,5 @@ spring.ai.ollama.chat.options.responseFormat=json_object spring.ai.ollama.embedding.options.model=llama2 spring.ai.vectorstore.pgvector.dimensions=4096 +## As dimensions are more than 2000 both available indexes cannot be applied +spring.ai.vectorstore.pgvector.indexType=NONE From 2d8c0ac1fc3850594acbe04b15a575bdcced8eee Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Thu, 28 Mar 2024 05:58:05 +0000 Subject: [PATCH 7/7] fix : format issue and removes unnecessary code --- .../TestLlmRagWithSpringAiApplication.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/llm-rag-with-springai/src/test/java/com/learning/ai/llmragwithspringai/TestLlmRagWithSpringAiApplication.java b/llm-rag-with-springai/src/test/java/com/learning/ai/llmragwithspringai/TestLlmRagWithSpringAiApplication.java index 97d4ac9..8344e21 100644 --- a/llm-rag-with-springai/src/test/java/com/learning/ai/llmragwithspringai/TestLlmRagWithSpringAiApplication.java +++ b/llm-rag-with-springai/src/test/java/com/learning/ai/llmragwithspringai/TestLlmRagWithSpringAiApplication.java @@ -1,7 +1,5 @@ package com.learning.ai.llmragwithspringai; -import java.io.IOException; - import org.springframework.boot.SpringApplication; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.testcontainers.service.connection.ServiceConnection; @@ -14,14 +12,14 @@ @TestConfiguration(proxyBeanMethods = false) public class TestLlmRagWithSpringAiApplication { - @Bean - OllamaContainer ollama(DynamicPropertyRegistry properties) throws UnsupportedOperationException, IOException, InterruptedException { - OllamaContainer ollama = new OllamaContainer(DockerImageName.parse("ghcr.io/thomasvitale/ollama-llama2").asCompatibleSubstituteFor("ollama/ollama")); - properties.add("spring.ai.ollama.base-url", ollama::getEndpoint); - return ollama; + OllamaContainer ollama(DynamicPropertyRegistry properties) { + OllamaContainer ollama = new OllamaContainer( + DockerImageName.parse("ghcr.io/thomasvitale/ollama-llama2").asCompatibleSubstituteFor("ollama/ollama")); + properties.add("spring.ai.ollama.base-url", ollama::getEndpoint); + return ollama; } - + @Bean @ServiceConnection PostgreSQLContainer pgvectorContainer() {