§
    šŠtjÚz  ã                   ó  — d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
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mZ d dlmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& d d	l'm(Z(m)Z)m*Z* d d
l+m,Z, d dl-m.Z.m/Z/m0Z0m1Z1 d dl2m3Z3m4Z4m5Z5 d dl6m7Z7m8Z8m9Z9 d dl:m;Z; d dl<m=Z= d dl>m?Z? d dl@mAZAmBZBmCZC d dlDmEZE  G d„ de¦  «        ZFde)deGfd„ZHde(deGfd„ZIdeeJef         defd„ZKdedefd„ZLdedeGfd„ZMdeeJef         d ee         defd!„ZNdS )"é    N)Ú
itemgetter)ÚPath)ÚAnyÚCallableÚDictÚIteratorÚListÚMappingÚOptionalÚSequenceÚTypeÚUnionÚcast)ÚCallbackManagerForLLMRun)ÚLanguageModelInput)ÚBaseChatModelÚgenerate_from_stream)Ú	AIMessageÚAIMessageChunkÚBaseMessageÚBaseMessageChunkÚChatMessageÚChatMessageChunkÚFunctionMessageÚFunctionMessageChunkÚHumanMessageÚHumanMessageChunkÚSystemMessageÚSystemMessageChunkÚToolMessageÚToolMessageChunk)ÚInvalidToolCallÚToolCallÚToolCallChunk)ÚOutputParserLike)ÚJsonOutputKeyToolsParserÚPydanticToolsParserÚmake_invalid_tool_callÚparse_tool_call)ÚChatGenerationÚChatGenerationChunkÚ
ChatResult)ÚRunnableÚRunnableMapÚRunnablePassthrough)ÚBaseTool©Úconvert_to_openai_tool)Úis_basemodel_subclass)Ú	BaseModelÚFieldÚmodel_validator)ÚSelfc                   ó~  ‡ — e Zd ZU dZdZeed<   eed<   	 dZe	e         ed<   	 dZ
e	e         ed<   	 dZeed<   	 d	Zeed
<   	 d	Zeed<   	 dZeed<   	 dZeed<   	 dZeed<   	 dZeed<   	 dZe	e         ed<   	 dZeed<   	 dZe	e         ed<   	 dZe	e         ed<   	 dZeed<   	 dZeed<   	 dZeed<   	 dZe	e         ed<   	 dZeed<   	 dZe	ee                  ed<   	 d Z eed!<   	 d"Z!eed#<   	 d$Z"eed%<   	 dZ#eed&<   	 d'Z$eed(<   	 d)Z%eed*<   	  e&e'¬+¦  «        Z(e)eef         ed,<   	 dZ*eed-<   	 dZ+e	e,ee-f                  ed.<   	 dZ.eed/<   	 dZ/eed0<   	  e0d1¬2¦  «        d3e1fd4„¦   «         Z2de	ee                  d3e)eef         fd5„Z3d6ee4         d3ee)eef                  fd7„Z5d8e'd3e6fd9„Z7	 	 dId6ee4         de	ee                  d:e	e8         d;ed3e6f
d<„Z9	 	 dId6ee4         de	ee                  d:e	e8         d;ed3e:e;         f
d=„Z<dd>œd?e=e,e)eef         e>e?         e@eAf                  d@e	e,e'eef                  d;ed3eBeCeDf         fˆ fdA„ZE	 dJddBœdCe	e,e)e>e?         f                  dDed;ed3eBeCe,e)e?f         f         fdE„ZFeGd3e)eef         fdF„¦   «         ZHeGd3efdG„¦   «         ZIeGd3e)eef         fdH„¦   «         ZJˆ xZKS )KÚChatLlamaCppzëllama.cpp model.

    To use, you should have the llama-cpp-python library installed, and provide the
    path to the Llama model as a named parameter to the constructor.
    Check out: https://github.com/abetlen/llama-cpp-python

    NÚclientÚ
