Skip to content

langchain.ChatAnthropicDataset

kedro_datasets.langchain.ChatAnthropicDataset

ChatAnthropicDataset(credentials={}, kwargs={})

Bases: AbstractDataset[None, ChatAnthropic]

ChatAnthropicDataset loads a ChatAnthropic langchain model.

Example usage for the YAML API

catalog.yml

claude_instant_1:
    type: langchain.ChatAnthropicDataset
    kwargs:
        model: "claude-instant-1"
        temperature: 0.0
    credentials: anthropic  # Optional, can use environment variables instead

credentials.yml (optional if using environment variables) If credentials are passed through credentials.yml, they take precedence over environment variables.

anthropic:
    base_url: <anthropic-api-base>  # Optional, defaults to Anthropic default
    api_key: <anthropic-api-key>   # Optional if ANTHROPIC_API_KEY is set

Or use environment variables:

export ANTHROPIC_API_KEY=<your-api-key>
export ANTHROPIC_API_URL=<anthropic-api-base>  # Optional

Example usage for the Python API
from kedro_datasets.langchain import ChatAnthropicDataset

# With explicit credentials
llm = ChatAnthropicDataset(
    credentials={
        "base_url": "xxx",
        "api_key": "xxx",  # pragma: allowlist secret
    },
    kwargs={
        "model": "claude-instant-1",
        "temperature": 0.0,
    },
).load()

# Or without credentials (using environment variables)
llm = ChatAnthropicDataset(
    kwargs={
        "model": "claude-instant-1",
        "temperature": 0.0,
    },
).load()

# See: https://python.langchain.com/docs/integrations/chat/anthropic
llm.invoke("Hello world!")

Parameters:

  • credentials (Optional, default: {} ) –

    contains api_key and base_url. If not provided, will use environment variables ANTHROPIC_API_KEY and ANTHROPIC_API_URL.

  • kwargs (dict[str, Any], default: {} ) –

    keyword arguments passed to the ChatAnthropic constructor.

Source code in kedro_datasets/langchain/chat_anthropic_dataset.py
72
73
74
75
76
77
78
79
80
81
def __init__(self, credentials: dict[str, str] = {}, kwargs: dict[str, Any] = {}):
    """Constructor.

    Args:
        credentials (Optional): contains `api_key` and `base_url`.
            If not provided, will use environment variables ANTHROPIC_API_KEY and ANTHROPIC_API_URL.
        kwargs: keyword arguments passed to the ChatAnthropic constructor.
    """
    self.credentials = credentials or {}
    self.kwargs = kwargs or {}

credentials instance-attribute

credentials = credentials or {}

kwargs instance-attribute

kwargs = kwargs or {}

_describe

_describe()

Returns a description of the dataset.

Returns:

  • dict[str, Any]

    Dictionary containing the kwargs passed to ChatAnthropic.

Source code in kedro_datasets/langchain/chat_anthropic_dataset.py
83
84
85
86
87
88
89
90
91
92
def _describe(self) -> dict[str, Any]:
    """Returns a description of the dataset.

    Returns:
        dict[str, Any]: Dictionary containing the kwargs passed to ChatAnthropic.
    """
    credentials = (
        {k: "***" for k in self.credentials.keys()} if self.credentials else {}
    )
    return {**credentials, **self.kwargs}

load

load()

Load and return a ChatAnthropic model instance.

Constructs a ChatAnthropic instance using the provided kwargs and optional credentials. If credentials are not provided, the ChatAnthropic instance will automatically use environment variables ANTHROPIC_API_KEY and ANTHROPIC_API_URL for authentication.

Returns:

  • ChatAnthropic

    A configured ChatAnthropic model instance.

Source code in kedro_datasets/langchain/chat_anthropic_dataset.py
102
103
104
105
106
107
108
109
110
111
112
113
def load(self) -> ChatAnthropic:
    """Load and return a ChatAnthropic model instance.

    Constructs a ChatAnthropic instance using the provided kwargs and optional
    credentials. If credentials are not provided, the ChatAnthropic instance
    will automatically use environment variables ANTHROPIC_API_KEY and
    ANTHROPIC_API_URL for authentication.

    Returns:
        ChatAnthropic: A configured ChatAnthropic model instance.
    """
    return ChatAnthropic(**self.credentials, **self.kwargs)  # type: ignore[arg-type]

save

save(data)

Save operation is not supported for ChatAnthropicDataset.

Raises:

  • DatasetError

    Always raised as this dataset is read-only.

Source code in kedro_datasets/langchain/chat_anthropic_dataset.py
 94
 95
 96
 97
 98
 99
100
def save(self, data: None) -> NoReturn:
    """Save operation is not supported for ChatAnthropicDataset.

    Raises:
        DatasetError: Always raised as this dataset is read-only.
    """
    raise DatasetError(f"{self.__class__.__name__} is a read only dataset type")