From 9a15c49073707b18d6c75e0f49279d3c631cd59c Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Sun, 7 Apr 2024 15:00:19 +0000 Subject: [PATCH] feat : set the system message --- rag/rag-springai-ollama-llm/pom.xml | 4 ++++ .../service/AIChatService.java | 19 ++++++------------- .../src/main/resources/application.properties | 2 ++ 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/rag/rag-springai-ollama-llm/pom.xml b/rag/rag-springai-ollama-llm/pom.xml index c217f33..cec70af 100644 --- a/rag/rag-springai-ollama-llm/pom.xml +++ b/rag/rag-springai-ollama-llm/pom.xml @@ -41,6 +41,10 @@ org.springframework.ai spring-ai-redis-spring-boot-starter + + org.apache.commons + commons-pool2 + org.springframework.ai spring-ai-pdf-document-reader diff --git a/rag/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/service/AIChatService.java b/rag/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/service/AIChatService.java index 2030415..ad72c81 100644 --- a/rag/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/service/AIChatService.java +++ b/rag/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/service/AIChatService.java @@ -13,6 +13,7 @@ import org.springframework.ai.chat.prompt.Prompt; import org.springframework.ai.chat.prompt.SystemPromptTemplate; import org.springframework.ai.document.Document; +import org.springframework.ai.vectorstore.SearchRequest; import org.springframework.ai.vectorstore.VectorStore; import org.springframework.stereotype.Service; @@ -23,18 +24,9 @@ public class AIChatService { private static final String template = """ - You're assisting with questions about cricket - - Cricket is a bat-and-ball game that is played between two teams of eleven players on a field at the centre of which is a 22-yard (20-metre) pitch with a wicket at each end, - each comprising two bails balanced on three stumps. - Two players from the batting team (the striker and nonstriker) stand in front of either wicket, - with one player from the fielding team (the bowler) bowling the ball towards the striker's wicket from the opposite end of the pitch. - The striker's goal is to hit the bowled ball and then switch places with the nonstriker, - with the batting team scoring one run for each exchange. - Runs are also scored when the ball reaches or crosses the boundary of the field or when the ball is bowled illegally. - - Use the information from the DOCUMENTS section to provide accurate answers but act as if you knew this information innately. - If unsure, simply state that you don't know. + You are a helpful assistant, conversing with a user about the subjects contained in a set of documents. + Use the information from the DOCUMENTS section to provide accurate answers. If unsure or if the answer + isn't found in the DOCUMENTS section, simply state that you don't know the answer. DOCUMENTS: {documents} @@ -52,7 +44,8 @@ public AIChatService(ChatClient aiClient, VectorStore vectorStore) { public String chat(String query) { // Querying the VectorStore using natural language looking for the information about info asked. LOGGER.info("Querying vector store with query :{}", query); - List listOfSimilarDocuments = this.vectorStore.similaritySearch(query); + List listOfSimilarDocuments = this.vectorStore.similaritySearch( + SearchRequest.query(query).withTopK(2).withSimilarityThreshold(0.8d)); String documents = listOfSimilarDocuments.stream() .map(Document::getContent) .collect(Collectors.joining(System.lineSeparator())); diff --git a/rag/rag-springai-ollama-llm/src/main/resources/application.properties b/rag/rag-springai-ollama-llm/src/main/resources/application.properties index 8f5d332..57ad6e1 100644 --- a/rag/rag-springai-ollama-llm/src/main/resources/application.properties +++ b/rag/rag-springai-ollama-llm/src/main/resources/application.properties @@ -9,3 +9,5 @@ spring.ai.ollama.embedding.options.model=llama2 spring.ai.vectorstore.redis.index=vector_store spring.ai.vectorstore.redis.prefix=ai + +spring.ai.ollama.baseUrl=http://localhost:11434