Skip to content

Commit

Permalink
feat : upgrade to milestone 1.0.0-M1 (#62)
Browse files Browse the repository at this point in the history
* feat : upgrade to milestone 1.0.0-M1

* fix : RAG

* fix : tests

* Updated Sequence Diagram
  • Loading branch information
rajadilipkolli authored Jun 13, 2024
1 parent e900c78 commit 143ae37
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 45 deletions.
19 changes: 11 additions & 8 deletions rag/rag-springai-ollama-llm/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ As LLMs need heavy computational power, they need GPU to be enabled for quick pr
sequenceDiagram
participant User
participant AIChatService
participant ChatClient.Builder
participant Redis
User->>AIChatService: Send chat request
AIChatService->>ChatClient.Builder: Initialize chat client
ChatClient.Builder-->>AIChatService: Return chat client instance
AIChatService->>Redis: Enrich and load document metadata
Redis-->>AIChatService: Confirm metadata loaded
participant QuestionAnswerAdvisor
participant RedisVectorStore
participant AIApiClient
User->>AIChatService: Initiate chat request
AIChatService->>QuestionAnswerAdvisor: Generate advisory request
QuestionAnswerAdvisor->>RedisVectorStore: Query relevant information
RedisVectorStore-->>QuestionAnswerAdvisor: Return search results
QuestionAnswerAdvisor->>AIChatService: Provide advisory response
AIChatService->>AIApiClient: Forward chat input and advisory response
AIApiClient-->>AIChatService: Chat outcome
AIChatService-->>User: Return chat response
```

Expand Down
22 changes: 11 additions & 11 deletions rag/rag-springai-ollama-llm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<properties>
<java.version>21</java.version>
<spring-ai.version>1.0.0-SNAPSHOT</spring-ai.version>
<spring-ai.version>1.0.0-M1</spring-ai.version>
<spotless.version>2.43.0</spotless.version>
</properties>

Expand Down Expand Up @@ -189,22 +189,22 @@

<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</releases>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</releases>
</snapshots>
</pluginRepository>
</pluginRepositories>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package com.learning.ai.llmragwithspringai.service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.model.Generation;
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;

Expand All @@ -28,7 +22,7 @@ public class AIChatService {
isn't found in the DOCUMENTS section, simply state that you don't know the answer.
DOCUMENTS:
{documents}
{question_answer_context}
""";

Expand All @@ -41,20 +35,11 @@ public AIChatService(ChatClient.Builder builder, VectorStore vectorStore) {
}

public String chat(String query) {
// Querying the VectorStore using natural language looking for the information about info asked.
LOGGER.debug("Querying vector store with query :{}", query);
List<Document> listOfSimilarDocuments = this.vectorStore.similaritySearch(query);
String documents = listOfSimilarDocuments.stream()
.map(Document::getContent)
.collect(Collectors.joining(System.lineSeparator()));
LOGGER.info("Response from vector store :{}", documents);
// Constructing the systemMessage to indicate the AI model to use the passed information
// to answer the question.
Message systemMessage = new SystemPromptTemplate(template).createMessage(Map.of("documents", documents));
UserMessage userMessage = new UserMessage(query);
Prompt prompt = new Prompt(List.of(systemMessage, userMessage));
LOGGER.info("Calling ai with prompt :{}", prompt);
ChatResponse aiResponse = aiClient.prompt(prompt).call().chatResponse();
ChatResponse aiResponse = aiClient.prompt()
.advisors(new QuestionAnswerAdvisor(vectorStore, SearchRequest.query(query), template))
.user(query)
.call()
.chatResponse();
LOGGER.info("Response received from call :{}", aiResponse);
Generation generation = aiResponse.getResult();
return (generation != null) ? generation.getOutput().getContent() : "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.prompt.transformer.TransformerContentType;
import org.springframework.ai.document.DocumentReader;
import org.springframework.ai.document.DocumentTransformer;
import org.springframework.ai.reader.ExtractedTextFormatter;
Expand Down Expand Up @@ -54,7 +53,7 @@ public void loadData(Resource documentResource) {
DocumentTransformer metadataEnricher = documents -> {
documents.forEach(d -> {
Map<String, Object> metadata = d.getMetadata();
metadata.put(TransformerContentType.EXTERNAL_KNOWLEDGE, "true");
metadata.put("EXTERNAL_KNOWLEDGE", "true");
});
return documents;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void testRag2() {
.post("/api/ai/chat")
.then()
.statusCode(200)
.body("queryResponse", containsString("don't"))
.body("queryResponse", containsString("No"))
.log()
.all();
}
Expand Down

0 comments on commit 143ae37

Please sign in to comment.