model_pathÚ	lora_baseÚ	lora_pathi   Ún_ctxéÿÿÿÿÚn_partsÚseedTÚf16_kvFÚ
logits_allÚ
vocab_onlyÚ	use_mlockÚ	n_threadsé   Ún_batchÚn_gpu_layersÚsuffixé   Ú
max_tokensgš™™™™™é?Útemperaturegffffffî?Útop_pÚlogprobsÚechoÚstopgš™™™™™ñ?Úrepeat_penaltyé(   Útop_ké@   Úlast_n_tokens_sizeÚuse_mmapg      ð?Úrope_freq_scaleg     ˆÃ@Úrope_freq_base)Údefault_factoryÚmodel_kwargsÚ	streamingÚgrammar_pathÚgrammarÚverboseÚafter)ÚmodeÚreturnc                 ót  ‡ — 	 ddl m}m} n# t          $ r t          d¦  «        ‚w xY w‰ j        }g d¢}ˆ fd„|D ¦   «         }‰ j        �
‰ j        |d<   |                     ‰ j        ¦  «         	  ||fi |¤Ž‰ _        n'# t          $ r}t          d|› d	|› �¦  «        ‚d}~ww xY w‰ j        r+‰ j        r$‰ j        }‰ j        }t          d
|›d|›d�¦  «        ‚t          ‰ j        t          ¦  «        r |                     ‰ j        ¦  «        ‰ _        n(‰ j        r |                     ‰ j        ¦  «        ‰ _        n	 ‰ S )z4Validate that llama-cpp-python library is installed.r   )ÚLlamaÚLlamaGrammarz�Could not import llama-cpp-python library. Please install the llama-cpp-python library to use this embedding model: pip install llama-cpp-python)rX   rY   r=   r<   r>   r@   rA   rB   rC   rD   rE   rF   rH   rW   rV   r_   c                 ó2   •— i | ]}|t          ‰|¦  «        “ŒS © )Úgetattr)Ú.0ÚkÚselfs     €úf/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain_community/chat_models/llamacpp.pyú
<dictcomp>z5ChatLlamaCpp.validate_environment.<locals>.<dictcomp>Ï   s%   ø€ ÐGÐGÐG°˜�7 4¨Ñ+Ô+ÐGÐGÐGó    NrI   z&Could not load Llama model from path: z. Received error zCCan only pass in one of grammar and grammar_path. Received grammar=z and grammar_path=ú.)Ú	llama_cpprd   re   ÚImportErrorr;   rI   Úupdater[   r:   Ú	ExceptionÚ
ValueErrorr^   r]   Ú
isinstanceÚstrÚfrom_stringÚ	from_file)	rk   rd   re   r;   Úmodel_param_namesÚmodel_paramsÚer^   r]   s	   `        rl   Úvalidate_environmentz!ChatLlamaCpp.validate_environment°   sá  ø€ ð	Ø5Ð5Ð5Ð5Ð5Ð5Ð5Ð5Ð5øÝð 	ð 	ð 	ÝðIñô ð ð	øøøð ”_ˆ
ð
ð 
ð 
Ðð$ HÐGÐGÐGÐ5FÐGÑGÔGˆàÔÐ(Ø+/Ô+<ˆL˜Ñ(à×Ò˜DÔ-Ñ.Ô.Ð.ð	Ø˜% 
Ð;Ð;¨lÐ;Ð;ˆDŒKˆKøÝð 	ð 	ð 	Ýð&¸ð &ð &Ø"#ð&ð &ñô ð øøøøð	øøøð Œ<ð 	˜DÔ-ð 	Ø”lˆGØÔ,ˆLÝð3Øð3ð 3Ø".ð3ð 3ð 3ñô ð õ ˜œ¥cÑ*Ô*ð 	Ø'×3Ò3°D´LÑAÔAˆDŒLˆLØÔð 	Ø'×1Ò1°$Ô2CÑDÔDˆDŒLˆLàØˆs    ƒ Œ&Á.A= Á=
B!ÂBÂB!c                 ó^   — | j         }|                     d¦  «        }|p
|p| j        pg |d<   |S )z©
        Performs sanity check, preparing parameters in format needed by llama_cpp.

        Returns:
            Dictionary containing the combined parameters.
        Ústop_sequencesrQ   )Ú_default_paramsÚpoprQ   )rk   rQ   Úparamsr~   s       rl   Ú_get_parameterszChatLlamaCpp._get_parametersí   sA   € ð Ô%ˆð  ŸšÐ$4Ñ5Ô5ˆð ÐB ÐB°4´9ÐBÀˆˆv‰àˆrn   Úmessagesc                 ó   — d„ |D ¦   «         }|S )Nc                 ó,   — g | ]}t          |¦  «        ‘ŒS rg   )Ú_convert_message_to_dict)ri   Úms     rl   ú
<listcomp>z6ChatLlamaCpp._create_message_dicts.<locals>.<listcomp>  s!   € ÐGÐGÐG¸Õ1°!Ñ4Ô4ÐGÐGÐGrn   rg   )rk   rƒ   Úmessage_dictss      rl   Ú_create_message_dictsz"ChatLlamaCpp._create_message_dictsÿ   s   € ð HÐG¸hÐGÑGÔGˆàÐrn   Úresponsec                 óL  — g }|d         D ]o}t          |d         ¦  «        }t          |                     d¦  «        ¬¦  «        }d|v r|d         |d<   t          ||¬¦  «        }|                     |¦  «         Œp|                     di ¦  «        }d|i}t          ||¬	¦  «        S )
NÚchoicesÚmessageÚfinish_reason)r�   rO   ©rŽ   Úgeneration_infoÚusageÚtoken_usage)ÚgenerationsÚ
llm_output)Ú_convert_dict_to_messageÚdictÚgetr*   Úappendr,   )	rk   r‹   r”   ÚresrŽ   r‘   Úgenr“   r•   s	            rl   Ú_create_chat_resultz ChatLlamaCpp._create_chat_result  s¾   € ØˆØ˜IÔ&ð 	$ð 	$ˆCÝ.¨s°9¬~Ñ>Ô>ˆGÝ"°·²¸Ñ1IÔ1IÐJÑJÔJˆOØ˜SÐ Ð Ø.1°*¬o� 
Ñ+Ý ¨À/ÐRÑRÔRˆCØ×Ò˜sÑ#Ô#Ð#Ð#Ø—l’l 7¨BÑ/Ô/ˆà˜;ð
ˆ
õ  k¸jÐIÑIÔIÐIrn   Úrun_managerÚkwargsc                 ó&  — i |                       |¦  «        ¥|¥}| j        r4|                     d¦  «        s | j        |fd|i|¤Ž}t	          |¦  «        S |                      |¦  «        } | j        j        dd|i|¤Ž}|                      |¦  «        S )NÚtool_choicer�   rƒ   rg   )	r‚   r\   r˜   Ú_streamr   rŠ   r:   Úcreate_chat_completionrœ   )	rk   rƒ   rQ   r�   rž   r�   Ústream_iterr‰   r‹   s	            rl   Ú	_generatezChatLlamaCpp._generate  s³   € ð :�D×(Ò(¨Ñ.Ô.Ð9°&Ð9ˆð Œ>ð 	5 &§*¢*¨]Ñ";Ô";ð 	5Ø&˜$œ, xÐSÐS¸[ÐSÈFÐSÐSˆKÝ'¨Ñ4Ô4Ð4à×2Ò2°8Ñ<Ô<ˆà5�4”;Ô5ÐWÐW¸}ÐWÐPVÐWÐWˆà×'Ò'¨Ñ1Ô1Ð1rn   c              +   ó„  K  — i |                       |¦  «        ¥|¥}|                      |¦  «        } | j        j        d|ddœ|¤Ž}t          }d}	|D ]î}
|	dz  }	t          |
t          ¦  «        s|
                     ¦   «         }
t          |
d         ¦  «        dk    rŒJ|
d         d         }|d         €Œat          |d         |¦  «        }
i }| 
                    d¦  «        x}r||d<   | 
                    d¦  «        }|r||d<   |
j        }t          |
|pd ¬	¦  «        }
|r|                     |
j        |
|¬
¦  «         |
V — Œïd S )NT)rƒ   Ústreamr   é   r�   Údeltar�   rO   r�   )ÚchunkrO   rg   )r‚   rŠ   r:   r¢   r   ru   r—   Ú
model_dumpÚlenÚ_convert_delta_to_message_chunkr˜   Ú	__class__r+   Úon_llm_new_tokenÚtext)rk   rƒ   rQ   r�   rž   r�   r‰   ÚresultÚdefault_chunk_classÚcountr©   Úchoicer‘   r�   rO   s                  rl   r¡   zChatLlamaCpp._stream+  s¯  è è € ð :�D×(Ò(¨Ñ.Ô.Ð9°&Ð9ˆØ×2Ò2°8Ñ<Ô<ˆà3�”Ô3ð 
Ø"¨4ð
ð 
Ø39ð
ð 
ˆõ 7EÐØˆØð 	ð 	ˆEØ�Q‰JˆEÝ˜e¥TÑ*Ô*ð +Ø×(Ò(Ñ*Ô*�Ý�5˜Ô#Ñ$Ô$¨Ò)Ð)ØØ˜9Ô% aÔ(ˆFØ�gŒÐ&ØÝ3Ø�w”Ð!4ñô ˆEð !ˆOØ &§
¢
¨?Ñ ;Ô ;Ð;ˆ}ð AØ3@� Ñ0Ø—z’z *Ñ-Ô-ˆHØð 7Ø.6� 
Ñ+Ø"'¤/ÐÝ'Ø¨Ð/FÀ$ðñ ô ˆEð ð YØ×,Ò,¨U¬Z¸uÈxÐ,ÑXÔXÐXØˆKˆKˆKˆKð1	ð 	rn   ©r    Útoolsr    c                ó^  •‡— d„ |D ¦   «         }d„ |D ¦   «         }‰ræt          ‰t          ¦  «        r2t          ˆfd„|D ¦   «         ¦  «        st          d‰›d|› d�¦  «        ‚nŸt          ‰t          ¦  «        r'ˆfd„|D ¦   «         }|st          d‰›d|› d�¦  «        ‚nct          ‰t
          ¦  «        r<t          |¦  «        dk    r t          d	t          |¦  «        › d
�¦  «        ‚|d         Šnt          d‰› �¦  «        ‚‰|d<   d„ |D ¦   «         } t          ¦   «         j        dd|i|¤ŽS )a  Bind tool-like objects to this chat model

        tool_choice: does not currently support "any", "auto" choices like OpenAI
            tool-calling API. should be a dict of the form to force this tool
            {"type": "function", "function": {"name": <<tool_name>>}}.
        c                 ó,   — g | ]}t          |¦  «        ‘ŒS rg   r1   ©ri   Útools     rl   rˆ   z+ChatLlamaCpp.bind_tools.<locals>.<listcomp>b  ó!   € ÐJÐJÐJ¸DÕ1°$Ñ7Ô7ÐJÐJÐJrn   c                 ó*   — g | ]}|d          d         ‘ŒS ©ÚfunctionÚnamerg   )ri   Úfts     rl   rˆ   z+ChatLlamaCpp.bind_tools.<locals>.<listcomp>c  s!   € ÐGÐGÐG°�b˜”n VÔ,ÐGÐGÐGrn   c              3   ó<   •K  — | ]}‰d          d         |k    V — ŒdS )r½   r¾   Nrg   )ri   r¾   r    s     €rl   ú	<genexpr>z*ChatLlamaCpp.bind_tools.<locals>.<genexpr>f  sC   øè è € ð ð Ø@D�K 
Ô+¨FÔ3°tÒ;ðð ð ð ð ð rn   zTool choice tool_choice=z1 was specified, but the only provided tools were ro   c                 ó8   •— g | ]}|d          d         ‰k    ¯|‘ŒS r¼   rg   )ri   Úfr    s     €rl   rˆ   z+ChatLlamaCpp.bind_tools.<locals>.<listcomp>n  s3   ø€ ð ð ð Ø°!°J´-ÀÔ2GÈ;Ò2VÐ2V�AÐ2VÐ2VÐ2Vrn   r§   zQtool_choice=True can only be specified when a single tool is passed in. Received z tools.r   zœUnrecognized tool_choice type. Expected dict having format like
                    this {"type": "function", "function": {"name": <<tool_name>>}}Received: r    c                 ó,   — g | ]}t          |¦  «        ‘ŒS rg   r1   r¸   s     rl   rˆ   z+ChatLlamaCpp.bind_tools.<locals>.<listcomp>…  rº   rn   rµ   rg   )	ru   r—   Úanyrt   rv   Úboolr«   ÚsuperÚbind)rk   rµ   r    rž   Úformatted_toolsÚ
tool_namesÚchosenr­   s     `    €rl   Ú
bind_toolszChatLlamaCpp.bind_toolsU  s  øø€ ð KÐJÀEÐJÑJÔJˆØGÐG°ÐGÑGÔGˆ
Øð 	Ý˜+¥tÑ,Ô,ð Ýð ð ð ð ØHRðñ ô ñ ô ð õ %ð= {ð =ð =Ø/9ð=ð =ð =ñô ð ðõ ˜K­Ñ-Ô-ð ðð ð ð Ø.ðñ ô �ð ð Ý$ð= {ð =ð =Ø/9ð=ð =ð =ñô ð ðõ
 ˜K­Ñ.Ô.ð Ý�Ñ'Ô'¨!Ò+Ð+Ý$ðCÝ/2°5©z¬zðCð Cð Cñô ð ð .¨aÔ0��å ð/à!,ð/ð /ñô ð ð !,ˆˆ}ÑØJÐJÀEÐJÑJÔJˆØ�u‰wŒwŒ|Ð<Ð< /Ð<°VÐ<Ð<Ð<rn   )Úinclude_rawÚschemarÍ   c                óX  — |rt          d|› �¦  «        ‚t          |t          ¦  «        ot          |¦  «        }|€t          d¦  «        ‚t	          |¦  «        d         d         }dd|idœ}|                      |g|¬¦  «        }|r&t          t          t          |¦  «        gd¬	¦  «        }nt          |d¬
