
    Ngf"                         d dl Z d dlmZmZmZ d dlmZmZ d dlm	Z	 d dl
mZmZ  G d de          Z G d d	e          ZdS )
    N)AnyListOptional)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)	LLMResult)OpenAI
OpenAIChatc                       e Zd ZU dZeee                  ed<   dZee	         ed<   e
de	fd            Z	 	 ddee         d	eee                  d
ee         dedef
 fdZ	 	 ddee         d	eee                  d
ee         dedef
 fdZ xZS )PromptLayerOpenAIaS  PromptLayer OpenAI large language models.

    To use, you should have the ``openai`` and ``promptlayer`` python
    package installed, and the environment variable ``OPENAI_API_KEY``
    and ``PROMPTLAYER_API_KEY`` set with your openAI API key and
    promptlayer key respectively.

    All parameters that can be passed to the OpenAI LLM can also
    be passed here. The PromptLayerOpenAI LLM adds two optional

    parameters:
        ``pl_tags``: List of strings to tag the request with.
        ``return_pl_id``: If True, the PromptLayer request ID will be
            returned in the ``generation_info`` field of the
            ``Generation`` object.

    Example:
        .. code-block:: python

            from langchain_community.llms import PromptLayerOpenAI
            openai = PromptLayerOpenAI(model_name="gpt-3.5-turbo-instruct")
    pl_tagsFreturn_pl_idreturnc                     dS )NF )clss    g/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/promptlayer_openai.pyis_lc_serializablez$PromptLayerOpenAI.is_lc_serializable(   s    u    Npromptsstoprun_managerkwargsc                    ddl m}m} t          j                                                                        }t                                          |||          }t          j                                                                        }	t          t          |                    D ]}
||
         }|j
        |
         d         }|j        |j        d}i | j        |} |dd|g|| j        |||	 |            | j        
  
        }| j        r2|j        t#          |j        t$                    si |_        ||j        d<   |S )	FCall OpenAI generate and then call PromptLayer API to log the request.r   get_api_keypromptlayer_api_requesttext
llm_outputzlangchain.PromptLayerOpenAI	langchainr   Npl_request_idpromptlayer.utilsr   r   datetimenow	timestampsuper	_generaterangelengenerationsr    r!   _identifying_paramsr   r   generation_info
isinstancedictselfr   r   r   r   r   r   request_start_timegenerated_responsesrequest_end_timeiprompt
generationrespparamsr$   	__class__s                   r   r+   zPromptLayerOpenAI._generate,   so    	KJJJJJJJ%.2244>>@@#gg//{KK#,0022<<>>s7||$$ 	L 	LAQZF,8;A>J"1< D <0;F;F33-" !.  M   L-5Z.> >5 24J.>K
*?;""r   c                   K   ddl m}m} t          j                                                                        }t                                          |||           d {V }t          j                                                                        }	t          t          |                    D ]}
||
         }|j
        |
         d         }|j        |j        d}i | j        |} |dd|g|| j        |||	 |            | j        
  
         d {V }| j        r2|j        t#          |j        t$                    si |_        ||j        d<   |S )Nr   r   promptlayer_api_request_asyncr   z!langchain.PromptLayerOpenAI.asyncr"   r#   r$   r&   r   r@   r'   r(   r)   r*   
_agenerater,   r-   r.   r    r!   r/   r   r   r0   r1   r2   r4   r   r   r   r   r   r@   r5   r6   r7   r8   r9   r:   r;   r<   r$   r=   s                   r   rB   zPromptLayerOpenAI._agenerateU   s      	QPPPPPPP%.2244>>@@$)GG$6$6wk$R$RRRRRRR#,0022<<>>s7||$$ 	L 	LAQZF,8;A>J"1< D <0;F;F"?"?3" !.# # #      M   L-5Z.> >5 24J.>K
*?;""r   NN)__name__
__module____qualname____doc__r   r   str__annotations__r   boolclassmethodr   r   r   r   r+   r   rB   __classcell__r=   s   @r   r   r      sX         . d3i    #(L(4.(((4    [ %):>	'# '#c'# tCy!'# 67	'#
 '# 
