Skip to content

Commit

Permalink
feat : fix compilation issue
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Jun 13, 2024
1 parent b386585 commit 013d514
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 37 deletions.
9 changes: 9 additions & 0 deletions chatbot/chatbot-openai-springai/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pgvector-store-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>
Expand Down Expand Up @@ -64,6 +68,11 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,31 @@
package com.example.chatbot.config;

import java.util.List;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.client.advisor.VectorStoreChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryChatServiceListener;
import org.springframework.ai.chat.memory.ChatMemoryRetriever;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.chat.memory.LastMaxTokenSizeContentTransformer;
import org.springframework.ai.chat.memory.SystemPromptChatMemoryAugmentor;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.service.ChatService;
import org.springframework.ai.chat.service.PromptTransformingChatService;
import org.springframework.ai.tokenizer.JTokkitTokenCountEstimator;
import org.springframework.ai.tokenizer.TokenCountEstimator;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class ChatConfig {

@Bean
ChatMemory chatHistory() {
ChatMemory chatMemory() {
return new InMemoryChatMemory();
}

@Bean
TokenCountEstimator tokenCountEstimator() {
return new JTokkitTokenCountEstimator();
}

@Bean
ChatService chatService(ChatModel chatModel, ChatMemory chatMemory, TokenCountEstimator tokenCountEstimator) {
return 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)))
ChatClient chatClient(ChatClient.Builder chatClientBuilder, ChatMemory chatMemory, VectorStore vectorStore) {
return chatClientBuilder
.defaultAdvisors(
new MessageChatMemoryAdvisor(chatMemory),
new QuestionAnswerAdvisor(vectorStore, SearchRequest.defaults()), // RAG
new VectorStoreChatMemoryAdvisor(vectorStore))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
package com.example.chatbot.service;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

import com.example.chatbot.model.request.AIChatRequest;
import com.example.chatbot.model.response.AIChatResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.messages.UserMessage;
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.client.ChatClient;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.stereotype.Service;

@Service
public class ChatbotService {

private static final Logger LOGGER = LoggerFactory.getLogger(ChatbotService.class);

private final ChatService chatService;
private final ChatClient chatClient;

public ChatbotService(ChatService chatService) {
this.chatService = chatService;
public ChatbotService(ChatClient chatClient) {
this.chatClient = chatClient;
}

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.getPromptContext().getConversationId());

ChatResponse chatResponse = this.chatClient
.prompt()
.user(request.query())
.advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, request.conversationId())
.param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 100))
.call()
.chatResponse();
LOGGER.info("Response :{}", chatResponse.getResult());
return new AIChatResponse(chatResponse.getResult().getOutput().getContent(), request.conversationId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

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.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;

@TestConfiguration(proxyBeanMethods = false)
public class TestChatbotOpenaiApplication {

@Bean
@ServiceConnection
PostgreSQLContainer<?> pgvectorContainer() {
return new PostgreSQLContainer<>(DockerImageName.parse("pgvector/pgvector:pg16"));
}

public static void main(String[] args) {
SpringApplication.from(ChatbotOpenaiApplication::main)
.with(TestChatbotOpenaiApplication.class)
Expand Down

0 comments on commit 013d514

Please sign in to comment.