¦  «        }|rht          j        t          d¦  «        |z  d„ ¬¦  «        }	t          j        d„ ¬¦  «        }
|	                     |
gd¬¦  «        }t          |¬¦  «        |z  S ||z  S )a¶  Model wrapper that returns outputs formatted to match the given schema.

        Args:
            schema: The output schema as a dict or a Pydantic class. If a Pydantic class
                then the model output will be an object of that class. If a dict then
                the model output will be a dict. With a Pydantic class the returned
                attributes will be validated, whereas with a dict they will not be. If
                `method` is "function_calling" and `schema` is a dict, then the dict
                must match the OpenAI function-calling spec or be a valid JSON schema
                with top level 'title' and 'description' keys specified.
            include_raw:
                If `False` then only the parsed structured output is returned.

                If an error occurs during model output parsing it will be raised.

                If `True` then both the raw model response (a `BaseMessage`) and the
                parsed model response will be returned.

                If an error occurs during output parsing it will be caught and returned
                as well.

                The final output is always a `dict` with keys `'raw'`, `'parsed'`, and
                `'parsing_error'`.
            kwargs: Any other args to bind to model, ``self.bind(..., **kwargs)``.

        Returns:
            A Runnable that takes any ChatModel input and returns as output:

                If include_raw is True then a dict with keys:
                    raw: BaseMessage
                    parsed: Optional[_DictOrPydantic]
                    parsing_error: Optional[BaseException]

                If include_raw is False then just _DictOrPydantic is returned,
                where _DictOrPydantic depends on the schema:

                If schema is a Pydantic class then _DictOrPydantic is the Pydantic
                    class.

                If schema is a dict then _DictOrPydantic is a dict.

        Example: Pydantic schema (include_raw=False):
            .. code-block:: python

                from langchain_community.chat_models import ChatLlamaCpp
                from pydantic import BaseModel

                class AnswerWithJustification(BaseModel):
                    '''An answer to the user question along with justification for the answer.'''
                    answer: str
                    justification: str

                llm = ChatLlamaCpp(
                    temperature=0.,
                    model_path="./SanctumAI-meta-llama-3-8b-instruct.Q8_0.gguf",
                    n_ctx=10000,
                    n_gpu_layers=4,
                    n_batch=200,
                    max_tokens=512,
                    n_threads=multiprocessing.cpu_count() - 1,
                    repeat_penalty=1.5,
                    top_p=0.5,
                    stop=["<|end_of_text|>", "<|eot_id|>"],
                )
                structured_llm = llm.with_structured_output(AnswerWithJustification)

                structured_llm.invoke("What weighs more a pound of bricks or a pound of feathers")

                # -> AnswerWithJustification(
                #     answer='They weigh the same',
                #     justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
                # )

        Example: Pydantic schema (include_raw=True):
            .. code-block:: python

                from langchain_community.chat_models import ChatLlamaCpp
                from pydantic import BaseModel

                class AnswerWithJustification(BaseModel):
                    '''An answer to the user question along with justification for the answer.'''
                    answer: str
                    justification: str

                llm = ChatLlamaCpp(
                    temperature=0.,
                    model_path="./SanctumAI-meta-llama-3-8b-instruct.Q8_0.gguf",
                    n_ctx=10000,
                    n_gpu_layers=4,
                    n_batch=200,
                    max_tokens=512,
                    n_threads=multiprocessing.cpu_count() - 1,
                    repeat_penalty=1.5,
                    top_p=0.5,
                    stop=["<|end_of_text|>", "<|eot_id|>"],
                )
                structured_llm = llm.with_structured_output(AnswerWithJustification, include_raw=True)

                structured_llm.invoke("What weighs more a pound of bricks or a pound of feathers")
                # -> {
                #     'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
                #     'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
                #     'parsing_error': None
                # }

        Example: dict schema (include_raw=False):
            .. code-block:: python

                from langchain_community.chat_models import ChatLlamaCpp
                from pydantic import BaseModel
                from langchain_core.utils.function_calling import convert_to_openai_tool

                class AnswerWithJustification(BaseModel):
                    '''An answer to the user question along with justification for the answer.'''
                    answer: str
                    justification: str

                dict_schema = convert_to_openai_tool(AnswerWithJustification)
                llm = ChatLlamaCpp(
                    temperature=0.,
                    model_path="./SanctumAI-meta-llama-3-8b-instruct.Q8_0.gguf",
                    n_ctx=10000,
                    n_gpu_layers=4,
                    n_batch=200,
                    max_tokens=512,
                    n_threads=multiprocessing.cpu_count() - 1,
                    repeat_penalty=1.5,
                    top_p=0.5,
                    stop=["<|end_of_text|>", "<|eot_id|>"],
                )
                structured_llm = llm.with_structured_output(dict_schema)

                structured_llm.invoke("What weighs more a pound of bricks or a pound of feathers")
                # -> {
                #     'answer': 'They weigh the same',
                #     'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
                # }

        zReceived unsupported arguments NzJschema must be specified when method is 'function_calling'. Received None.r½   r¾   )Útyper½   r´   T)rµ   Úfirst_tool_only)Úkey_namerÑ   Úrawc                 ó   — d S ©Nrg   ©Ú_s    rl   ú<lambda>z5ChatLlamaCpp.with_structured_output.<locals>.<lambda>1  s   € ÐRV€ rn   )ÚparsedÚparsing_errorc                 ó   — d S rÕ   rg   rÖ   s    rl   rØ   z5ChatLlamaCpp.with_structured_output.<locals>.<lambda>3  s   € Àd€ rn   )rÙ   rÚ   )Úexception_key)rÓ   )rt   ru   rÐ   r3   r2   rÌ   r'   r   r   r&   r/   Úassignr   Úwith_fallbacksr.   )rk   rÎ   rÍ   rž   Úis_pydantic_schemaÚ	tool_namer    ÚllmÚoutput_parserÚparser_assignÚparser_noneÚparser_with_fallbacks               rl   Úwith_structured_outputz#ChatLlamaCpp.with_structured_outputˆ  s€  € ðf ð 	IÝÐG¸vÐGÐGÑHÔHÐHÝ'¨µÑ5Ô5ÐWÕ:OÐPVÑ:WÔ:WÐØˆ>Ýð!ñô ð õ +¨6Ñ2Ô2°:Ô>¸vÔFˆ	Ø)¸À	Ð7JÐKÐKˆØ�oŠo˜v˜h°KˆoÑ@Ô@ˆØð 	Ý.AÝ�D &Ñ)Ô)Ð*¸Dð/ñ /ô /ˆMˆMõ 5Ø"°Dðñ ô ˆMð ð 
	'Ý/Ô6Ý! %Ñ(Ô(¨=Ñ8ÈÈðñ ô ˆMõ .Ô4¸N¸NÐKÑKÔKˆKØ#0×#?Ò#?Ø�¨_ð $@ñ $ô $Ð õ  3Ð'Ñ'Ô'Ð*>Ñ>Ð>à˜Ñ&Ð&rn   c                 ó&   — i d| j         i¥| j        ¥S )zÈReturn a dictionary of identifying parameters.

        This information is used by the LangChain callback system, which
        is used for tracing purposes make it possible to monitor LLMs.
        r;   )r;   r   ©rk   s    rl   Ú_identifying_paramsz ChatLlamaCpp._identifying_params;  s*   € ð
