Package org.springframework.ai.anthropic
Class Citation
java.lang.Object
org.springframework.ai.anthropic.Citation
Represents a citation reference in a Claude response. Citations indicate which parts of
the provided documents were referenced when generating the response.
Citations are returned in the response metadata under the "citations" key and include:
- The cited text from the document
- The document index (which document was cited)
- The document title (if provided)
- Location information (character ranges, page numbers, or content block indices)
Citation Types
- CHAR_LOCATION: For plain text documents, includes character start/end indices
- PAGE_LOCATION: For PDF documents, includes page start/end numbers
- CONTENT_BLOCK_LOCATION: For custom content documents, includes block start/end indices
Example Usage
ChatResponse response = chatModel.call(prompt);
List<Citation> citations = (List<Citation>) response.getMetadata().get("citations");
for (Citation citation : citations) {
System.out.println("Document: " + citation.getDocumentTitle());
System.out.println("Location: " + citation.getLocationDescription());
System.out.println("Text: " + citation.getCitedText());
}
- Since:
- 1.1.0
- Author:
- Soby Chacko
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumTypes of citation locations based on document format. -
Method Summary
Modifier and TypeMethodDescriptionintGet a human-readable location description.getType()static CitationofCharLocation(String citedText, int documentIndex, String documentTitle, int startCharIndex, int endCharIndex) Create a character location citation for plain text documents.static CitationofContentBlockLocation(String citedText, int documentIndex, String documentTitle, int startBlockIndex, int endBlockIndex) Create a content block location citation for custom content documents.static CitationofPageLocation(String citedText, int documentIndex, String documentTitle, int startPageNumber, int endPageNumber) Create a page location citation for PDF documents.toString()
-
Method Details
-
ofCharLocation
public static Citation ofCharLocation(String citedText, int documentIndex, String documentTitle, int startCharIndex, int endCharIndex) Create a character location citation for plain text documents.- Parameters:
citedText- the text that was cited from the documentdocumentIndex- the index of the document (0-based)documentTitle- the title of the documentstartCharIndex- the starting character index (0-based, inclusive)endCharIndex- the ending character index (exclusive)- Returns:
- a new Citation with CHAR_LOCATION type
-
ofPageLocation
public static Citation ofPageLocation(String citedText, int documentIndex, String documentTitle, int startPageNumber, int endPageNumber) Create a page location citation for PDF documents.- Parameters:
citedText- the text that was cited from the documentdocumentIndex- the index of the document (0-based)documentTitle- the title of the documentstartPageNumber- the starting page number (1-based, inclusive)endPageNumber- the ending page number (exclusive)- Returns:
- a new Citation with PAGE_LOCATION type
-
ofContentBlockLocation
public static Citation ofContentBlockLocation(String citedText, int documentIndex, String documentTitle, int startBlockIndex, int endBlockIndex) Create a content block location citation for custom content documents.- Parameters:
citedText- the text that was cited from the documentdocumentIndex- the index of the document (0-based)documentTitle- the title of the documentstartBlockIndex- the starting content block index (0-based, inclusive)endBlockIndex- the ending content block index (exclusive)- Returns:
- a new Citation with CONTENT_BLOCK_LOCATION type
-
getType
-
getCitedText
-
getDocumentIndex
public int getDocumentIndex() -
getDocumentTitle
-
getStartCharIndex
-
getEndCharIndex
-
getStartPageNumber
-
getEndPageNumber
-
getStartBlockIndex
-
getEndBlockIndex
-
getLocationDescription
Get a human-readable location description. -
toString
-