o
    Jjg1                     @   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)sincemessage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d	eee  d
ee fddZe
eefdededededed
d fddZed
ee fddZed
ee fddZ	ddeeef dee d
eeef fddZeded
ef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 )Nc                 S   s   g | ]}t | qS  r   ).0namer   r   _/var/www/html/zoom/venv/lib/python3.10/site-packages/langchain/chains/constitutional_ai/base.py
<listcomp>   s    z6ConstitutionalChain.get_principles.<locals>.<listcomp>)listr   values)clsr   r   r   r    get_principles   s   z"ConstitutionalChain.get_principlesllmcritique_promptrevision_promptkwargsc                 K   s.   t ||d}t ||d}| d|||d|S )zCreate a chain from an LLM.)r&   prompt)r   r   r   Nr   r   )r$   r&   r   r'   r(   r)   r   r   r   r   r    from_llm   s   
zConstitutionalChain.from_llmc                 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_managerc              
   C   sd  |pt  }| jjdi |d|di}|}| jjjdi |}|jd| d | jdd g }| j	D ]l}| j
j|||j|dd}	| j|	d	 }
d
|
 v rZ||
df q4| 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 q4d|i}| jr||d< ||d< |S )N	callbacksoriginalzInitial response: 

yellow)textverbosecolorcritique)input_promptoutput_from_modelcritique_requestr5   output_stringzno critique needed revision)r=   r>   r?   r<   revision_requestr5   z	Applying z...greenz
Critique: bluezUpdated response: r/   r1   r0   r   )r   get_noop_managerr   run	get_childr*   formaton_textr:   r   r   r?   _parse_critiquestriplowerappendr   rD   r   r   )r.   r3   r4   _run_managerresponseinitial_responser=   r0   constitutional_principleraw_critiquer<   rC   final_outputr   r   r    _call   sz   




zConstitutionalChain._callrA   c                 C   s4   d| vr| S |  dd } d| v r|  dd } | S )NzRevision request:r   r7   )splitr@   r   r   r    rL   :  s   z#ConstitutionalChain._parse_critique)N)__name__
__module____qualname____doc__r   __annotations__r   r   r   boolclassmethodr   strr%   r   r   r   r	   r   r+   propertyr,   r2   r   r   rV   staticmethodrL   r   r   r   r    r      sZ   
 
 &
	


Lr   N)r[   typingr   r   r   r   langchain_core._apir   langchain_core.callbacksr   langchain_core.language_modelsr   langchain_core.promptsr	   langchain.chains.baser
   )langchain.chains.constitutional_ai.modelsr   -langchain.chains.constitutional_ai.principlesr   *langchain.chains.constitutional_ai.promptsr   r   langchain.chains.llmr   r   r   r   r   r    <module>   s"    	