o
    i                  
   @   s   d Z ddlmZmZ ddlmZ ddlmZmZm	Z	m
Z
mZ ddlmZ ddlmZmZ zddlmZ ddlmZ W n eyR Z zed	 ed
e dZ[ww G dd deZdS )z,Langchain integration processor for Pipecat.    )OptionalUnion)logger)FrameLLMContextFrameLLMFullResponseEndFrameLLMFullResponseStartFrame	TextFrame)OpenAILLMContextFrame)FrameDirectionFrameProcessor)AIMessageChunk)RunnablezLIn order to use Langchain, you need to `pip install pipecat-ai[langchain]`. zMissing module: Nc                       s|   e Zd ZdZddedef fddZdefdd	Zd
ede	f fddZ
edeeef defddZdefddZ  ZS )LangchainProcessora.  Processor that integrates Langchain runnables with Pipecat's frame pipeline.

    This processor takes LLM message frames, extracts the latest user message,
    and processes it through a Langchain runnable chain. The response is streamed
    back as text frames with appropriate response markers.
    inputchaintranscript_keyc                    s    t    || _|| _d| _dS )zInitialize the Langchain processor.

        Args:
            chain: The Langchain runnable to use for processing messages.
            transcript_key: The key to use when passing input to the chain.
        N)super__init___chain_transcript_key_participant_id)selfr   r   	__class__ [/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/processors/frameworks/langchain.pyr   '   s   

zLangchainProcessor.__init__participant_idc                 C   s
   || _ dS )zSet the participant ID for session tracking.

        Args:
            participant_id: The participant ID to use for session configuration.
        N)r   )r   r   r   r   r   set_participant_id3   s   
z%LangchainProcessor.set_participant_idframe	directionc                    s   t  ||I dH  t|ttfr:td|  t|tr#|jjn|j	 }|d d }| 
| I dH  dS | ||I dH  dS )zProcess incoming frames and handle LLM message frames.

        Args:
            frame: The incoming frame to process.
            direction: The direction of frame flow in the pipeline.
        NzGot transcription frame content)r   process_frame
isinstancer   r
   r   debugcontextmessagesget_messages_ainvokestrip
push_frame)r   r   r    r'   textr   r   r   r#   ;   s   
z LangchainProcessor.process_framer,   returnc                 C   s.   |  t d r
  | S  td r | jS  	 dS )zExtract token value from various text types.

        Args:
            text: The text or message chunk to extract value from.

        Returns:
            The extracted string value.
        r    )strr   r"   )r,   r   r   r   __get_token_valueS   s   

z$LangchainProcessor.__get_token_valuec              
      s:  t d|  | t I dH  zz,| jj| j|idd| jiid2 z3 dH W }t| 	|}d|_
| |I dH  q$6 W n/ tyP   t |  d Y n+ tyn } z| jd| |d	I dH  W Y d}~nd}~ww W | t I dH  dS W | t I dH  dS W | t I dH  dS | t I dH  w )
z5Invoke the Langchain runnable with the provided text.zInvoking chain with Nconfigurable
session_id)configTz! generator was closed prematurelyzUnknown error occurred: )	error_msg	exception)r   r%   r+   r   r   astreamr   r   r	   $_LangchainProcessor__get_token_valueincludes_inter_frame_spacesGeneratorExitwarning	Exception
push_errorr   )r   r,   tokenr   er   r   r   r)   e   s0   &*zLangchainProcessor._ainvoke)r   )__name__
__module____qualname____doc__r   r/   r   r   r   r   r#   staticmethodr   r   r7   r)   __classcell__r   r   r   r   r      s    r   )rB   typingr   r   logurur   pipecat.frames.framesr   r   r   r   r	   1pipecat.processors.aggregators.openai_llm_contextr
   "pipecat.processors.frame_processorr   r   langchain_core.messagesr   langchain_core.runnablesr   ModuleNotFoundErrorr>   errorr;   r   r   r   r   r   <module>   s   
