Skip to content

Anthropic

Installation

You need to install anthropic to use this in blendsql.

AnthropicLLM

Bases: RemoteModel

Class for Anthropic Model API.

Parameters:

Name Type Description Default
model_name_or_path str

Name of the Anthropic model to use

required
env str

Path to directory of .env file, or to the file itself to load as a dotfile. Should contain the variable ANTHROPIC_API_KEY

'.'
config Optional[dict]

Optional argument mapping to use in loading model

None
caching bool

Bool determining whether we access the model's cache

True

Examples:

Given the following .env file in the directory above current:

ANTHROPIC_API_KEY=my_api_key
from blendsql.models import AnthropicLLM

model = AnthropicLLM(
    "claude-3-5-sonnet-20240620",
    env="..",
    config={"temperature": 0.7}
)

Source code in blendsql/models/remote/_anthropic.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class AnthropicLLM(RemoteModel):
    """Class for Anthropic Model API.

    Args:
        model_name_or_path: Name of the Anthropic model to use
        env: Path to directory of .env file, or to the file itself to load as a dotfile.
            Should contain the variable `ANTHROPIC_API_KEY`
        config: Optional argument mapping to use in loading model
        caching: Bool determining whether we access the model's cache

    Examples:
        Given the following `.env` file in the directory above current:
        ```text
        ANTHROPIC_API_KEY=my_api_key
        ```
        ```python
        from blendsql.models import AnthropicLLM

        model = AnthropicLLM(
            "claude-3-5-sonnet-20240620",
            env="..",
            config={"temperature": 0.7}
        )
        ```
    """

    def __init__(
        self,
        model_name_or_path: str,
        env: str = ".",
        config: Optional[dict] = None,
        caching: bool = True,
        **kwargs,
    ):
        if not _has_anthropic:
            raise ImportError(
                "Please install anthropic with `pip install anthropic`!"
            ) from None

        if config is None:
            config = {}
        super().__init__(
            model_name_or_path=model_name_or_path,
            # No public anthropic tokenizer ):
            tokenizer=None,
            requires_config=True,
            refresh_interval_min=30,
            load_model_kwargs=config | DEFAULT_CONFIG,
            env=env,
            caching=caching,
            **kwargs,
        )

    def _load_model(self) -> ModelObj:
        from guidance.models import Anthropic

        return Anthropic(
            self.model_name_or_path, echo=False, api_key=os.getenv("ANTHROPIC_API_KEY")
        )