
    3fi0                         d Z ddlmZmZmZmZ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  G d de	e      Z	 dd	d
deeeef   ee   f   dedee   dee   def
dZy	)z6Document transformers that use OpenAI Functions models    )AnyDictOptionalSequenceTypeUnion)BaseDocumentTransformerDocument)BaseLanguageModel)ChatPromptTemplate)	BaseModelc                   ^    e Zd ZU dZeed<   	 dee   dedee   fdZdee   dedee   fdZ	y)	OpenAIMetadataTaggera  Extract metadata tags from document contents using OpenAI functions.

    Example:
        .. code-block:: python

                from langchain_community.chat_models import ChatOpenAI
                from langchain_community.document_transformers import OpenAIMetadataTagger
                from langchain_core.documents import Document

                schema = {
                    "properties": {
                        "movie_title": { "type": "string" },
                        "critic": { "type": "string" },
                        "tone": {
                            "type": "string",
                            "enum": ["positive", "negative"]
                        },
                        "rating": {
                            "type": "integer",
                            "description": "The number of stars the critic rated the movie"
                        }
                    },
                    "required": ["movie_title", "critic", "tone"]
                }

                # Must be an OpenAI model that supports functions
                llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")
                tagging_chain = create_tagging_chain(schema, llm)
                document_transformer = OpenAIMetadataTagger(tagging_chain=tagging_chain)
                original_documents = [
                    Document(page_content="Review of The Bee Movie
By Roger Ebert

This is the greatest movie ever made. 4 out of 5 stars."),
                    Document(page_content="Review of The Godfather
By Anonymous

This movie was super boring. 1 out of 5 stars.", metadata={"reliable": False}),
                ]

                enhanced_documents = document_transformer.transform_documents(original_documents)
    tagging_chain	documentskwargsreturnc                     g }|D ]]  }| j                   j                  |j                        }t        |j                  i ||j                        }|j                  |       _ |S )zgAutomatically extract and populate metadata
        for each document according to the provided schema.)page_contentmetadata)r   runr   r
   r   append)selfr   r   new_documentsdocumentextracted_metadatanew_documents          x/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/document_transformers/openai_functions.pytransform_documentsz(OpenAIMetadataTagger.transform_documents4   st     ! 	/H'+'9'9'='=h>S>S'T#%22D.D(2C2CDL   .	/     c                    K   t         wN)NotImplementedError)r   r   r   s      r   atransform_documentsz)OpenAIMetadataTagger.atransform_documentsE   s      "!s   	N)
__name__
__module____qualname____doc__r   __annotations__r   r
   r   r$    r    r   r   r      sa    #J @!(+7:	(	""!(+"7:"	(	"r    r   N)tagging_chain_kwargsmetadata_schemallmpromptr+   r   c                    ddl m} t        | t              r| n| j	                         } |xs i } || |fd|i|}t        |      S )aN	  Create a DocumentTransformer that uses an OpenAI function chain to automatically
        tag documents with metadata based on their content and an input schema.

    Args:
        metadata_schema: Either a dictionary or pydantic.BaseModel class. If a dictionary
            is passed in, it's assumed to already be a valid JsonSchema.
            For best results, pydantic.BaseModels should have docstrings describing what
            the schema represents and descriptions for the parameters.
        llm: Language model to use, assumed to support the OpenAI function-calling API.
            Defaults to use "gpt-3.5-turbo-0613"
        prompt: BasePromptTemplate to pass to the model.

    Returns:
        An LLMChain that will pass the given function to the model.

    Example:
        .. code-block:: python

                from langchain_community.chat_models import ChatOpenAI
                from langchain_community.document_transformers import create_metadata_tagger
                from langchain_core.documents import Document

                schema = {
                    "properties": {
                        "movie_title": { "type": "string" },
                        "critic": { "type": "string" },
                        "tone": {
                            "type": "string",
                            "enum": ["positive", "negative"]
                        },
                        "rating": {
                            "type": "integer",
                            "description": "The number of stars the critic rated the movie"
                        }
                    },
                    "required": ["movie_title", "critic", "tone"]
                }

                # Must be an OpenAI model that supports functions
                llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")

                document_transformer = create_metadata_tagger(schema, llm)
                original_documents = [
                    Document(page_content="Review of The Bee Movie
By Roger Ebert

This is the greatest movie ever made. 4 out of 5 stars."),
                    Document(page_content="Review of The Godfather
By Anonymous

This movie was super boring. 1 out of 5 stars.", metadata={"reliable": False}),
                ]

                enhanced_documents = document_transformer.transform_documents(original_documents)
    r   )create_tagging_chainr.   )r   ))langchain_classic.chains.openai_functionsr0   
isinstancedictschemar   )r,   r-   r.   r+   r0   _tagging_chain_kwargsr   s          r   create_metadata_taggerr6   K   sd    p O ot, 	##% 
 16B(%+/DM  m<<r    r"   )r(   typingr   r   r   r   r   r   langchain_core.documentsr	   r
   langchain_core.language_modelsr   langchain_core.promptsr   pydanticr   r   strr6   r*   r    r   <module>r=      s    < = = F < 5 ="2I ="F ,0C=
 ,0C=4S>4	?:;C=	C= '(C=
 #4.C= C=r    