Skip to content

Conversation

sobychacko
Copy link
Contributor

Implement Anthropic's Citations API to enable Claude to reference
specific parts of provided documents when generating responses.

New classes:

  • Citation: Represents citation metadata with three location types (CHAR_LOCATION, PAGE_LOCATION, CONTENT_BLOCK_LOCATION)
  • CitationDocument: Builder for creating citation-enabled documents supporting plain text, PDF, and custom content blocks

Implementation changes:

  • AnthropicChatModel: Extract citations from API responses and aggregate via CitationContext into response metadata
  • AnthropicChatOptions: Add citationDocuments configuration field
  • AnthropicApi: Add CitationResponse and CitationsConfig records

Design decisions:

  • Static factory methods for Citation instantiation
  • Response-level metadata only (no message-level storage)
  • Opt-in behavior with citationsEnabled defaulting to false
  • Defensive logging for unexpected citation object types

Testing:

  • Integration tests for plain text, PDF, custom content, and multiple document scenarios

Documentation:

  • Reference documentation in anthropic-chat.adoc with usage examples, real-world use cases, and best practices

Supported on Claude 3.7 Sonnet and Claude 4 models.

Thank you for taking time to contribute this pull request!
You might have already read the contributor guide, but as a reminder, please make sure to:

  • Add a Signed-off-by line to each commit (git commit -s) per the DCO
  • Rebase your changes on the latest main branch and squash your commits
  • Add/Update unit tests as needed
  • Run a build and make sure all tests pass prior to submission

For more details, please check the contributor guide.
Thank you upfront!

  Implement Anthropic's Citations API to enable Claude to reference
  specific parts of provided documents when generating responses.

  New classes:
  - `Citation`: Represents citation metadata with three location types
    (`CHAR_LOCATION`, `PAGE_LOCATION`, `CONTENT_BLOCK_LOCATION`)
  - `CitationDocument`: Builder for creating citation-enabled documents
    supporting plain text, PDF, and custom content blocks

  Implementation changes:
  - `AnthropicChatModel`: Extract citations from API responses and aggregate
    via `CitationContext` into response metadata
  - `AnthropicChatOptions`: Add `citationDocuments` configuration field
  - `AnthropicApi`: Add `CitationResponse` and `CitationsConfig` records

  Design decisions:
  - Static factory methods for `Citation` instantiation
  - Response-level metadata only (no message-level storage)
  - Opt-in behavior with `citationsEnabled` defaulting to false
  - Defensive logging for unexpected citation object types

  Testing:
  - Integration tests for plain text, PDF, custom content, and multiple
    document scenarios

  Documentation:
  - Reference documentation in `anthropic-chat.adoc` with usage examples,
    real-world use cases, and best practices

  Supported on Claude 3.7 Sonnet and Claude 4 models.

Signed-off-by: Soby Chacko <soby.chacko@broadcom.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants