I’m building a RAG system with Spring AI using MongoDB as my vector database. I need help getting the source URLs from my vector store results.
Here’s my current setup:
@RestController
public class AiController {
private final OllamaChatModel aiModel;
private final VectorStore docStore;
private static final Logger log = LoggerFactory.getLogger(AiController.class);
@Autowired
public AiController(OllamaChatModel aiModel, VectorStore docStore) {
this.aiModel = aiModel;
this.docStore = docStore;
}
@GetMapping("/chat/ask")
public Map<String, String> askQuestion(
@RequestParam(value = "query", defaultValue = "What is AI?") String query) {
var queryRequest = SearchRequest.builder().topK(5).similarityThreshold(.75).build();
ChatResponse result =
ChatClient.builder(aiModel).build().prompt(SystemPrompts.MAIN_PROMPT)
.advisors(new QuestionAnswerAdvisor(docStore, queryRequest)).user(query).call()
.chatResponse();
log.info("AI Result: {}", result);
return Map.of("answer", result.getResult().getOutput().getText());
}
}
I’m using the mongodb atlas starter without custom config. My vector collection has documents with a source field in the metadata object. I can see this data is available in the QuestionAnswerAdvisor.
I tried creating a response class to capture both answer and source:
public class AiResponse {
private String answer;
private String source;
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
}
But when I use .entity(AiResponse.class) the source field stays empty while the answer works fine.
How do I get Spring AI to return the source URLs along with the generated response? I want to show users where the information came from.