ð
 ˜Tœ_Ð-ð
ð Ô"ð
ð 	
rn   c                 ó   — dS )z7Get the type of language model used by this chat model.zllama-cpp-pythonrg   rè   s    rl   Ú	_llm_typezChatLlamaCpp._llm_typeK  s
   € ð "Ð!rn   c                 ó‚   — | j         | j        | j        | j        | j        | j        | j        dœ}| j        r
| j        |d<   |S )z>Get the default parameters for calling create_chat_completion.)rL   rM   rN   rT   rO   r~   rR   r^   )rL   rM   rN   rT   rO   rQ   rR   r^   )rk   r�   s     rl   r   zChatLlamaCpp._default_paramsP  sR   € ð œ/ØÔ+Ø”ZØ”ZØœØ"œiØ"Ô1ð
ð 
ˆð Œ<ð 	-Ø $¤ˆF�9ÑØˆrn   )NNrÕ   )LÚ__name__Ú
__module__Ú__qualname__Ú__doc__r:   r   Ú__annotations__rv   r<   r   r=   r>   Úintr@   rA   rB   rÆ   rC   rD   rE   rF   rH   rI   rJ   rL   rM   ÚfloatrN   rO   rP   rQ   r	   rR   rT   rV   rW   rX   rY   r5   r—   r[   r   r\   r]   r   r   r^   r_   r6   r7   r|   r‚   r   rŠ   r,   rœ   r   r¤   r   r+   r¡   r   r   r4   r   r0   r-   r   r   rÌ   ræ   Úpropertyré   rë   r   Ú__classcell__)r­   s   @rl   r9   r9   =   s  ø€ € € € € € ðð ð €FˆCÐÐÑà€O€O�OØ+à#€Iˆx˜Œ}Ð#Ð#Ñ#Ø0à#€Iˆx˜Œ}Ð#Ð#Ñ#ØAà€Eˆ3ÐÐÑØà€GˆSÐÐÑð?ð €Dˆ#€N€N�NØ-à€FˆDÐÐÑØ1à€J�ÐÐÑØ@à€J�ÐÐÑØ/à€IˆtÐÐÑØ,à#€Iˆx˜Œ}Ð#Ð#Ñ#ðCð €GˆSÐÐÑð/ð #'€L�(˜3”-Ð&Ð&Ñ&ØFà €FˆH�SŒMÐ Ð Ñ ØSà€J�ÐÐÑØ3à€K�ÐÐÑØ.à€Eˆ5ÐÐÑØ.à"€Hˆh�sŒmÐ"Ð"Ñ"ØNà€Dˆ$ÐÐÑØ%à $€Dˆ(�4˜”9Ô
Ð$Ð$Ñ$Ø@à€N�EÐÐÑØ2à€Eˆ3€O€O�OØ.à Ð˜Ð Ð Ñ ØMà€HˆdÐÐÑØ1à €O�UÐ Ð Ñ Ø)à#€N�EÐ#Ð#Ñ#Ø+à#( 5¸Ð#>Ñ#>Ô#>€L�$�s˜C�x”.Ð>Ð>Ñ>Ø?à€IˆtÐÐÑØ8à/3€L�(˜5  d Ô+Ô,Ð3Ð3Ñ3ðð €GˆSÐÐÑðð €GˆTÐÐÑØ)à€_˜'Ð"Ñ"Ô"ð: dð :ð :ð :ñ #Ô"ð:ðx H¨T°#¬YÔ$7ð ¸DÀÀcÀ¼Nð ð ð ð ð$Ø˜[Ô)ðà	ˆd�3˜�8ŒnÔ	ðð ð ð ðJ¨Dð J°Zð Jð Jð Jð Jð& %)Ø:>ð	2ð 2à�{Ô#ð2ð �t˜C”yÔ!ð2ð Ð6Ô7ð	2ð
 ð2ð 
