a
    ag1                     @   s   d Z ddlmZmZmZmZ ddlmZ ddlm	Z	 ddl
mZ ddl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 eddddG dd deZdS )zMChain for applying constitutional principles to the outputs of another chain.    )AnyDictListOptional)
deprecated)CallbackManagerForChainRun)BaseLanguageModel)BasePromptTemplate)Chain)ConstitutionalPrinciple
PRINCIPLES)CRITIQUE_PROMPTREVISION_PROMPTLLMChainz0.2.13zThis class is deprecated and will be removed in langchain 1.0. See API reference for replacement: https://api.python.langchain.com/en/latest/chains/langchain.chains.constitutional_ai.base.ConstitutionalChain.htmlz1.0)ZsincemessageZremovalc                	   @   s   e Zd ZU dZeed< ee ed< eed< eed< dZe	ed< e
deee  ee d	d
dZe
eefeeeeed dddZeee dddZeee dddZdeeef ee eeef dddZeeedddZdS )ConstitutionalChainaG  Chain for applying constitutional principles.

    Note: this class is deprecated. See below for a replacement implementation
        using LangGraph. The benefits of this implementation are:

        - Uses LLM tool calling features instead of parsing string responses;
        - Support for both token-by-token and step-by-step streaming;
        - Support for checkpointing and memory of chat history;
        - Easier to modify or extend (e.g., with additional tools, structured responses, etc.)

        Install LangGraph with:

        .. code-block:: bash

            pip install -U langgraph

        .. code-block:: python

            from typing import List, Optional, Tuple

            from langchain.chains.constitutional_ai.prompts import (
                CRITIQUE_PROMPT,
                REVISION_PROMPT,
            )
            from langchain.chains.constitutional_ai.models import ConstitutionalPrinciple
            from langchain_core.output_parsers import StrOutputParser
            from langchain_core.prompts import ChatPromptTemplate
            from langchain_openai import ChatOpenAI
            from langgraph.graph import END, START, StateGraph
            from typing_extensions import Annotated, TypedDict

            llm = ChatOpenAI(model="gpt-4o-mini")

            class Critique(TypedDict):
                """Generate a critique, if needed."""
                critique_needed: Annotated[bool, ..., "Whether or not a critique is needed."]
                critique: Annotated[str, ..., "If needed, the critique."]

            critique_prompt = ChatPromptTemplate.from_template(
                "Critique this response according to the critique request. "
                "If no critique is needed, specify that.\n\n"
                "Query: {query}\n\n"
                "Response: {response}\n\n"
                "Critique request: {critique_request}"
            )

            revision_prompt = ChatPromptTemplate.from_template(
                "Revise this response according to the critique and reivsion request.\n\n"
                "Query: {query}\n\n"
                "Response: {response}\n\n"
                "Critique request: {critique_request}\n\n"
                "Critique: {critique}\n\n"
                "If the critique does not identify anything worth changing, ignore the "
                "revision request and return 'No revisions needed'. If the critique "
                "does identify something worth changing, revise the response based on "
                "the revision request.\n\n"
                "Revision Request: {revision_request}"
            )

            chain = llm | StrOutputParser()
            critique_chain = critique_prompt | llm.with_structured_output(Critique)
            revision_chain = revision_prompt | llm | StrOutputParser()


            class State(TypedDict):
                query: str
                constitutional_principles: List[ConstitutionalPrinciple]
                initial_response: str
                critiques_and_revisions: List[Tuple[str, str]]
                response: str


            async def generate_response(state: State):
                """Generate initial response."""
                response = await chain.ainvoke(state["query"])
                return {"response": response, "initial_response": response}

            async def critique_and_revise(state: State):
                """Critique and revise response according to principles."""
                critiques_and_revisions = []
                response = state["initial_response"]
                for principle in state["constitutional_principles"]:
                    critique = await critique_chain.ainvoke(
                        {
                            "query": state["query"],
                            "response": response,
                            "critique_request": principle.critique_request,
                        }
                    )
                    if critique["critique_needed"]:
                        revision = await revision_chain.ainvoke(
                            {
                                "query": state["query"],
                                "response": response,
                                "critique_request": principle.critique_request,
                                "critique": critique["critique"],
                                "revision_request": principle.revision_request,
                            }
                        )
                        response = revision
                        critiques_and_revisions.append((critique["critique"], revision))
                    else:
                        critiques_and_revisions.append((critique["critique"], ""))
                return {
                    "critiques_and_revisions": critiques_and_revisions,
                    "response": response,
                }

            graph = StateGraph(State)
            graph.add_node("generate_response", generate_response)
            graph.add_node("critique_and_revise", critique_and_revise)

            graph.add_edge(START, "generate_response")
            graph.add_edge("generate_response", "critique_and_revise")
            graph.add_edge("critique_and_revise", END)
            app = graph.compile()

        .. code-block:: python

            constitutional_principles=[
                ConstitutionalPrinciple(
                    critique_request="Tell if this answer is good.",
                    revision_request="Give a better answer.",
                )
            ]

            query = "What is the meaning of life? Answer in 10 words or fewer."

            async for step in app.astream(
                {"query": query, "constitutional_principles": constitutional_principles},
                stream_mode="values",
            ):
                subset = ["initial_response", "critiques_and_revisions", "response"]
                print({k: v for k, v in step.items() if k in subset})

    Example:
        .. code-block:: python

            from langchain_community.llms import OpenAI
            from langchain.chains import LLMChain, ConstitutionalChain
            from langchain.chains.constitutional_ai.models                 import ConstitutionalPrinciple

            llm = OpenAI()

            qa_prompt = PromptTemplate(
                template="Q: {question} A:",
                input_variables=["question"],
            )
            qa_chain = LLMChain(llm=llm, prompt=qa_prompt)

            constitutional_chain = ConstitutionalChain.from_llm(
                llm=llm,
                chain=qa_chain,
                constitutional_principles=[
                    ConstitutionalPrinciple(
                        critique_request="Tell if this answer is good.",
                        revision_request="Give a better answer.",
                    )
                ],
            )

            constitutional_chain.run(question="What is the meaning of life?")
    chainconstitutional_principlescritique_chainrevision_chainFreturn_intermediate_stepsN)namesreturnc                 C   s&   |d u rt t S dd |D S d S )Nc                 S   s   g | ]}t | qS  r   ).0namer   r   u/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain/chains/constitutional_ai/base.py
