
    Zǻi%                        d dl mZ d dlZd dlmZmZ d dlmZmZm	Z	m
Z
mZmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ d
dlmZmZ  ej6                  e      Z G d de      Z G d de      Zy)    )annotationsN)ABCabstractmethod)AnyListOptionalSequenceTypeUnion)	BaseModel)MessageHistory)Tool)
LLMMessage)DEFAULT_RATE_LIMIT_HANDLERRateLimitHandler   )LLMResponseToolCallResponsec                      e Zd ZU dZdZded<   	 	 	 d	 	 	 	 	 	 	 ddZe	 	 d	 	 	 	 	 	 	 dd       Ze	 	 d	 	 	 	 	 	 	 dd       Z		 	 d	 	 	 	 	 	 	 	 	 dd	Z
	 	 d	 	 	 	 	 	 	 	 	 dd
Zy)LLMInterfacea  Interface for large language models.

    Args:
        model_name (str): The name of the language model.
        model_params (Optional[dict]): Additional parameters passed to the model when text is sent to it. Defaults to None.
        rate_limit_handler (Optional[RateLimitHandler]): Handler for rate limiting. Defaults to retry with exponential backoff.
        **kwargs (Any): Arguments passed to the model when for the class is initialised. Defaults to None.
    Fboolsupports_structured_outputNc                |    t         j                  d       || _        |xs i | _        ||| _        y t
        | _        y )NzeLLMInterface is deprecated and will be removed in future versions. Please use LLMInterfaceV2 instead.)loggerwarning
model_namemodel_params_rate_limit_handlerr   selfr   r   rate_limit_handlerkwargss        U/opt/lhia/marcimex/agent/venv/lib/python3.12/site-packages/neo4j_graphrag/llm/base.py__init__zLLMInterface.__init__3   s?     	s	
 %(.B)'9D$'AD$    c                     y)a3  Sends a text input to the LLM and retrieves a response.

        Args:
            input (str): Text sent to the LLM.
            message_history (Optional[Union[List[LLMMessage], MessageHistory]]): A collection previous messages,
                with each message having a specific role assigned.
            system_instruction (Optional[str]): An option to override the llm system message for this invocation.

        Returns:
            LLMResponse: The response from the LLM.

        Raises:
            LLMGenerationError: If anything goes wrong.
        N r    inputmessage_historysystem_instructions       r#   invokezLLMInterface.invokeE       r%   c                   K   yw)aB  Asynchronously sends a text input to the LLM and retrieves a response.

        Args:
            input (str): Text sent to the LLM.
            message_history (Optional[Union[List[LLMMessage], MessageHistory]]): A collection previous messages,
                with each message having a specific role assigned.
            system_instruction (Optional[str]): An option to override the llm system message for this invocation.

        Returns:
            LLMResponse: The response from the LLM.

        Raises:
            LLMGenerationError: If anything goes wrong.
        Nr'   r(   s       r#   ainvokezLLMInterface.ainvoke[           c                    t        d      )a  Sends a text input to the LLM with tool definitions and retrieves a tool call response.

        This is a default implementation that should be overridden by LLM providers that support tool/function calling.

        Args:
            input (str): Text sent to the LLM.
            tools (Sequence[Tool]): Sequence of Tools for the LLM to choose from. Each LLM implementation should handle the conversion to its specific format.
            message_history (Optional[Union[List[LLMMessage], MessageHistory]]): A collection previous messages,
                with each message having a specific role assigned.
            system_instruction (Optional[str]): An option to override the llm system message for this invocation.

        Returns:
            ToolCallResponse: The response from the LLM containing a tool call.

        Raises:
            LLMGenerationError: If anything goes wrong.
            NotImplementedError: If the LLM provider does not support tool calling.
        0This LLM provider does not support tool calling.NotImplementedErrorr    r)   toolsr*   r+   s        r#   invoke_with_toolszLLMInterface.invoke_with_toolsq   s    2 ""TUUr%   c                    K   t        d      w)a  Asynchronously sends a text input to the LLM with tool definitions and retrieves a tool call response.

        This is a default implementation that should be overridden by LLM providers that support tool/function calling.

        Args:
            input (str): Text sent to the LLM.
            tools (Sequence[Tool]): Sequence of Tools for the LLM to choose from. Each LLM implementation should handle the conversion to its specific format.
            message_history (Optional[Union[List[LLMMessage], MessageHistory]]): A collection previous messages,
                with each message having a specific role assigned.
            system_instruction (Optional[str]): An option to override the llm system message for this invocation.

        Returns:
            ToolCallResponse: The response from the LLM containing a tool call.

        Raises:
            LLMGenerationError: If anything goes wrong.
            NotImplementedError: If the LLM provider does not support tool calling.
        r3   r4   r6   s        r#   ainvoke_with_toolszLLMInterface.ainvoke_with_tools   s     2 ""TUUs   NNr   strr   zOptional[dict[str, Any]]r!   zOptional[RateLimitHandler]r"   r   )r)   r=   r*   1Optional[Union[List[LLMMessage], MessageHistory]]r+   Optional[str]returnr   )