ð2ð 2ð 2ð 2ð0 %)Ø:>ð	(ð (à�{Ô#ð(ð �t˜C”yÔ!ð(ð Ð6Ô7ð	(ð
 ð(ð 
Ð%Ô	&ð(ð (ð (ð (ð\ 9=ð	1=ð 1=ð 1=à˜˜d 3¨ 8œn¨d°9¬o¸xÈÐQÔRÔSð1=ð ˜e D¨$° OÔ4Ô5ð	1=ð
 ð1=ð 
Ð$ iÐ/Ô	0ð1=ð 1=ð 1=ð 1=ð 1=ð 1=ðj :>ðq'ð "ð	q'ð q'ð q'à˜˜t T¨)¤_Ð4Ô5Ô6ðq'ð ð	q'ð
 ðq'ð 
Ð$ e¨D°)¨OÔ&<Ð<Ô	=ðq'ð q'ð q'ð q'ðf ð
 T¨#¨s¨(¤^ð 
ð 
ð 
ñ „Xð
ð ð"˜3ð "ð "ð "ñ „Xð"ð ð  c¨3 h¤ð ð ð ñ „Xðð ð ð ð rn   r9   Ú	tool_callrb   c                 ó\   — d| d         | d         t          j        | d         ¦  «        dœdœS ©Nr½   Úidr¾   Úargs)r¾   Ú	arguments)rÐ   rù   r½   )ÚjsonÚdumps)rö   s    rl   Ú!_lc_tool_call_to_openai_tool_callrþ   a  s?   € àØ˜Œoà˜fÔ%Ýœ I¨fÔ$5Ñ6Ô6ð
ð 
ðð ð rn   Úinvalid_tool_callc                 ó8   — d| d         | d         | d         dœdœS rø   rg   )rÿ   s    rl   Ú)_lc_invalid_tool_call_to_openai_tool_callr  l  s8   € ð Ø Ô%à% fÔ-Ø*¨6Ô2ð
ð 
ðð ð rn   Ú_dictc           	      óü  — |                       d¦  «        }|                       d¦  «        }|                       d¦  «        }|dk    r&t          |                       dd¦  «        ||¬¦  «        S |dk    rð|                       dd¦  «        pd}i }|                       d	¦  «        x}rt          |¦  «        |d	<   g }g }|                       d
¦  «        x}	r}|	|d
<   |	D ]u}
	 t          |