<listcomp>       z6ConstitutionalChain.get_principles.<locals>.<listcomp>)listr   values)clsr   r   r   r   get_principles   s    z"ConstitutionalChain.get_principles)llmr   critique_promptrevision_promptkwargsr   c                 K   s.   t ||d}t ||d}| f |||d|S )zCreate a chain from an LLM.)r%   prompt)r   r   r   r   )r#   r%   r   r&   r'   r(   r   r   r   r   r   from_llm   s    
zConstitutionalChain.from_llm)r   c                 C   s   | j jS )zInput keys.)r   
input_keysselfr   r   r   r+      s    zConstitutionalChain.input_keysc                 C   s   | j rg dS dgS )zOutput keys.)outputcritiques_and_revisionsinitial_outputr.   )r   r,   r   r   r   output_keys   s    zConstitutionalChain.output_keys)inputsrun_managerr   c              
   C   sf  |p
t  }| jjf i |d|di}|}| jjjf i |}|jd| d | jdd g }| j	D ]}| j
j|||j|dd}	| j|	d	 }
d
|
 v r||
df qh| jj|||j|
|j|dd }|}||
|f |jd|j dd | jdd |jd|
 d | jdd |jd| d | jdd qhd|i}| jrb||d< ||d< |S )N	callbacksoriginalzInitial response: 

yellow)textverbosecolorcritique)input_promptoutput_from_modelcritique_requestr4   output_stringzno critique needed revision)r<   r=   r>   r;   revision_requestr4   z	Applying z...greenz
Critique: bluezUpdated response: r.   r0   r/   )r   Zget_noop_managerr   runZ	get_childr)   formatZon_textr9   r   r   r>   _parse_critiquestriplowerappendr   rC   r   r   )r-   r2   r3   Z_run_managerresponseZinitial_responser<   r/   Zconstitutional_principleZraw_critiquer;   rB   Zfinal_outputr   r   r   _call   sr    






zConstitutionalChain._call)r@   r   c                 C   s4   d| vr| S |  dd } d| v r0|  dd } | S )NzRevision request:r   r6   )splitr?   r   r   r   rH   :  s    z#ConstitutionalChain._parse_critique)N)N)__name__
__module____qualname____doc__r   __annotations__r   r   r   boolclassmethodr   strr$   r   r   r   r	   r   r*   propertyr+   r1   r   r   rM   staticmethodrH   r   r   r   r   r      sH   

 & 
	 

Lr   N)rR   typingr   r   r   r   Zlangchain_core._apir   Zlangchain_core.callbacksr   Zlangchain_core.language_modelsr   Zlangchain_core.promptsr	   Zlangchain.chains.baser
   Z)langchain.chains.constitutional_ai.modelsr   Z-langchain.chains.constitutional_ai.principlesr   Z*langchain.chains.constitutional_ai.promptsr   r   Zlangchain.chains.llmr   r   r   r   r   r   <module>   s    	