I am working on creating a RAG application using Spring AI and MongoDB for the vector store.
My current entry point/controller is as follows (based heavily on Spring AI's own docs):
@RestController
public class ChatController {
private final OllamaChatModel chatModel;
private final VectorStore vectorStore;
private static final Logger logger = LoggerFactory.getLogger(ChatController.class);
@Autowired
public ChatController(OllamaChatModel chatModel, VectorStore vectorStore) {
this.chatModel = chatModel;
this.vectorStore = vectorStore;
}
@GetMapping("/ai/generate")
public Map<String, String> generate(
@RequestParam(value = "message", defaultValue = "Tell me a joke.") String message) {
var searchRequest = SearchRequest.builder().topK(3).similarityThreshold(.8).build();
ChatResponse response =
ChatClient.builder(chatModel).build().prompt(AppConstants.DEFAULT_SYS_PROMPT)
.advisors(new QuestionAnswerAdvisor(vectorStore, searchRequest)).user(message).call()
.chatResponse();
logger.info("Reponse is {}", response);
return Map.of("generation", response.getResult().getOutput().getText());
}
}
The "VectorStore" is not explicitly configured in code (i.e. no customization); it relies merely on the "spring-ai-mongodb-atlas-store-spring-boot-starter" dependency and application properties for DB url, DB name, vector index, etc.
The collection used in the VectorStore has a "url" String property set in the "metadata" Object property. I configured that "metadata.url" is available in the documents that are used in the "before" function of QuestionAnswerAdvisor.
How can I have the model return a link to the source URL it uses as context? For reference, I am trying to recreate something like this: /
I have tried the following:
ChatClient.builder(chatModel).build().prompt(AppConstants.DEFAULT_SYS_PROMPT)
.advisors(new QuestionAnswerAdvisor(vectorStore, searchRequest)).user(message).call()
.entity(ResponseModel.class)
where ResponseModel is a very simple POJO:
public class ResponseModel {
private String response;
private String url;
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
but the application was not able to populate the URL with the data (the response property was set correctly).
How can this be implemented in SpringAI? Thanks.