-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : adds vector store to chatbot (#58)
* feat : adds vector store to chatbot * Update application.properties * upgrade to latest version * Update pom.xml * fix : wrong pom update * adds sequence diagram * tweak prompt for more data accuracy * feat : removes test case which doesnt adds context * feat : return conversationalId * reads conversational Id and passes to next question * minor polish
- Loading branch information
1 parent
f9f3956
commit b2692dd
Showing
10 changed files
with
152 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
chatbot-ollama-springai/src/main/java/com/example/chatbot/model/request/AIChatRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package com.example.chatbot.model.request; | ||
|
||
public record AIChatRequest(String query) {} | ||
public record AIChatRequest(String query, String conversationId) {} |
2 changes: 1 addition & 1 deletion
2
chatbot-ollama-springai/src/main/java/com/example/chatbot/model/response/AIChatResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package com.example.chatbot.model.response; | ||
|
||
public record AIChatResponse(String answer) {} | ||
public record AIChatResponse(String answer, String conversationId) {} |
30 changes: 14 additions & 16 deletions
30
chatbot-ollama-springai/src/main/java/com/example/chatbot/service/ChatbotService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,34 @@ | ||
package com.example.chatbot.service; | ||
|
||
import com.example.chatbot.model.request.AIChatRequest; | ||
import com.example.chatbot.model.response.AIChatResponse; | ||
import java.util.List; | ||
import org.springframework.ai.chat.memory.*; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.ai.chat.messages.UserMessage; | ||
import org.springframework.ai.chat.model.ChatModel; | ||
import org.springframework.ai.chat.prompt.Prompt; | ||
import org.springframework.ai.chat.prompt.transformer.ChatServiceContext; | ||
import org.springframework.ai.chat.service.ChatService; | ||
import org.springframework.ai.chat.service.ChatServiceResponse; | ||
import org.springframework.ai.chat.service.PromptTransformingChatService; | ||
import org.springframework.ai.tokenizer.TokenCountEstimator; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class ChatbotService { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ChatbotService.class); | ||
|
||
private final ChatService chatService; | ||
|
||
ChatbotService(ChatModel chatModel, ChatMemory chatMemory, TokenCountEstimator tokenCountEstimator) { | ||
this.chatService = PromptTransformingChatService.builder(chatModel) | ||
.withRetrievers(List.of(new ChatMemoryRetriever(chatMemory))) | ||
.withContentPostProcessors(List.of(new LastMaxTokenSizeContentTransformer(tokenCountEstimator, 1000))) | ||
.withAugmentors(List.of(new SystemPromptChatMemoryAugmentor())) | ||
.withChatServiceListeners(List.of(new ChatMemoryChatServiceListener(chatMemory))) | ||
.build(); | ||
public ChatbotService(ChatService chatService) { | ||
this.chatService = chatService; | ||
} | ||
|
||
public AIChatResponse chat(String message) { | ||
Prompt prompt = new Prompt(new UserMessage(message)); | ||
ChatServiceResponse chatServiceResponse = this.chatService.call(new ChatServiceContext(prompt)); | ||
public AIChatResponse chat(AIChatRequest request) { | ||
Prompt prompt = new Prompt(new UserMessage(request.query())); | ||
String conversationId = request.conversationId() == null ? "default" : request.conversationId(); | ||
ChatServiceResponse chatServiceResponse = this.chatService.call(new ChatServiceContext(prompt, conversationId)); | ||
LOGGER.info("Response :{}", chatServiceResponse.getChatResponse().getResult()); | ||
return new AIChatResponse( | ||
chatServiceResponse.getChatResponse().getResult().getOutput().getContent()); | ||
chatServiceResponse.getChatResponse().getResult().getOutput().getContent(), | ||
chatServiceResponse.getPromptContext().getConversationId()); | ||
} | ||
} |
6 changes: 5 additions & 1 deletion
6
chatbot-ollama-springai/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
spring.application.name=chatbot-ollama | ||
|
||
spring.threads.virtual.enabled=true | ||
spring.mvc.problemdetails.enabled=true | ||
|
||
spring.ai.ollama.chat.options.model=llama3 | ||
spring.ai.ollama.embedding.options.model=llama3 | ||
|
||
spring.threads.virtual.enabled=true | ||
spring.testcontainers.beans.startup=parallel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters