-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
46 additions
and
8 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
42 changes: 42 additions & 0 deletions
42
src/main/java/org/springframework/samples/petclinic/chat/ModeledQuestionAnswerAdvisor.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,42 @@ | ||
package org.springframework.samples.petclinic.chat; | ||
|
||
import org.springframework.ai.chat.client.AdvisedRequest; | ||
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor; | ||
import org.springframework.ai.chat.model.ChatModel; | ||
import org.springframework.ai.vectorstore.SearchRequest; | ||
import org.springframework.ai.vectorstore.VectorStore; | ||
|
||
import java.util.Map; | ||
|
||
public class ModeledQuestionAnswerAdvisor extends QuestionAnswerAdvisor { | ||
private static final String TRANSLATE = "Generate 1 different versions of a provided user query. " + | ||
"but they should all retain the original meaning. " + | ||
"It will be used to retrieve relevant documents and it should be in English \n" + | ||
"Without enumerations, hyphens, or any additional formatting!"; | ||
|
||
private ChatModel chatModel; | ||
|
||
public ModeledQuestionAnswerAdvisor(VectorStore vectorStore, ChatModel chatModel, String modeledText) { | ||
super(vectorStore); | ||
this.chatModel = chatModel; | ||
} | ||
|
||
public ModeledQuestionAnswerAdvisor(VectorStore vectorStore, SearchRequest searchRequest, ChatModel chatModel) { | ||
super(vectorStore, searchRequest); | ||
this.chatModel = chatModel; | ||
} | ||
|
||
public ModeledQuestionAnswerAdvisor(VectorStore vectorStore, SearchRequest searchRequest, String userTextAdvise, ChatModel chatModel) { | ||
super(vectorStore, searchRequest, userTextAdvise); | ||
this.chatModel = chatModel; | ||
} | ||
|
||
@Override | ||
public AdvisedRequest adviseRequest(AdvisedRequest request, Map<String, Object> context) { | ||
String originalUserText = request.userText(); | ||
String processedMessage = chatModel.call(TRANSLATE + "\n" + request.userText()); | ||
AdvisedRequest processedRequest = AdvisedRequest.from(request).withUserText(processedMessage).build(); | ||
request = super.adviseRequest(processedRequest, context); | ||
return AdvisedRequest.from(request).withUserText(originalUserText).build(); | ||
} | ||
} |