a
    ”˜ëjE  ã                   @   sÂ   d Z ddlmZmZmZmZ ddl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 ddlmZmZ dd	lmZ dd
lmZmZmZmZ ddlmZ eƒ G dd„ deƒƒZ dS )ú0Structured prompt template for a language model.é    )ÚAsyncIteratorÚIteratorÚMappingÚSequence)ÚAnyÚCallableÚOptionalÚUnion)Ú	BaseModelÚField)Úoverride)Úbeta)ÚBaseLanguageModel)ÚChatPromptTemplateÚMessageLikeRepresentation)ÚPromptTemplateFormat)ÚOtherÚRunnableÚRunnableSequenceÚRunnableSerializable)Úget_pydantic_field_namesc                       sÐ  e Zd ZU dZeeef ed< eed�Z	ee
ef ed< ddddœee eeeee f  eee
ef  eeddœ‡ fd	d
„Zeee
 dœdd„ƒZeee eeef eedœdd„ƒZeeeeef eee gee f eee gee f eegef ee
eeeef eegef ef f f eeef dœdd„ƒZddœeeeef eee gee f eee gee f eegef ee
eeeef eegef ef f f ee
 eeef dœdd„Z ‡  Z!S )ÚStructuredPromptr   Úschema_)Údefault_factoryÚstructured_output_kwargsNzf-string)r   Útemplate_format)Úmessagesr   r   r   ÚkwargsÚreturnc                   s^   |p|  d¡}|pi }t|ƒ t| jƒ¡D ]}|  |¡||< q*tƒ jf ||||dœ|¤Ž dS )a+  Create a structured prompt template.

        Args:
            messages: sequence of messages.
            schema_: schema for the structured prompt.
            structured_output_kwargs: additional kwargs for structured output.
            template_format: template format for the prompt.
        Úschema)r   r   r   r   N)ÚpopÚsetÚ
differencer   Ú	__class__ÚsuperÚ__init__)Úselfr   r   r   r   r   Úk©r$   © ú`/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain_core/prompts/structured.pyr&   &   s    üûzStructuredPrompt.__init__)r   c                 C   s   | j  d¡S )a  Get the namespace of the langchain object.

        For example, if the class is ``langchain.llms.openai.OpenAI``, then the
        namespace is ``["langchain", "llms", "openai"]``

        Returns:
            The namespace of the langchain object.
        Ú.)Ú
__module__Úsplit)Úclsr*   r*   r+   Úget_lc_namespaceC   s    
z!StructuredPrompt.get_lc_namespace)r   r    r   r   c                 K   s   | ||fi |¤ŽS )a—  Create a chat prompt template from a variety of message formats.

        Examples:
            Instantiation from a list of message templates:

            .. code-block:: python

                from langchain_core.prompts import StructuredPrompt


                class OutputSchema(BaseModel):
                    name: str
                    value: int


                template = StructuredPrompt(
                    [
                        ("human", "Hello, how are you?"),
                        ("ai", "I'm doing well, thanks!"),
                        ("human", "That's good to hear."),
                    ],
                    OutputSchema,
                )

        Args:
            messages: sequence of message representations.
                  A message can be represented using the following formats:
                  (1) BaseMessagePromptTemplate, (2) BaseMessage, (3) 2-tuple of
                  (message type, template); e.g., ("human", "{user_input}"),
                  (4) 2-tuple of (message class, template), (5) a string which is
                  shorthand for ("human", template); e.g., "{user_input}"
            schema: a dictionary representation of function call, or a Pydantic model.
            kwargs: Any additional kwargs to pass through to
                ``ChatModel.with_structured_output(schema, **kwargs)``.

        Returns:
            a structured prompt template

        r*   )r/   r   r    r   r*   r*   r+   Úfrom_messages_and_schemaO   s    .z)StructuredPrompt.from_messages_and_schema)Úotherr   c                 C   s
   |   |¡S )N)Úpipe)r'   r2   r*   r*   r+   Ú__or__   s    zStructuredPrompt.__or__)Úname)Úothersr5   r   c                G   sf   |rt |d tƒs t|d dƒrVt| |d j| jfi | j¤Žg|dd… ¢R d|iŽS d}t|ƒ‚dS )a  Pipe the structured prompt to a language model.

        Args:
            others: The language model to pipe the structured prompt to.
            name: The name of the pipeline. Defaults to None.

        Returns:
            A RunnableSequence object.

        Raises:
            NotImplementedError: If the first element of `others`
            is not a language model.
        r   Úwith_structured_outputé   Nr5   z8Structured prompts need to be piped to a language model.)Ú
isinstancer   Úhasattrr   r7   r   r   ÚNotImplementedError)r'   r5   r6   Úmsgr*   r*   r+   r3   Œ   s"    ÿÿÿþ
ûúzStructuredPrompt.pipe)N)"Ú__name__r-   Ú__qualname__Ú__doc__r
   ÚdictÚtypeÚ__annotations__r   r   Ústrr   r   r   r	   r   r   r&   ÚclassmethodÚlistr0   r   r1   r   r   r   r   r   r   r   r   r4   r3   Ú__classcell__r*   r*   r)   r+   r      s^   
 ýúø
û/
&üÿ
÷÷
&üÿ
ör   N)!r?   Úcollections.abcr   r   r   r   Útypingr   r   r	   r
   Zpydanticr   r   Ztyping_extensionsr   Z"langchain_core._api.beta_decoratorr   Z#langchain_core.language_models.baser   Zlangchain_core.prompts.chatr   r   Zlangchain_core.prompts.stringr   Zlangchain_core.runnables.baser   r   r   r   Zlangchain_core.utilsr   r   r*   r*   r*   r+   Ú<module>   s   