a
    •˜ëjN  ã                	   @   s$  d dl 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 d dlmZmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZmZ d dlmZ d dlmZ G dd„ deƒZedddd�deee e!e f e"eeeef  e#edœdd„ƒZ$edddd�dee#eedœdd„ƒZ%dS )é    )ÚAnyÚOptionalÚUnionÚcast)Ú
deprecated)ÚBaseLanguageModel)ÚHumanMessageÚSystemMessage)ÚBaseLLMOutputParser)ÚOutputFunctionsParserÚPydanticOutputFunctionsParser)ÚPromptTemplate)ÚChatPromptTemplateÚHumanMessagePromptTemplate)Úis_basemodel_subclass)Ú	BaseModelÚField)ÚLLMChain)Úget_llm_kwargsc                   @   s>   e Zd ZU dZeddd�Zeed< eddd�Ze	e ed< dS )	ÚAnswerWithSourcesz(An answer to the question, with sources..z%Answer to the question that was asked)ÚdescriptionÚanswerz+List of sources used to answer the questionÚsourcesN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ÚstrÚ__annotations__r   Úlist© r    r    úr/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/chains/openai_functions/qa_with_structure.pyr      s   
þr   z0.2.13z1.0zÈThis function is deprecated. Refer to this guide on retrieval and question answering with structured responses: https://python.langchain.com/docs/how_to/qa_sources/#structure-sources-in-model-response)ZsinceZremovalÚmessageÚbaseNF)ÚllmÚschemaÚoutput_parserÚpromptÚverboseÚreturnc                 C   sê   |dkr2t |tƒrt|ƒs&d}t|ƒ‚t|d�}n$|dkrBtƒ }nd|› d�}t|ƒ‚t |tƒrxt|ƒrxtd| ¡ ƒ}n
td|ƒ}|d |d	 |d
œ}t|ƒ}	t	dd�t
dd�t d¡t d¡t
dd�g}
|pÖt|
d�}t| ||	||d�S )a#  Create a question answering chain with structure.

    Create a question answering chain that returns an answer with sources
    based on schema.

    Args:
        llm: Language model to use for the chain.
        schema: Pydantic schema to use for the output.
        output_parser: Output parser to use. Should be one of `pydantic` or `base`.
            Default to `base`.
        prompt: Optional prompt to use for the chain.
        verbose: Whether to run the chain in verbose mode.

    Returns:
        The question answering chain.

    ÚpydanticzJMust provide a pydantic class for schema when output_parser is 'pydantic'.)Zpydantic_schemar#   zGot unexpected output_parser: z(. Should be one of `pydantic` or `base`.ÚdictÚtitler   )Únamer   Ú
parameterszIYou are a world class algorithm to answer questions in a specific format.)Úcontentz+Answer question using the following contextz	{context}zQuestion: {question}z/Tips: Make sure to answer in the correct format)Úmessages)r$   r'   Ú
llm_kwargsr&   r(   )Ú
isinstanceÚtyper   Ú
ValueErrorr   r   r   Zmodel_json_schemar   r	   r   r   Zfrom_templater   r   )r$   r%   r&   r'   r(   ÚmsgZ_output_parserZschema_dictÚfunctionr1   r0   r    r    r!   Úcreate_qa_with_structure_chain   sJ    !ÿÿ
ÿ
ýþöûr7   z»This function is deprecated. Refer to this guide on retrieval and question answering with sources: https://python.langchain.com/docs/how_to/qa_sources/#structure-sources-in-model-response)r$   r(   Úkwargsr)   c                 K   s   t | tfd|i|¤ŽS )ap  Create a question answering chain that returns an answer with sources.

    Args:
        llm: Language model to use for the chain.
        verbose: Whether to print the details of the chain
        **kwargs: Keyword arguments to pass to `create_qa_with_structure_chain`.

    Returns:
        Chain (LLMChain) that can be used to answer questions with citations.
    r(   )r7   r   )r$   r(   r8   r    r    r!   Úcreate_qa_with_sources_chainr   s    þýür9   )r#   NF)F)&Útypingr   r   r   r   Zlangchain_core._apir   Zlangchain_core.language_modelsr   Zlangchain_core.messagesr   r	   Zlangchain_core.output_parsersr
   Z.langchain_core.output_parsers.openai_functionsr   r   Zlangchain_core.promptsr   Zlangchain_core.prompts.chatr   r   Zlangchain_core.utils.pydanticr   r*   r   r   Zlangchain.chains.llmr   Z'langchain.chains.openai_functions.utilsr   r   r+   r3   r   Úboolr7   r9   r    r    r    r!   Ú<module>   sP   
ü   ûúKü þü