Deep Learning (TF, Keras, PyTorch)/Natural Language Processing

[LangChain] 여러 개의 체인을 나누고 합치기 (Branching and Merging of Multiple Chains)

Rfriend 2023. 12. 26. 15:21

LangChain 을 사용하면 편리한 점 중의 하나가 Runnables 을 '|' 을 사용해서 Chaining 할 수 있다는 점입니다. 이번 포스팅에서는 한발 더 나가서, LangChain 에서 여러 개의 체인을 나누고 합치기를 소개하겠습니다. 

(Branching and Merging of Multiple Chains) 

 

 

LangChain - 여러 개의 체인을 나누고 합치기 (Branching and Merging of Multiple Chains)

 

 

먼저 openai, langchian 모듈이 설치되어 있지 않다면 터미널에서 pip install 로 이들 모듈을 설치해주세요. 

 

! pip install -q openai langchain

 

 

 

다음으로 os.environment["OPENAI_API_KEY"] 에 API Key를 등록해 보겠습니다. 

 

import os
from operator import itemgetter

os.environ["OPENAI_API_KEY"]="sk-xxxxx..." # set with yours

 

 

 

예제로 사용할 Multiple Chains의 내용은 다음과 같습니다. 

 

- (1) planner: {topic}을 인풋으로 받으면 논쟁거리를 생성합니다. 

- (2) arguments_for: planner가 생성한 {topic}의 논쟁거리에 대한 찬성 입장을 생성합니다. 

- (3) agruments_against: planner가 생성한 {topic}의 논쟁거리에 대한 반대 입장을 생성합니다.

- (4) final_responder: (1) 최초 논쟁거리, (2) 찬성 입장, (3) 반대 입장을 모두 고려해서 최종 입장을 생성합니다. 이때 ChatPromptTemplate.from_messages() 이고, 프롬프트가 "ai", "human", "system"으로 나누어서 작성한 점 확인해주세요. 

 

RunnableParallel() 을 사용하면 다수의 Chains을 병렬처리 할 수 있습니다. (순차처리 대비 병렬처리 시 수행시간이 단축됩니다. %timeit 로 확인해보세요.)

 

- (5) chain: (1) planner 를 먼저 실행하고, (2) arguments_for, arguments_against, itemgetter("base_response")가 RunnableParallel() 로 해서 Branching 되어서 병렬로 처리됩니다. 

그리고 이 결과를 모두 Merging 해서, 앞서의 결과를 모두 고려하여 final_responder 에서 최종 답변이 생성됩니다. 

 

from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.schema import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_core.runnables import RunnableParallel

planner = (
    ChatPromptTemplate.from_template("Generate an argument about: {topic}")
    | ChatOpenAI()
    | StrOutputParser()
    | {"base_response": RunnablePassthrough()}
)

arguments_for = (
    ChatPromptTemplate.from_template(
        "List the pros or positive aspects of {base_response}"
    )
    | ChatOpenAI()
    | StrOutputParser()
)

arguments_against = (
    ChatPromptTemplate.from_template(
        "List the cons or negative aspects of {base_response}"
    )
    | ChatOpenAI()
    | StrOutputParser()
)

final_responder = (
    ChatPromptTemplate.from_messages(
        [
            ("ai", "{original_response}"),
            ("human", "Pros:\n{results_1}\n\nCons:\n{results_2}"),
            ("system", "Generate a final response given the critique"),
        ]
        )
        | ChatOpenAI()
        | StrOutputParser()
)

chain = (
    planner
    | {
        "results_1": arguments_for,
        "results_2": arguments_against, 
        "original_response": itemgetter("base_response"),
    }
    | final_responder
)

## Equivantly using RunnableParallel()
# chain = (
#     planner
#     | RunnableParallel(
#         results_1 = arguments_for,
#         results_2 = arguments_against, 
#         original_response = itemgetter("base_response"),
#     )
#     | final_responder
# )

 

 

chain.invoke({"topic": "AI"}) 로 실행시켜 보겠습니다. 여러 개의 chain을 연결하다보니 약 43초 걸리네요.  

 

chain.invoke({"topic": "AI"})

# While there are legitimate concerns and potential drawbacks associated with Artificial Intelligence (AI), it is crucial to address these challenges and work towards responsible and ethical development and deployment of AI technologies. Here are some possible solutions and considerations:
# 1. Job displacement: Governments, businesses, and educational institutions should invest in retraining programs and initiatives to help workers transition into new roles and industries. This can involve reskilling programs, job placement assistance, and support for entrepreneurship and innovation.
# 2. Bias and discrimination: Efforts should be made to ensure that AI systems are trained on diverse and representative datasets, and that biases are identified and eliminated. Ongoing monitoring and auditing of AI systems can help identify and address any biases that may arise.
# 3. Transparency and accountability: Developers and organizations should strive for transparency in AI systems, providing explanations for decisions and ensuring that AI algorithms are auditable. Regulatory frameworks can be put in place to ensure accountability and transparency in AI systems.
# 4. Human skills and creativity: Emphasis should be placed on developing and nurturing human skills that complement AI technologies, such as critical thinking, creativity, and emotional intelligence. Education systems can adapt to focus on these skills and encourage lifelong learning.
# 5. Security and privacy: Strong cybersecurity measures and data protection protocols should be implemented to safeguard personal and sensitive information. Privacy regulations can provide guidelines for the responsible handling of data in AI systems.
# 6. Ethical considerations: A robust ethical framework should be developed to guide the development and deployment of AI technologies. Organizations and developers should adhere to ethical guidelines and principles, ensuring that AI is used for the benefit of humanity and respects individual rights and dignity.
# 7. Economic equality: Efforts should be made to ensure that the benefits of AI adoption are distributed equitably. This can involve policies that promote access to AI technologies, support for inclusive innovation, and measures to address economic disparities.
# 8. Unemployment and retraining: Governments, businesses, and educational institutions should collaborate to provide support and resources for retraining workers and facilitating job transitions. This can include financial assistance, vocational training programs, and job placement services.
# 9. Human touch and empathy: AI technologies should be designed to complement human capabilities rather than replace them entirely. In domains such as healthcare and customer service, a balance should be struck between AI automation and maintaining human connection and personalized care.
# 10. Dependence and vulnerability: Efforts should be made to diversify technological solutions and avoid over-reliance on AI systems. Continual monitoring, testing, and contingency planning can help mitigate the risks associated with dependence on AI technologies.
# By addressing these concerns and challenges, we can ensure that AI technologies are developed and deployed in a responsible and ethical manner, maximizing their benefits while minimizing their potential drawbacks. Collaboration between stakeholders, including governments, organizations, researchers, and the public, is key to achieving this goal.

 

 

[ Reference ]

 

* RangChain - Multiple Chains:

https://python.langchain.com/docs/expression_language/cookbook/multiple_chains

 

 

이번 포스팅이 많은 도움이 되었기를 바랍니다. 

행복한 데이터 과학자 되세요!  :-)

 

728x90
반응형