-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update library Spring Ai 1.0.0-Snapshot
- Loading branch information
Showing
8 changed files
with
153 additions
and
112 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
44 changes: 44 additions & 0 deletions
44
src/main/java/com/ai/springdemo/aispringdemo/controller/ChatController.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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.ai.springdemo.aispringdemo.controller; | ||
|
||
import com.ai.springdemo.aispringdemo.exception.InvalidMessageException; | ||
import com.ai.springdemo.aispringdemo.service.ChatService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import reactor.core.publisher.Flux; | ||
|
||
@RestController | ||
public class ChatController { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ChatController.class); | ||
|
||
private final ChatService questionService; | ||
|
||
@Autowired | ||
public ChatController(ChatService questionService){ | ||
this.questionService = questionService; | ||
} | ||
|
||
@PostMapping("/chat") | ||
public Flux<String> getQuestion(@RequestBody String question){ | ||
|
||
logger.info("Received question: {}", question); | ||
|
||
if (question == null || question.trim().isEmpty()) { | ||
logger.error("Invalid question received: empty or null message"); | ||
return Flux.error(new InvalidMessageException("Question cannot be null or empty")); | ||
} | ||
|
||
return questionService.chat(question) | ||
.doOnError(error -> logger.error("Error processing question: {}", error.getMessage())); | ||
} | ||
|
||
@ExceptionHandler(InvalidMessageException.class) | ||
public ResponseEntity<String> handleInvalidMessageException(InvalidMessageException e) { | ||
logger.error("Invalid message error: {}", e.getMessage()); | ||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST); | ||
} | ||
} |
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
34 changes: 0 additions & 34 deletions
34
src/main/java/com/ai/springdemo/aispringdemo/controller/QuestionController.java
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
src/main/java/com/ai/springdemo/aispringdemo/exception/InvalidMessageException.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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.ai.springdemo.aispringdemo.exception; | ||
|
||
public class InvalidMessageException extends RuntimeException { | ||
|
||
public InvalidMessageException(String message) { | ||
super(message); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
src/main/java/com/ai/springdemo/aispringdemo/service/ChatService.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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.ai.springdemo.aispringdemo.service; | ||
|
||
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.model.ChatResponse; | ||
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.VectorStore; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.stereotype.Service; | ||
import reactor.core.publisher.Flux; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
public class ChatService { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ChatService.class); | ||
|
||
@Value("classpath:/prompts/system-qa.st") | ||
private Resource qaSystemPromptResource; | ||
|
||
@Value("classpath:/prompts/system-chatbot.st") | ||
private Resource chatbotSystemPromptResource; | ||
|
||
private final ChatClient chatClient; | ||
|
||
|
||
private final VectorStore vectorStore; | ||
|
||
@Autowired | ||
public ChatService(ChatClient.Builder chatClientBuilder, VectorStore vectorStore) { | ||
this.chatClient = chatClientBuilder.build(); | ||
this.vectorStore = vectorStore; | ||
} | ||
|
||
public Flux<String> chat(String userMessageText) { | ||
Message systemMessage = createSystemMessage(userMessageText); | ||
UserMessage userMessage = new UserMessage(userMessageText); | ||
Prompt prompt = createPrompt(systemMessage, userMessage); | ||
|
||
logger.info("Sending prompt to AI model."); | ||
|
||
return chatClient.prompt(prompt) | ||
.stream() | ||
.chatResponse() | ||
.flatMap(this::processChatResponse) | ||
.doOnComplete(() -> logger.info("AI response processing completed.")) | ||
.doOnError(error -> logger.error("Error during AI response processing", error)); | ||
} | ||
|
||
private Message createSystemMessage(String userMessageText) { | ||
logger.info("Fetching relevant documents for message: {}", userMessageText); | ||
List<Document> relevantDocuments = vectorStore.similaritySearch(userMessageText); | ||
logger.info("Found {} relevant documents.", relevantDocuments.size()); | ||
String documentsContent = relevantDocuments.stream() | ||
.map(Document::getContent) | ||
.collect(Collectors.joining("\n")); | ||
return new SystemPromptTemplate(qaSystemPromptResource) | ||
.createMessage(Map.of("documents", documentsContent)); | ||
} | ||
|
||
private Prompt createPrompt(Message systemMessage, UserMessage userMessage) { | ||
return new Prompt(List.of(systemMessage, userMessage)); | ||
} | ||
|
||
private Flux<String> processChatResponse(ChatResponse chatResponse) { | ||
return Optional.ofNullable(chatResponse) | ||
.filter(response -> response.getResults() != null && !response.getResults().isEmpty()) | ||
.map(response -> response.getResults().getFirst()) | ||
.map(result -> result.getOutput().getContent()) | ||
.filter(content -> content != null) | ||
.map(Flux::just) | ||
.orElseGet(() -> { | ||
logger.warn("Received an empty or null response."); | ||
return Flux.empty(); | ||
}); | ||
} | ||
} |
76 changes: 0 additions & 76 deletions
76
src/main/java/com/ai/springdemo/aispringdemo/service/QuestionService.java
This file was deleted.
Oops, something went wrong.
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