r)   r=   r7   zSequence[Tool]r*   r>   r+   r?   r@   r   )__name__
__module____qualname____doc__r   __annotations__r$   r   r,   r/   r8   r:   r'   r%   r#   r   r   &   se    (-,l
 269=	BB /B 7	B
 B$  NR,0	 K *	
 
 *  NR,0	 K *	
 
 2 NR,0VV V K	V
 *V 
V> NR,0VV V K	V
 *V 
Vr%   r   c                  t    e Zd ZdZ	 	 d	 	 	 	 	 	 	 ddZe	 d	 	 	 	 	 	 	 d	d       Ze	 d	 	 	 	 	 	 	 d	d       Zy)
LLMInterfaceV2a  Interface for large language models compatible with LangChain.

    Args:
        model_name (str): The name of the language model.
        model_params (Optional[dict]): Additional parameters passed to the model when text is sent to it. Defaults to None.
        rate_limit_handler (Optional[RateLimitHandler]): Handler for rate limiting. Defaults to retry with exponential backoff.
        **kwargs (Any): Arguments passed to the model when for the class is initialised. Defaults to None.
    Nc                R    || _         |xs i | _        ||| _        y t        | _        y N)r   r   r   r   r   s        r#   r$   zLLMInterfaceV2.__init__   s.     %(.B)'9D$'AD$r%   c                     y)a  Sends a list of messages to the LLM and retrieves a response.

        Args:
            input (List[LLMMessage]): Text sent to the LLM as a list of LLMMessage objects.
            response_format (Optional[Union[Type[BaseModel], dict[str, Any]]]): Optional
                response format specification. Can be a Pydantic model class for structured
                output or a dict for provider-specific formats. Defaults to None.

        Returns:
            LLMResponse: The response from the LLM.

        Raises:
            LLMGenerationError: If anything goes wrong.
            NotImplementedError: If the LLM provider does not support structured output.
        Nr'   r    r)   response_formatr"   s       r#   r,   zLLMInterfaceV2.invoke   r-   r%   c                   K   yw)a  Asynchronously sends a list of messages to the LLM and retrieves a response.

        Args:
            input (List[LLMMessage]): List of messages sent to the LLM.
            response_format (Optional[Union[Type[BaseModel], dict[str, Any]]]): Optional
                response format specification. Can be a Pydantic model class for structured
                output or a dict for provider-specific formats. Defaults to None.

        Returns:
            LLMResponse: The response from the LLM.

        Raises:
            LLMGenerationError: If anything goes wrong.
            NotImplementedError: If the LLM provider does not support structured output.
        Nr'   rK   s       r#   r/   zLLMInterfaceV2.ainvoke   r0   r1   r;   r<   rI   )r)   zList[LLMMessage]rL   z0Optional[Union[Type[BaseModel], dict[str, Any]]]r"   r   r@   r   )rA   rB   rC   rD   r$   r   r,   r/   r'   r%   r#   rG   rG      s     269=	BB /B 7	B
 B  MQ J 	
 
 ,  MQ J 	
 
 r%   rG   ) 
__future__r   loggingabcr   r   typingr   r   r   r	   r
   r   pydanticr   neo4j_graphrag.message_historyr   neo4j_graphrag.toolr   neo4j_graphrag.typesr   neo4j_graphrag.utils.rate_limitr   r   typesr   r   	getLoggerrA   r   r   rG   r'   r%   r#   <module>rY      s_    #  # = =  9 $ +
 1 
		8	$V3 VDES Er%   