d¬¦  «        }|sŒ|                     |¦  «         Œ-# t
          $ r<}t          |
t          |¦  «        ¦  «        }|                     |¦  «         Y d}~Œnd}~ww xY wt          ||||||¬¦  «        S |dk    r&t          |                       dd¦  «        ||¬¦  «        S |dk    rLt          |                       dd¦  «        t          t          |                       d¦  «        ¦  «        |¬¦  «        S |dk    r_i }d| v r| d         |d<   t          |                       dd¦  «        t          t          |                       d¦  «        ¦  «        |||¬¦  «        S t          |                       dd¦  «        t          t          |¦  «        |¬¦  «        S )zˆConvert a dictionary to a LangChain message.

    Args:
        _dict: The dictionary.

    Returns:
        The LangChain message.
    Úroler¾   rù   ÚuserÚcontentÚ )r  rù   r¾   Ú	assistantÚfunction_callÚ
tool_callsT)Ú	return_idN)r  Úadditional_kwargsr¾   rù   r
  Úinvalid_tool_callsÚsystem©r  r¾   rù   r½   r¹   Útool_call_id)r  r  r  r¾   rù   ©r  r  rù   )r˜   r   r—   r)   r™   rs   r(   rv   r   r   r   r   r    r   )r  r  r¾   Úid_r  r  r	  r
  r  Úraw_tool_callsÚraw_tool_callÚtcr{   Ú
invalid_tcs                 rl   r–   r–   y  sÝ  € ð �9Š9�VÑÔ€DØ�9Š9�VÑÔ€DØ
�)Š)�D‰/Œ/€CØˆv‚~€~Ý E§I¢I¨i¸Ñ$<Ô$<ÀÈ4ÐPÑPÔPÐPØ	�Ò	Ð	ð —)’)˜I rÑ*Ô*Ð0¨bˆØ"$ÐØ!ŸIšI oÑ6Ô6Ð6ˆ=ð 	EÝ15°mÑ1DÔ1DÐ˜oÑ.Øˆ
ØÐØ"ŸYšY |Ñ4Ô4Ð4ˆ>ð 	.Ø.<Ð˜lÑ+Ø!/ð 
.ð 
.�ð	.Ý(¨À$ÐGÑGÔG�Bð
 ð .Ø à"×)Ò)¨"Ñ-Ô-Ð-Ð-øõ !ð :ð :ð :Ý!7¸ÅsÈ1ÁvÄvÑ!NÔ!N�JØ&×-Ò-¨jÑ9Ô9Ð9Ð9Ð9Ð9Ð9Ð9øøøøð:øøøõ ØØ/ØØØ!Ø1ð
ñ 
ô 
ð 	
ð 
�Ò	Ð	Ý U§Y¢Y¨y¸"Ñ%=Ô%=ÀDÈSÐQÑQÔQÐQØ	�Ò	Ð	ÝØ—I’I˜i¨Ñ,Ô,µ4½¸U¿YºYÀvÑ=NÔ=NÑ3OÔ3OÐTWð
ñ 
ô 
ð 	
ð 
�ŠˆØÐØ�Uˆ?ˆ?Ø(-¨f¬Ð˜fÑ%ÝØ—I’I˜i¨Ñ,Ô,Ý�c 5§9¢9¨^Ñ#<Ô#<Ñ=Ô=Ø/ØØð
ñ 
ô 
ð 	
õ Ø—I’I˜i¨Ñ,Ô,µ4½¸T±?´?Àsð
ñ 
ô 
ð 	
s   ÃDÄ
E
Ä2EÅE
r  c                 ó¾   — | rXt          | t          ¦  «        rCg }| D ]=}t          |t          ¦  «        rd|v r|d         dk    rŒ(|                     |¦  «         Œ>n| }|S )zFormat message content.rÐ   Útool_use)ru   Úlistr—   r™   )r  Úformatted_contentÚblocks      rl   Ú_format_message_contentr  ¼  s‡   € àð $•:˜g¥tÑ,Ô,ð $àÐØð 	0ð 	0ˆEå˜5¥$Ñ'Ô'ð0à˜e�O�OØ˜&”M ZÒ/Ð/àà!×(Ò(¨Ñ/Ô/Ð/Ð/ð	0ð $ÐàÐrn   rŽ   c                 óŠ  ‡‡— dt          | j        ¦  «        i}| j        p| j                             d¦  «        x}�||d<   t          | t          ¦  «        r| j        |d<   �nat          | t          ¦  «        rd|d<   �nEt          | t          ¦  «        rŸd|d<   d| j        v r| j        d         |d<   | j
        s| j        r'd„ | j
        D ¦   «         d	„ | j        D ¦   «         z   |d