'# '# '# '# '# '#X %)?C	&# &#c&# tCy!&# ;<	&#
 &# 
&# &# &# &# &# &# &# &# &# &#r   r   c                       e Zd ZU dZeee                  ed<   dZee	         ed<   	 	 ddee         deee                  dee
         d	ed
ef
 fdZ	 	 ddee         deee                  dee         d	ed
ef
 fdZ xZS )PromptLayerOpenAIChataZ  PromptLayer OpenAI large language models.

    To use, you should have the ``openai`` and ``promptlayer`` python
    package installed, and the environment variable ``OPENAI_API_KEY``
    and ``PROMPTLAYER_API_KEY`` set with your openAI API key and
    promptlayer key respectively.

    All parameters that can be passed to the OpenAIChat LLM can also
    be passed here. The PromptLayerOpenAIChat adds two optional

    parameters:
        ``pl_tags``: List of strings to tag the request with.
        ``return_pl_id``: If True, the PromptLayer request ID will be
            returned in the ``generation_info`` field of the
            ``Generation`` object.

    Example:
        .. code-block:: python

            from langchain_community.llms import PromptLayerOpenAIChat
            openaichat = PromptLayerOpenAIChat(model_name="gpt-3.5-turbo")
    r   Fr   Nr   r   r   r   r   c                    ddl m}m} t          j                                                                        }t                                          |||          }t          j                                                                        }	t          t          |                    D ]}
||
         }|j
        |
         d         }|j        |j        d}i | j        |} |dd|g|| j        |||	 |            | j        
  
        }| j        r2|j        t#          |j        t$                    si |_        ||j        d<   |S )	r   r   r   r   zlangchain.PromptLayerOpenAIChatr"   r#   Nr$   r%   r3   s                   r   r+   zPromptLayerOpenAIChat._generate   so    	KJJJJJJJ%.2244>>@@#gg//{KK#,0022<<>>s7||$$ 	L 	LAQZF,8;A>J"1< D <0;F;F331" !.  M   L-5Z.> >5 24J.>K
*?;""r   c                   K   ddl m}m} t          j                                                                        }t                                          |||           d {V }t          j                                                                        }	t          t          |                    D ]}
||
         }|j
        |
         d         }|j        |j        d}i | j        |} |dd|g|| j        |||	 |            | j        
  
         d {V }| j        r2|j        t#          |j        t$                    si |_        ||j        d<   |S )Nr   r?   r   z%langchain.PromptLayerOpenAIChat.asyncr"   r#   r$   rA   rC   s                   r   rB   z PromptLayerOpenAIChat._agenerate   s      	QPPPPPPP%.2244>>@@$)GG$6$6wk$R$RRRRRRR#,0022<<>>s7||$$ 	L 	LAQZF,8;A>J"1< D <0;F;F"?"?7" !.# # #      M   L-5Z.> >5 24J.>K
*?;""r   rD   )rE   rF   rG   rH   r   r   rI   rJ   r   rK   r   r   r   r+   r   rB   rM   rN   s   @r   rP   rP   ~   s3         . d3i    #(L(4.(((
 %):>	'# '#c'# tCy!'# 67	'#
 '# 
'# '# '# '# '# '#X %)?C	&# &#c&# tCy!&# ;<	&#
 &# 
&# &# &# &# &# &# &# &# &# &#r   rP   )r'   typingr   r   r   langchain_core.callbacksr   r   langchain_core.outputsr   langchain_community.llms.openair	   r
   r   rP   r   r   r   <module>rW      s     & & & & & & & & & &        - , , , , , > > > > > > > >n# n# n# n# n# n# n# n#bj# j# j# j# j#J j# j# j# j# j#r   