a
    •˜ëj  ã                	   @   sú   d dl 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 d dlmZ d dlmZ d dlmZ d d	lmZmZmZ eed
œdd„ZdZeddddd�deeee eee  eedœdd„ƒZeddddd�deeee eedœdd„ƒZdS )é    )ÚAnyÚOptional)Ú
deprecated)ÚBaseLanguageModel)ÚJsonKeyOutputFunctionsParserÚ!PydanticAttrOutputFunctionsParser)ÚBasePromptTemplateÚChatPromptTemplate)Ú	BaseModel)ÚChain)ÚLLMChain)Ú_convert_schemaÚ_resolve_schema_referencesÚget_llm_kwargs)Úentity_schemaÚreturnc                 C   s$   dddddt | ƒdœidgdœdœS )	NZinformation_extractionz3Extracts the relevant information from the passage.ÚobjectÚinfoÚarray)ÚtypeÚitems)r   Z
propertiesÚrequired)ÚnameÚdescriptionÚ
parameters)r   )r   © r   úk/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/chains/openai_functions/extraction.pyÚ_get_extraction_function   s    ÿûýr   a<  Extract and save the relevant entities mentioned in the following passage together with their properties.

Only extract the properties mentioned in the 'information_extraction' function.

If a property is not present and is not required in the function parameters, do not include it in the output.

Passage:
{input}
z0.1.14a  LangChain has introduced a method called `with_structured_output` thatis available on ChatModels capable of tool calling.You can read more about the method here: <https://python.langchain.com/docs/modules/model_io/chat/structured_output/>. Please follow our extraction use case documentation for more guidelineson how to do information extraction with LLMs.<https://python.langchain.com/docs/use_cases/extraction/>. If you notice other issues, please provide feedback here:<https://github.com/langchain-ai/langchain/discussions/18154>z1.0a)  
            from pydantic import BaseModel, Field
            from langchain_anthropic import ChatAnthropic

            class Joke(BaseModel):
                setup: str = Field(description="The setup of the joke")
                punchline: str = Field(description="The punchline to the joke")

            # Or any other chat model that supports tools.
            # Please reference to to the documentation of structured_output
            # to see an up to date list of which models support
            # with_structured_output.
            model = ChatAnthropic(model="claude-3-opus-20240229", temperature=0)
            structured_llm = model.with_structured_output(Joke)
            structured_llm.invoke("Tell me a joke about cats.
                Make sure to call the Joke function.")
            )ZsinceÚmessageZremovalÚalternativeNF)ÚschemaÚllmÚpromptÚtagsÚverboser   c           	      C   s<   t | ƒ}|pt t¡}tdd�}t|ƒ}t||||||d�S )aZ  Creates a chain that extracts information from a passage.

    Args:
        schema: The schema of the entities to extract.
        llm: The language model to use.
        prompt: The prompt to use for extraction.
        tags: Optional list of tags to associate with the chain.
        verbose: Whether to run in verbose mode. In verbose mode, some intermediate
            logs will be printed to the console. Defaults to the global `verbose` value,
            accessible via `langchain.globals.get_verbose()`.

    Returns:
        Chain that can be used to extract information from a passage.
    r   )Zkey_name)r!   r"   Ú
llm_kwargsÚoutput_parserr#   r$   )r   r	   Úfrom_templateÚ_EXTRACTION_TEMPLATEr   r   r   )	r    r!   r"   r#   r$   ÚfunctionÚextraction_promptr&   r%   r   r   r   Úcreate_extraction_chain/   s    8
úr+   )Úpydantic_schemar!   r"   r$   r   c           
         s~   G ‡ fdd„dt ƒ}tˆ dƒr(ˆ  ¡ }nˆ  ¡ }t|| di ¡ƒ}t|ƒ}|pVt t	¡}t
|dd�}t|ƒ}	t|||	||d�S )a@  Creates a chain that extracts information from a passage using pydantic schema.

    Args:
        pydantic_schema: The pydantic schema of the entities to extract.
        llm: The language model to use.
        prompt: The prompt to use for extraction.
        verbose: Whether to run in verbose mode. In verbose mode, some intermediate
            logs will be printed to the console. Defaults to the global `verbose` value,
            accessible via `langchain.globals.get_verbose()`

    Returns:
        Chain that can be used to extract information from a passage.
    c                       s   e Zd ZU e”  ed< dS )z8create_extraction_chain_pydantic.<locals>.PydanticSchemar   N)Ú__name__Ú
__module__Ú__qualname__ÚlistÚ__annotations__r   ©r,   r   r   ÚPydanticSchema¬   s   
r3   Úmodel_json_schemaZdefinitionsr   )r,   Ú	attr_name)r!   r"   r%   r&   r$   )r
   Úhasattrr4   r    r   Úgetr   r	   r'   r(   r   r   r   )
r,   r!   r"   r$   r3   Zopenai_schemar)   r*   r&   r%   r   r2   r   Ú create_extraction_chain_pydanticu   s,    7


þþûr8   )NNF)NF)Útypingr   r   Zlangchain_core._apir   Zlangchain_core.language_modelsr   Z.langchain_core.output_parsers.openai_functionsr   r   Zlangchain_core.promptsr   r	   Zpydanticr
   Zlangchain.chains.baser   Zlangchain.chains.llmr   Z'langchain.chains.openai_functions.utilsr   r   r   Údictr   r(   r0   ÚstrÚboolr+   r8   r   r   r   r   Ú<module>   sT   ð&   û
ú#ð&  üû