<   n6d
| j        v r,| j        d
         |d
<   h d£Šˆfd„|d
         D ¦   «         |d
<   n	 d|v sd
|v r|d         pd|d<   n‘t          | t          ¦  «        rd|d<   nvt          | t          ¦  «        rd|d<   n[t          | t          ¦  «        r4d|d<   | j        |d<   h d£Šˆfd„|                     ¦   «         D ¦   «         }nt#          d| › �¦  «        ‚|S )zŠConvert a LangChain message to a dictionary.

    Args:
        message: The LangChain message.

    Returns:
        The dictionary.
    r  r¾   Nr  r  r  r	  c                 ó,   — g | ]}t          |¦  «        ‘ŒS rg   )rþ   ©ri   r  s     rl   rˆ   z,_convert_message_to_dict.<locals>.<listcomp>é  s.   € ð *ð *ð *Ø:<Õ1°"Ñ5Ô5ð*ð *ð *rn   c                 ó,   — g | ]}t          |¦  «        ‘ŒS rg   )r  r  s     rl   rˆ   z,_convert_message_to_dict.<locals>.<listcomp>ë  s0   € ð ð ð àõ :¸"Ñ=Ô=ðð ð rn   r
  >   rù   rÐ   r½   c                 óP   •— g | ]"}ˆfd „|                      ¦   «         D ¦   «         ‘Œ#S )c                 ó$   •— i | ]\  }}|‰v ¯	||“ŒS rg   rg   )ri   rj   ÚvÚtool_call_supported_propss      €rl   rm   z7_convert_message_to_dict.<locals>.<listcomp>.<dictcomp>ó  s*   ø€ ÐVÐVÐV™$˜!˜Q°qÐ<UÐ7UÐ7U��AÐ7UÐ7UÐ7Urn   )Úitems)ri   rö   r$  s     €rl   rˆ   z,_convert_message_to_dict.<locals>.<listcomp>ò  sH   ø€ ð *ð *ð *àð WÐVÐVÐV )§/¢/Ñ"3Ô"3ÐVÑVÔVð*ð *ð *rn   r  r½   r¹   r  >   r  r  r  c                 ó$   •— i | ]\  }}|‰v ¯	||“ŒS rg   rg   )ri   rj   r#  Úsupported_propss      €rl   rm   z,_convert_message_to_dict.<locals>.<dictcomp>  s)   ø€ ÐVÐVÐV¡  AÀÀoÐAUÐAU˜˜1ÐAUÐAUÐAUrn   zGot unknown type )r  r  r¾   r  r˜   ru   r   r  r   r   r
  r  r   r   r    r  r%  Ú	TypeError)rŽ   Úmessage_dictr¾   r'  r$  s      @@rl   r†   r†   Ð  st  øø€ ð 	Õ*¨7¬?Ñ;Ô;ð$€Lð ”ÐE Ô 9× =Ò =¸fÑ EÔ EÐEˆÐRØ#ˆ�VÑõ �'�;Ñ'Ô'ð &7Ø&œ|ˆ�VÑÑÝ	�G�\Ñ	*Ô	*ð $7Ø%ˆ�VÑÑÝ	�G�YÑ	'Ô	'ð "7Ø*ˆ�VÑØ˜gÔ7Ð7Ð7Ø,3Ô,EÀoÔ,VˆL˜Ñ)ØÔð 	 Ô!;ð 	ð*ð *Ø@GÔ@Rð*ñ *ô *ðð à!Ô4ðñ ô ñ*ˆL˜Ñ&Ð&ð ˜WÔ6Ð6Ð6Ø)0Ô)BÀ<Ô)PˆL˜Ñ&Ø(BÐ(BÐ(BÐ%ð*ð *ð *ð *à!-¨lÔ!;ð*ñ *ô *ˆL˜Ñ&Ð&ð
 à˜lÐ*Ð*¨l¸lÐ.JÐ.JØ&2°9Ô&=Ð&EÀˆL˜Ñ#øÝ	�G�]Ñ	+Ô	+ð 7Ø'ˆ�VÑÐÝ	�G�_Ñ	-Ô	-ð 	7Ø)ˆ�VÑÐÝ	�G�[Ñ	)Ô	)ð 7Ø%ˆ�VÑØ'.Ô';ˆ�^Ñ$à=Ð=Ð=ˆØVÐVÐVÐV¨×);Ò);Ñ)=Ô)=ÐVÑVÔVˆˆåÐ5¨GÐ5Ð5Ñ6Ô6Ð6ØÐrn   Údefault_classc                 ón  — |                       d¦  «        }t          t          |                       d¦  «        ¦  «        }t          t          |                       d¦  «        pd¦  «        }i }|                       d¦  «        r+t          | d         ¦  «        }d|v r|d         €d|d<   ||d<   g }|                       d¦  «        x}rŽ||d<   |D ]†}		 t	          |	d                               d¦  «        |	d                               d	¦  «        |	                      d¦  «        |	d
         ¬¦  «        }
