Extracting Source URLs from MongoDB Vector Store in Spring AI RAG Implementation

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.

Had this same problem a few weeks ago. Create a custom advisor that extends QuestionAnswerAdvisor and override the getDocuments() method to expose the retrieved documents. You’ll get both the chat response and source metadata. I also tried storing search results in a ThreadLocal variable during advisor execution, then grabbing them after the chat call finishes. Both ways give you clean access to all document metadata and source URLs without messing with prompts or running separate queries.

The problem is that Spring AI’s .entity(AiResponse.class) only grabs the LLM’s text response - it doesn’t capture the RAG context metadata with your source URLs. Here’s how to fix it: run the similarity search yourself before or after the chat call. This way you’ll get the full Document objects with all their metadata directly from the vector store. You could also try tweaking your system prompt to make the LLM include source citations in its response, but that’s pretty hit-or-miss.

Yeah, ran into this last month too. QuestionAnswerAdvisor hides the source docs, which is super annoying. Easy fix though - use the advisor’s context parameter. After the call finishes, grab advisor.getContext().getDocuments() and pull the source metadata from there. It’s hacky but works until Spring AI fixes it.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.