Module: claude_llm

Expand source code
# Copyright (C) 2023-present The Project Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from dataclasses import dataclass
from typing import ClassVar
from anthropic import Anthropic
from cl.convince.llms.llm import Llm
from cl.convince.settings.anthropic_settings import AnthropicSettings


@dataclass(slots=True, kw_only=True)
class ClaudeLlm(Llm):
    """Implements Claude LLM API."""

    model_name: str | None = None
    """Model name in Anthropic format including version if any, defaults to 'llm_id'."""

    max_tokens: int = 4096
    """Maximum number of tokens the model will generate in response to the query."""

    _client: ClassVar[Anthropic] = None
    """Anthropic client instance."""

    def uncached_completion(self, request_id: str, query: str) -> str:
        """Perform completion without CompletionCache lookup, call completion instead."""

        # Prefix a unique RequestID to the model for audit log purposes and
        # to stop model provider from caching the results
        query_with_request_id = f"RequestID: {request_id}nn{query}"

        model_name = self.model_name if self.model_name is not None else self.llm_id
        messages = [{"role": "user", "content": query_with_request_id}]
        client = self._get_client()
        response = client.messages.create(
            model=model_name,
            messages=messages,
            max_tokens=self.max_tokens,
        )
        if len(response.content) != 1:
            raise RuntimeError(f"More than one response message received for query: {query}: {str(response)}")
        result = response.content[0].text
        return result

    @classmethod
    def _get_client(cls) -> Anthropic:
        """Instantiate and cache the Anthropic client instance."""
        if cls._client is None:
            cls._client = Anthropic(
                api_key=AnthropicSettings.instance().api_key,
            )
        return cls._client

Classes

class ClaudeLlm (*, llm_id: str = None, model_name: str | None = None, max_tokens: int = 4096)

Implements Claude LLM API.

Expand source code
@dataclass(slots=True, kw_only=True)
class ClaudeLlm(Llm):
    """Implements Claude LLM API."""

    model_name: str | None = None
    """Model name in Anthropic format including version if any, defaults to 'llm_id'."""

    max_tokens: int = 4096
    """Maximum number of tokens the model will generate in response to the query."""

    _client: ClassVar[Anthropic] = None
    """Anthropic client instance."""

    def uncached_completion(self, request_id: str, query: str) -> str:
        """Perform completion without CompletionCache lookup, call completion instead."""

        # Prefix a unique RequestID to the model for audit log purposes and
        # to stop model provider from caching the results
        query_with_request_id = f"RequestID: {request_id}nn{query}"

        model_name = self.model_name if self.model_name is not None else self.llm_id
        messages = [{"role": "user", "content": query_with_request_id}]
        client = self._get_client()
        response = client.messages.create(
            model=model_name,
            messages=messages,
            max_tokens=self.max_tokens,
        )
        if len(response.content) != 1:
            raise RuntimeError(f"More than one response message received for query: {query}: {str(response)}")
        result = response.content[0].text
        return result

    @classmethod
    def _get_client(cls) -> Anthropic:
        """Instantiate and cache the Anthropic client instance."""
        if cls._client is None:
            cls._client = Anthropic(
                api_key=AnthropicSettings.instance().api_key,
            )
        return cls._client

Ancestors

Static methods

def get_key_type() -> Type

Inherited from: Llm.get_key_type

Return key type even when called from a record.

Fields

var llm_id -> str

Inherited from: Llm.llm_id

Unique LLM identifier.

var max_tokens -> int

Maximum number of tokens the model will generate in response to the query.

var model_name -> str | None

Model name in Anthropic format including version if any, defaults to ‘llm_id’.

Methods

def completion(self, query: str, *, trial_id: str | int | None = None) -> str

Inherited from: Llm.completion

Text-in, text-out single query completion without model-specific tags (uses response caching).

def get_key(self) -> LlmKey

Inherited from: Llm.get_key

Return a new key object whose fields populated from self, do not return self.

def init_all(self) -> None

Inherited from: Llm.init_all

Invoke ‘init’ for each class in the order from base to derived, then validate against schema.

def uncached_completion(self, request_id: str, query: str) -> str

Inherited from: Llm.uncached_completion

Perform completion without CompletionCache lookup, call completion instead.