|                     |
¦  «         Œw# t          $ r Y Œƒw xY w|dk    s|t          k    rt          ||¬¦  «        S |dk    s|t          k    rt          ||||¬¦  «        S |dk    s|t          k    rt          ||¬¦  «        S |dk    s|t          k    rt          || d         |¬¦  «        S |dk    s|t          k    rt          || d         |¬¦  «        S |s|t          k    rt          |||¬¦  «        S  |||¬¦  «        S )Nrù   r  r  r  r	  r¾   r
  r½   rû   Úindex)r¾   rú   rù   r,  r  )r  rù   r  )r  r  rù   Útool_call_chunksr  r  r¹   r  )r  r  rù   r  )r˜   r   rv   r—   r$   r™   ÚKeyErrorr   r   r   r   r!   r   )r  r*  r  r  r  r  r	  r-  r  Úrtcrö   s              rl   r¬   r¬   
  sž  € ð �)Š)�D‰/Œ/€CÝ•�U—Y’Y˜vÑ&Ô&Ñ'Ô'€DÝ•3˜Ÿ	š	 )Ñ,Ô,Ð2°Ñ3Ô3€GØ ÐØ‡y‚y�Ñ!Ô!ð ;Ý˜U ?Ô3Ñ4Ô4ˆØ�]Ð"Ð" }°VÔ'<Ð'DØ$&ˆM˜&Ñ!Ø-:Ð˜/Ñ*ØÐØŸš <Ñ0Ô0Ð0€~ð Ø*8Ð˜,Ñ'Ø!ð 
	ð 
	ˆCð	Ý)Ø˜Zœ×,Ò,¨VÑ4Ô4Ø˜Zœ×,Ò,¨[Ñ9Ô9Ø—w’w˜t‘}”}Ø˜gœ,ð	ñ ô �	ð !×'Ò'¨	Ñ2Ô2Ð2Ð2øÝð ð ð Ø�ðøøøð ˆv‚~€~˜Õ*;Ò;Ð;Ý ¨°SÐ9Ñ9Ô9Ð9Ø	�Ò	Ð	 µÒ ?Ð ?ÝØØ/ØØ-ð	
ñ 
ô 
ð 	
ð 
�Ò	Ð	˜]Õ.@Ò@Ð@Ý!¨'°cÐ:Ñ:Ô:Ð:Ø	�Ò	Ð	˜}Õ0DÒDÐDÝ#¨G¸%À¼-ÈCÐPÑPÔPÐPØ	�Šˆ˜=Õ,<Ò<Ð<ÝØ¨%°Ô*?ÀCð
ñ 
ô 
ð 	
ð 
ð 6�Õ"2Ò2Ð2Ý¨°d¸sÐCÑCÔCÐCàˆ} W°Ð5Ñ5Ô5Ð5s   ÃA3EÅ
EÅE)Orü   Úoperatorr   Úpathlibr   Útypingr   r   r   r   r	   r
   r   r   r   r   r   Úlangchain_core.callbacksr   Úlangchain_core.language_modelsr   Ú*langchain_core.language_models.chat_modelsr   r   Úlangchain_core.messagesr   r   r   r   r   r   r   r   r   r   r   r   r    r!   Úlangchain_core.messages.toolr"   r#   r$   Ú"langchain_core.output_parsers.baser%   Ú*langchain_core.output_parsers.openai_toolsr&   r'   r(   r)   Úlangchain_core.outputsr*   r+   r,   Úlangchain_core.runnablesr-   r.   r/   Úlangchain_core.toolsr0   Ú%langchain_core.utils.function_callingr2   Úlangchain_core.utils.pydanticr3   Úpydanticr4   r5   r6   Útyping_extensionsr7   r9   r—   rþ   r  rv   r–   r  r†   r¬   rg   rn   rl   ú<module>rA     s  ðØ €€€Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð >Ð =Ð =Ð =Ð =Ð =Ø =Ð =Ð =Ð =Ð =Ð =ðð ð ð ð ð ð ð ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð  RÐ QÐ QÐ QÐ QÐ QÐ QÐ QÐ QÐ QØ ?Ð ?Ð ?Ð ?Ð ?Ð ?ðð ð ð ð ð ð ð ð ð ð ð ð SÐ RÐ RÐ RÐ RÐ RÐ RÐ RÐ RÐ RØ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OØ )Ð )Ð )Ð )Ð )Ð )Ø HÐ HÐ HÐ HÐ HÐ HØ ?Ð ?Ð ?Ð ?Ð ?Ð ?ðð ð ð ð ð ð ð ð ð ð
 #Ð "Ð "Ð "Ð "Ð "ðað að að að a�=ñ aô að aðH°ð ¸dð ð ð ð ð
Ø&ð
à	ð
ð 
ð 
ð 
ð@
 G¨C°¨HÔ$5ð @
¸+ð @
ð @
ð @
ð @
ðF Sð ¨Sð ð ð ð ð(7 kð 7°dð 7ð 7ð 7ð 7ðt/6Ø�3˜�8Ôð/6Ø-1Ð2BÔ-Cð/6àð/6ð /6ð /6ð /6ð /6ð /6rn   