Source code for neo4j_graphrag.llm.anthropic_llm

#  Neo4j Sweden AB [https://neo4j.com]
#  #
#  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
#  #
#      https://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 __future__ import annotations

from typing import Any, Optional

from neo4j_graphrag.exceptions import LLMGenerationError
from neo4j_graphrag.llm.base import LLMInterface
from neo4j_graphrag.llm.types import LLMResponse


[docs] class AnthropicLLM(LLMInterface): """Interface for large language models on Anthropic Args: model_name (str, optional): Name of the LLM to use. Defaults to "gemini-1.5-flash-001". model_params (Optional[dict], optional): Additional parameters passed to the model when text is sent to it. Defaults to None. **kwargs (Any): Arguments passed to the model when for the class is initialised. Defaults to None. Raises: LLMGenerationError: If there's an error generating the response from the model. Example: .. code-block:: python from neo4j_graphrag.llm import AnthropicLLM llm = AnthropicLLM( model_name="claude-3-opus-20240229", model_params={"max_tokens": 1000}, api_key="sk...", # can also be read from env vars ) llm.invoke("Who is the mother of Paul Atreides?") """ def __init__( self, model_name: str, model_params: Optional[dict[str, Any]] = None, **kwargs: Any, ): try: import anthropic except ImportError: raise ImportError( """Could not import Anthropic Python client. Please install it with `pip install "neo4j-graphrag[anthropic]"`.""" ) super().__init__(model_name, model_params) self.anthropic = anthropic self.client = anthropic.Anthropic(**kwargs) self.async_client = anthropic.AsyncAnthropic(**kwargs)
[docs] def invoke(self, input: str) -> LLMResponse: """Sends text to the LLM and returns a response. Args: input (str): The text to send to the LLM. Returns: LLMResponse: The response from the LLM. """ try: response = self.client.messages.create( model=self.model_name, messages=[ { "role": "user", "content": input, } ], **self.model_params, ) return LLMResponse(content=response.content) except self.anthropic.APIError as e: raise LLMGenerationError(e)
[docs] async def ainvoke(self, input: str) -> LLMResponse: """Asynchronously sends text to the LLM and returns a response. Args: input (str): The text to send to the LLM. Returns: LLMResponse: The response from the LLM. """ try: response = await self.async_client.messages.create( model=self.model_name, messages=[ { "role": "user", "content": input, } ], **self.model_params, ) return LLMResponse(content=response.content) except self.anthropic.APIError as e: raise LLMGenerationError(e)