Replies: 1 comment
|
Spring AI's default Accessing grounding metadataimport org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.metadata.ChatResponseMetadata;
import org.springframework.ai.vertexai.gemini.VertexAiGeminiChatOptions;
@Service
public class CitationService {
private final ChatClient chatClient;
public CitationService(ChatClient.Builder builder) {
this.chatClient = builder
.defaultOptions(VertexAiGeminiChatOptions.builder()
.model("gemini-2.0-flash-001")
.googleSearchRetrieval(true) // enable grounding
.build())
.build();
}
public void askWithCitations(String question) {
ChatResponse response = chatClient.prompt()
.user(question)
.call()
.chatResponse();
// Grounding metadata lives in ChatResponseMetadata under "groundingMetadata"
ChatResponseMetadata metadata = response.getMetadata();
// The raw VertexAI grounding metadata object
Object groundingRaw = metadata.get("groundingMetadata");
if (groundingRaw != null) {
// Cast to the VertexAI Gemini type
// com.google.cloud.vertexai.api.GroundingMetadata
var groundingMetadata = (com.google.cloud.vertexai.api.GroundingMetadata) groundingRaw;
groundingMetadata.getGroundingChunksList().forEach(chunk -> {
if (chunk.hasWeb()) {
System.out.println("Source: " + chunk.getWeb().getUri());
System.out.println("Title: " + chunk.getWeb().getTitle());
}
});
// Grounding supports also gives you the text segments and confidence scores
groundingMetadata.getGroundingSupports().forEach(support -> {
System.out.println("Claim: " + support.getSegment().getText());
System.out.println("Confidence: " + support.getConfidenceScoresList());
});
}
}
}Alternative: access from generation-level metadataThe grounding data can also appear per response.getResults().forEach(generation -> {
var meta = generation.getMetadata();
// Try the raw map key
Object grounding = meta.get("groundingMetadata");
if (grounding != null) {
// process same as above
}
});Why
|
Uh oh!
There was an error while loading. Please reload this page.
I am creating a Chatbot in spring-ai using VertexAI and i want to add citation to the response generated, but I am having probelms getting the cited sources from the ChatResponse object. I have set the .googleSearchRetreival(TRUE) and i am able to get answers to recent events, but when I look through the MetaData for citations, its being populated with the DefaultChatGenerationMetaData which does not contain the citation sources (ie. groundingMetadata)
Any help regarding this would be helpful
All reactions